主题:[讨论]本人新手,求个小编程
Greenshepher
[专家分:0] 发布于 2008-12-02 16:00:00
1. Read input data from a data file.
2. Print the total number of words in the file.
3. Print all the words begin with “s”
4. Print the word with the most number of alphabet of “m” in that word
5. The total numbers of words ended with “e”
QQ 519080496 谁会的给我发下,谢谢了..拜托..`[em5]
最后更新于:2008-12-02 16:02:00
回复列表 (共2个回复)
沙发
shekaka [专家分:1630] 发布于 2008-12-05 20:48:00
看看行不行?
[code=c]
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main() {
string inFileName;
cout << "Input the file name: ";
cin >> inFileName;
ifstream inFile(inFileName.c_str());
if(!inFile) {
cerr << "error: unable to open the file: " << inFileName << endl;
return -1;
}
string outFileName = inFileName + ".out";
ofstream outFile(outFileName.c_str());
if(!outFile) {
cerr << "error: unable to open the file: " << outFileName << endl;
return -2;
}
string buffer;
vector<string> text_s, text_e, text_m;
int cnt = 0, cntMax = 0, cnt_m = 0;
int cnt_e = 0;
while(inFile >> buffer) {
cnt++;
if(*buffer.begin() == 's' || *buffer.begin() == 'S')
text_s.push_back(buffer);
if(*(buffer.end() - 1) == 'e' || *buffer.end() == 'E')
cnt_e++;
const char* iterator = buffer.begin();
while(iterator != buffer.end()) {
if(*iterator == 'm' || *iterator == 'M') cnt_m++;
iterator++;
}
if(cnt_m > cntMax) {
text_m.clear();
text_m.push_back(buffer);
cntMax = cnt_m;
} else if(cnt_m == cntMax) {
text_m.push_back(buffer);
}
cnt_m = 0;
}
outFile << "1. the total number of words in the file:\n" << cnt << endl;
outFile << "\n2. all the words begin with 's':" << endl;
vector<string>::iterator iter;
for(iter = text_s.begin(); iter != text_s.end(); iter++)
outFile << *iter << " ";
outFile << "\n\n3. the words with the most number of alphabet of 'm' in that word:\n";
for(iter = text_m.begin(); iter != text_m.end(); iter++)
outFile << *iter << " ";
outFile << "\n\n4. the total numbers of words ended with 'e':\n" << cnt_e << endl;
return 0;
}
[/code]
板凳
moke9 [专家分:30] 发布于 2010-09-02 07:56:00
你好.我是全职网赚工作者.
如果你有时间有电脑.
想在网络上创业.请联系我..
项目绝对真实.详情QQ空间资料
加盟请联系 QQ908889846
我来回复