C++ Primer Plus(第六版)—— 第六章 分支语句和逻辑运算符 笔记和答案


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
  1. char automobile[50];  
  2. int year;  
  3. double a_price;  
  4. double d_price;  
  5.   
  6. ofstream of;//声明一个输出的对象  
  7. of.open("/Users/feiyin001/Documents/myTest.txt");//打开文件  
  8. cout << "Enter the make and model of automobile: ";  
  9. cin.getline(automobile, 50);  
  10. cout << "Enter the model year: ";  
  11. cin >> year;  
  12. cout << "Enter the original asking price: ";  
  13. cin >> a_price;  
  14. d_price = 0.913 * a_price;  
  15.   
  16. //控制台输出  
  17. cout << fixed;  
  18. cout.precision(2);  
  19. cout.setf(ios_base::showpoint);  
  20. cout << "Make and model: " << automobile <<endl;  
  21. cout << "Year: " << year <<endl;  
  22. cout << "Was asking $ " << a_price << endl;  
  23. cout << "Now asking $ " << d_price << endl;  
  24.   
  25. //文件输出  
  26. of << fixed;  
  27. of.precision(2);  
  28. of.setf(ios_base::showpoint);  
  29. of << "Make and model: " << automobile <<endl;  
  30. of << "Year: " << year <<endl;  
  31. of << "Was asking $ " << a_price << endl;  
  32. of << "Now asking $ " << d_price << endl;  
  33.   
  34. 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
  1. ifstream ifile;  
  2. string filename = "/Users/feiyin001/Documents/myTest2.txt";  
  3. ifile.open(filename);  
  4. if(!ifile.is_open())  
  5. {  
  6.     cout << "Could not open the file " << filename << endl;  
  7.     cout << "Program terminating." <<endl;  
  8.     exit(EXIT_FAILURE);  
  9. }  
  10. double value = 0;  
  11. double sum = 0;  
  12. int count = 0;  
  13. ifile >> value;  
  14. while (ifile.good()) {  
  15.     ++count;  
  16.     sum += value;  
  17.     ifile >> value;  
  18. }  
  19. //读取停止,检测一下原因  
  20. if (ifile.eof())  
  21.     cout << "End of file reached.\n";  
  22. else if (ifile.fail())  
  23.     cout << "Input terminated by data mismatch.\n";  
  24. else  
  25.     cout << "Input terminated for unknown reason.\n";  
  26. if (count == 0)  
  27. {  
  28.     cout << "No data processed.\n";  
  29. }  
  30. else  
  31. {  
  32.     cout << "Item read: " << count << endl;  
  33.     cout << "Sum: " << sum << endl;  
  34.     cout << "Average: " << sum / count << endl;  
  35. }  
  36. 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
  1. char ch;  
  2. std::cout<< "type, and I shall repeat. \n";  
  3. cin.get(ch);  
  4. while (ch != '.') {  
  5.     if (ch == '\n') {  
  6.         cout << ch;  
  7.     }  
  8.     else  
  9.     {  
  10.         cout <<++ch;  
  11.     }  
  12.     std::cin.get(ch);  
  13. }  
  14. 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
  1. char ch;  
  2. std::cout<< "type, and I shall repeat. \n";  
  3. cin.get(ch);  
  4. while (ch != '.') {  
  5.     if (ch == '\n') {  
  6.         cout << ch;  
  7.     }  
  8.     else  
  9.     {  
  10.         cout <<ch + 1;  
  11.     }  
  12.     std::cin.get(ch);  
  13. }  
  14. 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
  1. char ch;  
  2. int ct1 = 0, ct2 = 0;  
  3. while ((ch = cin.get()) != '$') {  
  4.     cout << ch;  
  5.     ct1++;  
  6.     if (ch = '$') {  
  7.         ct2++;  
  8.     }  
  9.     cout << ch;  
  10. }  
  11. 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
  1. a. weight >= 115 && weight < 125  
  2. b. 'q' == ch || 'Q' == ch  
  3. c. x % 2 == 0 && x != 26  
  4. d. x % 2 == 0 && x % 26 != 0  
  5. e. (donation >= 1000 && donation <= 2000) || 1 == guest  
  6. 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
  1. switch (ch) {  
  2.     case 'A':  
  3.         a_grade++;  
  4.         break;  
  5.     case 'B':  
  6.         b_grade++;  
  7.         break;  
  8.     case 'C':  
  9.         c_grade++;  
  10.         break;  
  11.     case 'D':  
  12.         d_grade++;  
  13.         break;  
  14.     default:  
  15.         f_grade++;  
  16.         break;  
  17. }  


8.如果使用int,输入的是字符,则会cin出错,如果使用char,可以接受任何输入。


9.

[cpp]  view plain  copy
  1. int line = 0;  
  2. char ch;  
  3. while ( cin.get() && ch != 'Q') {  
  4.     if (ch == '\n') {  
  5.         line++;  
  6.     }  
  7. }  

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;

需要输入是十一个数, 最后结果显示前十个平均。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值