【任务1】结构体数据组_读取txt文档,提取信息并依次赋给变量

141 篇文章 4 订阅
6 篇文章 1 订阅
#include <fstream>                     //用于从文件中读入数据的头文件
#include <iomanip>                     //用于处理一定输出格式的头文件
#include <string>                      //此程序一定要将C++标准库中的string头文件包含进来 
# include <iostream>
             
using namespace std;

struct student_data                    //声明一个结构体,用来存放学生的数据
{
	string name;
	int c;
	int math;
	int english;
	int sum;
};
student_data str[180], str_2;

int main()
{
    int i, j, k;
    ifstream infile("score.txt", ios::in);                 //开始从文件中读出数据,存放到结构体数组中
	ofstream outfile("name_list.txt", ios::out);
	
	if(!infile)
    {
		cerr<< "open error!" << endl;
		exit(1);
    }
    for(i = 0; i < 100; i++)                               //将文件数据读入结构体中
    {
		infile >> str[i].name >> str[i].c >> str[i].math >> str[i].english;      
		str[i].sum = str[i].c + str[i].math + str[i].english;
    }
	
	for(j = 1; j <= 100; j++)//共进行99轮比较
	{
		for(i = 0; i < 100; i++)//在每轮中进行(100-j)次比较
			if(str[i].sum < str[i+1].sum)//把每两个数中较小的一个移到靠前的地方
			{
				str_2 = str[i];
				str[i] = str[i+1];
				str[i+1] = str_2;
			}
	}
	//	cout << str[0].sum<<endl;
	
	cout << "按总分高低排序的成绩单:" <<endl;
	cout << setiosflags(ios::fixed);
	cout << "姓名"<<setw(9) << "C++" << setw(11) << "数学" << setw(10) << "英语"
		 << setw(10) << "总分" << endl;
	outfile << "姓名" << "   " << setw(9) << "C++" << " " << setw(11) 
		    << "数学" << " " << setw(10) << "英语" << " " << setw(10) 
			<< "总分" << endl;
	for(i = 0; i < 100; i++)                                                    //用于将排好序的数据输出
	{
		cout << setiosflags(ios::left);            //用于输出一定格式的数据
		cout << setw(8) << str[i].name << "  " << setw(8) << str[i].c << "  "
			<< setw(8) << str[i].math << "  " << setw(8) << str[i].english << "  " << setw(8) << str[i].sum << endl;
		outfile << setiosflags(ios::left) << setw(7) << str[i].name << " "
			    << setiosflags(ios::right) << setw(8) << str[i].c << "   "
				<< setw(8) << str[i].math << "   " << setw(8) << str[i].english
				<< "    " << setw(8) << str[i].sum << endl;      
		 
	} 
	cout << endl;

	infile.close();
	outfile.close();

	ofstream outfile_2("best.txt", ios::out);
	
	k = 0;
	i = 0;
	cout << "*******************************************\n";
	cout << "获得奖学金的学生名单:" <<endl;
	outfile_2 << "姓名" << "   " << setw(9) << "C++" << " " << setw(11) << "数学"
		      << " " << setw(10) << "英语" << " " << setw(10) << "总分" << endl;
	do
	{
		if(str[i].c >= 60 && str[i].math >= 60 && str[i].english >= 60)
		{
			cout << setiosflags(ios::left);            
			cout << setw(8) << str[i].name << "  " << setw(8) << str[i].c << "  "
				 << setw(8) << str[i].math << "  " << setw(8) << str[i].english
				 << "  " << setw(8) << str[i].sum << endl;
			outfile_2 << setiosflags(ios::left) << setw(7) << str[i].name << " "
				      << setiosflags(ios::right) << setw(8) << str[i].c << "   "
					  << setw(8) << str[i].math << "   " << setw(8) << str[i].english
					  << "    " << setw(8) << str[i].sum << endl;
 			k++;
		}

		i++;

	}while(k < 30);
	
	outfile_2.close();

	return 0;
}


 

在相同目录下新建一个 name_list.txt 和 best.txt 用来存放输出的数据(有一个问题,就是人名对不齐,有哪位改下)。


输出结果:

 

困惑:

文件的读取有很多方法,该怎么很好的使用???

总结:

学到了新东西:

ifstream infile("文件名", ios::in);

infile >>a;

我们要学的东西还很多,加油!!

 

score.txt数据:

刘得意 60 98 75
王锐 63 90 96
何煜中 90 73 82
王磊 87 86 92
冯松 89 98 83
裴培 75 82 91
马骁 62 67 90
马婧 98 84 87
周俊升 57 68 96
贺祺 61 96 72
李桐 93 83 86
高路 63 74 98
冯佳媛 61 79 81
张迪 99 88 80
张里响 85 65 96
王瑞麒 89 83 91
徐金竹 75 89 73
赵媛媛 77 75 66
宋宗杰 94 100 92
张佳玮 61 98 96
王姝 70 91 90
宋媛媛 61 94 92
于莉 55 66 78
于浩 78 84 72
王竞 90 87 67
崔赞 91 67 93
宋静 69 85 73
王磊 71 78 77
方圆 70 79 76
李朋 90 82 97
马佳 60 90 100
张龙 62 100 78
马里 73 95 73
韩明 83 97 88
马立 73 90 83
吴清正 89 97 85
印虹 92 68 75
田苗苗 75 91 71
卫青 66 73 77
冷云 89 88 71
葛志伟 100 79 71
范振光 98 87 89
王芳 71 97 99
杨超 67 73 82
杨梦婕 89 99 67
梁雅宁 55 88 100
王琦 98 95 98
吴玮 69 76 68
杨阔 90 91 98
贾伟林 63 90 86
刘亚新 77 81 95
金昕 92 67 69
董一伟 93 88 80
汤娜 68 85 71
周恒 87 82 69
张笑 86 88 76
文静 93 88 85
杨华鑫 81 81 68
苏明霞 59 79 94
黄京 62 75 96
佟欣 60 79 98
张雯 69 70 93
刘京西 67 78 78
徐嘉琦 90 75 87
魏佳 100 94 80
高举 81 99 91
边里 56 94 87
何佳成 70 75 78
赵旭洋 87 91 94
孙大伟 65 69 98
鲁继森 84 79 75
白涛 57 82 75
蔺剑飞 88 75 79
兰天 83 66 74
王悦 79 82 70
陈世勃 70 92 65
张昊 94 83 96
高清 76 83 84
王欣欣 71 83 78
王欢欢 57 33 66
叶丹 87 80 96
李悦 63 79 97
郭倩 69 94 69
刘盈 99 72 93
杨洁 96 79 87
徐一菡 85 45 62
王蒙 67 97 89
张敏 85 75 89
桂佳 60 73 65
林倩 67 77 80
任盛达 57 86 88
吴佳林 96 65 82
黄金龙 85 90 78
陈美珠 82 72 83
冼丹 100 89 89
唐楠 68 97 77
张扬 77 65 93
宋航彬 80 71 91
薛淇文 89 71 75
刘紫亮 72 98 84


 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值