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

来源:互联网 发布:淘宝提前收款钱不见了 编辑:程序博客网 时间:2024/06/06 00:51
 

第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. 结构体变量不能整体输入和输出,必须用域

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

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