读书人

(初学者上路)帮小弟我改下错最好能

发布时间: 2012-02-05 12:07:14 作者: rapoo

(菜鸟上路)帮我改下错,最好能帮我说明下原因
主要是对参数函数和结构的传值这些不是很清楚,最好能详细点
#include <stdio.h>
struct student{
int no;
char name[20];
int score[3];
double num;
};
void input(struct student *s);
void display(struct student *s,int a);
void play(struct student *s,int a);
double avg(struct student *s);
void main()
{
struct student stu[50];
int i=0,count=0;
char ans= 'y ';
do{
printf( "请输入学员信息:\n\n ");
input(&stu[i]);
avg(&stu[i]);
printf( "是否继续输入 Y OR N ");
ans=getchar();
fflush(stdin);
i++;
}while(ans== 'Y '||ans== 'y ');
count=i;
printf( "排序前的学员信息如下\n ");
display(&stu,count);
sort(&stu,count);
printf( "排序后的学员信息如下\n ");
plsy(&stu,count);
printf( "是否要添加某个学员? Y OR N\n ");
ans=getchar();
fflush(stdin);
while(ans== 'Y '||ans== 'y ');
{
input(&stu[count+1]);
avg(&stu[count+1]);
count++;
sort(&stu,count);
pley(&stu,count);
printf( "是否要继续添加学员? Y OR N\n ");
ans=getchar();
fflush(stdin);
}
printf( "是否要删除某个学员? Y OR N\n ");
do{
ans=getchar();
fflush(stdin);
if(ans== 'Y '||ans== 'y ')
{
dele(&stu,count);
}
deleplay(&stu,count)
printf( "是否要继续删除某个学员? Y OR N\n ");
ans=getchar();
fflush(stdin);
}while(ans== 'Y '||ans== 'y ')
}
void input(struct student *s)
{
printf( "学号:\n ");
scanf( "%d ",s-> no);
fflush(stdin);
printf( "姓名:\ ");
gets(s-> name);
fflush(stdin);
printf( "三门成绩:\n ");
printf( "成绩1:\n ");
scanf( "%d ",s-> score[0]);
fflush(stdin);
printf( "成绩2:\n ");
scanf( "%d ",s-> score[1]);
fflush(stdin);
printf( "成绩3:\n ");
scanf( "%d ",s-> score[2]);
fflush(stdin);
}
void display(struct student *s,int a){

int i;
printf( "排序前的学院信息如下:\n ");
printf( "学号\t姓名\t\t平均成绩\n ");
for(i=0;i <=a;i++)
{
printf( "%d\t ",s[i]-> no);
printf( "%c\t\t ",s[i]-> name);
printf( "%c\n ",s[i]-> num);
}
}
void play(struct student *s,int a){

int i;
printf( "排序后的学院信息如下:\n ");
printf( "学号\t姓名\t\t平均成绩\n ");
for(i=0;i <=a;i++)
{
printf( "%d\t ",s[i]-> no);
printf( "%c\t\t ",s[i]-> name);
printf( "%c\n ",s[i]-> num);
}
}
void deleplay(struct student *s,int a){

int i;
printf( "删除后学员的信息如下:\n ");
printf( "学号\t姓名\t\t平均成绩\n ");
for(i=0;i <=a;i++)
{
printf( "%d\t ",s[i]-> no);
printf( "%c\t\t ",s[i]-> name);
printf( "%c\n ",s[i]-> num);
}
double avg(struct student *s){
return s-> num=(s-> score[0]+s-> score[1]+s-> score[2])/3


}
void sort(struct student *s,int a){
int i,j;
double temp;
for(i=0;i <=a;i++)
{
for(j=0;j <a-i-1;j++)
{
if(s[j]-> num > s[j+1]-> num)
{
temp=s[j+1]-> num;
s[j+1]-> num=s[j]-> num;
s[j]-> num=temp;
}
}
}
}
void dele(struct student *s,int a){
int i,c;
printf( "请输入要删除的学员的学号\n ");
scanf( "%d ",c);
for(i=0;i <=a;i++)
{
if(s[i].no==c)
{
for(j=0;j <=a-i;j++)
{
s[i]=s[i+1];
}
}
}


}

[解决办法]
你的太长,没仔细看。
关于函数和结构体传参数,参考下面的小例子。

#include <iostream>
using namespace std;

struct student{
public:
int no;
};

void output(student a_student)
{
cout < <a_student.no < <endl;
}

int main()
{
student ZhangSan;
ZhangSan.no = 5;

output(ZhangSan);

return 0;
}
[解决办法]
如上,student就是一个新类型,是自己定义的类型。简单点,你把student当成int,double这样一样看待。

output函数,需要一个student类型的参数。
no是struct结构体的成员,把它定义成public,这样我们就可以通过点操作符“.”直接访问。

main函数调用output。
当然得传给它一个student类型的变量了。也就是张三。

明白了吗?
[解决办法]
没怎么看,但感觉好多细节都不怎么注意
void display(struct student *s,int a){

int i;
printf( "排序前的学院信息如下:\n ");
printf( "学号\t姓名\t\t平均成绩\n ");
for(i=0;i <=a;i++)
{
printf( "%d\t ",s[i]-> no);
printf( "%c\t\t ",s[i]-> name); 这里应该是用字符串打印,s%
printf( "%c\n ",s[i]-> num); num 是dobule型的,你要打印char型吗?
}
}
而且函数都没有判断传入的指针是否为空。很不稳定,容易死机。就像下面:
double avg(struct student *s){
if (null==s)
return XXX;
return s-> num=(s-> score[0]+s-> score[1]+s-> score[2])/3
}

一个排序没必要用三重循环。 用快速排序什么的,不更快吗?
del的函数 条件判断不合适。有数组越界的可能。

写程序一定要养成好习惯,注意细节,要不很容易出现死机。 总体还能看明白程序是什么意思。。加倍

努力吧。。
[解决办法]
#include <stdio.h>
#include <stdlib.h>

struct student{
int no;
char name[20];
int score[3];
double num;
};

void input(struct student *s);
void display(struct student *s,int a);
void play(struct student *s,int a);
double avg(struct student *s);
void deleplay(struct student *s,int a); //!!!函数声明缺少
void sort(struct student *s,int a); //!!!函数声明缺少
void dele(struct student *s,int a); //!!!函数声明缺少

int main() //!!!main 请使用 int 返回, void 返回为非标准方式
{
struct student stu[50];
int i=0,count=0;
char ans= 'y ';
do{
printf( "请输入学员信息:\n\n ");
input(&stu[i]);
avg(&stu[i]);
printf( "是否继续输入 Y OR N ");
ans=getchar();
fflush(stdin);
i++;
}while(ans== 'Y '||ans== 'y ');
count=i;
printf( "排序前的学员信息如下\n ");
display(stu,count); //!!!参数是 struct student *,数组传首地址即可,不需要再&
sort(stu,count); //!!!参数是 struct student *,数组传首地址即可,不需要再&
printf( "排序后的学员信息如下\n ");
plsy(&stu,count);
printf( "是否要添加某个学员? Y OR N\n ");


ans=getchar();
fflush(stdin);
while(ans== 'Y '||ans== 'y ');
{
input(&stu[count+1]);
avg(&stu[count+1]);
count++;
sort(stu,count); //!!!参数是 struct student *,数组传首地址即可,不需要再&
pley(&stu,count);
printf( "是否要继续添加学员? Y OR N\n ");
ans=getchar();
fflush(stdin);
}
printf( "是否要删除某个学员? Y OR N\n ");
do{
ans=getchar();
fflush(stdin);
if(ans== 'Y '||ans== 'y ')
{
dele(stu,count); //!!!参数是 struct student *,数组传首地址即可,不需要再&
}
deleplay(stu,count); //!!! 漏了分号 , 参数不需要取地址
printf( "是否要继续删除某个学员? Y OR N\n ");
ans=getchar();
fflush(stdin);
}while(ans== 'Y '||ans== 'y '); // !!! 漏了分号
system( "PAUSE ");
return 0;
}

void input(struct student *s)
{
printf( "学号:\n ");
scanf( "%d ",s-> no);
fflush(stdin);
printf( "姓名: "); //!!!多了一个斜杠转义, 错误
gets(s-> name);
fflush(stdin);
printf( "三门成绩:\n ");
printf( "成绩1:\n ");
scanf( "%d ",s-> score[0]);
fflush(stdin);
printf( "成绩2:\n ");
scanf( "%d ",s-> score[1]);
fflush(stdin);
printf( "成绩3:\n ");
scanf( "%d ",s-> score[2]);
fflush(stdin);
}
void display(struct student *s,int a){

int i;
printf( "排序前的学院信息如下:\n ");
printf( "学号\t姓名\t\t平均成绩\n ");
for(i=0;i <=a;i++)
{
printf( "%d\t ",s[i].no); //!! -> 访问成员方式用于指针, s[i]不是指针,使用 .访问成员
printf( "%c\t\t ",s[i].name); //!!!同上
printf( "%c\n ",s[i].num);//!!!同上
}
}
void play(struct student *s,int a){

int i;
printf( "排序后的学院信息如下:\n ");
printf( "学号\t姓名\t\t平均成绩\n ");
for(i=0;i <=a;i++)
{
printf( "%d\t ",s[i].no);//!!!同上
printf( "%c\t\t ",s[i].name);//!!!同上
printf( "%c\n ",s[i].num);//!!!同上
}
}

void deleplay(struct student *s,int a){
int i;
printf( "删除后学员的信息如下:\n ");
printf( "学号\t姓名\t\t平均成绩\n ");
for(i=0;i <=a;i++)
{
printf( "%d\t ",s[i].no);//!!!同上
printf( "%c\t\t ",s[i].name);//!!!同上
printf( "%c\n ",s[i].num);//!!!同上
}
}

double avg(struct student *s){
return s-> num=(s-> score[0]+s-> score[1]+s-> score[2])/3;//!!!分号遗漏
}

void sort(struct student *s,int a){
int i,j;
double temp;
for(i=0;i <=a;i++)
{
for(j=0;j <a-i-1;j++)
{
if(s[j].num > s[j+1].num) //-> 用于指针!!!
{
temp=s[j+1].num;
s[j+1].num=s[j].num;
s[j].num=temp;
}
}
}
}
void dele(struct student *s,int a){
int i,c, j ; // !!! j变量没有定义
printf( "请输入要删除的学员的学号\n ");
scanf( "%d ",c);
for(i=0;i <=a;i++)
{
if(s[i].no==c)
{
for(j=0;j <=a-i;j++)
{
s[i]=s[i+1];
}
}
}
}

读书人网 >C语言

热点推荐