- #include<iostream>
- #include<fstream>
- #include<string>
- #include<iomanip>
- #include<Windows.h>
- #include<algorithm>
- using namespace std;
- int maxm(int x,int y)//返回最大数
- {
- return x>y?x:y;
- }
- struct student
- {
- int number;//存放号数
- char name[10];//存放姓名
- string answer;//存放各题答案
- int zong_fen;//存放总分
- int count;//存放答对题个数
- int record;//记录排名
- };
- struct grade
- {
- int number;//存放出现最多选项的次数
- char a;//存放标准答案
- };
- void print(grade[],double);//输出标准答案
- void cha_xun(student*,int);//查询学生成绩
- student* tong_ji(student*,grade[],int);//统计学生成绩
- void pai_xu(student*,int,int);//进行排序
- void shu_chu(student*,int);//输出试卷分析到外部文件
- int main()
- {
- string s;
- int i(0),m,n,n1,A(0),B(0),C(0),D(0);
- grade cheng_ji[35];//用于存放标准答案
- system("color 1e");
- student *person;
- person=new student[1000];//最多可处理1000个学生的信息,也可动态处理
- cout<<setw(32)<<setfill(' ')<<' '<<"模拟改卷系统\\n";
- cout<<"所处理文件内容的格式应如下:\\n";
- cout<<"1 赵光 CADADCDCDACCCDACCBCAAACCDBBCBDDBBAD\\n";
- cout<<"2 罗志 CADBDCBCDADCABACBCCACACCAABCBDDBCAB\\n";
- cout<<"3 闫谷凡 CDCBDCCCDBDBBBCCBBCACACBAABCBDDBCAB\\n";
- cout<<"......\\n";
- cout<<"请将学生数据所在的此类文件拖到此处:\\n";
- cin>>s;
- ifstream f(s.c_str());
- system("cls");
- while(!f.eof()) //从文件中输入学生成绩信息
- {
- f>>person[i].number;
- f>>person[i].name;
- f>>person[i].answer;
- i++;
- }
- f.close();
- for(int r=0;r<35;r++)
- {
- for(int j=0;j<i-1;j++)
- {
- if(person[j].answer[r]=='A')
- A++;
- if(person[j].answer[r]=='B')
- B++;
- if(person[j].answer[r]=='C')
- C++;
- if(person[j].answer[r]=='D')
- D++;
- }
- m=maxm(maxm(maxm(A,B),C),D);//统计出现次数最多的答案
- if(m==A)
- {
- cheng_ji[r].a='A';
- cheng_ji[r].number=A;
- }
- if(m==B)
- {
- cheng_ji[r].a='B';
- cheng_ji[r].number=B;
- }
- if(m==C)
- {
- cheng_ji[r].a='C';
- cheng_ji[r].number=C;
- }
- if(m==D)
- {
- cheng_ji[r].a='D';
- cheng_ji[r].number=D;
- }
- A=0;
- B=0;
- C=0;
- D=0;
- }
- person=tong_ji(person,cheng_ji,i-1);//统计每个学生的总分
- //system("cls");
- q:cout<<setw(36)<<setfill(' ')<<' '<<"主菜单\\n";
- cout<<setw(32)<<setfill(' ')<<' '<<"1.输出参考答案\\n";
- cout<<setw(32)<<setfill(' ')<<' '<<"2.查询学生成绩\\n";
- cout<<setw(32)<<setfill(' ')<<' '<<"3.按升序对成绩进行排序\\n";
- cout<<setw(32)<<setfill(' ')<<' '<<"4.按降序对成绩进行排序\\n";
- cout<<setw(32)<<setfill(' ')<<' '<<"5.输出成绩及试卷分析\\n\\n";
- cout<<"请选择:"<<endl;
- cin>>n;
- if(n==1)
- {
- print(cheng_ji,i-1);//输出标准答案
- goto q;
- }
- if(n==2)
- {
- cha_xun(person,i-1);//查询学生成绩
- goto q;
- }
- if(n==3)
- {
- pai_xu(person,i-1,1);//按升序进行排序
- goto q;
- }
- if(n==4)
- {
- pai_xu(person,i-1,2);//按降序进行排序
- goto q;
- }
- if(n==5)
- {
- shu_chu(person,i-1);//输出成绩分析报告
- goto q;
- }
- return 0;
- }
- /*****输出标准答案*****/
- void print(grade cheng_ji[],double x)
- {
- system("cls");
- cout.precision(2);
- cout.setf(ios::fixed);
- for(int i=0;i<35;i++)
- {
- cout<<i+1<<"题答案:"<<cheng_ji[i].a;
- cout<<"出现次数:"<<cheng_ji[i].number<<"次";
- cout<<"出现几率为:"<<cheng_ji[i].number/x<<'\\t';
- if((i+1)%2==0)
- cout<<endl;
- }
- cout<<endl;
- system("pause");
- system("cls");
- }
- /*****查询学生成绩*****/
- void cha_xun(student *person,int x)
- {
- int n;
- system("cls");
- cout<<"学生名单如下:"<<endl;
- for(int i=0;i<x;i++)
- {
- cout<<person[i].number<<":"<<person[i].name<<'\\t';
- if((i+1)%6==0)
- cout<<endl;
- }
- cout<<"请输入要查询学生的序号:"<<endl;
- cin>>n;
- for(int j=0;j<x;j++)
- if(n==person[j].number)
- {
- cout<<person[j].name<<"同学的总成绩为:"<<person[j].zong_fen<<"分"<<endl;
- cout<<"他答对了:"<<person[j].count<<"道题"<<endl;
- break;
- }
- system("pause");
- system("cls");
- }
- /*****统计学生总分*****/
- student* tong_ji(student*person,grade cheng_ji[],int number)
- {
- for(int i=0;i<number;i++)
- {
- person[i].zong_fen=0;
- person[i].count=0;
- for(int j=0;j<35;j++)
- {
- if(person[i].answer[j]==cheng_ji[j].a&&j>=0&&j<=14)
- {
- person[i].zong_fen+=2;
- person[i].count+=1;
- }
- if(person[i].answer[j]==cheng_ji[j].a&&j>=15&&j<=24)
- {
- person[i].zong_fen+=3;
- person[i].count+=1;
- }
- if(person[i].answer[j]==cheng_ji[j].a&&j>=25&&j<=34)
- {
- person[i].zong_fen+=4;
- person[i].count+=1;
- }
- }
- }
- return person;
- }
- /*****排序*****/
- void pai_xu(student*person,int num,int x)
- {
- system("cls");
- int *a,*b,temp,k(0);
- a=new int[num];
- b=new int[num];
- for(int i=0;i<num;i++)
- a[i]=person[i].zong_fen;
- sort(a,a+num); //系统快速排序法
- for(int e=0;e<num;e++)//拷贝a
- b[num-e-1]=a[e];
- for(int y=0;y<num;y++)
- person[y].record=0;
- if(x==1)
- {
- cout<<"学生成绩按升序排列如下:"<<endl;
- for(int j=0;j<num;j++)//主要考虑有相同分数的情况
- {
- if(person[j].zong_fen==a[k])//记录排名
- {
- if(person[j].record==0)
- person[j].record=k+1;
- else
- ;
- }/***思考一下这里的处理技巧***/
- if(person[j].zong_fen==a[k]&&person[j].record==k)
- continue;
- if(person[j].zong_fen==a[k]&&person[j].record!=k)
- {
- cout<<"第"<<num-k<<"名:"<<person[j].name<<'\\t';
- cout<<"序号:"<<person[j].number<<'\\t';
- cout<<"总分:"<<person[j].zong_fen<<'\\t';
- cout<<"答对数量:"<<person[j].count<<endl;
- k++;
- j=0;
- }
- }
- }
- else//功能同上
- {
- k=0;
- cout<<"学生成绩按降序排列如下:"<<endl;
- for(int j=0;j<num;j++)
- {
- if(person[j].zong_fen==b[k])
- {
- if(person[j].record==0)
- person[j].record=k+1;
- else
- ;
- }
- if(person[j].zong_fen==b[k]&&person[j].record==k)
- continue;
- if(person[j].zong_fen==b[k]&&person[j].record!=k)
- {
- cout<<"第"<<k+1<<"名:"<<person[j].name<<'\\t';
- cout<<"序号:"<<person[j].number<<'\\t';
- cout<<"总分:"<<person[j].zong_fen<<'\\t';
- cout<<"答对数量:"<<person[j].count<<endl;
- k++;
- j=0;
- }
- }
- int u;
- for(int p=0;p<num;p++)
- if(person[p].zong_fen==b[num-1])
- u=p;
- cout<<"第"<<num<<"名:"<<person[u].name<<'\\t';
- cout<<"序号:"<<person[u].number<<'\\t';
- cout<<"总分:"<<person[u].zong_fen<<'\\t';
- cout<<"答对数量:"<<person[u].count<<endl;
- }
- system("pause");
- system("cls");
- }
- /*****输出试卷分析到外部文件*****/
- void shu_chu(student*person,int x)
- {
- int *a;
- int p1(0),p2(0),p3(0),p4(0),high,low;
- double sum(0),sum1(0);
- a=new int[x];
- for(int j=0;j<x;j++)
- {
- a[j]=person[j].zong_fen;
- sum+=person[j].zong_fen;
- if(person[j].zong_fen>=60)
- sum1++;
- }
- sort(a,a+x);
- high=a[x-1];
- low=a[0];
- for(int i=0;i<x;i++)
- {
- if(person[i].zong_fen>=60&&person[i].zong_fen<70)
- p1++;
- if(person[i].zong_fen>=70&&person[i].zong_fen<80)
- p2++;
- if(person[i].zong_fen>=80&&person[i].zong_fen<90)
- p3++;
- if(person[i].zong_fen>=90)
- p4++;
- }
- ofstream f1("C:\\\\试卷分析.txt",ios::app);
- f1.precision(2);
- f1.setf(ios::fixed);
- f1<<"****************试卷分析报告******************\\n";
- f1<<"60分以下有"<<x-p1-p2-p3-p4<<"人"<<" "<<"60-70有"<<p1<<"人"<<" "\\
- <<"70-80有"<<p2<<"人"<<" "<<"80-90有"<<p3<<"人"<<" "<<"90分以上有:"<<p4<<"人\\n";
- f1<<"最高分:"<<high<<"分"<<" "<<"最低分:"<<low<<"分"\\
- <<" "<<"平均分:"<<sum/x<<" "<<"及格率:"<<sum1/x<<endl;
- f1.close();
- system("C:\\\\试卷分析.txt");
- system("pause");
- system("cls");
- }
- //该片段来自于http://www.codesnippet.cn/detail/180320149080.html
来源: http://www.codesnippet.cn/detail/180320149080.html