大一下C++学习记录-简洁版本

C2变量及简单数据类型

C2.5字面量

2.5.1整数

#include<iostream>
#include<stdio.h>
using namespace std;

int main(){
   
    //字面量
    printf("17: %x, %d, %o\n",17,17,17);
    cout << "17: " << hex << 17 <<", " << dec << 17 << ", " << oct << 17 << endl;
    //整数字面量(interal literal)
    int b1 = 512;
    cout << "b1 = "<< dec << b1 << endl;//dec此处不可忽略,因为前面的oct把cout的环境配置成了八进制,这里要改过来
    return 0; 
}
输出
17: 11, 17, 21
17: 11, 17, 21
b1 = 512

2.5.2字符及字符串

#include<iostream>
#include<stdio.h>
using namespace std;

int main(){
   
    //字面量
    printf("17: %x, %d, %o\n",17,17,17);
    cout << "17: " << hex << 17 <<", " << dec << 17 << ", " << oct << 17 << endl;
    //字符及字符串
    char c2 = '\\';
    cout << "c2 = " << c2 << endl;
    cout << "I've told you, \"C/C++ is hard to learn.\"" << endl;
    cout << "Popular languages:\n\tJavaScript\n\tPython\n\tJava\n\tC++" << endl;
    string s2 = R"(I've told you, "C/C++ is hard to learn.")";
    cout << s2 << endl;
    return 0; 
}
输出
17: 11, 17, 21
17: 11, 17, 21
c2 = \
I've told you, "C/C++ is hard to learn."
Popular languages:
        JavaScript
        Python
        Java
        C++
I've told you, "C/C++ is hard to learn."

2.5.3浮点数

#include<iostream>
#include<stdio.h>
using namespace std;

int main(){
   
    //字面量
    printf("17: %x, %d, %o\n",17,17,17);
    cout << "17: " << hex << 17 <<", " << dec << 17 << ", " << oct << 17 << endl;
    //浮点数字面量
    auto a3 = 3;//auto编译器自动推断类型int
    auto b3 {
   3.0};//默认双精度
    auto c3 {
   1.2E-5};//double
    auto d3 = 1.2E+5L;//long double
    auto e3 = -3.01e+12f;//float

    cout << "Name:\ta3\tb3\tc3\td3\te3\n";
    cout << "Type:\t " << typeid(a3).name() << "\t" << typeid(b3).name()
         << "\t" << typeid(c3).name() << "\t" << typeid(d3).name()
         << "\t" << typeid(e3).name();

    float f5 = 3.1f;
    double g5 = 3.1;

    return 0; 
}
输出
17: 11, 17, 21
17: 11, 17, 21
Name:   a3      b3      c3      d3      e3
Type:    i      d       d       e       f 

C3语法基础

C3.1源代码格式

C3.2源代码符号

C3.3操作符(Assignment)

C3.3.1复合

C3.3.2求模表达式

#include<iostream>
using namespace std;

int main(){
   
    //求模操作符
    int a = 10;
    int b = 10 % 7;
    printf("%d is an %s number.",b,b%2 == 0?"even":"odd");
    //b%2 == 0?"even":"odd"条件表达式 如果True表达冒号之前,False表达冒号之后
    return 0;
    
}
输出
3 is an odd number.

#include<iostream>
using namespace std;

int main(){
   
    int age = 63;
    int retired = (age >= 60? 1:0);
    cout << "retired = " << retired << endl;

    return 0;   
}
输出
retired = 1

C3.3.3比较与逻辑

#include<iostream>
using namespace std;

int main(){
   
    //比较与逻辑
    
    int a = 10;
    cout << "a > 5 : " << (a>5) << endl;
    cout << "a < 20 and a >= 10 : " << (a < 20 && a >= 10) << endl;
    cout << "a == 9 : " << (a == 9) << endl;
    cout << "a != 3 : " << (a != 3) << endl;
    cout << "a > 100 or a < 20 : " << (a > 100 || a < 20) << endl;
    cout << "not a >= 10 : " << (!(a >= 10)) << endl;

    return 0;   
}
输出
a > 5 : 1
a < 20 and a >= 10 : 1
a == 9 : 0
a != 3 : 1
a > 100 or a < 20 : 1
not a >= 10 : 0

C3.3.4赋值操作符
C3.3.5递增与递减

#include<iostream>
using namespace std;

int main(){
   
    //递增与递减
    int a = 0;
    a ++;//++递增操作符,①a = 1,②a ++ 作为一个表达式,返回a递增之前的值
    //先取值后递增

    ++ a;//①a = 2,②++ a作为一个表达式,返回a递增之后的值
    //先递增后取值

    int i = 10,b,c;//b,c未作初始化,不知道是多少
    b = i ++;
    c = ++ i;
    cout <<"i = " << i << ",b = " << b << ",c = " << c << endl;

    return 0;   
}
输出
i = 12,b = 10,c = 12

#include<iostream>
using namespace std;

int main(){
   
    //递增与递减
    float f = 10.1f,g,h;//注意10.1f , 10.1默认double
    g = f --;
    h = -- f;
    cout << "f = " << f << ", g = " << g << ", h = " << h << endl;

    return 0;   
}
输出
f = 8.1, g = 10.1, h = 8.1

C3.5隐式类型转换

在以下三种情况,C/C++会进行隐式类型转换(implicit type cast):

  • 变量初始化或者赋值时,值与变量的类型不同;
  • 表达式中不同类型的变量、值进行运算时;
  • 函数参数传递时;

· 将存储范围和精度较低的对象赋值给存储范围更大、精度更高的对象是安全的,反之则不然。
· 当表达式中两种不同类型的对象进行算术运算时,编译器总是将较小类型转换为较大类型再进行计算。


#include<iostream>
using namespace std;

int main(){
   
    //以下除标明以外均是错误的,要避免!
    bool a = 3.0;//double ---> bool
    float b = -9999.2301; // double ---> float
    int c = b;//float ---> int
    unsigned int d = c;//int --->unsigned int 无符号
    short e = d; // unsigned int --->short 有符号
    double f = b; //可以,float --->double
    cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d <<
         ", e = " << e << ", f = " << f << endl;

    return 0;
}
输出
a = 1, b = -9999.23, c = -9999, d = 4294957297, e = -9999, f = -9999.23
  • 列表初始化可以帮忙检查是否含有不允许的隐式类型转换
#include<iostream>
using namespace std;

int main(){
   
    char c {
   712};//char:-127-128
    unsigned int f{
   -1};
    
    return 0;
}
输出
grammer.cpp: In function 'int main()':
grammer.cpp:5:16: error: narrowing conversion of '712' from 'int' to 'char' inside {
    } [-Wnarrowing]
     char c {
   712};//char:-127-128
                ^
grammer.cpp:6:22: error: narrowing conversion of '-1' from 'int' to 'unsigned int' inside {
    } [-Wnarrowing]
     unsigned int f{
   -1};
                      ^

#include<iostream>
using namespace std;

int main(){
   
    //凡事绕着走
    int i = 3;
    auto j = 10 / i; // 整数/整数 = 整数
    auto k = double(10.0) / i; // 不同类型的运算,要先转化为精度更高的那一种类型,再进行计算
    auto z = i/double(10.0);
    auto a = (unsigned int)10/i;//无符号整数/整数 = 无符号整数
    auto b = double(10.0)/3.0f;
    cout << "10/3 = " << j << ", type = " << typeid(j).name() << endl;
    cout << "10.0/3 = " << k << ", type = " << typeid(k).name() << endl;
    cout << "3/10.0 = " << z << ", type = " << typeid(z).name() << endl;
    cout << "(unsigned int)10/3 = " << a << ", type = " << typeid(a).name() << endl;
    cout << "10.0/3.0f = " << b << ", type = " << typeid(b).name() << endl;

    return 0;
}
输出
10/3 = 3, type = i
10.0/3 = 3.33333, type = d
3/10.0 = 0.3, type = d
(unsigned int)10/3 = 3, type = j
10.0/3.0f = 3.33333, type = d

3.6显式类型转换

#include<iostream>
using namespace std;

int main(){
   
    char c = 'k';
    cout <<"c = " << c << endl;
    cout << "ASCII code for 'k': " << int(c) << endl;
    cout << "ASCII code for 'k': " << (int)c << endl;
    cout << "ASCII code for 'k': " << static_cast<int>(c) << endl;

    return 0;
}

C3.7使用函数

#include<iostream>
#include<math.h>
#include <stdlib.h>
using namespace std;

int main(){
   
    double x = 2,y = 8;
    cout << "2^8 = " << pow(x,y) << endl;
    cout << "round(25.51) = " << round(25.51) << endl;
    cout << "floor(25.997) = " << floor(25.997) <<endl;
    cout << "ceil(25.01) = " << ceil(25.01) << endl;
    cout << "sqrt(9) = " << sqrt(9) <<endl;

    int r0 = rand(); //0~RAND_MAX之间的随机数
    int r1 = rand() % 100; //0~99之间的随机数
    double r2 = 0.1 + 0.1 * rand() / RAND_MAX; //0.1~0.2之间的随机数
    cout << "r0 = " << r0 << ", r1 = " << r1 << ", r2 = " << r2 << endl;
    cout <<"RAND_MAX = " << RAND_MAX << endl;

    return 0;
 }
输出
2^8</
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值