c语言中maxi通常做什么用,问一C语言的考试题!谁知道怎么做呀?用简单点的方法做下!我们就...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

}

i++;

}

average = average/N; /* 计算总平均分*/

/*输出*/

printf("NO.\tname\tscore1\tscore2\tscore3\ttotal\tave\n");

for(b=stu;b

{

printf("%d\t%s\t",b->num,b->name);

for(j=0;j<3;j++)

printf("%d\t",b->score[j]);

printf("%.2f\t%.2f\n",b->total,b->ave);

}

printf("average=%.2f\n",average);

b=stu;

b+=maxi;

printf("The highest score is :student %d ,%s\n",b->num,b->name);

printf("His score are:%d,%d,%d,total:%.2f,average:%6.2f\n",

b->score[0],b->score[1],b->score[2],b->total,b->ave);

}

程序运行情况如下:

input scores of student 1:

NO.:1001

name:Ma

score 1:84

score 2:75

score 3:64

input scores of student 2:

NO.:1002

name:Zhang

score 1:75

score 2:64

score 3:84

input scores of student 3:

NO.:1003

name:Fang

score 1:91

score 2:62

score 3:73

input scores of student 4;

NO.:1004

name:Wu

score 1:85

score 2:79

score 3:88

input scores of student 5:

NO.:1005

name:Han

score 1:86

score 2:92

score 3:77

运行结果如下:

NO. name score1 score2 score3 total ave

1001 Ma 84 75 64 223.00 74.33

1002 Zhang 75 64 84 223.00 74.33

1003 Fang 91 62 73 226.00 75.33

1004 Wu 85 79 88 252.00 84.00

1005 Han 86 92 77 255.00 85.00

average=78.60

The highest score is :student 1005 ,Han

His score are:86,92,77,total:255.00,average: 85.00

3.建立两个链表a和b。每个链表中的节点包括学号、成绩。要求把两个链表合并,按学号升序排列。

解:

#include "stdio.h"

#include "malloc.h"

#define LEN sizeof(struct student)

struct student

{

long num;

int score;

struct student *next;

};

struct student lista,listb;

int n,sum=0;

main()

{

struct student *creat(void); /*函数声明*/

struct student *insert(struct student *,struct student *); /*函数声明*/

void print(struct student *); /*函数声明*/

struct student *ahead,*bhead,*abh;

printf("input list a:\n");

ahead=creat(); /*调用creat函数,输入链表a*/

sum=sum+n;

printf("input list b:\n");

bhead=creat(); /*调用creat函数,输入链表b*/

sum=sum+n;

abh=insert(ahead,bhead); /*调用insert函数,合并两个链表*/

print(abh); /*输出合并以后的链表*/

}

struct student *creat(void) /*建立链表函数*/

{

struct student *p1,*p2,*head;

n=0;

p1=p2=(struct student *)malloc(LEN);

printf("input number & scores of student:\n");

printf("if number is 0,stop inputing.\n");

scanf("%ld,%d",&p1->num,&p1->score);

head=NULL;

while(p1->num!=0)

{

n=n+1;

if(n==1)

head=p1;

else

p2->next=p1;

p2=p1;

p1=(struct student *)malloc(LEN);

scanf("%ld,%d",&p1->num,&p1->score);

}

p2->next=NULL;

return(head);

}

struct student *insert(struct student *ah,struct student *bh)

/*定义insert函数,用来合并两个链表*/

{

struct student *pa1,*pa2,*pb1,*pb2;

pa2=pa1=ah;

pb2=pb1=bh;

do

{

while((pb1->num>pa1->num)&&(pa1->next!=NULL))

{

pa2=pa1;

pa1=pa1->next;

}

if(pb1->num<=pa1->num)

{

if(ah==pa1)

ah=pb1;

else pa2->next=pb1;

pb1=pb1->next;

pb2->next=pa1;

pa2=pb2;

pb2=pb1;

}

}while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));

if((pb1!=NULL)&&(pb1->num>pa1->num)&&(pa1->next==NULL))

pa1->next=pb1;

return(ah);

}

void print(struct student *head) /*输出函数*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值