C语言程序设计精品课件试题10
(2009-11-16 00:07:29)
标签:
ifnamestdio.h指针for教育 |
分类: C语言 |
【3.52】下面create函数的功能是建立一个带头结点的单向链表,新产生的结点总是插入在链表的末尾。单向链表的头指针作为函数值返回。
#include <stdio.h>
#define LEN sizeof(struct student)
struct student
{ long num;
int score;
struct student *next;
};
struct student *creat()
{ struct student *head=NULL,*tail;
long num;
int a;
tail=( ① )malloc(LEN);
do
{ scanf("%ld,%d",&num,&a);
if(num!=0)
{ if(head==NULL) head=tail;
else tail=tail->next;
tail->num=num;
tail->score=a;
tail->next=( ② )malloc(LEN);
}
else tail->next=NULL;
}while(num!=0);
③ ;
}
【3.53】下面程序的功能是统计文件中的字符的个数。
#include <stdio.h>
main()
{ long num=0;
① *fp;
if((fp=fopen("fname.dat", "r"))==NULL)
{ printf("Can't open the file! ");
exit(0);
}
while( ② )
{ fgetc(fp);
num++;
}
printf("num=%d\n",num);
fclose(fp);
}
【3.54】下面程序的功能是把从键盘输入的文件(用 @ 作为文件结束标志)复制到一个名为second.txt的新文件中。
#include <stdio.h>
FILE *fp;
main()
{ char ch;
if((fp=fopen( ① ))==NULL)
exit(0);
while((ch=getchar())!='@')
fputc(ch,fp);
② ;
}
【3.55】下面程序的功能是将磁盘上的一个文件复制到另一个文件中,两个文件名在命令行中给出(假定给定的文件名无误)。
#include <stdio.h>
main(int argc,char *argv[])
{ FILE &f1,*f2;
if(argc< ① )
{ printf("The command line error! ");
exit(0);
}
f1=fopen(argv[1], "r");
f2=fopen(arhv[2], "w");
while( ② )
fputs(fgetc(f1), ③ );
④ ;
⑤ ;
}
【3.56】下面程序的功能是根据命令行参数分别实现一个正整数的累加或阶乘。例如:如果可执行文件的文件名是sm,则执行该程序时输入:"sm + 10",可以实现10的累加;输入:"sm - 10",可以实现求10的阶乘。
#include <stdio.h>
#include <stdlib.h>
main (int argc,char *argv[])
{ int n;
void sum(),mult();
void (*funcp)();
n=atoi(argv[2]);
if(argc!=3 || n<=0)
dispform( );
switch ( ① )
{ case '+': funcp=sum;
break;
case '-': funcp=mult;
break;
default: dispform( );
}
② ;
}
void sum(int m)
{ int i,s=0;
for(i=1;i<m;i++ )
③ ;
printf("sum=%d\n",s);
}
void mult(int m)
{ long int i, s=1;
for(i=1;i<=m;i++ )
s *= i;
printf("mult= %ld\n";s);
}
dispform( )
{ printf ("usage:sm n(+/!) (n>0)\n");
exit (0);
}
【3.57】下面程序的功能是键盘上输入一个字符串,把该字符串中的小写字母转换为大写字母,输出到文件test.txt中,然后从该文件读出字符串并显示出来。
#include <stdio.h>
main()
{ char str[100];
int i=0;
FILE *fp;
if((fp=fopen("test.txt", ① ))==NULL)
{ printf("Can't open the file.\n");
exit(0);
}
printf("Input a string:\n");
gets(str);
while(str[i])
{ if(str[i]>= 'a'&&str[i]<= 'z')
str[i]= ② ;
fputc(str[i],fp);
i++;
}
fclose(fp);
fp=fopen("test.txt", ③ );
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
}
【3.58】下面程序的功能是将从终端上读入的10个整数以二进制方式写入名为"bi.dat"的新文件中。
#include <stdio.h>
FILE *fp;
main()
{ int i, j;
if(( fp=fopen( ① , "wb" )) == NULL )
exit (0);
for( i=0;i<10;i++ )
{ scanf("%d", &j );
fwrite( ② , sizeof(int), 1, ③ );
}
fclose( fp);
}
【3.59】以字符流形式读入一个文件,从文件中检索出六种C语言的关键字,并统计、 输出每种关键字在文件中出现的次数。本程序中规定:单词是一个以空格或'\t'、 '\n'结束的字符串。
#include <stdio.h>
#include <string.h>
FILE *cp;
char fname[20], buf[100];
int num;
struct key
{ char word[10];
int count;
}keyword[]={ "if", 0, "char", 0, "int", 0,
"else", 0, "while", 0, "return", 0};
char *getword (FILE *fp)
{ int i=0;
char c;
while((c=getc(fp)) != EOF && (c==' '||c=='\t'||c=='\n')) ;
if( c==EOF ) return (NULL) ;
else buf[i++]=c;
while((c = ① && c!= ' ' && c!= '\t' && c!= '\n' )
buf[i++] = c;
buf[i]= '\0';
return(buf);
}
lookup(char *p)
{ int i;
char *q, *s;
for(i=0;i<num;i++)
{ q = ② ;
s=p;
while( *s && (*s==*q) )
{ ③
}
if( ④ )
{ keyword[i].count++;
break;
}
}
return;
}
main()
{ int i;
char *word;
printf("Input file name:");
scanf("%s", fname);
if((cp=fopen(fname, "r")) ==NULL )
{ printf("File open error: %s\n", fname);
exit(0);
}
num = sizeof(keyword) / sizeof(struct key);
while( ⑤ )
lookup(word);
fclose(cp);
for(i=0;i<num;i++)
printf("keyword:%-20scount=%d\n",keyword[i].word,keyword[i].count);
}
【3.60】下面程序的功能是从键盘接受姓名(例如:输入"ZHANG SAN"),在文件"try.dat"中查找,若文件中已经存入了刚输入的姓名,则显示提示信息;若文件中没有刚输入的姓名,则将该姓名存入文件。要求:⑴若磁盘文件"try.dat",已存在,则要保留文件中原来的信息;若文件"try.dat"不存在,则在磁盘上建立一个新文件;⑵当输入的姓名为空时(长度为0),结束程序。
#include <stdio.h>
main()
{ FILE *fp;
int flag;
char name[30], data[30];
if((fp=fopen("try.dat", ① ))==NULL )
{ printf("Open file error\n");
exit(0);
}
do
{ printf("Enter name:");
gets(name);
if( strlen(name)==0 )
break;
strcat(name, "\n");
② ;
flag=1;
while( flag && (fgets(data, 30, fp) ③ ) )
if( strcmp(data, name) == 0 )
④ ;
if( flag )
fputs(name, fp);
else
printf("\tData enter error !\n");
} while( ⑤ );
fclose(fp);
}
【程序填空题参考答案】
【3.1】答案: ① a+b ② a-b ③ a-b
【3.2】答案:① 1.0/(float)(i*i) ② sqrt(6*s)
【3.3】答案:① *k=p
【3.4】答案:① t=t*i ② t=t>0?-1:1
【3.5】答案:① d=1 ② k++ ③ k<=n
【3.6】答案: ① x>=0 ② x<amin
【3.7】答案: ① 2 ② 2
【3.8】答案:① m=n ② m>0 ③ m=m/10
【3.9】答案:① i==j ② k!=i&&k!=j
【3.10】答案:① i<=9 ② j%3!=0
【3.11】答案:① m=n ② r!=0 ③ return(n)
【3.12】答案:① float a[10],x ② i<=9 ③ i<=8 ④ j<=9-i
⑤ a[j]>a[j+1] ⑥ a[j]=a[j+1] ⑦ i<=9 ⑧i%5==0
【3.13】答案:① &a[i] ② continue
注释:①是基本概念,使用scanf函数输入数组元素的值。当输入的元素值小于0时,应当跳过后面的语句,取下一个数,所以②要填入continue。
【3.14】答案:① s[j++]=s[i] ② s[j]=s[i]
【3.15】答案:① s[j++]=s[i]
【3.16】答案:① x[i++]
【3.17】答案:① i=strlen(a);i>=j;i-- ② a[i+1]=a[i]
【3.18】答案:① '\0' ② str1[i]-str2[i]
【3.19】答案:① j++ ② '\\' ③ j++ ④ '\\' ⑤ j++ ⑥ '\0'
【3.20】答案:① t[k]!= '\0' ② t[k]== '\0'