时间:2011-12-24 23:58:26
第8套题
一、程序填空题
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(char *);
void outlist(SLIST *);
int fun( SLIST *h, char ch)
{ SLIST *p; int n=0;
p=h->next;
/**********found**********/
while(p!=___1___)
{ n++;
/**********found**********/
if (p->data==ch) return ___2___;
else p=p->next;
}
return 0;
}
main()
{ SLIST *head; int k; char ch;
char a[N]={'m','p','g','a','w','x','r','d'};
head=creatlist(a);
outlist(head);
printf("Enter a letter:");
scanf("%c",&ch);
/**********found**********/
k=fun(___3___);
if (k==0) printf("nNot found!n");
else printf("The sequence number is : %dn",k);
}
SLIST *creatlist(char *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("nThe list is NULL!n");
else
{ printf("nHead");
do
{ printf("->%c",p->data); p=p->next; }
while(p!=NULL);
printf("->Endn");
}
}
二、程序改错题
#include <string.h>
#include <stdio.h>
#include <ctype.h>
fun ( char *p)
{ int i,t; char c[80];
/************found************/
For (i = 0,t = 0; p[i] ; i++)
if(!isspace(*(p+i))) c[t++]=p[i];
/************found************/
c[t]=" ";
strcpy(p,c);
}
main( )
{ char c,s[80];
int i=0;
printf("Input a string:");
c=getchar();
while(c!='#')
{ s[i]=c;i++;c=getchar(); }
s[i]=' ';
fun(s);
puts(s);
}
三、程序编写题
#include <stdio.h>
#include <string.h>
void fun ( char *ss )
{
}
main( )
{ char tt[81] ;
void NONO ( );
printf( "nPlease enter an string within 80 characters:n" ); gets( tt );
printf( "nnAfter changing, the stringn "%s"", tt );
fun( tt );
printf( "nbecomesn "%s"n", tt );
NONO ( );
}
void NONO ( )
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *fp, *wf ;
char tt[81] ;
int i ;
fp = fopen("K:\k01\24010001\in.dat","r") ;
wf = fopen("K:\k01\24010001\out.dat","w") ;
for(i = 0 ; i < 10 ; i++) {
fscanf(fp, "%s", tt) ;
fun( tt ) ;
fprintf(wf, "%sn", tt) ;
}
fclose(fp) ;
fclose(wf) ;
}