printf("max=%d",max);
}
七、编写函数fun:
从键盘输入一个大于3得整数,调用函数fun判断其就是否素数,然后在main函数中输出相应得结论信息。
说明:
素数就是仅能被1与自身整除得数
输入:
7
输出:
7 is a prime、
输入:
8
输出:
8 is not a prime、
#include
intfun(intn)
{
/**********Program**********/
inti,flag;
if(n<2)
flag=0;
else
for(i=2,flag=1;i<=n/2&&flag;i++)
if(n%i==0)flag=0;
return(flag);
/**********End**********/
}
main
{
intm,flag;
printf("inputaninteger:
");
scanf("%d",&m);
flag=fun(m);
if(flag)
printf("%disaprime、\n",m);
else
printf("%disnotaprime、\n",m);
}
八、给定程序中,程序得功能就是:
统计子串sub在母串中出现得次数,请填空。
例如输入:
abcabsdefabc
abc
输出:
2
#include
#include
intcount(char*p,char*q)
{intm,n,k,num=0;
for(m=0;p[m];m++)
for(n=m,k=0;q[k]==p[n];k++,n++)
if(q[k+1]=='\0')
{num++;break;}
returnnum;
}
voidmain
{chars[80],sub[80];
gets(s);
gets(sub);
printf("%d\n",count(s,sub));
}
九、编写函数fun:
程序得功能就是:
调用函数fun从字符串s中删除指定得字符c。
然后在main函数中输出删除指定字符后得字符串。
例如:
从指定得字符串中"turbo c and borland c++ "中删除指定得字符'a'
输出:
str=turbo c nd borlnd c++
#include
fun(chars[],charc)
{/**********Program**********/
inti,j;
for(i=j=0;s[i]!
='\0';i++)
if(s[i]!
=c)
s[j++]=s[i];
s[j]='\0';
/**********End**********/
}
main
{staticcharstr[]="turbocandborlandc++";
charc='a';
fun(str,c);
printf("str=%s\n",str);
}
十、给定程序中,程序得功能就是:
计算一个英文句子中最长单词得长度(字母个数)max。
假设该英文句子中只含有字母与空格,在空格之间连续得字母串称为单词,句子以'、'结束,请填空。
本例输出:
max=5
#include
main
{staticchars[]={"youmakemehappywhendaysaregrey、"},*t;
intmax=0,len=0;
t=s;
while(*t!
='、')
{
while(((*t<='Z')&&(*t>='A'))||((*t<='z')&&(*t>='a')))
{len++;
t++;
}
if(maxmax=len;
len=0;
t++;
}
printf("max=%d",max);
}
十一、在指定得C文件内编写程序。
程序得功能就是:
输入2个字符串,输出2个字符串中较短串得长度。
按指定格式输入输出
例如:
输入:
hello
abcdefg
输出:
5
#include
#include
voidmain
{
chara[999],b[999];
gets(a);
gets(b);
if(strlen(a)printf("%d",strlen(a));
else
printf("%d",strlen(b));
}
十二、给定程序中,程序得功能就是:
将给定得字符串、整数、浮点数写入文件f1、txt中,再用字符方式从此文本文件中逐个读入并显示在屏幕上请填空。
(正确答案未知)
#include
voidmain
{chars[10]="Hello!
";
inta=12345;
doublef=98、76;
FILE*fp;
charch;
/***************************************/
fp=&ch;
/***************************************/
fprintf(fp,"%s%d%f",s,a,f);
fclose(fp);
fp=fopen("f1、txt;","r");
printf("\nTheresult:
");
/***************************************/
while(!
feof(fp))
/***************************************/
{
ch=fgetc(fp);
(3)
}
/***************************************/
putchar('\n');
fclose(fp);
}
十三、给定程序中,程序得功能就是:
比较两个字符串就是否相等,若相等则返回1,否则返回0,请填空并将程序补充完整。
例如输入:
abcdef
abcdef
输出:
两个字符串相等
输入:
abcdefefg
abcdef
输出:
两个字符串不相等
#include
#include
intfun(char*s,char*t)
{intm=0;
while(*(s+m)==*(t+m)&&*(s+m)!
='\0')
m++;
return(m==strlen(s)&&m==strlen(t));
}
voidmain
{
/******************************/
chara[999],b[999];
intt;
gets(a);
gets(b);
t=fun(a,b);
if(t)
printf("两个字符串相等");
else
printf("两个字符串不相等");
/******************************/
}
十四、程序得功能就是:
反向输出一个链表,请填空。
(答案未知)
例如输入:
1020304050
输出:
5040302010
#include
structnode
{intdata;
structnode*next;
};
voidmain
{structnode*ptr,*head,*tail;
intnum,i;
/**************************************************/
tail=___
(1)___;
/**************************************************/
tail>next=NULL;
ptr=tail;
printf("\npleaseinput5data:
\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr>data=num;
head=(structnode*)malloc(sizeof(structnode));
/**************************************************/
head>next=
(2);
/**************************************************/
ptr=head;
}
/**************************************************/
ptr=___(3)___;
/**************************************************/
while(ptr!
=NULL)
{printf("%d",ptr>data);
ptr=ptr>next;
}
}
十五、编写函数fun:
从键盘为一维整型数组输入10个整数,调用fun函数找出其中最小得数,并在main函数中输出。
例如输入:
63 72 56 98 66 83 68 40 95 78
输出:
40
#include"stdio、h"
intfun(intx[],intn)
{
/**********Program**********/
intmin=x[0],i;
for(i=1;iif(x[i]min=x[i];
return(min);
/**********End**********/
}
main
{
inta[10],i,min;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
printf("%3d",a[i]);
printf("\n");
min=fun(a,10);
printf("%d\n",min);
}
十六、程序得功能就是:
从键盘输入姓名,存入文件data02、txt中。
如果文件该姓名已存在,
则显示文件已存在得相应信息;再次输入其她姓名,若文件中没有该姓名,则将其存入文件中,请填空。
(答案未知)
#include
#include
#include
voidmain
{
FILE*fp;
intflag=0;
charname[20],data[20];
/*******************************************/
if((fp=fopen("data02、txt",___
(1)___))=NULL)
/*******************************************/
{printf("Cannotopenfile、\n");
exit(0);
}
do
{
printf("Entername:
");
/*******************************************/
___
(2)___;
/*******************************************/
if(strlen(name)==0)
break;
elsestrcat(name,"\n");
rewind(fp);
flag=1;
while(flag&&((fgets(data,20,fp)!
=NULL)))
if(strcmp(data,name)==0)
flag=0;
if(flag)
/*******************************************/
___(3)___;
/*******************************************/
else
printf("\tThisnamehasbeenexisted!
\n");
}while(ferror(fp)==0);
fclose(fp);
}