看完16章之后觉得又行了 直接去看了2天windows程序设计 看晕了又回来继续cpp了
17.8.1
#include <iostream>
using namespace std;
int main(void)
{
int count = 0;
cout << "请输入一串字符串:";
char temp[30];
cin.get(temp, 30, '$');
for(int i = 0; temp[i]; i++)
count++;
cout << "一共有" << count << "个字符" << endl;
return 0;
}
17.8.2
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
//为了简单就自认为打开文件并不会发生错误
ofstream out(argv[1]);
cout << "请输入一串字符串:";
int ch;
while((ch = cin.get()) != EOF)
out << (char)ch;
out.close();
return 0;
}
17.8.3
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
if(argc != 3)
{
cerr << "参数不够" << endl;
return 0;
}
char temp[255];
ifstream in(argv[1]);
ofstream out(argv[2]);
while(!in.eof())
{
in.get(temp, 255).get();
out << temp;
}
in.close();
out.close();
return 0;
}
17.8.4
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ifstream in1("one"), in2("two");
ofstream out("put");
char one[255], two[255];
bool o, t;
while(!(o = in1.eof()) || !(t = in2.eof()))
{
if(!o)
{
in1.get(one, 255).get();
out << one << ' ';
}
if(!t)
{
in2.get(two, 255).get();
out << two;
}
out << endl;
}
in1.close();
in2.close();
return 0;
}
17.8.5
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void show(string);
int main(void)
{
ifstream Mat("mat.dat"), Pat("pat.dat");
ofstream MPat("matnpat.dat");
vector<string> M, P, MP;
string Mdata, Pdata;
while(!Mat.eof())
{
getline(Mat, Mdata);
M.push_back(Mdata);
}
sort(M.begin(), M.end());
for_each(M.begin(), M.end(), show);
while(!Pat.eof())
{
getline(Pat, Pdata);
P.push_back(Pdata);
}
sort(P.begin(), P.end());
for_each(P.begin(), P.end(), show);
MP.insert(MP.end(), M.begin(), M.end());
MP.insert(MP.end(), P.begin(), P.end());
sort(MP.begin(), MP.end());
unique(MP.begin(), MP.end());
for(auto x : MP)
MPat << x << endl;
Mat.close();
Pat.close();