1.字符函数库头文件<cctype>
isalnum() 字母或数字
isalpha() 字母
iscntr() 控制字符
isdigit() 数字(0-9)
isgraph() 空格之外的打印字符
islower() 小写字母
isprint() 打印字符,包括空格
ispunct() 标点符号
isspace() 标准空白字符,如空格、进纸、回车、水平制表符、垂直制表符
isupper() 大写字母
isxdigit() 十六进制字符
tolower() 如果是大写,转小写
toupper() 如果是小写,转大写
2.如果既可以使用if else if 又可以使用switch,当选项超过3个的时候,应该使用switch,效率会更高。
3.文件输出。如果不填绝对路径,生成的文件是在程序的目录下的。
[cpp] view plain copy char automobile[50]; int year; double a_price; double d_price; ofstream of;//声明一个输出的对象 of.open("/Users/feiyin001/Documents/myTest.txt");//打开文件 cout << "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 << 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; //文件输出 of << fixed; of.precision(2); of.setf(ios_base::showpoint); of << "Make and model: " << automobile <<endl; of << "Year: " << year <<endl; of << "Was asking $ " << a_price << endl; of << "Now asking $ " << d_price << endl; of.close();
结果:
myTest.txt
Make and model: fdsa Year: 0 Was asking $ 0.00 Now asking $ 0.00
4.读取文件。基本没怎么做过读写文本的事情。还是得亲自打一下代码。
myTest.txt
12331 3123 2312 3 123 12 321 3 123 3123
[cpp] view plain copy ifstream ifile; string filename = "/Users/feiyin001/Documents/myTest2.txt"; ifile.open(filename); if(!ifile.is_open()) { cout << "Could not open the file " << filename << endl; cout << "Program terminating." <<endl; exit(EXIT_FAILURE); } double value = 0; double sum = 0; int count = 0; ifile >> value; while (ifile.good()) { ++count; sum += value; ifile >> value; } //读取停止,检测一下原因 if (ifile.eof()) cout << "End of file reached.\n"; else if (ifile.fail()) cout << "Input terminated by data mismatch.\n"; else cout << "Input terminated for unknown reason.\n"; if (count == 0) { cout << "No data processed.\n"; } else { cout << "Item read: " << count << endl; cout << "Sum: " << sum << endl; cout << "Average: " << sum / count << endl; } ifile.close(); 结果:
控制台
End of file reached.
Item read: 9
Sum: 18351
Average: 2039
============答案================
6.10 复习题1.第二个执行效率更高,如果第一个if成立,esle后面的if就不用判断了。
2. 6.2代码
[cpp] view plain copy char ch; std::cout<< "type, and I shall repeat. \n"; cin.get(ch); while (ch != '.') { if (ch == '\n') { cout << ch; } else { cout <<++ch; } std::cin.get(ch); } cout<< "\nPlease excuse the slight confusion.\n"; 结果: type, and I shall repeat. welcome to fable game. xfmdpnf!up!gbcmf!hbnf Please excuse the slight confusion. ++ch对每个输入的字符都+了1,变成下一个字符。 如果改成ch + 1,因为要类型转换,就变成整形了,输出的是数字。 修改: [cpp] view plain copy char ch; std::cout<< "type, and I shall repeat. \n"; cin.get(ch); while (ch != '.') { if (ch == '\n') { cout << ch; } else { cout <<ch + 1; } std::cin.get(ch); } cout<< "\nPlease excuse the slight confusion.\n"; 结果: type, and I shall repeat. welcom to fable game。 120102109100112110331171123310398991091023310498110102 Please excuse the slight confusion.3.
[cpp] view plain copy char ch; int ct1 = 0, ct2 = 0; while ((ch = cin.get()) != '$') { cout << ch; ct1++; if (ch = '$') { ct2++; } cout << ch; } cout << "ct1 = " << ct1 << " , ct2 = " << ct2 << "\n"; 结果:
Hi!
H$i$!$
$Send $10 or $20 now!
S$e$n$d$ $ ct1 = 9 , ct2 = 9
4.
[cpp] view plain copy a. weight >= 115 && weight < 125 b. 'q' == ch || 'Q' == ch c. x % 2 == 0 && x != 26 d. x % 2 == 0 && x % 26 != 0 e. (donation >= 1000 && donation <= 2000) || 1 == guest f. (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
5.!!x,最后x会变成true或者false,不管x是什么类型。如果x本来就是bool,就一样了。
6. x < 0 ? -x : x;
7.
[cpp] view plain copy switch (ch) { case 'A': a_grade++; break; case 'B': b_grade++; break; case 'C': c_grade++; break; case 'D': d_grade++; break; default: f_grade++; break; }
8.如果使用int,输入的是字符,则会cin出错,如果使用char,可以接受任何输入。
9.
[cpp] view plain copy int line = 0; char ch; while ( cin.get() && ch != 'Q') { if (ch == '\n') { line++; } }
6.11编程练习
1.
int main() { char c; cout << "Enter some char : " ; while ( cin.get(c) && c != '@') { if (isalpha(c)) { if (islower(c)) { c = toupper(c); } else if (isupper(c)) { c = tolower(c); } } else if (isdigit(c)) { continue; } cout << c; } return 0; } 结果:
i have 3 kids, do you kno@w.
I HAVE KIDS, DO YOU KNO
2.
double ch; double cs[10]; cin >> ch; double total; int c = 0; for (int i = 0; i < MAX; i++) { if (cin.good()) { cs[i] = ch; total += ch; cin >> ch; ++c; } else { break; } while( i == 8) break; } if (' ' == cs[0]) cout << "No data."; double average = total / c; int largerNum = 0 ; for (int i = 0; i < c; i++) { if(cs[i] > average) largerNum++; } cout << "Average :" << average << " larger count : " << largerNum << endl; 需要输入是十一个数, 最后结果显示前十个平均。
