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

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

2017-06-04 16:45 267 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: