临近毛概考试,一位神师兄(还是说是师姐)祭出了一篇刷题神器:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ifstream fin("data.txt", ios::in);
if(!fin){
cout<<"file error!"<<endl;
system("pause");
return 0;
}
string line, temAnswer;
string CompareSet[] = {"ABCD", "ABC", "ABD", "ACD", "BCD", "AB", "AC", "AD", "BC", "BD",
"CD", "A", "B", "C", "D"};
int Counter[15];
for(int iter = 0; iter != 15; ++iter)
Counter[iter] = 0;
int wordsNum = 0, TotalNum_MultiCase = 0, TotalNum_SingleCase = 0;
bool latter, now;
while(!fin.eof()){
getline(fin, line);
string::iterator new_end = remove_if(line.begin(), line.end(), bind2nd(equal_to<char>(), ' '));
line.erase(new_end, line.end());
wordsNum = line.size();
temAnswer.clear();
latter = false;
for(int iter = 0; iter != wordsNum; ++iter){
if(line[iter] == 'A' || line[iter] == 'B'
|| line[iter] == 'C' || line[iter] == 'D'){
now = true;
}
else{
now = false;
}
if(now){
temAnswer.push_back(line[iter]);
}
if(latter && !now){
for(int r = 0; r != 15; ++r){
if(temAnswer == CompareSet[r]){
++Counter[r];
if(r >= 11)
++TotalNum_SingleCase;
else
++TotalNum_MultiCase;
break;
}
}
temAnswer.clear();
}
latter = now;
}
}
cout<<"Finish Read Data"<<endl;
cout<<"The Total Number of MultiCase_Answers is "<<TotalNum_MultiCase<<endl;
for(int iter = 0; iter != 11; ++iter){
cout<<CompareSet[iter]<<" : "<<Counter[iter]<<" Rate : "
<<static_cast<double>(Counter[iter]) / TotalNum_MultiCase * 100<<" 10^-2"<<endl;
}
cout<<endl;
cout<<"The Total Number of SingleCase_Answers is "<<TotalNum_SingleCase<<endl;
for(int iter = 11; iter != 15; ++iter){
cout<<CompareSet[iter]<<" : "<<Counter[iter]<<" Rate : "
<<static_cast<double>(Counter[iter]) / TotalNum_SingleCase * 100<<" 10^-2"<<endl;
}
system("pause");
}
这是运行结果,与原文统计的频率相仿。