注意,如果要将代码拷回去测试的话,注意输入的测试数据,因为我将几个问题融合在一起了,所以如果不看懂程序就输入数据进行测试的话,可能会出错
- #include<iostream>
- #include<climits>
- #include<cctype> //支持一些特殊函数,详见程序
- #include<fstream> //用于文件输入输出
- #include<cstdlib> //支持exit()的使用
- using namespace std;
- const int SIZE = 60;
- bool is_int(double);
- int main()
- {
- // c++ 递增++ 和 递减--的使用注意事项 /
- int guests = 8;
- while (guests++ < 10)
- cout << "guests = " << guests << endl;
- int x = 3;
- int y;
- y = (4 + x++) + (6 + x++); // 这种表达式中在计算表达式后,x递增两次,
- cout << "y = " << y << endl; // 不是在计算每个子表达式后将x的值递增。
- cout << "x = " << x << endl;
- y = (4 + ++x) + (6 + ++x); //在这里,是先将x的值递增两次,变成7之后在参与表达式运算
- cout << "y = " << y << endl;
- cout << "x = " << x << endl;
- 复合语句块,下面这一个例子是绝大多数新手没有想过的 ///
- int m = 20;
- {
- int n = 100;
- cout << "m = " << m << endl;
- cout << "n = " << n << endl;
- }
- cout << "m = " << m << endl;
- // cout << n << endl; 这里会报错,说n没有定义,可见n只是内部语句块中的变量,
- // 内部语句执行完后,变量释放
- // 文件尾条件 ///
- char ch;
- int count = 0;
- cin.get(ch);
- while(cin.fail() == false) //EOF结束输入
- {
- cout << ch;
- ++count; // 注意,这里计数的时候将换行符计入了
- cin.get(ch);
- }
- cout << endl << count << "characters read/n";
- // 上面这个例子的另一种表达方式 /
- cin.clear(); // 刚开始这里没有加这行代码,结果阻断了后面的输入,
- count = 0; //估计EOF遗留在输入缓存中,直接赋给了ch
- ch = cin.get();
- while(ch != EOF)
- {
- cout.put(ch);
- ++count;
- ch = cin.get();
- }
- cout << endl << count << "characters read/n";
- / 一个友好的程序
- cin.clear(); //测试的时候没有加这个,结果导致后面的while循环是一个死循环
- double num;
- cout << "Yo,dude! Enter an integer value: ";
- cin >> num;
- while(!is_int(num)) // 如果num超过int的表示范围,继续
- {
- cout << "Out of range -- please try again: ";
- cin >> num;
- }
- int val = int(num);
- cout << "You've entered the integer " << val << "/nBye/n";
- /* 如果给读取int值的程序输入一个过大的值,很多c++实现只是将这个值截短为合适的大小,并不会通知
- 丢失了数据。该程序避免了这样的问题,它首先将可能的int值作为double值来读取。然后自定义一个
- 判断函数is_int来判断*/
- // 字符函数库cctype的使用 ----该函数库我学c语言的时候就没用过,但是熟悉了很好用/
- cin.clear();
- cout << endl << "Enter text for analysis,and type @ "
- "to terminate input./n";
- char temp;
- int whitespace = 0;
- int digits = 0;
- int chars = 0;
- int punct = 0;
- int others = 0;
- cin.get(temp);
- while(temp != '@')
- {
- if(isalpha(temp))
- chars++;
- else if(isspace(temp))
- whitespace++; //大家试一下,输出结果中whitespace每次都会多1,为什么呢?
- else if(isdigit(temp))
- digits++;
- else if(ispunct(temp))
- punct++;
- else
- others++;
- cin.get(temp);
- }
- cout << chars <<" letters, "
- << whitespace << " whitespace, "
- << digits << " digits, "
- << punct << " punctuations, "
- << others << " others./n";
- /* 该字符库中还有一些其他的判定函数,有兴趣的可以上网查阅,或者调出该函数库*/
- /// 简单文件输入/输出 ///
- cin.get();
- char automobile[50];
- int year;
- double a_price;
- double d_price;
- ofstream outFile;
- outFile.open("carinfo.txt");
- cout << endl << "Enter the make and model of automobile: ";
- cin.getline(automobile,50);
- cout << "Enter the model year: ";
- cin >> year;
- cout << "Enter the original asking price: ";
- cin >>a_price;
- d_price = 0.913 * a_price;
- /* 通过cout将输出打印在屏幕上*/
- cout << fixed;
- cout.precision(2); // 输出的精度限制在这里,保留到小数点后两位数字
- // cout.setf(ios_base::showpoint);
- cout << "Make and model: " << automobile << endl;
- cout << "Year: " << year << endl;
- cout << "Was asking $" << a_price << endl;
- cout << "Now asking $" << d_price << endl;
- /* 通过文件将输出打印到屏幕上*/
- outFile << fixed;
- outFile.precision(2);
- // outFile.setf(ios_base::showpoint);
- outFile << "Year: " << year << endl;
- outFile << "was asking $" << a_price << endl;
- outFile << "Now asking $" << d_price << endl;
- outFile.close();
- /**
- * 在上面的一段代码中,有点知识点:
- cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); 什么意思?
- setf()是追加标志字的函数,而flags()是设置标志字
- fixed标志是以定点形式显示浮点数
- showpoint标志是强制显示小数点
- precision就是精度,表示输出多少小数位。
- 以下是一些常见的控制函数的:
- dec 置基数为10 相当于"%d"
- hex 置基数为16 相当于"%X"
- oct 置基数为8 相当于"%o"
- setfill(c) 设填充字符为c
- setprecision(n) 设显示小数精度为n位
- setw(n) 设域宽为n个字符
- 这个控制符的意思是保证输出宽度为n。如:
- cout<<setw(3)<<1<<setw(3)<<10<<setw(3)<<100; 输出结果为
- 1 10100 (默认是右对齐)当输出长度大于3时(<<1000),setw(3)不起作用。
- setioflags(ios::fixed) 固定的浮点显示
- setioflags(ios::scientific) 指数表示
- setiosflags(ios::left) 左对齐
- setiosflags(ios::right) 右对齐
- setiosflags(ios::skipws 忽略前导空白
- setiosflags(ios::uppercase) 16进制数大写输出
- setiosflags(ios::lowercase) 16进制小写输出
- setiosflags(ios::showpoint) 强制显示小数点
- setiosflags(ios::showpos) 强制显示符号
- **/
- /// 文件输出的例子 ///
- cin.get();
- char filename[SIZE];
- ifstream inFile;
- cout << "Enter name of data file: ";
- cin.getline(filename, SIZE);
- inFile.open(filename);
- if(!inFile.is_open()) //如果所使用的编译器不支持inFile.is_open(),那么就用inFile.good();
- {
- cout << "Could not open the file " << filename << endl;
- cout << "Program terminating./n";
- exit(EXIT_FAILURE);
- }
- double value;
- double sum = 0.0;
- int fcount = 0;
- inFile >> value;
- while(inFile.good()) //文件没有读完
- {
- ++fcount;
- sum += value;
- inFile >> value;
- }
- if(inFile.eof())
- cout << "End of file reached./n";
- else if(inFile.fail())
- cout << "Input terminated by data mismatch./n";
- else
- cout << "Input terminated for unknown reason./n";
- if(fcount == 0)
- cout << "No data processed./n";
- else
- {
- cout << "Items read: " << fcount << endl;
- cout << "Sum: " <<sum << endl;
- cout << "Average: " << sum/fcount <<endl;
- }
- inFile.close();
- return 0;
- }
- bool is_int(double x)
- {
- if (x <= INT_MAX && x >= INT_MIN)
- return true;
- else
- return false;
- }