/****************************************
从文件grades_in.txt中读取等级表示符;
从文件scores_in.txt中读取学生成绩;
将划分好的成绩等级输出至文件scores_out.txt中;
成绩<60为F,60-70为D,70-80为C,80-90为B,90-100为A,100为A++.
*****************************************/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inf1("grades_in.txt");
ifstream inf2("scores_in.txt");
ofstream outf("scores_out.txt");
vector<string> vec;
string str;
int i=0;
while(inf1>>str)
{
vec.push_back(str);
}
while(inf2>>str)
{
string temp1;
string temp2;
int scores;
auto beg=str.begin();
auto end=str.end();
while(beg!=end && !(*beg>='0' && *beg<='9'))
{
temp1.push_back(*beg);
++beg;
}
if(outf<<temp1)
{
while(beg!=end)
{
temp2.push_back(*beg);
++beg;
}
const char *c=temp2.c_str(); //string转字符串
scores=atoi(c); //字符串转整型
}
if(scores<60)
outf<<vec[0]<<endl; //输出一个学生成绩等级后换行
else
{
outf<<vec[(scores-50)/10]<<endl;
}
}
return 0;
}
文件内容如下
gardes_in.txt :
scores_in.txt :
程序执行结果(输出文件scores_out.txt 内容)如下所示: