【6.9】c++ primer plus 课后编程答案

来源:互联网 发布:国内熟女网络视频 编辑:程序博客网 时间:2024/06/05 03:55

C++ PRIMER PLUS 课后答案 
使用IDE为window7系统下的VS2010



#include <iostream>#include <Windows.h>#include <cctype>#include <fstream>#include <string>#include <iomanip>using namespace std;const int money_limit=10000;const int fsize=20;struct patrons{         charname[fsize];         doublemoney;};  int main(){         int size;               //标志         bool flag=true;         int read_flag=0;                  //逐行读取标志位         char* m=new char [15];         //数字缓冲区         char* filename=new char [fsize];              ifstream infile;         cout<<"pleaseinput file's name : ";         cin.getline(filename,fsize);         infile.open(filename);         if(!infile.is_open())         {                  cout<<"can'topen this file!"<<endl;                      system("pause");                  exit(1);         }         infile.getline(m,15,'\n');                //逐行读取,已\n结束         size=atoi(m);                          //atoi 将字符串转换成整形         patrons * p =new patrons[size];         while((infile.good())&&(!infile.eof())&&(read_flag<size))         {                  infile.getline(p[read_flag].name,fsize,'\n');                          infile.getline(m,15,'\n');                  p[read_flag].money=atof(m);                                     read_flag++;         }         delete[]filename;         deletem;         cout<<left<<setw(30)<<"Grandpatrons"<<setw(15)<<"money"<<endl;           //setw 占用多少位再<iomanip>中         for(int i=0;i<size;i++)         {                  if(p[i].money>money_limit)                  {                          cout<<left<<setw(30)<<p[i].name<<setw(15)<<p[i].money<<endl;                          flag=false;                  }         }         if(flag)                  cout<<endl<<right<<setw(22)<<"!!None!!"<<endl<<endl;         else{                  flag=true;         }         cout<<endl<<left<<setw(30)<<"patrons"<<setw(15)<<"money"<<endl;         for(int i=0;i<size;i++)         {                  if(p[i].money<=money_limit)                  {                          cout<<left<<setw(30)<<p[i].name<<setw(15)<<p[i].money<<endl;                          flag=false;                  }         }         if(flag)                  cout<<endl<<right<<setw(22)<<"!!None!!"<<endl<<endl;         delete[]p;         system("pause");         return 0;} 


原创粉丝点击