题目内容

【10-2】程序改错:用程序读出上题的文件x.txt中的所有数据,输出到显示器上,并求他们的和。修改程序中的错误后运行程序,完成本题。【答案:1060】#include int main(){int i,sum=0;/**********【1】FOUND**********/flie *px;/**********【2】FOUND**********/px=fopen("d:\\x.txt","w");while(!feof(px)){/**********【3】FOUND**********/fscanf(px,"%d",i);sum=sum+i;}printf("\nsum=%d\n",sum);fclose (px);return 0;}

查看答案
更多问题

程序填空:创建一个包含5个节点的链表,data项为5个整数,并输出链表。#include #include typedef struct list{int data;struct list *next;}LIST;int main(){LIST *ptr,*head;int num,i;ptr=(LIST*)malloc(sizeof(LIST));head=【1】;printf("请输入5个整数,以空格分隔:\n");for(i=0;i<=4;i++){scanf("%d",&num);ptr->data=num;ptr->next=(LIST*)malloc(sizeof(LIST));if(i==4)【2】 ;else ptr=ptr->next;}ptr=【3】;while(ptr!=NULL){printf("The value is==>%d\n",ptr->data);ptr=【4】;}return 0;}

程序填空:以下程序中pw指向一个单链表,函数display显示输出该单链表中所有节点的字符串。#include struct Node{char sub[3];struct Node *next;}w={"AA"},x={"BB"},y={"CC"};void display(struct Node *h){struct Node *p;p=h;while(【1】){printf("%s\n",p->sub);p=【2】;}}int main(){struct Node *pw=&w;w.next=&x;x.next=&y;y.next=【3】;display(pw);return 0;}

程序填空:下面程序是关于复数的求和运算,请将程序补充完整。#include typedef struct complex{float r;float i;}COMPLEX;COMPLEX *cal(COMPLEX *x1,COMPLEX *x2){COMPLEX t,【1】=&t;p->r=【2】;p->i=【3】;return p;}int main(){COMPLEX *p,x,y;printf("输入复数x:");scanf("%f%f",&x.r,&x.i);printf("输入复数y:");scanf("%f%f",&y.r,&y.i);p=cal(【4】);printf("x+y=%.2f+%.2fi\n",p->r,p->i);}

程序改错:下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数fun的功能是将单向链表结点(不包括头结点)数据域为奇数的值累加起来,并且作为函数值返回。#include #include #include typedef struct aa{int data;struct aa *next;} NODE;int fun (NODE *h){int sum=0;NODE *p;p=h->next;/***********【1】FOUND***********/while(p->next){if(p->data%2==1)sum+=p->data;/***********【2】FOUND***********/p=h->next;}return sum;}NODE *creatlink(int n){NODE *h,*p,*s;int i;h=p=(NODE*)malloc(sizeof(NODE));for(i=1;idata=rand()%16;s->next=NULL;p->next=s;p=p->next;}p->next=NULL;/***********【3】FOUND***********/return p;}void outlink(NODE *h){NODE *p;/***********【4】FOUND***********/p=h;printf("\n\n The LIST :\n\n HEAD");while(p){printf("->%d",p->data);p=p->next;}printf("\n");}int main(){NODE *head; int sum;system("CLS");head=creatlink(10);outlink(head);/***********【5】FOUND***********/fun(head);printf("\nSUM=%d",sum);return 0;}

答案查题题库