关于北林复试题,如果给出的是无格式的txt的做法

#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];//开辟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


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值