算数运算符 /
两端操作数 都是整型 结果为整型
两端操作数 有一个浮点类型数 结果为浮点数
c++%
两端操作数必须为整型类型
算数运算符 /
两端操作数 都是整型 结果为整型
两端操作数 有一个浮点类型数 结果为浮点数
c++%
两端操作数必须为整型类型
加 +
减 -
乘 *
除 /
求余 % 求余时运算数字均为整数
除法运算结果与除法的两个数字类型和结果类型有关
若结果赋值为整数类型,则结果只保留整数位
若两个数字均赋值为整数类型,则结果只保留整数位
例如
int a = 7;
int b =2;
float c = a/b;
则结果为整数3
int res =1+2
输出 cout<< <<endl;
除以%
想要谁先运算就加括号
运算符可以直接引用
乘除的结果类型与输入的数有关
#include <iostream>
using namespace std;
int main()
{
// + - * / %
int a;
cout<<"请输入第一个数字:";
cin >> a;
int b;
cout<<"请输入第二个数字:";
cin >> b;
int res1 = a+b;
int res2 = a-b;
int res3 = a*b;
int res4 = a/b;
cout << res1 <<" "<<res2<<" "<<res3<<" "<<res4<< endl;
int t;
cin >> t;
return 0;
}
请输入第一个数字:40
请输入第二个数字:10
50 30 400 4
请输入第一个数字:45
请输入第二个数字:10
55 35 450 4
:省略小数,除法运算,两边都是int类型,结果也是int类型
float a;
cout<<"请输入第一个数字:";
cin>>a;
float b;
cout<<"请输入第二个数字:";
cin>>b;
int res1 = a+b;
int res2 = a-b;
int res3 = a*b;
int res4 = a/b;
cout << res1 <<" "<<res2<<" "<<res3<<" "<<res4<< endl;
请输入第一个数字:1.2
请输入第二个数字:0.3
1 0 0 4
:接收输入是float,加减乘除接收的是int,
float a = 7.0;
float b = 2.5;
float res1 = a / b;
cout << res1 << endl; // 输出:2.8
int a=7;
int b=2;
float res1 =a/b;
cout <<res1<<endl; // 输出:3
int res1 = 13 % 5;
//int res2 = 13 %5.1; // C++不允许对小数求余数
int res0=3+3;
int res1=3+7*3;
int res2=(3+7)*3;
int res3=(42/7)*3;
cout<<res1<<" "<<res2<<" "<<res3<<" "<<res0<<endl;
()控制优先级
除法运算,两个数是什么类型的,得到的值就是什么类型
int a=7
int b=3
int c=a/b; c=2
float d=a/b; d=2
#include "stdafx.h"
#include <iostream>
int main(){
float a;
cout<<"请输入一个数字"<<endl;
cin>>a;
cout<<a;
cout<<
return 0;
}