C++ Primer Plus 第六章答案 分支语句和逻辑运算符

简介: 只有聪明人才能看见的摘要~( ̄▽ ̄~)~

 复习题

//6.10
//1
两个版本将给出相同的答案,但是if - else效率更高,因为第一个版本在检测到空格后再次判断是否为换行符
而第二个版本不会检测,因此不会浪费时间
//2
++ch和ch + 1得到的数值相同,但是++ch的类型为char,而ch + 1的类型为int,一个输出字符一个输出数字
//3
漏了一个等号
Hi!
H$i$!$
$Send $10 or $20 now!
S$e$n$d$ $ct1 = 0, ct2 = 9
会读取换行符,因此输出\n后再在第二行输出一个$
//4
wieght >= 125 && weight < 125;
ch == 'q' || ch == 'Q';
!(x % 2) && x != 26;
!(x % 2) && x % 26;
guest == 1 || donation >= 1000 && donatin <= 2000;
(ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A');
//5
不一样,x为int和x为bool
//6
(x > 0) ? x : -x;
//7
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++;
}
//8
如果使用整数标签,当用户输入了非整数时,程序会因为整数输入不能处理字符而挂起
但是如果使用字符标签,用户输入整数时可以把整数当作字符处理,执行default部分
//9
int line = 0;
char ch;
while (cin.get(ch) && ch!= 'Q');
{
  if (ch == '\n')
    line++;
}

image.gif

编程练习

//第六章编程练习
#include <iostream>
#include<cctype>
#include<iomanip>                               //使用setw(),控制输出格式,practice 3
#include<string>
#include<fstream>
using namespace std;
//practice 1
void p6_1()
{
  cout << "Enter text for output,and @ to terminate input.\n";
  char ch = 0;
  while ((ch = cin.get()) != '@')
  {
    if (isdigit(ch))
      continue;
    else if (islower(ch))
      cout << (char)toupper(ch);
    else if (isupper(ch))
      cout << (char)tolower(ch);
    else
      cout << ch;
  }
  cout << "Done!"<<endl;
}
//practice 2
void p6_2()
{
  double name[10];
  int num = 0;
  double average = 0;
  double sum = 0;
  cout << "Please enter most 10 numbers:\n";
  int enter = 0;
  while (enter<10 && cin >> name[enter])
  {
    sum += name[enter];
    enter++;
  }
  average = sum / enter;
  for (int i = 0; i < enter; i++)
    if (name[i] > average)
      num++;
  if (sum == 0)
    cout << "There is no input effective!" << endl;
  else
    cout << "There are " << num << " numbers bigger than the average of them :  "
    << average << endl;
}
//practice 3
void p6_3()
{
  cout << "Please enter one of the following choices:\n";
  cout.flags(ios::left);                                      //定义setw为左对齐
  cout << setw(20) << "c) carnivore" << "p) pianist\n";
  cout << setw(20) << "t) tree" << "g) game\n";
  char ch = 0;
  bool exit = 0;
    while (cin >> ch && !exit)                        //不成功需要重新输入,因此需要变量查看是否已输入正确的字符
    {
      switch (ch)
      {
      case 'c':
        cout << "one\n";
        exit = 1;
        break;
      case 'p':
        cout << "two\n";
        exit = 1;
        break;
      case 't':
        cout << "A maple is a tree.\n";
        exit = 1;
        break;
      case 'g':
        cout << "four\n";
        exit = 1;
        break;
      default:cout << "Please enter a c, p, t, or g: ";
      }
    }
}
//practice 4
const unsigned int Arsize = 50;
struct bop                                                           //定义结构
{
  char fullname[Arsize];
  char title[Arsize];
  char bopname[Arsize];
  int preference;
};
void p6_4()
{
  cout << "Benevolent Order of Programmers Reports\n";                                   
  cout << left << setw(30) << "a. display by name" << "b. display by title\n"; ;      //定义setw为左对齐
  cout << left << setw(30) << "c. display by bopname" << "d. display by preference\n";
  cout << "q. quit\n";
  const struct bop boparray[5] =                                    //初始化
  {
    {"Wimp Macho",    "one title",      "one bopname",    0},
    {"Raki Rhodes",   "Junior Programmer",  "two bopname",    1},
    {"Celia Laiter",  "three title",      "MIPS",       2},
    {"Hoppy Hipman",  "Analyst Trainee",    "four bopname",   1},
    {"Pat Hand",    "five title",     "LOOPY",      2}
  };
  char ch = 0;
  cout << "Enter your choice: ";
  int exit = 0;
  while (cin>>ch)
  {
    switch (ch)
    {
    case'a':
      for (int i = 0; i < 5; i++)
        cout << boparray[i].fullname << endl;
      break;
    case'b':
      for (int i = 0; i < 5; i++)
        cout << boparray[i].title << endl;
      break;
    case'c':
      for (int i = 0; i < 5; i++)
        cout << boparray[i].bopname << endl;
      break;
    case'd':
      for (int i = 0; i < 5; i++)
        if (boparray[i].preference == 0)
          cout << boparray[i].fullname << endl;
        else if (boparray[i].preference == 1)
          cout << boparray[i].title << endl;
        else if (boparray[i].preference == 2)
          cout << boparray[i].bopname << endl;
      break;
    case'q':
      exit = 1;
      cout << "Bye!" << endl;
    }
    if (exit)
      break;
    cout << "Next choice: ";
  }
}
//practice 5
void p6_5()
{
  cout << "Enter your income(by tvarp): ";
  double income = 0;
  double tax = 0;
  while (cin >> income && income > 0)
  {
    if (income <= 5000)
      tax = 0;
    else if (income <= 15000)
      tax = (income - 5000) * 0.1;
    else if (income <= 35000)
      tax = 10000 * 0.1 + (income - 15000) * 0.15;
    else
      tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.2;
    cout << "Your personal income tax is: " << tax << endl;
  }
  cout << "Exit!\n";
}
//practice 6
void p6_6()
{
  struct juanxian
  {
    string name;
    double num = 0;
  };
  int people = 0;
  cout << "Enter the number of people who has donated: ";
  cin >> people;                                               //cin读取数据后要用cin.get()读取换行符才能使用getline()
  cin.get();
  struct juanxian* pd = new struct juanxian[people];
  cout << "Enter the name and the number of donation: \n";
  for (int i = 0; i < people; i++)
  {
    cout << "Enter the name of " << i + 1 << " contributor: ";
    getline(cin,(pd+i)->name);                           //三种写法:pd[i].name   (*(pd+i)).name    (pd+1)->name
      cout << "Enter the amount of donation: ";
    cin >> pd[i].num;
    cin.get();
  }
  int one = 0;
  int two = 0;
  cout << "Grand Patrons:\n";
  for (int i = 0; i < people; i++)
  {
    if (pd[i].num > 10000)
    {
      cout << pd[i].name << " : " << pd[i].num << endl;
      one++;
    }
  }
  if (one == 0)
    cout << "None!\n";
  cout << "Patrons:\n";
  for (int i = 0; i < people; i++)
  {
    if (pd[i].num <= 10000)
    {
      cout << pd[i].name << " : " << pd[i].num << endl;
      two++;
    }
  }
  if (two == 0)
    cout << "None!\n";
  delete[]pd;
}
//practice 7
void p6_7()
{
  cout << "Enter words (q to quit): \n";
  string word;
  int yuan = 0;
  int fu = 0;
  int none = 0;
  while (cin >> word)
  {
    if (word.length() == 1 && word[0] == 'q')
      break;
    if (isalpha(word[0]))
    {
      switch (word[0])
      {
      case'a':
      case'o':
      case'i':
      case'e':
      case'u':yuan++; break;
      default:fu++;
      };
    }
    else none++;
  }
  cout << yuan << " words beginning with vowels\n";
  cout << fu << " words beginning with consonan\n";
  cout << none << " others\n";
}
//practice 8
void p6_8()
{
  ifstream infile;
  infile.open("practice.txt");
  char ch;
  int num = 0;
  while((ch = infile.get()) != EOF)
    num++;
  cout << "There are " << num << " characters in file!\n";
}
//practice 9
void p6_9()
{
  ifstream infile;
  infile.open("juankuan.txt");
  struct juanxian
  {
    string name;
    double num = 0;
  };
  int people = 0;
  infile >> people;                                               //cin读取数据后要用cin.get()读取换行符才能使用getline()
  infile.get();
  juanxian* pd = new juanxian[people];
  for (int i = 0; i < people; i++)
  {
    getline(infile, (pd + i)->name);                           //三种写法:pd[i].name   (*(pd+i)).name    (pd+1)->name
    infile >> pd[i].num;
    infile.get();
  }
  int one = 0;
  int two = 0;
  cout << "Grand Patrons:\n";
  for (int i = 0; i < people; i++)
  {
    if (pd[i].num > 10000)
    {
      cout << pd[i].name << " : " << pd[i].num << endl;
      one++;
    }
  }
  if (one == 0)
    cout << "None!\n";
  cout << "Patrons:\n";
  for (int i = 0; i < people; i++)
  {
    if (pd[i].num <= 10000)
    {
      cout << pd[i].name << " : " << pd[i].num << endl;
      two++;
    }
  }
  if (two == 0)
    cout << "None!\n";
  delete[]pd;
}
int main()
{
  p6_9();
  return 0;
}

image.gif


目录
相关文章
|
4月前
|
存储 关系型数据库 编译器
C++ Primer Plus 第6版 读书笔记(9)第 9章 函数——内存模型和名称空间
C++ Primer Plus 第6版 读书笔记(9)第 9章 函数——内存模型和名称空间
47 1
|
1月前
|
C++
来自C++ Primer 5的函数指针的定义,调用等
来自C++ Primer 5的函数指针的定义,调用等
10 0
|
3月前
|
编译器 Linux C语言
C++Primer 【学习笔记】第一章 深思
main 函数在很多方面都比较特别,其中最重要的是每个 0+程序必须含有 main 函数,且 main 函数是(唯一)被操作系统显式调用的函数。
29 0
|
4月前
|
存储 编译器 程序员
C++ Primer Plus 第6版 读书笔记(10) 第十章 类与对象
C++ Primer Plus 第6版 读书笔记(10) 第十章 类与对象
33 0
|
4月前
|
存储 算法 编译器
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
30 1
|
4月前
|
存储 Java 编译器
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(一)
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(一)
20 0
|
1天前
|
存储 C++
C++类与对象【多态】
C++类与对象【多态】
|
1天前
|
存储 编译器 C++
C++类与对象【继承】
C++类与对象【继承】
|
1天前
|
编译器 C++
C++类与对象【运算符重载】
C++类与对象【运算符重载】
|
1天前
|
C++
C++类与对象【友元】
C++类与对象【友元】