#include<iostream>#include<string>usingnamespace std;intmain(){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;return0;}
运算符
描述
实例
+
把两个操作数相加
A + B 得到 30
-
操作数1减去操作数2
A - B 得到 -10
*
两个操作数相乘
A * B 得到 200
/
分子除以分母
B / A 得到 2
%
取模运算符,整数后的余数
B % A 得到 0
3.2 浮点数(小数)运算
#include<iostream>#include<string>usingnamespace std;intmain(){float x =1.5, y =3.2;
cout << x * y <<' '<< x + y << endl;
cout << x - y <<' '<< x / y << endl;return0;}
3.3 整数变量的自增自减
#include<iostream>#include<string>usingnamespace std;intmain(){int a =1;int b = a ++;
cout << a <<' '<< b << endl;int c =++ a;
cout << a <<' '<< c << endl;return0;}
3.4 变量的类型转换
#include<iostream>#include<string>usingnamespace std;intmain(){float x =123.12;int y =(int)x;
cout << x <<' '<< y << endl;return0;}
4. 顺序语句
4.1 输出第二个整数
#include<iostream>#include<string>usingnamespace std;intmain(){int a, b, c;
cin >> a >> b >> c;
cout << b << endl;return0;}
4.2 计算 (a + b) * c 的值
#include<iostream>#include<string>usingnamespace std;intmain(){int a, b, c;
cin >> a >> b >> c;
cout <<(a + b)* c << endl;return0;}
4.3 带余除法
#include<iostream>#include<string>usingnamespace std;intmain(){int a, b;
cin >> a >> b;int c = a / b, d = a % b;
cout << c <<' '<< d << endl;return0;}
4.4 求反三位数
#include<iostream>#include<string>usingnamespace std;intmain(){int n;
cin >> n;int a = n %10;// 个位
n = n /10;int b = n %10;// 十位
n = n /10;int c = n;// 百位
cout << a << b << c << endl;return0;}
4.5 输出菱形
#include<iostream>#include<string>usingnamespace std;intmain(){char c;
cin >> c;
cout <<" "<< c << endl;
cout <<" "<< c << c << c << endl;
cout << c << c << c << c << c << endl;
cout <<" "<< c << endl;return0;}
5. 其他
5.1 浮点数的比较
#include<iostream>#include<cmath>usingnamespace std;constdouble eps=1e-6intmain(){double x =1.23456789;double a = x*x;double b =sqrt(a);printf("%.10f\n",b);if(fabs(x-b)<eps)puts("相等");return0;}