![]() |
#2
zxwangyun2012-06-12 17:42
![]() #include<iostream> 我来给你做做贡献吧#include<cstdio> #include<vector> #include<fstream> using namespace std; typedef struct tagSTUDENT_SCORE { unsigned int uNo;// char szName[50];// float fMath; float fEnglish; float fJava; float fAvg;//平均 float fTotal;//总 tagSTUDENT_SCORE() { memset(this,0,sizeof(tagSTUDENT_SCORE)); } }STUDENT_SCORE,*LPSTUDENT_SCORE; ///解析字符串函数 char * spliter_str(char * pBegin,int &len/*out*/,char* record_buf/*out*/,unsigned int record_buf_len/*缓冲区长度*/) { len=0; char *p = pBegin; while(*p != ',') { p++; len++; } if(len > 0 ) { if(len >= record_buf_len) len = record_buf_len - 1; memset(record_buf,0,record_buf_len); memcpy(record_buf,pBegin,len); } p++; return p; } //冒泡排序 void bubble_sort(vector<STUDENT_SCORE>&x) { int j,h,n=x.size(); STUDENT_SCORE t; for (h=n-1; h>0; h--) /*循环到没有比较范围*/ { for (j=0; j<h; j++) { if (x[j].fTotal < x[j+1].fTotal) /*小的放在后面,大的放到前面*/ { t = x[j]; x[j] = x[j+1]; x[j+1]= t; /*完成交换*/ } } } } //输出操作菜单 void print_ctrl_menu() { cout<<"========================"<<endl <<"\t 1 打印学生信息\n" <<"\t 2 输入学号,打印学生信息\n" <<"\t 3 打印学生名次\n" <<"\t 0 退出\n" <<"========================"<<endl; } //获取选择 int get_user_sel() { print_ctrl_menu(); cout<<"请输入选择:"; int sel; cin>> sel; while(sel<0 || sel > 3) { cout<<"输入有误,请重新输入"<<endl; print_ctrl_menu(); cout<<"请输入选择:"; cin>> sel; } return sel; } //获取输入的学号 unsigned int get_stu_no() { cout<<"\t 请输入学号:"; unsigned int no; cin>>no; return no; } //打印学生信息 void printf_stu_info(const vector<STUDENT_SCORE>&stuInfo,bool bPrintOrder=false) { for(int i=0;i<stuInfo.size();i++) { cout<<"\n学号:"<<stuInfo[i].uNo <<"\t姓名:"<<stuInfo[i].szName <<"\t数学:"<<stuInfo[i].fMath <<"\t英语:"<<stuInfo[i].fEnglish <<"\tJAVA:"<<stuInfo[i].fJava <<"\t平均分:"<<stuInfo[i].fAvg <<"\t总分:"<<stuInfo[i].fTotal; if(bPrintOrder) cout<<"\t名次:"<<i+1; cout<<endl; } } //查找学生信息 void fin_stu_info(unsigned int no,const vector<STUDENT_SCORE>&stuInfo) { for(int i=0;i<stuInfo.size();i++) { if(stuInfo[i].uNo == no) { cout<<"\n学号:"<<no <<"\t姓名:"<<stuInfo[i].szName <<"\t数学:"<<stuInfo[i].fMath <<"\t英语:"<<stuInfo[i].fEnglish <<"\tJAVA:"<<stuInfo[i].fJava <<"\t平均分:"<<stuInfo[i].fAvg <<"\t总分:"<<stuInfo[i].fTotal <<"\t名次:"<<i+1 <<endl; return; } } cout<<"\n无相关信息"<<endl; } int main() { const char * filename = "students.txt"; ifstream ifile(filename,ios_base::in);//注意,初始数据文本文件最后不要有多余换行 vector<STUDENT_SCORE> oldStuInfo; char buf[sizeof(STUDENT_SCORE)]={0}; ifile.getline(buf,sizeof(STUDENT_SCORE)); while(!ifile.eof())///开始解析字符串,并将内容保存至oldStuInfo { char record[50]={0};///保存解析后的字符串 STUDENT_SCORE stu; char *p = buf; int len=0; p = spliter_str(p,len,record,sizeof(record)); if(len>0) { stu.uNo = atoi(record);//获取学号 p = spliter_str(p,len,record,sizeof(record)); if(len>0) { memcpy(stu.szName,record,len);//获取姓名 p = spliter_str(p,len,record,sizeof(record)); if(len>0) { stu.fMath = atof(record);//数学 p = spliter_str(p,len,record,sizeof(record)); if(len>0) { stu.fEnglish = atof(record);//英语 p = spliter_str(p,len,record,sizeof(record)); if(len>0) { stu.fJava = atof(record);//java stu.fTotal = stu.fJava + stu.fMath + stu.fEnglish; stu.fAvg = stu.fTotal / 3; oldStuInfo.push_back(stu); }//java }//英语 }//数学 }//获取姓名 }//获取学号 memset(buf,0,sizeof(STUDENT_SCORE)); ifile.getline(buf,sizeof(STUDENT_SCORE)); } ifile.close();//关闭输入文件 vector<STUDENT_SCORE> stuInfo(oldStuInfo); bubble_sort(stuInfo);//排序 int sel = 0; while((sel = get_user_sel()) != 0) { switch(sel) { case 1:printf_stu_info(oldStuInfo);break; case 2: { unsigned int no = get_stu_no(); fin_stu_info(no,stuInfo); } break; case 3:printf_stu_info(stuInfo,true);break; default: break; } } ///保存信息至文件 FILE * lpFile = NULL; if(fopen_s(&lpFile,filename,"w") == 0) { if(lpFile) { for(int i = 0;i<oldStuInfo.size();i++) { fprintf(lpFile,"%02d,%s,%.0f,%.0f,%.0f\n", oldStuInfo[i].uNo, oldStuInfo[i].szName, oldStuInfo[i].fMath, oldStuInfo[i].fEnglish, oldStuInfo[i].fJava); } fclose(lpFile); } } stuInfo.clear(); oldStuInfo.clear(); } |
《高级语言程序设计》课程设计题目
题目一
1. 课题名称:成绩统计系统
2. 基本要求
根据下面的数据,为全班学生统计成绩,并排序。
学生成绩信息文件:
03,芦坚,48,90,65
09,白小红,78,85,53
12,祖晓明,83,81,62
23,靳龙刚,90,82,33
34,郭朋朋,35,35,100
36,邢海青,69,75,33
39,曹峰,77,62,56
解释:每行信息为“学号,姓名,高数,英语,java”,每行信息用英文逗
号隔开。
3. 功能要求及说明
(1)统计查询:
为每个学生统计总分,均分,并结束后将其按原有形式存储至原来的文件。
(2)查询功能:
输入一个学号时,查出此学生的成绩情况。
(3) 成绩排序:
可以按总成绩对学生进行排序并打印显示
可以附加用的是C++程序的哪一款吗?
[ 本帖最后由 a25581818 于 2012-6-11 20:00 编辑 ]