教材练习题参考答案 联系客服

发布时间 : 星期三 文章教材练习题参考答案更新完毕开始阅读9355ce9f51e79b896802262c

cout<

2.[程序如下]

# include # include struct score {

char name[8] ; int no ;

float c, english, maths, average ; } ;

score input(score st) ;

float average( float a, float b, float c ) ; void sort(score st[], int n) ; void print (score st[], int n) ; void main() {

score student[30] ;

for (int i=0 ; i<5 ; i++) student[i]=input(student[i]) ; sort(student, 5) ; print(student, 5) ; }

score input(score st) {

cout<<\请输入学生姓名:\ cin>>st.name ; cout<<\请输入学生学号:\ cin>>st.no ; cout<<\请输入C++成绩:\ cin>>st.c ;

cout<<\请输入英语成绩:\ cin>>st.english ; cout<<\请输入数学成绩:\ cin>>st.maths ; cout<

st.average=average(st.c, st.english, st.maths) ; return st ; }

float average( float a, float b, float c ) { return (a+b+c)/3 ; }

void sort(score st[], int n) { score temp ;

for ( int i=0 ; i

} void print (score st[], int n) {

cout<<\\ 姓名 \ 平均 \ C++ \ 英语 \ 数学 \ for ( int i=0 ; i

cout<<'\\t'<

cout<<'\\t'<

cout<<'\\t'<

3.[程序如下]#include struct student { long int num; float CPPmid; float CPPend; float ave; };

void main() {student s[3]; student *p; for(p=s;p

cout<<\请输入学生学号:\ cin>>p->num; cout<<\请输入C++期中成绩:\ cin>>p->CPPmid; cout<<\请输入C++期末成绩:\ cin>>p->CPPend; p->ave=(p->CPPmid+p->CPPend)/2; } cout<<\ 平均 \ C++期中 \期末 \ for(p=s;pnum ; cout<<'\\t'<ave;

cout<<'\\t'<CPPmid ; cout<<'\\t'<CPPend; cout<

} }

4.[程序如下] struct link {

int data; link*next; };

int sum(link *head) { link *p; int s=0; p=head->next; while(p) { s+=p->data; p=p->next; return s; } }

5.[程序如下] #include struct node {

int data;

node *next; };

node *create( ) {

node *head ; //头指针

node *p , *pend ; int a;

cout<<\请输入数据(为零表示结束输入):\ cin>>a; head=0 ; while(a!=0) {

p=new node ;

p->data=a ; if(head==0) {

head=p ; pend=p ;

}

else { pend->next=p ;

pend=p ;

}

cout<<\请输入数据(为零表示结束输入):\ cin>>a;

}

if(head) pend->next=0 ; return head ; }

node * invert(node *head) { node *p,*q; p=head->next; if(p!=NULL) {

head->next=NULL;

do { q=p->next; p->next=head; head=p; p=q;; }while(p!=NULL);

} return head; }

void print(node *head ) {

if(head==0) { cout<<\链表为空!\\n\ return ; } node *p=head ;

cout<<\链表上各个结点的值为:\\n\

while(p!=0) { cout < data<<'\\t' ; p=p->next ; } }

void release(node *head ) {

if(head==0) { cout<<\链表为空!\\n\ return ; node *p ; while( head )

{ p=head;

head=head->next ;

delete p; }

cout<<\结点空间释放完毕!\

}