第6章 分支语句和逻辑运算符

  • 6.1 if.cpp
  • 6.2 ifelse.cpp
  • 6.3 ifelseif.cpp
  • 6.4 or.cpp
  • 6.5 and.cpp
  • 6.6 more_and.cpp
  • 6.7 not.cpp
  • 6.8 cctypes.cpp
  • 6.9 condit.cpp
  • 6.10 switch.cpp
  • 6.11 enum.cpp
  • 6.12 jump.cpp
  • 6.13 cinfish.cpp
  • 6.14 cingolf.cpp
  • 6.15 outfile.cpp
  • 6.16 sumafile.cpp

6.1 if.cpp

#include<iostream>int main()
{using std::cin;using std::cout;char ch;int spaces = 0;int total = 0;cin.get(ch);while (ch != '.'){if (ch == ' '){++spaces;}++total;cin.get(ch);}cout << spaces << " spaces, " << total;cout << " characters total in sentence\n";return 0;
}

6.2 ifelse.cpp

#include<iostream>int main()
{char ch;std::cout<< "Type , and I shall repeat.\n";std::cin.get(ch);while (ch != '.'){if (ch == '\n'){std::cout << ch;}elsestd::cout << ++ch;std::cin.get(ch);}std::cout << "\n Please excuse the slight confusion.\n";return 0;
}

6.3 ifelseif.cpp

#include<iostream>
const int Fave = 27;
int main()
{using namespace std;int n;cout << "Enter a number in the range 1-100 to find ";cout << "my favorite number:";do{cin >> n;if (n < Fave)cout << "Too low -- guess again:";else if (n > Fave)cout << "Too high -- guess again:";elsecout << Fave << " is right!\n";} while (n != Fave);return 0;
}

6.4 or.cpp

#include<iostream>
int main()
{using namespace std;cout << "This program may reformat your hard disk\n""and destory all your data.\n""Do you wish to continue?<y/n>";char ch;cin >> ch;if (ch == 'y' || ch == 'Y')cout << "You were warned!\a\a\n";else if (ch == 'n' || ch == 'N')cout << "A wise choic ...bye\n";elsecout << "That wasn't a y or n!Apparently you ""can't follow\ninstructions,so ""I'll trash your disk anyway.\a\a\a\n";return 0;
}

6.5 and.cpp

#include<iostream>
const int ArSize = 6;int main()
{using namespace std;float naaq[ArSize];cout << "Enter the NAAQs (New Age Awareness Quotients) "<< "of \nyour neighbors. Program terminatesc "<< " when you make\n" << ArSize << " entries "<< " or enter a negative value.\n";int i = 0;float temp;cout << "First valude: ";cin >> temp;while (i < ArSize && temp >= 0){naaq[i] = temp;++i;if (i < ArSize){cout << "Next value: ";cin >> temp;}}if (i == 0){cout << "No data--bye\n";}else{cout << "Enter your NAAQ: ";float you;cin >> you;int count = 0;for (int j = 0; j < i;j++){if (naaq[j] > you){++count;}}cout << count;cout << " of your neighbors have greater awareness of\n"<< "the New Age than you do.\n";}return 0;
}

6.6 more_and.cpp

#include<iostream>
const char* qualify[4] =
{"10,000-meter race.\n","mud tug-of-war.\n","masters canoe jousting.\n","pie-throwing festival.\n"
};
int main()
{using namespace std;int age;cout << "Enter your age in years:";cin >> age;int index;if (age > 17 && age < 35)index = 0;else if (age >= 35 && age < 50)index = 1;else if (age >= 50 && age < 65)index = 2;elseindex = 3;cout << "You qualify for the " << qualify[index];return 0;
}

6.7 not.cpp

#include<iostream>
#include<climits>
bool is_int(double);
int main()
{using namespace std;double num;cout << "Yo,dude!Enter an integer value:";cin >> num;while (!is_int(num)){cout << "Out of range -- please try again:";cin >> num;}int val = int(num);cout << "You've entered the integer " << val << "\nBye\n";return 0;
}bool is_int(double x)
{if (x <= INT_MAX && x >= INT_MIN)return true;elsereturn false;
}

6.8 cctypes.cpp

#include<iostream>
#include<cctype>
int main()
{using namespace std;cout << "Enter text for analysis,and type @"" to terminate input.\n";char ch;int whitespace = 0;int digits = 0;int chars = 0;int punct = 0;int others = 0;cin.get(ch);while (ch != '@'){if (isalpha(ch))chars++;else if (isspace(ch))whitespace++;else if (isdigit(ch))digits++;else if (ispunct(ch))punct++;elseothers++;cin.get(ch);}cout << chars << " letters."<< whitespace << " whitespace."<< digits << " digits."<< punct << " punctuations."<< others << " others.\n";return 0;
}

6.9 condit.cpp

#include<iostream>
int main()
{using namespace std;int a, b;cout << "Enter two integers:";cin >> a >> b;cout << "The larger of " << a << " and " << b;int c = a > b ? a : b;cout << " is " << c << endl;return 0;
}

6.10 switch.cpp

#include<iostream>
using namespace std;
void showmenu();
void report();
void comfort();
int main()
{showmenu();int choice;cin >> choice;while (choice != 5){switch (choice){case 1:cout << "\a\n";break;case 2:report();break;case 3:cout << "The boss was in all day.\n";break;case 4:comfort();break;default:cout << "That's not a choice.\n";}showmenu();cin >> choice;}cout << "Bye!\n";return 0;
}void showmenu()
{cout << "Please enter1,2,3,4 or 5:\n""1) alarm                    2)report\n""3) alibi                       4)comfort\n""5)quit\n";
}void report()
{cout << "It's been an excellent week for business.\n""Sales are up 120%.Expenses are down 35%.\n";
}void comfort()
{cout << "Your employees think you are the finest CEO\n""in the industry.The board of directors think\n""you are the finest CEO in the industry.\n";
}

6.11 enum.cpp

#include<iostream>
enum { red, orange, yellow, green, blue, violet, indigo };int main()
{using namespace std;cout << "Enter color code(0-6): ";int code;cin >> code;while (code >= red && code <= indigo){switch (code){case red:cout << "Her lips were red.\n"; break;case orange:cout << "Her hair was orange.\n"; break;case yellow:cout << "Her shoes were yellow.\n"; break;case green:cout << "Her nails were green:\.\n"; break;case blue:cout << "Her sweatsuit was blue.\n"; break;case violet:cout << "Her eyes were violet.\n"; break;case indigo:cout << "Her mood was indigo.\n"; break;}cout << "Enter color code(0-6):";cin >> code;}cout << "Bye\n";return 0;
}

6.12 jump.cpp

#include<iostream>
const int ArSize = 80;
int main()
{using namespace std;char line[ArSize];int spaces = 0;cout << "Enter a line of text:\n";cin.get(line, ArSize);cout << "Complete line:\n" << line << endl;cout << "Line through first period:\n";for (int i = 0; line[i] != '\0'; i++){cout << line[i];if (line[i] == '.')break;if (line[i] != ' ')continue;spaces++;}cout << "\n" << spaces << " spaces\n";cout << "Done.\n";return 0;
}

6.13 cinfish.cpp

#include<iostream>
const int Max = 5;
int main()
{using namespace std;double fish[Max];cout << "Please enter the weights of your fish.\n";cout << "You may enter up to " << Max << " fish <q to terminate>.\n";cout << "fish #1: ";int i = 0;while (i < Max && cin >> fish[i]){if (++i < Max)cout << "fish #" << i + 1 << ":";}double total = 0.0;for (int j = 0; j < i; j++)total += fish[j];if (i == 0)cout << "No fish\n";elsecout << total / i << " = average weight of "<< i << " fish\n";cout << "Done.\n";return 0;
}

6.14 cingolf.cpp

#include<iostream>
const int Max = 5;
int main()
{using namespace std;int golf[Max];cout << "Please enter your golf scores.\n";cout << "You must enter " << Max << " rounds.\n";int i;for (i = 0; i < Max; i++){cout << "round #" << i + 1 << ":";while (!(cin >> golf[i])){cin.clear();while (cin.get() != '\n')continue;cout << "Please enter a number:";}}double total = 0;for (int i = 0; i < Max; i++)total += golf[i];cout << total / Max << " = average score "<< Max << " rounds\n";return 0;
}

6.15 outfile.cpp

#include<iostream>
#include<fstream>int main()
{using namespace std;char automobile[50];int year;double a_price;double d_price;ofstream outFile;outFile.open("carinfo.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;outFile << fixed;outFile.precision(2);outFile.setf(ios_base::showpoint);outFile << "Make and  model:" << automobile << endl;outFile << "Year:" << year << endl;outFile << "Was asking $" << a_price << endl;outFile << "Now asking $" << d_price << endl;outFile.close();return 0;
}

6.16 sumafile.cpp

#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 60;
int main()
{using namespace std;char filename[SIZE];ifstream inFile;cout << "Enter name of data file:";cin.getline(filename, SIZE);inFile.open(filename);if (!inFile.is_open()){cout << "Could not open the file " << filename << endl;cout << "Program terminating.\n";system("pause");exit(EXIT_FAILURE);}double value;double sum = 0.0;int count = 0;inFile >> value;while (inFile.good())                                                            //判断文件流是否符合标准{count++;sum += value;inFile >> value;}if (inFile.eof())cout << "End of file reached.\n";else if (inFile.fail())cout << "Input terminated by data mismatch.\n";elsecout << "Input terminated for unknown reason.\n";if (count == 0)cout << "No data processed.\n";else{cout << "Items read:" << count << endl;cout << "Sum:" << sum << endl;cout << "Average:" << sum / count << endl;}inFile.close();return 0;
}

C++primer Plus课本代码(第6章)相关推荐

  1. 【NoSQL数据库技术与应用】【课本代码】【课后题答案】【持续更新】

    文章目录 一.课本代码 第1章 初识NoSQL 第2章 文档存储数据库MongoDB 第3章 MongoDB数据库操作 3.8 使用Java操作MongoDB 1.搭建JAVA环境 (1)Java配置 ...

  2. C++ Primer 第五版 第6章——函数阅读笔记及习题答案(完整,附C++代码)

    C++Primer(第五版)第6章函数的阅读笔记及课后习题答案总结,课后习题答案是自己学习写出来的,如果有误,欢迎指正 还不完整,后续会更新添加 阅读笔记 C++ Primer 第五版 第6章 6.1 ...

  3. C++ Primer 学习笔记(第四章:表达式)

    2019独角兽企业重金招聘Python工程师标准>>> ##C++ Primer 学习笔记(第四章:表达式) [TOC] ###4.1 基础 左值和右值: 当一个对象被用作右值的时候 ...

  4. C++ Primer Plus 6th代码阅读笔记

    C++ Primer Plus 6th代码阅读笔记 第一章没什么代码 第二章代码 carrots.cpp : cout 可以拼接输出,cin.get()接受输入 convert.cpp 函数原型放在主 ...

  5. C++ Primer 第五版 第7章类 7.1——类讲解(成员函数、非成员函数、构造函数)习题答案

    理论讲解请参考:C++ Primer 第五版 第7章类 7.1--类讲解(成员函数.非成员函数.构造函数) 目录 7.1 定义抽象数据类型习题答案 7.4&7.5 7.6&7.7 7. ...

  6. C++ Primer 第五版 第7章类 7.1——类讲解(成员函数、非成员函数、构造函数)

    习题答案请参考:C++ Primer 第五版 第7章类 7.1--类讲解(成员函数.非成员函数.构造函数)习题答案 目录 7.1 类讲解(成员函数.非成员函数.构造函数) 成员函数 this cons ...

  7. C++ Primer 第五版 第6章 6.7——函数指针习题答案

    理论请参考:C++ Primer 第五版 第6章 6.7--函数指针阅读笔记 目录 6.7 函数指针习题答案 6.54 6.55 6.56 6.7 函数指针习题答案 6.54 vector是指向该函数 ...

  8. C++ Primer 第五版 第6章 6.7——函数指针阅读笔记

    习题答案请参考:C++ Primer 第五版 第6章 6.7--函数指针习题答案 目录 6.7 函数指针 使用函数指针 返回指向函数的指针 6.7 函数指针 声明一个函数指针,只需要用指针替代函数名即 ...

  9. C++ Primer 第五版 第6章 6.3——函数返回类型和return语句阅读笔记

    习题答案请参考:C++ Primer 第五版 第6章 6.3--函数返回类型和return语句习题答案 目录 6.3 返回类型和return语句 6.3.1 无返回值函数 无返回值函数的特性 6.3. ...

  10. C++ Primer 第五版 第6章 6.3——函数返回类型和return语句习题答案

    理论讲解请参考:C++ Primer 第五版 第6章 6.3--函数返回类型和return语句阅读笔记 目录 6.31 6.32 6.33 6.34 6.35 6.36 6.38 6.31 返回引用无 ...

最新文章

  1. 横河川仪压力变送器调零_YOKOGAWA/横河EJX110A差压变送器的性能误差和精度介绍!...
  2. python处理excel大数据-Python实现大数据收集至excel的思路详解
  3. python1080p壁纸高清图片_Python爬取高清桌面壁纸(附源码),直接运行即可
  4. 服务端接口中的那些坑
  5. c语言编程一个象棋游戏,急求:C语言编写的中国象棋游戏一个
  6. 2018-2019-2 实验四 Android程序设计
  7. servlet ---- 响应对象
  8. 远程键盘 App 被曝漏洞,成 Intel 弃子!
  9. Asp.net MVC 自定义路由
  10. 【Java必备技能二】防止表单重复提交方法
  11. Autodesk 3DSMax 2016 安装注册说明
  12. 如何下载飞思卡尔单片机的S19文件
  13. 亚马逊云科技的IoT+AI能力覆盖边缘与云,在云端提供稳定的云服务支持
  14. itext 5.3.0实现对pdf文件添加(文字和图片)水印
  15. Evil.js代码杀手
  16. 危化品道路运输车辆识别抓拍 YOLOv5
  17. 微信小程序怎么实现防止截屏
  18. 自定义漂亮的圆形进度条
  19. 高性能超融合服务器,深信服高性能单路服务器aServer-R-1600超融合一体机
  20. 安装Ubuntu镜像

热门文章

  1. 电脑 清理android,怎样清除系统垃圾 手机电脑一举拿下【图文】
  2. L2-1 特立独行的幸福 (25分)
  3. iOS应用安全Part1:搭建移动渗透测试平台
  4. 赵伯平--警惕台湾的企业管理垃圾!
  5. android 小球移动,android studio滑动小球移动
  6. Node与Express学习笔记3_版本控制与质量保证
  7. CentOS 7下限制ip访问
  8. CentOS 7.6基于lvm2快照备份恢复MariaDB
  9. jzoj1794 保镖排队 (树形dp)
  10. CFS任务的负载均衡