第17周报告1 score结构体变量排序

本文介绍了使用结构体变量进行数据排序的过程,并详细说明了如何通过冒泡排序算法对成绩数据进行排序,同时展示了如何输出排序后的数据以及筛选出获得奖学金的同学。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

第17周报告1:
实验目的:学会利用结构体变量

实验内容:实现机构体变量的调用
/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: score结构体变量排序
* 作 者: 姜雅明
* 完成日期: 2011 年 12 月 16 日
* 版 本号: 1.0

* 对任务及求解方法的描述部分
* 输入描述:要排序的数据在程序中初始化
* 问题描述:实现冒泡排序
* 程序输出:排序后的结果
* /

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void input_score();
void bubble_sort();
void output_score();
void output_luckystu();

struct Student
{
	char num[12];
	string name;
	int grade[3];
	int score;
};
Student stu[180];

int main()
{
	input_score();
	
	bubble_sort();
	
	output_score();
	
	output_luckystu();
	
	return 0;
	
}

void input_score()
{
	int i;
	
	ifstream infile("score.txt",ios::in);
	
	if(!infile)
	{
		cout << "open error!\n";
		
		exit(1);
	}
	for(i = 0; i < 180; i++)
	{
		infile >> stu[i].num
			
			>> stu[i].name
			
			>> stu[i].grade[0]
			
			>> stu[i].grade[1]
			
			>> stu[i].grade[2];
		
		stu[i].score= stu[i].grade[0] + stu[i].grade[1] + stu[i].grade[2];
	}
	infile.close();
	
	return;
}

void bubble_sort()
{
	int i, j;
	
	Student k;
	
	for(i = 0; i < 179; i++)
	{
		for(j = 0; j < 179 - i; j++)
		{
			if(stu[j].score < stu[j + 1].score)
			{
				k = stu[j];
				
				stu[j] = stu[j + 1];
				
				stu[j + 1] = k;
			}
		}
	}
	return;
}

void output_score()
{
	int i;
		
	for(i = 0; i < 180; i++)
	{
		cout << stu[i].num << setw(10)
			
			<< stu[i].name << "\t"
			
			<< stu[i].grade[0] << "\t"
			
			<< stu[i].grade[1] << "\t"
			
			<< stu[i].grade[2] << "\t"
			
			<< stu[i].score << "\t\n";
	}
	return;
}

void output_luckystu()
{
	int i = 0, j;
	
	cout << "\n获得奖学金的同学有:\n";
	
	for(i = 0; i < 30; i++)
	{
		if(stu[i].grade[0] >= 60)
		{
			if(stu[i].grade[1] >= 60)
			{
				if(stu[i].grade[2] >= 60)
				{
					if(i%5==0)cout << endl;
					
					cout << stu[i].name << '\t';
				}
			}
		}
	}
	cout << endl;
	return;
}


 

运行结果:



经验积累:
1. 机构体变量可以整体调用
2. 结构体变量不能整体输入和输出,必须用域

上机感言:
刚开始想把每一个人的信息放入结构体中整体输入,但是出错了···

本想让名字左对齐的,没成功···



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值