编程从键盘输入三个整数其中最大得数_「C++ Primer plus 习题」第十七章编程题...

d83faeeab6463d44df2abd35e6b09b13.png

1.编写一个程序计算输入流中第一个$之前的字符数目,并将$留在输入流中。

答案:

#includeint main(){ using namespace std; cout << "Enter a text,report the number(end with $):"; int count=0; char r; while (cin.peek()!='$') { cin.get(r); if (r != '$') { cout << r; count++; } } char next; cin >> next; cout << "The number of character is: " << count << endl; cout << "And the next character is: " << next << endl; return 0;}

2.编写一个程序,将键盘输入(知道模拟的文件尾)复制到通过命令行制定的文件中。

答案:

#include#include#includeint main(int argc,char *argv[]){ using namespace std; if (argc != 2) { cerr << "Usage: " << argv[0] << " filename[s]"; exit(EXIT_FAILURE); } ofstream fin; fin.open(argv[1]);  if (!fin.is_open())  { cerr << "Could not open " << argv[0] << ""; exit(EXIT_FAILURE); }  cout << "Give me something you want add to the file(++ to end): "; string ch; while (cin.fail()==false)  { getline(cin, ch); cout << ch; fin << ch; } cout << "Input complete!"; fin.close(); return 0;}

3.编写一个程序,将一个文件复制到另一个文件中。让程序通过命令行获取文件名。如果文件无法打开,程序将支出这一点。

答案:

#include#includeint main(int argc, char *argv[]){ using namespace std; if (argc != 3) { cerr << "Usage: " << argv[0] << " resource filename[s] target[s]"; exit(EXIT_FAILURE); } ofstream fin; fin.open(argv[2]);  if (!fin.is_open()) { cerr << "Could not open " << argv[0] << ""; exit(EXIT_FAILURE); } ifstream fou; fou.open(argv[1]); if (!fou.is_open()) { cerr << "Could not open " << argv[0] << ""; exit(EXIT_FAILURE); } char c; while (fou.get(c)) { cout << c; fin.put(c);  } cout << "Copy complete!";  fou.close(); fin.close(); return 0;}

4.编写一个程序,它打开两个文本文件进行输入,打开一个文本文件进行输出。该程序将两个输入文件汇总对应的行并接起来,并用空格分隔,然后将结果写入DOA输出文件中。如果一个文件比另一个短,则将较长文件中余下的几行直接复制到输出文件中。例如,假设第一个输入文件的内容如下:

eggs kites donutsballoons hammersstones而第二个输入文件的内容如下:zero lassitudefinance drama则得到的文件的内容将如下:eggs kites donuts zero lassitudeballons hammers finance dramastones

答案:

#include#include#include#includeint main(){ using namespace std; ofstream fin; cout << "This program is connect two file content to another file."; cout << "Enter the frist filename: "; string filename; getline(cin, filename); fin.open(filename.c_str()); cout << "Now,give me something to the frist resource file:"; string file; while (cin.fail() == false)  { getline(cin, file); fin<

5.Mat和Pat想邀请他们的朋友来参加派对,就像第16章中的变成练习8那样,但现在他们希望程序使用文件。他们请您编写一个完成下述任务的程序。

a.从文本文件mat.dat中读取Mat朋友的姓名清单,其中每行为一个朋友。姓名将被存储在容器中,然后按顺序显示出来。

b.从文本文件pat.dat中读取Pat朋友的姓名清单,其中每行为一个朋友。姓名将被存储在容器中,然后按顺序显示出来。

c.合并两个清单,删除重复的条目,并将结果保存在文件matnpat.dat中,其中每行为一个朋友。

答案:

#include#include#include#include#include#include int main(){ using namespace std; ostream_iteratorout(cout, "");  ifstream fin; char filename[10]; setfresource; char temp[10]; string file; cout << "Enter the Mat friends filename: "; cin.getline(filename, 10); fin.open(filename); if (!fin.is_open()) { cerr << "Cant open the file " << filename << endl; exit(EXIT_FAILURE); } while (!fin.eof()) { fin.getline(temp,10); fresource.insert(temp);  cout << temp<fresource2;  while (!fin.eof()) { fin.getline(temp, 10); fresource2.insert(temp); cout << temp << endl; } fin.close(); cout << "Enter the Target filename: "; cin.getline(filename, 10); ofstream fou; fou.open(filename); settarget;  set_union(fresource.begin(), fresource.end(), fresource2.begin(), fresource2.end(), insert_iterator>(target, target.begin())); cout << "Friends they have:"; copy(target.begin(), target.end(), out); ostream_iteratorfileout(fou, "");  copy(target.begin(), target.end(), fileout); fou.close(); return 0;}

6.考虑14章的编程联系5中的类定义,如果还没有完成这个练习,请现在就做,然后完成下面的任务。

编写一个程序,它使用标准C++ I/O、文件I/O以及14章变成联系5中定义的employee、managerfink和highfink类型的数据。该程序应该包含程序清单17.17中的代码行,即允许用户将新数据添加到文件中。该程序首次被运行时,将要求用户输入数据,然后显示所有的数据,并将这些信息保存到一个文件中。当该程序再次被运行时,将首先读取并显示i文件中的数据,然后让用户添加数据,并显示所有的数据。差别之一是,应通过一个纸箱employee类型的指针数组来处理数据。这样,指针可以指向employee对象,也可以纸箱从employee派生出来的其他三种对象中的任何一种。使数组较小有助于检查程序,例如,您可能将数组限定为最多包含10个元素:

const int MAX = 10;...employee * pc[MAX];

为通过键盘输入,程序应使用一个菜单,让用户选择要创建的对象类型。菜单将使用一个switch,以便使用new来创建制定类型的对象,并将它的地址赋给pc数组的一个指针。然后该对象可以使用虚函数setall()来提示用户输入相应的数据:

pc[i]->setall(); //invokes function corresponding to type of object

为将数据保存到文件中,应设计一个虚函数writeall():

for(i = 0; i < index; i++) pc[i]->writeall(fout); //fout ofstream connected to output file

比较难处理的部分是使用文件恢复数据。问题在于:程序如何才能知道接下来要恢复的项目是employee对象、manager对象、fink对象还是highfink对象?一种方法时,在对象的数据写入文件时,在数据前面加上一个知识对象类型的整数。这样,在文件输入时,程序便可以读取该整数,并使用switch语句创建一个适当的对象来接受数据:

 enum classkind{Employee, Manager, Fink, Highfink}; //in class header ... int classtype; while((fin >> classtype).get(ch)){ switch(classtype){ case Employee: pc[i] = new employee; break; } }

然后便可以使用指针调用虚函数getall()来读取信息:

 pc[i++]->getall();

答案:

//fileemp.h#ifndef FILEEMP_H#define FILEEMP_H#include#include#includeusing std::string;using namespace std;enum classkin { Employee, Manager, Fink, Highfink };class abstr_emp{private: string fname; string lname; string job;public: abstr_emp() :fname("No name"), lname("no name "), job("no job") {}; abstr_emp(const string&fn, const string&ln, const string &j) :fname(fn) , lname(ln), job(j) {}; virtual void ShowAll()const; virtual void SetAll(); virtual void writeall(ofstream &fo);  virtual void getall(ifstream &fi);  friend std::ostream&operator<> inchargeof; }private: int inchargeof;}; class fink :virtual public abstr_emp{private: string reportsto;protected: const string ReportsTo()const { return reportsto; } string &ReportsTo() { return reportsto; } void FileReportsTo(std::ofstream &fou) {fou << reportsto;} void FileReportsTo(std::ifstream&fin) { fin >> reportsto; }public: fink() :abstr_emp(), reportsto("null") {}; fink(const string &fn, const string &ln, const string &j, const string &rpo) :abstr_emp(fn, ln, j), reportsto(rpo) {}; fink(const abstr_emp&e, const string &rpo) :abstr_emp(e), reportsto(rpo) {}; fink(const fink&e); virtual void ShowAll()const; virtual void SetAll(); void writeall(std::ofstream &fo);  void getall(std::ifstream &fi); };class highfink :public manager, public fink{public: highfink() {};  highfink(const string &fn, const string &ln, const string &j, const string &rpo, int ico) :abstr_emp(fn, ln, j), fink(fn, ln, j, rpo), manager(fn, ln, j, ico) {}; highfink(const abstr_emp&e, const string &rpo, int ico) :abstr_emp(e), fink(e, rpo), manager(e, ico) {}; highfink(const fink &f, int ico) :abstr_emp(f), fink(f), manager(f, ico) {}; highfink(const manager &m, const string &rpo) :abstr_emp(m), manager(m), fink(m, rpo) {}; highfink(const highfink&h) :abstr_emp(h), manager(h), fink(h) {}; virtual void ShowAll()const; virtual void SetAll(); void writeall(std::ofstream &fo);  void getall(std::ifstream &fi); };#endif // !EMP_H//fileemp.cpp#include"fileemp.h"using std::cout;using std::endl;using std::cin;//abstr_emp methods void abstr_emp::ShowAll()const{ cout <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值