神秘博客

C++ Primer Plus [第六版]第6章编程练习题答案

1

#include <iostream>
#include <cctype>

int main()
{
  using namespace std;
  char ch;

  while ((ch = cin.get()) != '@')
  {
    if (isdigit(ch))
      continue;
    else if (isupper(ch))
      cout.put(tolower(ch));
    else if (islower(ch))
      cout.put(toupper(ch));
    else
      cout.put(ch);
  }
  cout.put('\n');

  system("pause");
  return 0;
}

2

#include <iostream>
#include <array>

int main()
{
  using namespace std;
  const int SIZE = 10;
  array<double, SIZE> donation;
  int count = 0;
  double total = 0;
  for (int i = 0; i < SIZE; i++)
  {
    cout << "请输入第 " << i + 1 << " 个捐款: ";
    if (!(cin >> donation[i]))
      break;
    count++;
    total += donation[i];
  }
  if (count == 0)
    cout << "你没有输入内容.\n";
  else
  {
    double Average;  //平均值
    Average = total / count;
    cout << "总共 " << count << " 个捐款平均值为 " << Average << endl;
    for (int i = 0; i < count; i++)
    {
      if (donation[i] > Average)
        cout << "第 " << i + 1 << " 个大于平均值: " << donation[i] << endl;
    }
  }

  system("pause");
  return 0;
}

3

#include <iostream>

using namespace std;

void showmenu();

int main()
{
  char ch;

  showmenu();
  while (cin >> ch && ch != 'c'&&ch != 'p'&&ch != 't'&&ch != 'g')
  {
    cout << "请输入 c, p, t, or g: ";
    while (cin.get() != '\n')
      continue;
  }
  switch (ch)
  {
  case 'c':
    cout << "老虎是肉食动物.\n";
    break;
  case 'p':
    cout << "贝多芬是一位钢琴家.\n";
    break;
  case 't':
    cout << "枫树是一棵树.\n";
    break;
  case 'g':
    cout << "PS4是一个游戏主机.\n";
    break;
  default:
    break;
  }

  system("pause");
  return 0;
}

void showmenu()
{
  cout << "请输入以下选项之一: \n";
  cout << "c. 肉食动物 \tp. 钢琴家\n";
  cout << "t. 树 \t\tg. 游戏\n";
}

4

#include <iostream>
#define STRSIZE 20
using namespace std;

struct bop
{
  char fullname[STRSIZE];  //真实姓名
  char title[STRSIZE];  //职位
  char bopname[STRSIZE];  //秘密 BOP 姓名
  int preference;  // 0 = fullname, 1 = title, 2 = bopname
};

void showmenu();

int main()
{
  bop Bop[5] = {
  { "Wimp Macho","Junior Programmer","DOUM",0 },
  { "Raki Rhodes","Junior Programmer","HEI",1 },
  { "Celia Laiter","Junior Programmer","MIPS",2 },
  { "Hoppy Hipman","Analyst Trainee","WORLD",1 },
  { "Pat Hand","Analyst Trainee","LOOPY",2 }
  };
  char ch;
  showmenu();

  while ((ch = cin.get()) != 'q')
  {
    int count = 0;
    switch (ch)
    {
    case 'a':
      for (int i = 0; i < 5; i++)
        cout << Bop[i].fullname << endl;
      break;
    case 'b':
      for (int i = 0; i < 5; i++)
        cout << Bop[i].title << endl;
      break;
    case 'c':
      for (int i = 0; i < 5; i++)
        cout << Bop[i].bopname << endl;
      break;
    case 'd':
      for (int i = 0; i < 5; i++)
      {
        switch (Bop[i].preference)
        {
        case 0:
          cout << Bop[i].fullname << endl;
          break;
        case 1:
          cout << Bop[i].title << endl;
          break;
        case 2:
          cout << Bop[i].bopname << endl;
          break;
        } 
      }
      break;
    default:
      cout << "请输入 a, b, c, d, or q: ";
      while (cin.get() != '\n')
        continue;
      count++;
      break;
    }
    if (count == 0)
    {
      cout << "下一个选择: ";
      while (cin.get() != '\n')
        continue;
    }
  }
  cout << "再见!\n";

  system("pause");
  return 0;
}

void showmenu()
{
  cout << "仁慈的程序员报告: \n";
  cout << "a. 按姓名显示 \t\tb. 按职位显示\n";
  cout << "c. 按bop姓名显示 \td. 按优先顺序显示\n";
  cout << "q. 退出\n";
  cout << "请输入选择: ";
}

5

#include <iostream>

int main()
{
  using namespace std;
  double money;
  double tax;

  cout << "Neutronia 王国收入所得税计算\n";
  cout << "请输入你的收入: ";
  while (cin >> money && money > 0)
  {
    tax = 0;
    if (money > 5000 && money <= 15000)
      tax = (money - 5000)*0.1;
    else if (money > 15000 && money <= 35000)
      tax = (15000 - 5000)*0.1 + (money - 15000)*0.15;
    else if (money > 35000)
      tax = (15000 - 5000)*0.1 + (35000 - 15000)*0.15 + (money - 35000)*0.20;

    cout << "收入: " << money << " tvarps 所得税: " << tax << " tvarps\n";
    cout << "请输入你的收入: ";
  }

  system("pause");
  return 0;
}

6

#include <iostream>
#include <string>

using namespace std;
struct patrons
{
  string name; //捐款人姓名
  double money; //捐款数目
};

int main()
{
  int num;

  cout << "请输入捐款人数: ";
  cin >> num;
  patrons *Patrons = new patrons[num];
  for (int i = 0; i < num; i++)
  {
    cin.get();
    cout << "请输入第 " << i + 1 << " 个捐款人姓名: ";
    getline(cin, Patrons[i].name);
    cout << "请输入第 " << i + 1 << " 个捐款数额: ";
    cin >> Patrons[i].money;
  }
  int count = 0;
  cout << "\n重要捐款人 \n";
  for (int i = 0; i < num; i++)
  {
    if (Patrons[i].money > 10000)
    {
      count++;
      cout << "捐款人: " << Patrons[i].name << "  捐款数额: " << Patrons[i].money << endl;
    }
  }
  if (count == 0)
  {
    cout << "None.\n";
  }
  count = 0;
  cout << "\n普通捐款人 \n";
  for (int i = 0; i < num; i++)
  {
    if (Patrons[i].money <= 10000)
    {
      count++;
      cout << "捐款人: " << Patrons[i].name << "  捐款数额: " << Patrons[i].money << endl;
    }
  }
  if (count == 0)
  {
    cout << "None.\n";
  }

  delete[]Patrons;
  system("pause");
  return 0;
}

7

#include <iostream>
#include <cctype>
#include <string>

int main()
{
  using namespace std;
  string word;
  int vowel = 0;
  int consonant = 0;
  int other = 0;
  int count = 0;

  cout << "输入单词[q退出]: \n";
  while (cin >> word && word != "q")
  {
    if (isalpha(word[0]))
    {
      switch (word[0])
      {
      case 'a':
        vowel++;
        break;
      case 'e':
        vowel++;
        break;
      case 'i':
        vowel++;
        break;
      case 'o':
        vowel++;
        break;
      case 'u':
        vowel++;
        break;
      default:
        consonant++;
        break;
      }
    }
    else
      other++;

    count++;
  }
  if (count == 0)
    cout << "没有输入内容.\n";
  else
  {
    cout << "元音打头的单词数量: " << vowel << endl;
    cout << "辅音打头的单词数量: " << consonant << endl;
    cout << "其他数量: " << other << endl;
  }

  system("pause");
  return 0;

}

8

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  using namespace std;
  ifstream inFile;
  string name;

  cout << "请输入你要打开的文件名: ";
  cin >> name;
  inFile.open(name);
  if (!inFile.is_open())
  {
    cout << "打开文件失败, 请检查文件 " << name << " 是否存在.\n";
    exit(EXIT_FAILURE);
  }

  char ch;
  int count = 0;
  while (inFile.get(ch))
    count++;
  if (inFile.eof())
  {
    cout << "读取文件完成.";
    cout << "\n文件包含字符: " << count << " 个\n";
  }
  else
    cout << "读取文件错误.\n";
  
  inFile.close();
  system("pause");
  return 0;
}

9

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
struct patrons
{
  string name; //捐款人姓名
  double money; //捐款数目
};

int main()
{
  string name;

  cout << "请输入要打开的文件名: ";
  getline(cin, name);
  ifstream inFile;
  inFile.open(name);
  if (!inFile.is_open())
  {
    cout << "打开文件失败, 请检查文件 " << name << " 是否存在.\n";
    system("pause");
    exit(EXIT_FAILURE);
  }


  int num;
  inFile >> num;
  patrons *Patrons = new patrons[num];
  for (int i = 0; i < num; i++)
  {
    inFile.get();
    getline(inFile, Patrons[i].name);
    inFile >> Patrons[i].money;
  }
  int count = 0;
  cout << "\n重要捐款人 \n";
  for (int i = 0; i < num; i++)
  {
    if (Patrons[i].money > 10000)
    {
      count++;
      cout << "捐款人: " << Patrons[i].name << "  捐款数额: " << Patrons[i].money << endl;
    }
  }
  if (count == 0)
  {
    cout << "None.\n";
  }
  count = 0;
  cout << "\n普通捐款人 \n";
  for (int i = 0; i < num; i++)
  {
    if (Patrons[i].money <= 10000)
    {
      count++;
      cout << "捐款人: " << Patrons[i].name << "  捐款数额: " << Patrons[i].money << endl;
    }
  }
  if (count == 0)
  {
    cout << "None.\n";
  }
  delete[]Patrons;
  inFile.close();
  system("pause");
  return 0;
}

 

版权说明:
点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏