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


目录
相关文章
|
6月前
|
编译器 数据安全/隐私保护 C++
c++primer plus 6 读书笔记 第十三章 类继承
c++primer plus 6 读书笔记 第十三章 类继承
|
6月前
|
C++
C++ Primer Plus (第6版)中文版 (使用XMind整理)
C++ Primer Plus (第6版)中文版 (使用XMind整理)
C++ Primer Plus (第6版)中文版 (使用XMind整理)
|
6月前
|
C++
c++primer plus 6 读书笔记 第十四章 C++中的代码重用
c++primer plus 6 读书笔记 第十四章 C++中的代码重用
|
6月前
|
SQL 人工智能 算法
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer
|
6月前
|
程序员 C++
c++primer plus 6 读书笔记 第十二章 类和动态内存分配
c++primer plus 6 读书笔记 第十二章 类和动态内存分配
|
29天前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
50 2
|
1月前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
103 5
|
1月前
|
存储 编译器 C++
【c++】类和对象(中)(构造函数、析构函数、拷贝构造、赋值重载)
本文深入探讨了C++类的默认成员函数,包括构造函数、析构函数、拷贝构造函数和赋值重载。构造函数用于对象的初始化,析构函数用于对象销毁时的资源清理,拷贝构造函数用于对象的拷贝,赋值重载用于已存在对象的赋值。文章详细介绍了每个函数的特点、使用方法及注意事项,并提供了代码示例。这些默认成员函数确保了资源的正确管理和对象状态的维护。
90 4
|
1月前
|
存储 编译器 Linux
【c++】类和对象(上)(类的定义格式、访问限定符、类域、类的实例化、对象的内存大小、this指针)
本文介绍了C++中的类和对象,包括类的概念、定义格式、访问限定符、类域、对象的创建及内存大小、以及this指针。通过示例代码详细解释了类的定义、成员函数和成员变量的作用,以及如何使用访问限定符控制成员的访问权限。此外,还讨论了对象的内存分配规则和this指针的使用场景,帮助读者深入理解面向对象编程的核心概念。
108 4
|
2月前
|
存储 编译器 对象存储
【C++打怪之路Lv5】-- 类和对象(下)
【C++打怪之路Lv5】-- 类和对象(下)
32 4