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


相关文章
|
编译器 数据安全/隐私保护 C++
c++primer plus 6 读书笔记 第十三章 类继承
c++primer plus 6 读书笔记 第十三章 类继承
115 0
C++ Primer Plus (第6版)中文版 (使用XMind整理)
C++ Primer Plus (第6版)中文版 (使用XMind整理)
103 2
C++ Primer Plus (第6版)中文版 (使用XMind整理)
c++primer plus 6 读书笔记 第十四章 C++中的代码重用
c++primer plus 6 读书笔记 第十四章 C++中的代码重用
124 1
|
SQL 人工智能 算法
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer
|
程序员 C++
c++primer plus 6 读书笔记 第十二章 类和动态内存分配
c++primer plus 6 读书笔记 第十二章 类和动态内存分配
|
8月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
4月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
94 0
|
4月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
170 0
|
6月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
183 12
|
7月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
131 16