c问题

cl90326 2009-03-21 12:59:48
#include <stdio.h>
typedef struct
{
char number[10];
char name[10];
int scores[5];
int total;
}student;
void input(student *p)
{
int i;
printf("number,name:");
scanf("%s%s",&(*p).number,&(*p).name);
for(i=0;i <5;i++)
{
printf("请输入第%d门课成绩:",i+1);
scanf("%d",&(*p).scores[i]);
}
}

void output(student *p)
{
int i;
printf("number:%s\n",(*p).number);
printf("name:%s\n",(*p).name);
for(i=0;i <5;i++)
printf("%5d",(*p).scores[i]);
}

void last(student *p)
{
int t=0,i;
for(i=0;i <5;i++)
{
t=t+(*p).scores[i];
}
(*p).total=t;
}

main()
{
int i,j,order[5],temp;
student std[5],*p;
for(p=std,i=0;i <5;i++,p++)
{
input(p);
last(p);
}
for(i=0;i <4;i++)
{
order[i]=i;
for(j=1;j <5;j++)
{
if(std[order[i]].total <std[order[j]].total)
{
temp=order[i];
order[i]=order[j];
order[j]=temp;
}
}
}
for(i=0,p=std;i <5;i++,p++)
output(p);
}

输入5个学生学号姓名,5门课成绩,算出总分后
按从高到低的顺序输出,帮忙看下哪出错了;
每个函数单独试了下,都没问题,放在一起就不行了
...全文
114 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
热学沸腾56 2009-03-28
  • 打赏
  • 举报
回复
bug问题最好是自己找,找多了也就有经验了,对自己有帮助,过来人说话
tanlee007 2009-03-28
  • 打赏
  • 举报
回复
#include <stdio.h>
typedef struct
{
char number[10];
char name[10];
int scores[5];
int total;
}student;
void input(student *p)
{
int i;
printf("number,name:");
scanf("%s%s",&(*p).number,&(*p).name);
for(i=0;i <5;i++)
{
printf("请输入第%d门课成绩:",i+1);
scanf("%d",&(*p).scores[i]);
}
}

void output(student *p)
{
int i;
printf("number:%s\n",(*p).number);
printf("name:%s\n",(*p).name);
for(i=0;i <5;i++)
printf("%5d",(*p).scores[i]);
}

void last(student *p)
{
int t=0,i;
for(i=0;i <5;i++)
{
t=t+(*p).scores[i];
}
(*p).total=t;
}

main()
{
int i,j,order[5],temp;
student std[5],*p;
for(p=std,i=0;i <5;i++,p++)
{
input(p);
last(p);
}
for(i=0;i <4;i++)
{
order[i]=i; ///执行第一次时只是初始化了order[0]
for(j=1;j <5;j++)
{
if(std[order[i]].total <std[order[j]].total) /////请问执行这儿时order[1]有值吗?没有值的嘛怎么执行呢?
{ /////你只是交换下标不起作用的要交换值呀!std[5]里的顺序仍没
temp=order[i]; /////变
order[i]=order[j];
order[j]=temp;
}
}
}
for(i=0,p=std;i <5;i++,p++)
output(p);
}

这是调试好的程序:
#include <stdio.h>
#include <iostream>
using namespace std;
typedef struct
{
char number[10];
char name[10];
int scores[5];
int total;
}student;
void input(student *p)
{
int i;
printf("number,name:");
scanf("%s%s",&(*p).number,&(*p).name);
for(i=0;i <5;i++)
{
printf("请输入第%d门课成绩:",i+1);
scanf("%d",&(*p).scores[i]);
}
}

void output(student *p)
{
int i;
printf("number:%s\n",(*p).number);
printf("name:%s\n",(*p).name);
for(i=0;i <5;i++)
printf("%5d",(*p).scores[i]);
printf("total=%d",p->total);
cout < <endl;
}

void last(student *p)
{
int t=0,i;
for(i=0;i <5;i++)
{
t=t+(*p).scores[i];
}
(*p).total=t;
}

void main()
{
int i,j,order[5];
student temp;
student std[5],*p;
for(p=std,i=0;i <5;i++,p++)
{
input(p);
last(p);
}
for(i=0;i <4;i++)
{
for(j=i+1;j <5;j++) ////比较的时候当下的值应该和它以后的值比较
{
if(std[i].total <std[j].total)
{
temp=std[i]; ////需要交换值啊!
std[i]=std[j];
std[j]=temp;
}
}
}
for(i=0,p=std;i <5;i++,p++)
output(p);
system("pause");
}
经过调试绝对正确!
mark545887138 2009-03-22
  • 打赏
  • 举报
回复
拿走去调试一下先~~
leewon1988 2009-03-22
  • 打赏
  • 举报
回复
学习
na2650945 2009-03-22
  • 打赏
  • 举报
回复
观望。
自己慢慢调下。
把具体问题确定位置再说啊。
别一下就问大段程序。
谁愿意慢慢调啊。
fairchild811 2009-03-21
  • 打赏
  • 举报
回复
泡泡排序法排错了?
shanzengguang 2009-03-21
  • 打赏
  • 举报
回复
up
wintsy 2009-03-21
  • 打赏
  • 举报
回复
/*-----------------------------------------
输入5个学生学号姓名,5门课成绩,算出总分后
按从高到低的顺序输出,帮忙看下哪出错了;
每个函数单独试了下,都没问题,放在一起就不行了
-------------------------------------------*/

#include <stdio.h>
#include <iostream>
#include <limits>
using std::numeric_limits;
using std::endl;
using std::cin;
using std::cout;


typedef struct{
char number[10]; // 学号
char name[10]; // 姓名
double scores[5]; // 五门课程分数
double total; // 五门课程总分
}Student;

void input(Student &p){
cout << "Enter name: ";
(cin.get(p.name, 10)).get();
cout << "Enter number: ";
(cin.get(p.number, 10)).get();

cout << "每门课程得分: ";
for(int i=0; i<5; ++i){
double t=0.0;
if(cin >> t){
p.scores[i] = t;
p.total += t; // 将分数相加
}

}
// ignore函数清空流中剩下的数据
cin.ignore( numeric_limits<std::streamsize>::max(), '\n' );
//cin.get();
}

void output(const Student &p)
{
cout << "Name: " << p.name << "\nNumber: " << p.number
<< "\nScores: " << endl;
for(int i=0; i<5; ++i){
cout << p.scores[i] << " ";
}
cout << "\nTotal: " << p.total << endl;
}

void BubbleSort(double *ip, const int n){
for(int i=0; i<n; ++i){
for(int j=0; j<n-i-1; ++j){
if(ip[j] < ip[j+1]){
double temp = ip[j];
ip[j] = ip[j+1];
ip[j+1] = temp;
}
}
}
}

void BubbleSort2(Student *pstu, const int n){
for(int i=0; i<n; ++i){
for(int j=0; j<n-i-1;++j){ // 减1是因为当前的将与其后一位相比较
if(pstu[j].total < pstu[j+1].total){
Student temp=pstu[j];
pstu[j]=pstu[j+1];
pstu[j+1]=temp;
}

}
}
}

int main()
{
Student stus[5];
for(int i=0; i<5; ++i){
input(stus[i]);

}
BubbleSort2(stus, 5);
for(int i=0; i<5; ++i){
output(stus[i]);
}



return 0;
}
scsnsjsl_cs_dn 2009-03-21
  • 打赏
  • 举报
回复
排序有问题吧
for(i=0;i <4;i++) 
{
order[i]=i;
for(j=1;j <(5 - i);j++)
{
if(std[order[i]].total <std[order[j]].total)
{
temp=order[i];
order[i]=order[j];
order[j]=temp;
}
}
}
cyldf 2009-03-21
  • 打赏
  • 举报
回复

for(i=0;i <4;i++)
{
order[i]=i;
for(j=1;j <5;j++)
{
if(std[order[i]].total <std[order[j]].total) //这里order[j]=?
{
temp=order[i];
order[i]=order[j];
order[j]=temp;
}
}
}

cl90326 2009-03-21
  • 打赏
  • 举报
回复
帮忙看下吧,什么越界啊,我检查不出来
jn989 2009-03-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ch1oE 的回复:]
最近这种问题好多啊。。。
唉。。
[/Quote]
最近研究生考试进入复试阶段,有好多人都忙着准备机试了啊,好多都是ACM的题
ch1oE 2009-03-21
  • 打赏
  • 举报
回复
最近这种问题好多啊。。。
唉。。
lingyin55 2009-03-21
  • 打赏
  • 举报
回复
有可能是越界的问题,再仔细检查一下吧

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧