c++ PRimer Plus(習(xí)題17.1)
//編寫一個程序計算$之前的字符數(shù),并且把它留在輸入流中#include<iostream>int main(){ using namespace std; cout << "Enter a text,report the number(end with $):/n"; int count=0; char r; while (cin.peek()!='$') //peek方法查看下一個字符 { cin.get(r); //用<<讀取會跳過空格 if (r != '$') { cout << r; count++; } } char next; cin >> next; cout << "/nThe number of character is: " << count << endl; cout << "And the next character is: " << next << endl; return 0;}c++ Primer Plus(習(xí)題17.2)//將鍵盤輸入弄到文件里面,直到模擬的文件結(jié)尾#include<iostream>#include<fstream>#include<string>int main(int argc,char *argv[]){ using namespace std; if (argc != 2) { cerr << "Usage: " << argv[0] << " filename[s]/n"; exit(EXIT_FAILURE); } ofstream fin; fin.open(argv[1]); //這是命令行的第二個參數(shù) if (!fin.is_open()) { cerr << "Could not open " << argv[0] << "/n"; exit(EXIT_FAILURE); } cout << "Give me something you want add to the file(<CTRL>+<Z>+<ENTER> to end): "; string ch; while (cin.fail()==false) //這里是用輸入流的方法來檢查文件結(jié)尾 { getline(cin, ch); cout << ch; fin << ch; } cout << "Input complete!/n"; fin.close(); return 0;}c++ Primer Plus(習(xí)題17.3)//和上一題差不多,不過是將一個文件的內(nèi)容復(fù)制到另一個文件//也是用命令行來指定文件的參數(shù)#include<iostream>#include<fstream>int main(int argc, char *argv[]){ using namespace std; if (argc != 3) { cerr << "Usage: " << argv[0] << " resource filename[s] target[s]/n"; exit(EXIT_FAILURE); } ofstream fin; fin.open(argv[2]); //這是命令行的第二個參數(shù) if (!fin.is_open()) { cerr << "Could not open " << argv[0] << "/n"; exit(EXIT_FAILURE); } ifstream fou; fou.open(argv[1]); if (!fou.is_open()) { cerr << "Could not open " << argv[0] << "/n"; exit(EXIT_FAILURE); } char c; while (fou.get(c)) { cout << c; fin.put(c); //逐字符讀取和寫入 } cout << "/nCopy complete!/n"; //程序出現(xiàn)一個問題,就是會把源文件最后的字符寫入兩下到目標(biāo)文件中 fou.close(); fin.close(); return 0;}c++ Primer Plus(習(xí)題17.4)//三個文件的輸入輸出,對文件進行拼接,并寫入到第三個文件中//這個程序是讓用戶給出兩個文件的內(nèi)容,然后程序顯示拼接后的結(jié)果//輸出文件有個小小的空格,這個問題很小,可是我不知道#include<iostream>#include<fstream>#include<cstdlib>#include<string>int main(){ using namespace std; ofstream fin; cout << "This program is connect two file content to another file./n"; 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:/n"; string file; while (cin.fail() == false) //檢查文件尾 { getline(cin, file); fin<<file << endl; } fin.close(); cin.clear(); cout << "Enter the second filename: "; string filename2; getline(cin, filename2); fin.open(filename2.c_str()); cout << "Now,give me someing to the second resource file:/n"; while (cin.fail() == false) { getline(cin, file); fin<<file << endl; } fin.close(); cin.clear(); cout << "Enter the target filename: "; string target; getline(cin, target); fin.open(target.c_str()); ifstream fou,fou2; fou.open(filename); fou2.open(filename2); if (!fou.is_open()||!fou2.is_open()) { cerr << "Could not open the source!"<<filename<<endl<<filename2<< "/n"; exit(EXIT_FAILURE); } string file2; while (!fou.eof()&&!fou2.eof()) { getline(fou, file); getline(fou2, file2); fin << file + ' ' + file2 << endl; cout<< file + ' ' + file2 << endl; } while (!fou.eof()) { getline(fou, file); fin << file << endl; cout << file << endl; } while (!fou2.eof()) { getline(fou2, file); fin << file << endl; cout << file << endl; } fou.close(); fou2.close(); fin.close(); return 0;}c++ Primer Plus(習(xí)題17.5)//這題和16章的差不多,不過是在文件里面讀取名字到容器里面//所以自己準(zhǔn)備好名字,容器這里有#include<iostream>#include<fstream>#include<string>#include<set>#include<algorithm>#include<iterator> //迭代器的頭文件int main(){ using namespace std; ostream_iterator<string, char>out(cout, "/n"); //輸出迭代器 ifstream fin; char filename[10]; set<string>fresource; 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<<endl; } fin.close(); cout << "Enter the Pat friends filename: "; cin.getline(filename, 10); fin.open(filename); if (!fin.is_open()) { cerr << "Cant open the file " << filename << endl; exit(EXIT_FAILURE); } set<string>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); set<string>target; //還是書上的老方法,用一個匿名的插入迭代器 set_union(fresource.begin(), fresource.end(), fresource2.begin(), fresource2.end(), insert_iterator<set<string>>(target, target.begin())); cout << "Friends they have:/n"; copy(target.begin(), target.end(), out); ostream_iterator<string, char>fileout(fou, "/n"); //輸出到文件迭代器,自己起名的 copy(target.begin(), target.end(), fileout); fou.close(); return 0;}c++ Primer Plus(習(xí)題17.6)//第十七章的17.6題,跟前面14.5的相似,不過是添加了一點東西//書上提供了解決方案,添加數(shù)據(jù)到文件中,保存讀寫數(shù)據(jù)的方法//對了,書上的getall()方法個人覺得是要帶參數(shù)的//這題打出來之后出現(xiàn)已有主體C2084錯誤,原因是該函數(shù)重定義了//去掉之后出現(xiàn)錯誤LNK2001,又是這個類設(shè)計很常見,起碼是我這里很常見的//主要是無法識別的外部命令,//錯誤原因:empoyee的虛函數(shù)沒有定義//我的文件讀取方式是/*0TripHarrisThumper*/#include<iostream>using namespace std;#include"fileemp.h"const int MAX = 10;void showClassType();int main(void){ abstr_emp *pc[MAX]; //不可以創(chuàng)建抽象類型的數(shù)組,但是可以創(chuàng)建指針 cout << "This program show the file of class Empoyee,input the data name to continue./n"; cout << "Enter the target file:"; char filename[MAX]; cin.getline(filename, MAX); ifstream fin; fin.open(filename); if (!fin.is_open()) { cerr << "Sorry ,can't open " << filename << endl; exit(EXIT_FAILURE); } char ch; int i = 0; int classtype; while (fin >> classtype) { switch (classtype) { case Employee:pc[i] = new employee; break; case Manager:pc[i] = new manager; break; case Fink:pc[i] = new fink; break; case Highfink:pc[i] = new highfink; break; } pc[i]->getall(fin); pc[i]->ShowAll(); i++; } fin.close(); ofstream fou; fou.open(filename, ios_base::out | ios_base::app); //追加模式打開文件 int idex=0; showClassType(); while (cin >> ch&&ch != 'q'&&idex < MAX) { cin.get(); switch (ch) { case 'a':pc[idex]= new employee; pc[idex]->SetAll(); break; case 'b':pc[idex] = new manager; pc[idex]->SetAll(); break; case 'c':pc[idex] = new fink; pc[idex]->SetAll(); break; case 'd':pc[idex] = new highfink; pc[idex]->SetAll(); break; default:cout << "Error type!/n"; break; } idex++; showClassType(); } for (i = 0; i < idex; i++) pc[i]->writeall(fou); fou.close(); fin.clear(); fin.open(filename); cout << "Here is the redice file:/n"; while ((fin >> classtype).get(ch)) { switch (classtype) { case Employee:pc[i] = new employee; break; case Manager:pc[i] = new manager; break; case Fink:pc[i] = new fink; break; case Highfink:pc[i] = new highfink; break; } pc[i]->getall(fin); pc[i]->ShowAll(); i++; } fin.close(); cout << "Bye!/n"; return 0;}void showClassType(){ cout << "What class type do you want to creat:/n " "a)Employee b)Manage c)Fink d)Highfink q)quit/n";}#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式//這個頭文件屬于14.5題書上提供的多態(tài)繼承頭文件的修改版,輸出數(shù)據(jù)到文件,//可以按照書上的方法進行修改#ifndef FILEEMP_H#define FILEEMP_H#include<iostream>#include<string>#include<fstream>using 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); //寫入到文件的虛函數(shù),又是這里出現(xiàn)無法識別的外部命令 virtual void getall(ifstream &fi); //從文件中讀取的虛函數(shù) friend std::ostream&Operator<<(std::ostream &os, const abstr_emp&e); virtual ~abstr_emp() = 0 {};//這里少了{(lán)}會報錯,鏈接錯誤一大堆};class employee :public abstr_emp{public: employee(const string &fn, const string&ln, const string&j) :abstr_emp(fn, ln, j) {}; employee() :abstr_emp() {}; virtual void ShowAll()const { abstr_emp::ShowAll(); }; virtual void SetAll() { abstr_emp::SetAll(); }; void writeall(std::ofstream &fo); //寫入到文件的虛函數(shù) void getall(std::ifstream &fi); //從文件中讀取的虛函數(shù)};class manager :virtual public abstr_emp{public: manager() :abstr_emp(), inchargeof(0) {}; manager(const string &fn, const string &ln, const string &j, int ico = 0) :abstr_emp(fn, ln, j), inchargeof(ico) {}; manager(const abstr_emp&e, int ico) :abstr_emp(e), inchargeof(ico) {}; manager(const manager&m); virtual void ShowAll()const; virtual void SetAll(); void writeall(std::ofstream &fo); //寫入到文件的虛函數(shù) void getall(std::ifstream &fi); //從文件中讀取的虛函數(shù)protected: int InChargeOf()const { return inchargeof; } //outut method int &InChargeOf() { return inchargeof; } //input method void FileChangeOf(std::ofstream &fou)const { fou << inchargeof << "/t"; } void FileChangeOf(std::ifstream&fin) { fin >> 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); //寫入到文件的虛函數(shù) void getall(std::ifstream &fi); //從文件中讀取的虛函數(shù)};class highfink :public manager, public fink{public: highfink() {}; //顯式調(diào)用基類構(gòu)造函數(shù) 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); //寫入到文件的虛函數(shù) void getall(std::ifstream &fi); //從文件中讀取的虛函數(shù)};#endif // !EMP_H#include"fileemp.h"using std::cout;using std::endl;using std::cin;//abstr_emp methodsvoid abstr_emp::ShowAll()const{ cout <<"/t/t"<< lname << "," << fname << "/t" << job;}//設(shè)置各成員的值void abstr_emp::SetAll(){ cout << "Enter frist name: "; std::getline(cin, fname); cout << "Enter last name: "; std::getline(cin, lname); cout << "Job: "; std::getline(cin, job);}//寫入數(shù)據(jù)到文件的方法void abstr_emp::writeall(ofstream &fo){ fo<<lname <<endl<< fname <<endl<<job<<endl;}void abstr_emp::getall(ifstream &fi) //書上沒有用參數(shù),想想不太可能{ std::getline(fi, lname); std::getline(fi, fname); std::getline(fi, job);}//only display namestd::ostream&operator<<(std::ostream &os, const abstr_emp&e){ os << "Name: " << e.lname << " , " << e.fname; return os;}//employee的類方法void employee::writeall(std::ofstream &fo){ fo << Employee; abstr_emp::writeall(fo);}void employee::getall(std::ifstream &fi){ abstr_emp::getall(fi);}//manage methodsmanager::manager(const manager&m) :abstr_emp(m) //copy construction function{ inchargeof = m.inchargeof;}void manager::ShowAll()const{ cout << endl; abstr_emp::ShowAll(); cout <<"/t"<< inchargeof;}void manager::SetAll(){ abstr_emp::SetAll(); cout << "Enter the inchangeof count: "; cin >> inchargeof; while (cin.get() != '/n') continue;}void manager::writeall(std::ofstream &fou){ fou << Manager; abstr_emp::writeall(fou); fou << inchargeof<<endl;}void manager::getall(std::ifstream &fin){ abstr_emp::getall(fin); fin >> inchargeof;}//fink methodsfink::fink(const fink&e) :abstr_emp(e){ reportsto = e.reportsto;}void fink::ShowAll()const{ cout << endl; abstr_emp::ShowAll(); cout << "/t" << reportsto;}void fink::SetAll(){ cout << "Enter the reportsto: "; std::getline(cin, reportsto); while (cin.get() != '/n') continue;}void fink::writeall(std::ofstream &fou){ fou << Fink; abstr_emp::writeall(fou); fou << reportsto<<endl;}void fink::getall(std::ifstream &fin){ abstr_emp::getall(fin); std::getline(fin,reportsto);}//highlink methodsvoid highfink::ShowAll()const{ cout << endl; abstr_emp::ShowAll(); //使用直接基類的保護方法進行輸出 cout<<"/t"<< manager::InChargeOf(); cout<<"/t" << fink::ReportsTo()<<endl;}void highfink::SetAll(){ abstr_emp::SetAll(); string temp; //這里要弄好一點,不然會設(shè)置基類兩次,也可以自己 int tem; cout << "Enter the inchangeof count: "; //用的是書上那個保護方法,返回引用就可以対值修改 cin >> tem; cin.get(); //處理換行符 cout << "Enter the reportsto: "; std::getline(cin, temp); ReportsTo() = temp; InChargeOf() = tem;}void highfink::writeall(std::ofstream &fou){ fou << Highfink; abstr_emp::writeall(fou); manager::FileChangeOf(fou); fink::FileReportsTo(fou);}void highfink::getall(std::ifstream &fin){ abstr_emp::getall(fin); manager::FileChangeOf(fin); fink::FileReportsTo(fin);}c++ Primer Plus(習(xí)題17.7)//大大的填空題//參考了別人的,才發(fā)現(xiàn)自己理解錯了#include<iostream>#include<fstream>#include<algorithm>#include<string>#include<vector>#include<iterator>void ShowStr(const std::string&s) { std::cout << s << std::endl; }void GetStrs(std::ifstream&fi, std::vector<std::string>&s);class Store{private: std::ofstream&fou;public: Store(std::ofstream&os) :fou(os) {}; void operator()(const std::string&s) { size_t len = s.size(); //size的返回類型,len存儲字符串的長度 fou.write((char*)&len, sizeof(std::size_t)); //先存儲長度,書上的用意很明顯 //這樣寫是先把字符串的長度len長度信息復(fù)制到相關(guān)聯(lián)的文件中 fou.write(s.data(), len); //這里是指定字符串的字符數(shù),把字字符串不包括'/0'寫入文件 }};int main(){ using namespace std; vector<string>vostr; string temp; cout << "Enter strings(empty line to quit):/n"; while (getline(cin, temp) && temp[0] != '/0') vostr.push_back(temp); cout << "Here is your input./n"; for_each(vostr.begin(), vostr.end(), ShowStr); ofstream fout("string.dat", ios_base::out | ios_base::binary); for_each(vostr.begin(), vostr.end(), Store(fout)); fout.close(); vector<string>vistr; ifstream fin("string.dat", ios_base::in | ios_base::binary); if (!fin.is_open()) { cerr << "Could not open file for input./n"; exit(EXIT_FAILURE); } GetStrs(fin, vistr); cout << "/nHere are the strings read from the file:/n"; for_each(vostr.begin(), vostr.end(), ShowStr); return 0;}void GetStrs(std::ifstream&fi, std::vector<std::string>&ve){ size_t len; //使用read來獲取長度,這個方法很贊 while (fi.read((char *)&len, sizeof(size_t))) { //這里要知道循環(huán)條件就是字符數(shù) char *st = new char[len]; fi.read(st, len); //len按我的理解就是每個字符所占據(jù)的二進制位數(shù) st[len + 1] = '/0'; //由于沒有存儲空字符,所以這里人為加 ve.push_back(st); }}
新聞熱點
疑難解答
圖片精選