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.文件输出。如果不填绝对路径,生成的文件是在程序的目录下的。
- 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
结果:
控制台
End of file reached.
Item read: 9
Sum: 18351
Average: 2039
============答案================
6.10 复习题
1.第二个执行效率更高,如果第一个if成立,esle后面的if就不用判断了。
2.
6.2代码
type, and I shall repeat.
welcome to fable game.
xfmdpnf!up!gbcmf!hbnf
Please excuse the slight confusion.
++ch对每个输入的字符都+了1,变成下一个字符。
如果改成ch + 1,因为要类型转换,就变成整形了,输出的是数字。
修改:
结果:
type, and I shall repeat.
welcom to fable game。
120102109100112110331171123310398991091023310498110102
Please excuse the slight confusion.
3.
结果:
Hi!
H$i$!$
$Send $10 or $20 now!
S$e$n$d$ $ ct1 = 9 , ct2 = 9
4.
6. x < 0 ? -x : x;
7.
9.
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.
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;
需要输入是十一个数, 最后结果显示前十个平均。