1.字符型数据进行运算
#include <iostream>
//using namespace std;
void main()
{
char ch1, ch2;
ch1 = 'a';
ch2 = 'A';
printf("ch1=%d\n",ch1);
printf("ch2=%d\n",ch2);
printf("ch1=%c,ch2=%c\n", ch1 - 32, ch2 + 32);
system("pause");
}
结果:
实现了A与a的互换
2实数与零比较(更可靠的一种方法)
#include <iostream>
void main()
{
float eps = 0.0000001;
float fvar = 0.00001;
if (fvar >= -eps&&fvar <= eps)
printf("zero\n");
else
printf("不是零\n");
system("pause");
}
结果:
3cin和cout
#include <iostream>
using namespace std;
void main()
{
int input;
cout << "please input a number" << endl; //endl是换行
cin >> input;
cout << "the number is" << input << endl;
system("pause");
}
结果:
4简单输出字符
#include <iostream>
using namespace std;
void main()
{
int i=1;
cout << i << endl;
cout << "I love you" << endl;
system("pause");
}
结果:
5控制打印格式程序
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
double a = 123.123456789123;
cout << a << endl; //默认精度为6
cout << setprecision(9) << a << endl; //精度设为9
cout << setprecision(6) << a << endl; //恢复精度
cout << setiosflags(ios::fixed);
cout << setiosflags(ios::fixed)<< setprecision(8) << a << endl; //小数点后八位
//cout << setiosflags(ios::scientific)<< a << endl;
//cout << setiosflags(ios::scientific)<<setprecision(4) << a << endl;//有疑问
system("pause");
}
结果