C++基础语法变量输入输出

体系

头文件

#include
#include

使用命名空间

using namespace std;

程序执行入口

int main()
{

}

return 0;返回0

变量

假设人15岁,身高150,即age=15,hight=150

常用变量

布尔 :存储两种值的真假,true=0 false=1
字符:char,用单引号,\n表示换行
整型:int表示-23~23 4位字符
浮点型:1.21e2 单精度6-7有效数字
双精度:doule 15-16位有效数字
长整数型型:longlong
更长整数型:longdoule有18-19有效数字

实践

cin&cout()

1、先定义变量类型,用逗号隔开
可以是变量也可以是赋值,,还可以是赋值表达式
eg、int a, b = 2, c = b;
float d = 1.5, e = 1, f = 1.23e2;
bool g = true, h =flase;
char j = ‘a’, k = ‘b’;
longlong l = 1231564:
2、输入输出
cin >> a >>b;//输入
cout << a+b << endl//输出 endl表示回车
注每一个语句结束后加一个分号
eg

#include <iostream>

using namespace std;

int main()
{ 
    int a, b;
    
    cin >> a >> b;
    cout << a + b << endl;
    
    return 0;
}

scanf&print 输入法

读入读出整数的方式
scanf:前面是字符串%d是整数类型占位,&a是读取到a
print:将整数类型读入到第一个位置,第二个%d读取到第二个位置
eg

#include <iostream>
#include <cstdio>

using namespace std;

int main() //print输出
{
    int a, b;  //定义两个变量
    
    scanf("%d%d", &a, &b);
    printf("a + b = %d\na * b = %d\n", a + b, a * b); //\n表示回车
    
    return 0;
}

转换成浮点数
float:浮点型 %f是浮点数

#include <iostream>
#include <cstdio>

using namespace std;

int main() //print输出
{
    float a, b;  //定义两个变量
    
    scanf("%f%f", &a, &b); 
    printf("a + b = %f\na * b = %f\n", a + b, a * b); //\n表示回车,注保留两位小数a + b = %.2f
    
    return 0;
}

char字符型
注%c%读入空格

 #include <iostream>
#include <cstdio>

using namespace std;

int main() //print输出
{
    char a, b;  //定义两个变量
    
    scanf("%c%c", &a, &b);
    printf("%c %c\n", a, b); //\n表示回车
    
    return 0;
}

注加入平方要引入cmath库sqrt(x)
double型
scanf("%lf%lf", &a, &b);
print("%lf %lf", a, b);

longlong型
scanf("%lld%lld", &a, &b);
print("%lld %lld", a, b);

整理

/*
int:%d
double:%lf
longlong:%lld
char:%c
float:%f
*/

表达式

整数加减乘除,整数相除是整除浮点数相除是浮点数,取模运算,表示前面的数%后面的数取余
eg、cout <<5 % 2 << endl; 输出结果取决于前面的符号且是整数。
eg`

运算

cin/cout+cstring

#include <iostream>
#include <string>

using namespace std;
int main()
{
    int a = 6 + 3 * 4 / 2 - 2;
    
    cout << a << endl;
    int b = a *10 + 5 / 2;
    cout << b << endl;
    cout <<23 *56 - 78 / 3 << endl;
    
    return 0;
}

注:整数的自加自减运算
1、c=a++ 表示先把a的值赋给c
2、d=++a表示把a+1后赋给d
3、int a = 6;
int b =7;
b=b + a;//等价于b += a;
b -= a; //b = b - a
b *= a; //b = b * a
b /= a; //b = b / a
b %= a;// b = b % a
4、变量类型的转化(赋值)
char转换int char’a’ = 97
eg

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    int a = 5;
    float b = (float)a; //表示把后面的a变成b
    cout << b << endl;
    
}

pintf +cstdio

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    int a = 5;
    float b = (float)a; //表示把后面的a变成b
    printf("%f", b);
    
}

将浮点数转化为整数

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    float b = 12.5;
    int a = (int)b;//表示把后面的b变成a
    printf("%d", a);
    
}

整数转换字符

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    int a = 97;
    char b = (char)a;
    cout << b << endl;
    
}

字符型转换整数

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    char b = 'a';
    int t = (int)b;
    printf("%d", t);
    
}

利用char做运算

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    char b = 'A';
    printf("%c", b + 32);
    
}
输出结果为a

隐运算

整数和浮点数运算会显示浮点数

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    int a = 6;
    float b = 1.3;
    
    cout << a * b << endl;
    return 0;
    
}
输出7.8

隐形类型转换默认转换程精度高的,char最低

顺序语句

执行是从上往下顺序

浮点数比较

用float ,double有效位数不精确
eg:

#include <iostream>
#include <cmath>

using namespace std;
int main()
{
    float x = 1.23456789;
    float a = x * x;
    float b = sqrt(a);
    
    printf("%.10f\n", b);
    
    return 0;
}

一般的会添加容忍误差
如果|x- y| < eps(无限小);可取相等
定义常量

#include <iostream>
#include <cmath>

using namespace std;

const double eps = 1e-6;

int main()
{
    int a = 3;
    if (sqrt(3) * sqrt(3) < 3) puts ("!!!");
    return 0;
}

结果:根号三的平方 小于 3 输出不相等

当两个数的差小于无限小时,会输出相等。

#include <iostream>
#include <cmath>

using namespace std;

const double eps = 1e-6;

int main()
{
    int a = 3;
    if (fabs(sqrt(3) * sqrt(3) - 3) < eps) puts ("相等"); 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值