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

C++ 的简单应用

2009-04-22 16:21 393 查看
好久没用C++了, 今天在百度知道上看到一个人的提问, 于是就小试了一下。

题目: 数据结构


悬赏分:30 - 离问题结束还有 14 天 17 小时
一个班的高等数学、英语和数据结构成绩存放在一个指定文件中,根据平时成绩按从高到底的排列顺序输出学生的学号姓名和各科成绩以及平均分,并保存在另一个指定文件中。
要求只有一个:程序要完整~~~
程序是很简单, 不过还是活动了一下大脑。
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<fstream>
#include<algorithm>
using namespace std;

typedef struct tagStudentInfo {
string strName;
int    nStudentID;
double ldMathScore;
double ldEnglishScore;
double ldDSScore;
double ldMeanScore;
} StudentInfo;

bool ReadInputFile(string strFileName, vector<StudentInfo> & aInfoArr)
{
string strLine;
ifstream infile(strFileName.c_str());
if (!infile) return false;

while (getline(infile, strLine)) {
if (strLine.length() == 0 || strLine.at(0) == '#') continue;

StudentInfo  info;
std::stringstream fileline(strLine);
fileline >> info.strName;
while (fileline >> info.nStudentID >> info.ldMathScore >> info.ldEnglishScore >> info.ldDSScore){};
info.ldMeanScore = (info.ldMathScore + info.ldEnglishScore + info.ldDSScore) / 3;
aInfoArr.push_back(info);
}

infile.close();

return true;
}

bool WriteOutputFile(string strFileName,  vector<StudentInfo> & aInfoArr)
{
vector<StudentInfo>::iterator it;
ofstream  outfile(strFileName.c_str());

if (!outfile) return false;

if (aInfoArr.size() > 0) {
outfile << "#----------------------------------------------------------------------------/n";
outfile << "#Name" << "/t"  << "ID" << "/t" << "Math" << "/t" << "English" /
<< "/t" << "Data" << "/t" << "MeanScore" << "/n";
outfile << "#----------------------------------------------------------------------------/n";
}

for (it = aInfoArr.begin(); it != aInfoArr.end(); ++it)	{
outfile << it->strName << "/t" << it->nStudentID << "/t" << it->ldMathScore << "/t"  /
<< it->ldEnglishScore << "/t" << it->ldDSScore << "/t" << it->ldMeanScore << "/n";
}

outfile.close();
return true;
}

int ArrCompareFunc(const StudentInfo& info1, const StudentInfo& info2)
{
return (info1.ldMeanScore - info2.ldMeanScore) > 0.000001;
}

int main()
{
string strInputFile("c://data.txt");
string strOutputFile("c://outfile.txt");

vector<StudentInfo>  infoArr;

if (!ReadInputFile(strInputFile, infoArr))  return 1; // Fail to read input file;

sort(infoArr.begin(), infoArr.end(), ArrCompareFunc);

if (!WriteOutputFile(strOutputFile, infoArr)) return 2; // Fail to write output file;

return 0;
}

通过main()中这两行来修改输入输出文件
string strInputFile("c://data.txt");
string strOutputFile("c://outfile.txt");

输入格式如下:
#---------------------------------
#Name ID Math English Data
#---------------------------------
abc 123 90 20 50
cde 124 30 70 80
#test
aaa 125 70 70 70
bbb 126 80 80 20
第一个字符为#的表示这一行为注释行, 这行的数据不会读取。

输出格式:
#------------------------------------------------------
#Name ID Math English Data MeanScore
#------------------------------------------------------
aaa 125 70 70 70 70
cde 124 30 70 80 60
bbb 126 80 80 20 60
abc 123 90 20 50 53.3333
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: