您的位置:首页 > 其它

关于北林复试题,如果给出的是无格式的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: