有序的结构体数组
来源:互联网 发布:视频去广告软件 编辑:程序博客网 时间:2024/06/10 16:12
/* * 程序的版权和版本声明部分: * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:初瑞 * 完成日期:2014 年3 月6 日 * 版 本 号:v1.0 * 对任务及求解方法的描述部分: * 输入描述:无 * 问题描述:有序的结构体数组 * 程序输入:略 * 程序输出:略 * 算法设计:略 */ #include <iostream>#include <iomanip>using namespace std;struct Student //同项目1{char num[13]; char name[10]; int cpp; int math; int english; int grade; double average;};void calculate(Student s[],int n); //计算总分和均分,数据在s数组中,共有n个元素void sort1(Student [],int); //按总分降序排序void sort2(Student [],int); //按学号升序排序void output(Student [],int); //输出成绩单void outputExcellent(Student [],int); //输出优秀者:总分超285,单科不低于90int main(){ Student stu[]= {{"201152501104","Tom",65,69 ,68 }, {"201152501114","Jeery",94 ,89 ,63 }, {"201152501138","Speike",67 ,62 ,84 }, {"201152501204","Tyke",100 ,95 ,91 }, {"201152501202","Tuffy",59 ,80 ,55 }, {"201152501115","Butch",100 ,99 ,89 }, {"201152501201","Lightning",95 ,92 ,99 }, {"201152501145","Topsy",88 ,56 ,67 }, {"201152501203","Droopy",62 ,62 ,95 }, {"201152501140","Drupa",80 ,60 ,86 }, {"201152501205","Buzz",73 ,90 ,94} }; int stuNum=sizeof(stu)/sizeof(stu[0]); //用sizeof运算确定数组中元素个数 //计算总分和均分后输出 calculate(stu,stuNum); cout<<"下面是成绩单:"<<endl; output(stu,stuNum); cout<<"优秀者名单:"<<endl; outputExcellent(stu,stuNum); //按总分降序排序后输出 sort1(stu,stuNum); cout<<"按总分降序排序后:"<<endl; output(stu,stuNum); //按学号升序排序后输出 sort2(stu,stuNum); cout<<"按学号升序排序后:"<<endl; output(stu,stuNum); return 0;}void calculate(Student s[],int n){ int i; for(i=0;i<n;i++) { s[i].grade=s[i].math+s[i].cpp+s[i].english;//计算总分 s[i].average=s[i].grade/3;//计算平均值 }}void sort1(Student s[],int n)//按总分降序排序{ int i,j; Student k; for (i=0;i<n-1;i++) { for (j=0;j<n-i-1;j++) { if (s[j].grade<s[j+1].grade) { k=s[j]; s[j]=s[j+1]; s[j+1]=k; } } }}void sort2(Student s[],int n)//按升序排列{ int i,j; Student k; for (i=0;i<n;i++) { for (j=0;j<n-i;j++) { if(s[i].num>s[i+1].num) { k=s[j]; s[j]=s[j+1]; s[j+1]=k; } } }}void output(Student s[],int n)//输出成绩单{ cout<<"|+"<<setw(9)<<"学号"<<setw(8)<<"姓名"<<setw(16)<<"总分"<<setw(18)<<"均分"<<endl; for (int i=0;i<n;i++) { cout<<"|+"<<setw(9)<<s[i].num<<setw(8)<<s[i].name<<setw(16)<<s[i].grade<<setw(18)<<s[i].average<<endl; }}void outputExcellent(Student s[],int n)//输出优秀者:总分超285,单科不低于90{ int i; for(i=0;i<n;i++) { if(s[i].grade>=285&&s[i].cpp>=90&&s[i].math>=90&&s[i].english>=90) { cout<<s[i].num<<s[i].name<<endl; } }}
运算结果:
心得体会:还是不熟练啊
0 0
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组、
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- 有序的结构体数组
- PHP中生成静态html文件
- codeforce 205C - Little Elephant and Interval
- android fragment getActivity() null
- git常用命令(一)
- [delphi]小程序一例,自动生成随机数。
- 有序的结构体数组
- easyui panel 使用汇总
- checkio (Determine the order)
- 智能手机 华为 索尼
- (CF)A. Inna and Choose Options
- Ubuntu中查看各种设备和资源的命名汇总
- 完成端口(CompletionPort)详解 - 手把手教你玩转网络编程系列之三
- 第六周:图书馆管理系统
- SDUTOJ 2119 数据结构实验之链表四:有序链表的归并