关于北林复试题,如果给出的是无格式的txt的做法
2018-03-21 00:55
225 查看
#define _CRT_SECURE_NO_WARNINGS #define FILE_PATH "C:\\s1.txt" #define FILE_PATH_SORTED "C:\\s2.txt" #include<iostream> #include<iomanip> using namespace std; struct student { char num[20];//学号 char name[20];//姓名 char str_score1[10];//读取字符串面试成绩 char str_score2[10];//读取字符串上机成绩 double dou_score1; //计算数字面试成绩 double dou_score2;//计算数字上机成绩 double score;//总成绩 }; void main() { FILE *fp = fopen(FILE_PATH, "r");//读取文件 int n = 0;//学生个数 char c = fgetc(fp); while (c != EOF) { c = fgetc(fp); if (c == '\n') n++; } freopen(FILE_PATH, "r", fp); while (1)//跳过第一行 { char c = fgetc(fp); if (c == '\n') break; } struct student *stu = new student ;//开辟n个学生空间 for (int i = 0; i < 11; i++) { /* 读取学生学号 */ int num_index = 0; while (1) { c = fgetc(fp); if (c == ' ') break; stu[i].num[num_index++] = c; } stu[i].num[num_index] = '\0'; while (1) { c = fgetc(fp); if (c != ' ') break; } /* 读取姓名 */ int name_index = 0; while (1) { stu[i].name[name_index++] = c; c = fgetc(fp); if (c == ' ') break; } stu[i].name[name_index] = '\0'; while (1) { if (c != ' ') break; c = fgetc(fp); } /* 读取面试成绩字符串,转化char *到double */ int s1_index = 0; while (1) { stu[i].str_score1[s1_index++] = c; c = fgetc(fp); if (c == ' ') break; } stu[i].str_score1[s1_index] = '\0'; stu[i].dou_score1 = atof(stu[i].str_score1); while (1) { c = fgetc(fp); if (c != ' ') break; } /* 读取上机成绩字符串,转化char *到double */ int s2_index = 0; while (1) { if (c == EOF) break;//处理最后一个字符 stu[i].str_score2[s2_index++] = c; c = fgetc(fp); if (c == '\n') break; } stu[i].str_score2[s2_index] = '\0'; stu[i].dou_score2 = atof(stu[i].str_score2); //c = fgetc(fp); stu[i].score = stu[i].dou_score1 + stu[i].dou_score2;//计算总成绩 } fclose(fp);//关闭文件 /* 排序 */ for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (stu[j].score>stu[j + 1].score) { student t = stu[j]; stu[j] = stu[j + 1]; stu[j + 1] = t; } } } /* 写入文件:"C:\\s2.txt" */ FILE *newfp = fopen(FILE_PATH_SORTED,"w"); fprintf(newfp, "学号 学生姓名 面试成绩 上机成绩 总成绩\n"); for (int i = 0; i < n; i++) { fprintf(newfp, "%-16s%-16s%-16.2f%-16.2f%-16.2f", stu[i].num, stu[i].name, stu[i].dou_score1, stu[i].dou_score2, stu[i].score); fprintf(newfp, "\n"); } /* 输出学生信息 */ cout << "学号 学生姓名 面试成绩 上机成绩 总成绩" << endl; for (int i = 0; i < n; i++) { printf("%-16s%-16s%-16.2f%-16.2f%-16.2f", stu[i].num, stu[i].name, stu[i].dou_score1, stu[i].dou_score2, stu[i].score); } /* 查询学生 */ cout << "请输入要查询的学生姓名:"; char name[20]; cin >> name; int index = n; for (int i = 0; i < n; i++) { if (strcmp(name, stu[i].name) == 0) { index = i; break; } } if (index == n ) { cout << "没有找到!" << endl; } else { cout << "学号为:" << stu[index].num << endl; cout << "成绩为:" << setprecision(2) << fixed <<stu[index].score << endl; } fclose(newfp); system("pause"); }测试数据:
学号 学生姓名 面试成绩 上机成绩
2014153001 杨阳 58.5 78.3
2014153004 杨一 48.5 28.2
2014153055 杨二 65.5 38.2
2014153008 杨三 34.5 18.2
2014153005 薛林 98.5 48.2
2014153045 王诗萌 23.3 78.2
2014153043 哈哈大 52.5 48.2
2014153032 杨5 44.4 68.2
2014153012 杨6 28.5 45.2
2014153032 杨7 23.5 23.2
2014153041 杨66 43.5 68.2
相关文章推荐
- CurrentCulture and UICulture解决关于全球的的日期格式都能插入数据库和一些如果¥,$的正确显示问题...
- 关于Android4.4的图片路径获取,如果回来的Uri的格式有两种
- 关于txt文本的内容与html页面上显示的内容格式一致的问题
- 关于Manifest.txt 写入jar包的格式问题
- 关于Android4.4的图片路径获取,如果回来的Uri的格式有两种
- 关于Android4.4的图片路径获取,如果回来的Uri的格式有两种
- 关于vs2010 中c++输出字符串到txt文件中的格式问题
- 关于嵌入式Linux系统flash分区设计及文件系统格式选择的一些浅见
- 关于编码(Unicode)与转换格式(UTF)的区别
- 关于javascript中时间格式和时间戳的转换
- 关于mysql(或MariaDB)中的用户账号格式
- 文档格式批量转换(doc,txt,pdf等)
- 关于在ASP.NET环境下,利用FFMPEG视频格式转换的问题。
- 关于windows与unix之间文件格式转换问题。UE编辑器中(CR/LF)问题
- C#关于DateTime得到的当前时间的格式和用法
- 关于矩阵变化的3种做法(1)
- 关于日期格式的转换问题
- 关于matlab里mat2gray函数格式
- 关于epub格式电子书和PC上的ePub阅读器...
- 有没有pdf格式怎么转换成txt格式