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


目录
相关文章
|
8月前
|
编译器 数据安全/隐私保护 C++
c++primer plus 6 读书笔记 第十三章 类继承
c++primer plus 6 读书笔记 第十三章 类继承
|
8月前
|
C++
C++ Primer Plus (第6版)中文版 (使用XMind整理)
C++ Primer Plus (第6版)中文版 (使用XMind整理)
C++ Primer Plus (第6版)中文版 (使用XMind整理)
|
8月前
|
C++
c++primer plus 6 读书笔记 第十四章 C++中的代码重用
c++primer plus 6 读书笔记 第十四章 C++中的代码重用
|
8月前
|
SQL 人工智能 算法
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer
|
8月前
|
程序员 C++
c++primer plus 6 读书笔记 第十二章 类和动态内存分配
c++primer plus 6 读书笔记 第十二章 类和动态内存分配
|
4天前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
1天前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
1天前
|
安全 编译器 C语言
【C++篇】深度解析类与对象(中)
在上一篇博客中,我们学习了C++类与对象的基础内容。这一次,我们将深入探讨C++类的关键特性,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载、以及取地址运算符的重载。这些内容是理解面向对象编程的关键,也帮助我们更好地掌握C++内存管理的细节和编码的高级技巧。
|
1天前
|
存储 程序员 C语言
【C++篇】深度解析类与对象(上)
在C++中,类和对象是面向对象编程的基础组成部分。通过类,程序员可以对现实世界的实体进行模拟和抽象。类的基本概念包括成员变量、成员函数、访问控制等。本篇博客将介绍C++类与对象的基础知识,为后续学习打下良好的基础。
|
1月前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
70 19