C++ 的简单应用
2009-04-22 16:21
393 查看
好久没用C++了, 今天在百度知道上看到一个人的提问, 于是就小试了一下。
题目: 数据结构
悬赏分:30 - 离问题结束还有 14 天 17 小时
一个班的高等数学、英语和数据结构成绩存放在一个指定文件中,根据平时成绩按从高到底的排列顺序输出学生的学号姓名和各科成绩以及平均分,并保存在另一个指定文件中。
要求只有一个:程序要完整~~~
程序是很简单, 不过还是活动了一下大脑。
通过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
题目: 数据结构
悬赏分: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
相关文章推荐
- C++容器排序算法的简单应用
- 小P寻宝记——粗心的基友(c++ 滚动数组的简单应用)
- linux下c/c++实例之十socket简单应用
- C++学习之电话薄功能(类的简单应用)
- C++下Berkeley DB的配置和简单应用
- [C++]数据结构:散列表HashTable的实现与简单应用
- C++ freopen简单应用
- C++ freopen简单应用
- 图的简单应用(C/C++实现)
- C++对fabs的简单应用
- C++ map的简单应用
- C++ find函数简单应用
- c++中虚函数的简单应用
- 【C++】异常处理的简单应用。
- C++ 容器的综合应用的一个简单实例——文本查询程序
- linux下c/c++实例之十socket简单应用
- linux下c/c++实例之十socket简单应用
- OpenMP并行编程应用 简单实例 C++ 多线程
- C/C++中substr函数的应用(简单讲解)
- C++中赋值函数,拷贝函数的简单应用