首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请各位帮着写一段C++代码解决思路

2012-03-09 
请各位帮着写一段C++代码输入三个学生的姓名,成绩,按照成绩高低,将姓名依次排序输出![解决办法]看着改改吧

请各位帮着写一段C++代码
输入三个学生的姓名,成绩,
按照成绩高低,
将姓名依次排序输出!

[解决办法]
看着改改吧:
设计一个程序,统计一个班(最多有35人)的学生成绩,要求能实现如下四个功能:
(1)由键盘输入每个学生的学号和四门课程的成绩
(2)计算每个学生的平均成绩和总成绩
(3)按总成绩从高到低排名,并按名次输出每个学生的情况,包括:学号,各科成绩,平均成绩,总成绩,排名
(4)根据要求输出某门课程(由键盘输入课程号)成绩在90分(含90分)以上且总分在前5名的学生情况

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 35
typedef struct tagSTU
{
char id[28];
float scores[4];
float avg;
float tot;
}STU;
STU* createSTU()
{
STU *stu=(STU*)malloc(sizeof(STU));
printf( "请输入ID及成绩(以空格分开)\n ");
scanf( "%s%f%f%f%f ",stu-> id,&stu-> scores[0],&stu-> scores[1],\
&stu-> scores[2],&stu-> scores[3]);
stu-> tot=stu-> scores[0]+stu-> scores[1]+stu-> scores[2]+stu-> scores[3];
stu-> avg=stu-> tot/4;
return stu;
}
void Equal(STU *a,STU *b)
{
a-> scores[0]=b-> scores[0];
a-> scores[1]=b-> scores[1];
a-> scores[2]=b-> scores[2];
a-> scores[3]=b-> scores[3];
strcpy(a-> id,b-> id);
a-> avg=b-> avg;
a-> tot=b-> tot;
}
void Sort(STU *addr,int num)
{
int i,j;
STU temp;
for(i=num-1;i> 0;i--)
{
for(j=0;j <i;j++)
{
if(addr[j].tot <addr[j+1].tot)
{
Equal(&temp,&addr[j]);
Equal(&addr[j],&addr[j+1]);
Equal(&addr[j+1],&temp);
}
}
}
}

void Show(STU *addr,int num)
{
int i=0;
while(i <num)
{
printf( "id:%s\n%.2f %.2f %.2f %.2f %.2f %.2f\n ",addr[i].id,addr[i].scores[0],\
addr[i].scores[1],addr[i].scores[2],addr[i].scores[3],addr[i].avg,addr[i].tot);
i++;
}
}

void Showsub(STU *addr,int num,int s)
{
int i=0;
while(i <num&&i <5)
{
if(addr[i].scores[s]> 89)
printf( "id:%s\n%.2f %.2f %.2f %.2f %.2f %.2f\n ",addr[i].id,addr[i].scores[0],\
addr[i].scores[1],addr[i].scores[2],addr[i].scores[3],addr[i].avg,addr[i].tot);
i++;
}
}
int main()
{
int num,i=0,s=0;
STU stu[MAX];
printf( "请输入人数( <35)\n ");
scanf( "%d ",&num);
if(num <1||num> MAX)return -1;
while(i <num)
{
stu[i]=*createSTU();
i++;
}
Sort(stu,num);
Show(stu,num);
printf( "输入课号(0-3)\n ");
scanf( "%d ",&s);
if(s <0||s> 3)return -1;
Showsub(stu,num,s);
system( "PAUSE ");
return 0;
}

[解决办法]
写了一个满足楼主要求的,运行了没有问题,楼主看看吧,有注释

#include <iostream>
using namespace std;

typedef struct
{
char name[10]; //名字
double score; //分数
}Stu;
void main()
{
Stu st[3],temp;
int i;
for(i=0;i <3;i++) //读入三个学生的名字和成绩
{
cout < < "input the name and the score of student " < <i+1 < <endl;
cin> > st[i].name;
cin> > st[i].score;
}
cout < < "after sort: " < <endl;
for(i=1;i <3;i++) //冒泡法进行排序


{
for(int j=0;j <3-i;j++)
{
if(st[j].score <st[j+1].score)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
for(i=0;i <3;i++) //排序后,从分高到分低输出姓名及对应成绩
{
cout < <st[i].name < < ": " < <st[i].score < <endl;
}
}
[解决办法]
简单用map实现了个……

#include <iostream>
#include <algorithm>
#include <map>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::map;
using std::string;

int main(int argc, char *argv[])
{
map < short, string, std::greater <short> > smap;

string name;
short score;
short stu_num;

cout < < "Input student number: ";
cin > > stu_num;
for (int i=0; i <stu_num; ++i) {
cout < < "Input student infomation - \n\tname: ";
cin > > name;
cout < < "\tscore: ";
cin > > score;

smap.insert(std::make_pair(score, name));
}

for (map < short, string, std::greater <short> > ::iterator siter = smap.begin();
siter != smap.end();
++siter) {
cout < < siter-> second < < "\t: " < < siter-> first < < endl;
}
}

热点排行