您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus(第6版) 第6章 编程练习

2013-05-31 21:07 465 查看
#include <iostream>
#include <cctype>
#include <array>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
struct bop {
string fullname;
string title;
string bopname;
int    preference;
};
struct patron {
string name;
double amount;
};
/*----------------------- exercise 1 -----------------------------*/
//int main(void)
//{
//  using namespace std;
//
//  char ch;
//
//  cout << "Please enter text for analysis, and type @ to terminate input.\n ";
//
//  while (cin.get(ch) && ch != '@'){
//      if (isalpha(ch)){
//          cout << (char)(islower(ch) ? toupper(ch) : tolower(ch));  //字符函数返回的都是int型值
//      }
//  }
//
//  return 0;
//}
/*----------------------- exercise 2 -----------------------------*/
//int main(void)
//{
//  using namespace std;
//
//  array<double, 10> donation;
//  double aver;
//  double sum = 0;
//  int i = 0;
//  int count = 0;
//
//  cout << "Please enter up to 10 numbers or type non-numeric to quit ";
//
//  while (cin >> donation[i] && i < 10){
//      sum += donation[i++];
//  }
//
//  aver = sum / i;
//
//  for (int j = 0; j < i; j++){
//      if (donation[j] > aver){
//          count++;
//      }
//  }
//  cout << "sum = " << sum << endl;
//  cout << "aver = " << aver << endl;
//  cout << "There have " << count << " num bigger than aver.\n";
//}
/*----------------------- exercise 3 -----------------------------*/
//int main(void)
//{
//  using namespace std;
//
//  char ch;
//
//  cout << "Please enter one of the following choices:\n"
//       << "c) carnivore p) pianist\n"
//       << "t) tree      g) game\n";
//
//  cin.get(ch);
//
//  while (ch != 'q'){
//      switch (ch){
//          case 'c':
//              cout << "a maple is a carnivore\n";
//              cin.get();  //read the '\n' in input stream
//              cin.get(ch);
//              break;
//          case 'p':
//              cout << "a maple is a pianist\n";
//              cin.get();
//              cin.get(ch);
//              break;
//          case 't':
//              cout << "a maple is a tree\n";
//              cin.get();
//              cin.get(ch);
//              break;
//          case 'g':
//              cout << "a maple is a game\n";
//              cin.get();
//              cin.get(ch);
//              break;
//          default :
//              cout << "Please enter a 'c', 'p', 't', 'g':";
//              cin.get();
//              cin.get(ch);
//              break;
//      }
//  }
//}
/*----------------------- exercise 4 -----------------------------*/
//int main(void)
//{
//  using namespace std;
//
//  struct bop bop_membs[5] = {
//      {"Wimp Macho", "Wimp Macho", "Winp tzw", 0},
//      {"Raki Rhodes", "Junior Programmer", "Junior tzw", 1},
//      {"Celia Laiter", "MIPS", "MIPS tzw", 1},
//      {"Hoppy Hipman", "Analyst Trainee", "Analyst tzw", 2},
//      {"Pat Hand", "Loopy", "Looppy tzw", 0}
//  };
//
//  cout << "Benevolent Order of Programmers Report\n"
//       << "a. display by fullname       b. display by title\n"
//       << "c. display by bopname      d. display by preference\n"
//       << "q. quit\n";
//
//  char ch;
//  cout << "Enter your choice: ";
//
//  while (cin >> ch){
//      switch (ch){
//          case 'a':
//              for (int i = 0; i < 5; i++){
//                  cout << bop_membs[i].fullname << endl;
//              }
//              cout << "Next choice: ";
//              break;
//          case 'b':
//              for (int i = 0; i < 5; i++){
//                  cout << bop_membs[i].title << endl;
//              }
//              cout << "Next choice: ";
//              break;
//          case 'c':
//              for (int i = 0; i < 5; i++){
//                  cout << bop_membs[i].bopname << endl;
//              }
//              cout << "Next choice: ";
//              break;
//          case 'd':
//              for (int i = 0; i < 5; i++){
//                  switch (bop_membs[i].preference){
//                      case 0:
//                          cout << bop_membs[i].fullname << endl;
//                          break;
//                      case 1:
//                          cout << bop_membs[i].title << endl;
//                          break;
//                      case 2:
//                          cout << bop_membs[i].bopname << endl;
//                          break;
//                      default :
//                          break;
//                  }
//              }
//              cout << "Next choice: ";
//              break;
//          case 'q':
//              cout << "Bye!\n";
//              break;
//          default :
//              cout << "Please enter a 'a', 'b', 'c', 'd': ";
//              break;
//      }
//      if (ch == 'q'){
//          break;
//      }
//  }
/*----------------------- exercise 5 -----------------------------*/
//int main(void)
//{
//  double income_tax;
//  double income;
//
//  cout << "Please enter your income: ";
//  cin >> income;
//
//  while (income >= 0){
//      if (income <= 5000){
//          income_tax = 0;
//      } else if (income <= 15000){
//          income_tax = (income - 5000) * 0.10;
//      } else if (income <= 35000){
//          income_tax = (income - 15000) * 0.15 + 10000 * 0.10;
//      } else {
//          income_tax = (income - 35000) * 0.20 + 20000 * 0.15 + 10000 * 0.10;
//      }
//
//      cout << "Your income_tax are " << income_tax << endl;
//      cout << "Please enter your income: ";
//      cin >> income;
//  }
//
//  return 0;
//}
/*----------------------- exercise 6 -----------------------------*/
//int main(void)
//{
//  int count;
//
//  cout << "Please enter the number of donors: ";
//  cin >> count;
//  const int cnt = count;
//  struct patron *ptr = new struct patron [count];
//  int i = 0;
//
//  while (count){
//      cout << "Please enter the name and amount: ";
//      cin >> ptr[i].name;
//      cin >> ptr[i].amount;
//      i++;
//      count--;
//  }
//
//  cout << "Grand Patrons:\n";
//    int grand_count = 0;
//  for (int i = 0; i < cnt; i++){
//      if (ptr[i].amount > 10000){
//          cout << ptr[i].name << endl;
//          grand_count++;
//      }
//  }
//  if (grand_count == 0){
//      cout << "none\n";
//  }
//
//  cout << "Patrons:\n";
//    int patron_count = 0;
//  for (int i = 0; i < cnt; i++){
//      if (ptr[i].amount <= 10000){
//          cout << ptr[i].name << endl;
//          patron_count++;
//      }
//  }
//  if (patron_count == 0){
//      cout << "none\n";
//  }
//
//  delete [] ptr;
//  return 0;
//}
/*----------------------- exercise 7 -----------------------------*/
//int main(void)
//{
//  string word;
//  int vowel_count = 0;
//  int consonante_count = 0;
//  int other_count = 0;
//
//  cout << "Enter words (q to quit)\n";
//  cin >> word;
//  while (word != "q"){
//      if (isalpha(word[0])){
//          switch (word[0]){
//              case 'a':
//              case 'e':
//              case 'i':
//              case 'o':
//              case 'u':
//              case 'A':
//              case 'E':
//              case 'I':
//              case 'O':
//              case 'U':
//                  vowel_count++;
//                  break;
//              default :
//                  consonante_count++;
//                  break;
//          }
//      } else {
//          other_count++;
//      }
//      cin >> word;
//  }
//
//  cout << vowel_count << " words beginning with vowels\n";
//  cout << consonante_count << " words beginning with consonante\n";
//  cout << other_count << " other\n";
//
//  return 0;
//}

/*----------------------- exercise 8 -----------------------------*/
//int main(void)
//{
//  ifstream fin;
//  char ch;
//  int count = 0;
//  fin.open("testfile");
//
//  if (fin.is_open()){
//  while (!fin.eof()){
//      fin >> ch;
//      count++;
//  }
//
//  cout << "There have " << count << " characters\n";
//
//  }
//  return 0;
//}
/*----------------------- exercise 9 -----------------------------*/
int main(void)
{
ifstream fin;
struct patron *ptr = new struct patron [10];
int i = 0;
int cnt = 0;
fin.open("testfile");
if (fin.is_open()){
while (fin.good()){
fin >> ptr[i].amount;
fin.get();
getline(fin, ptr[i].name);
cnt++;
i++;
}
cout << "Grand Patrons:\n";
int grand_count = 0;
for (int i = 0; i < cnt; i++){
if (ptr[i].amount > 10000){
cout << ptr[i].name << endl;
grand_count++;
}
}
if (grand_count == 0){
cout << "none\n";
}

cout << "Patrons:\n";
int patron_count = 0;
for (int i = 0; i < cnt; i++){
if (ptr[i].amount <= 10000){
cout << ptr[i].name << endl;
patron_count++;
}
}
if (patron_count == 0){
cout << "none\n";
}
}
fin.close();
return 0;
}
水平有限,错误之处还请指正,谢谢!

本文出自 “谭智伟的博客” 博客,请务必保留此出处http://longtzw.blog.51cto.com/7219689/1213986
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: