printf("第%d个整数为:
%d\n",i+1,a[i]);
return0;
}
2.完成encrypt函数,实现加密功能:
将字符串中所有小写英文字母循环加密。
如a到b,
b到c,…,z到a。
如输入zyhavealittleapple!
,输出为:
azibwfbmjuumfbqqmf!
#include
#include
voidencrypt(char*s)
{
for(;*s!
='\0';s++)
if(((*s)>='a')&&((*s)<='z'))
if((*s)=='z')
(*s)='a';
else
(*s)=(*s)+1;
}
intmain()
{
chart1[80],ch;
gets(t1);
printf("\ntheoriginaldatais:
%s",t1);
encrypt(t1);
printf("\ntheresulteddatais:
%s",t1);
printf("\n");
return0;
}
3.用选择法实现对10个整数按从小到大的顺序排序输出(要求用指针实现)。
#include
intmain()
{
inta[10],i,j;
voidsort(int*);
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\ntheoriginaldatais:
");
for(i=0;i<10;i++)printf("%d",a[i]);
sort(a);
printf("\ntheresultdatais:
");
for(i=0;i<10;i++)
printf("%d",a[i]);
puts("");
return0;
}
voidsort(int*p)
{
inti,j,t;
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
if(*(p+i)>*(p+j))
{
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
4.编写一个函数实现字符串复制功能,不能使用strcpy函数。
(完成copy_string函数)
#include
intmain()
{
chara[100],b[20];
voidcopy_string(char*,char*);
gets(b);
printf("\ntheoriginaldatais:
%s",b);
copy_string(a,b);
printf("\ntheresultdatais:
%s\n",a);
return0;
}
voidcopy_string(char*p,char*q)
{
while(*p++=*q++);
}
又解:
#include
intmain()
{
chara[100],b[20];
voidcopy_string(char*,char*);
gets(b);
printf("\ntheoriginaldatais:
%s",b);
copy_string(a,b);
printf("\ntheresultdatais:
%s\n",a);
return0;
}
voidcopy_string(char*p,char*q)
{
while(*q)
*(p++)=*(q++);
*p='\0';
}
5.完成fun函数:
统计tt所指字符串中'a'到'z'共26各小写字母各自出现的次数,并依此放在pp所指数组中。
例如:
当输入abcdefgabcdeabc后,程序的输出结果应该为:
3332211000000000000
#include
voidfun(char*tt,intpp[])
{inti=1;
for(i=0;i<26;i++)pp[i]=0;
for(;*tt!
=0;tt++)
pp[*tt-97]++;
}
voidmain()
{
charaa[1000];
intbb[26],k;
printf("\npleaseenteracharstring:
");
scanf("%s",aa);
fun(aa,bb);
for(k=0;k<26;k++)
printf("%d",bb[k]);
printf("\n");
}
6.用冒泡法实现对10个整数按从大到小的顺序排序输出(要求用指针实现)。
#include
intmain()
{
inta[10],i,j;
voidsort(int*);
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\ntheoriginaldatais:
");
for(i=0;i<10;i++)printf("%d",a[i]);
sort(a);
printf("\ntheresultdatais:
");
for(i=0;i<10;i++)
printf("%d",a[i]);
puts("");
return0;
}
voidsort(int*p)
{
inti,j,t;
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(*(p+j)>*(p+j+1))
{
t=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=t;
}
}
7.编写函数:
将ss所指字符串中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。
#include
#include
#defineMAX100
voidfun(char*ss)
{
char*t=ss+1;
while(*t)
{if((*t>='a')&&(*t<='z'))*t=*t-32;
t=t+2;}
}
voidmain()
{
chartt[51];
printf("inputastringwithin50characters:
\n");
gets(tt);
printf("\n\nafterchanging,thestring\n\"%s\"",tt);
fun(tt);
printf("\nbecomes\n\"%s\"",tt);
printf("\n");
}
8.有一篇文章,共有4行文章,每行有10个字符。
要求分别统计出其中英文大写字符、小写字母、数字、空格以及其他字符的个数。
(完成count函数)
#include
intmain()
{
voidcount(char(*p)[10],int[]);
charstr[4][10];
intwzxmt[5]={0,0,0,0,0};//分别统计大写、小写、数字、空格、其他字符个数
printf("input4string:
\n");
for(inti=0;i<4;i++)
gets(str[i]);
count(str,wzxmt);
printf("\ntheresultisbigcharacternis:
%d",wzxmt[0]);
printf("\ntheresultislittlecharacternis:
%d",wzxmt[1]);
printf("\ntheresultisdigitcharacternis:
%d",wzxmt[2]);
printf("\ntheresultisspacecharacternis:
%d",wzxmt[3]);
printf("\ntheresultisothercharacternis:
%d",wzxmt[4]);
return0;
}
voidcount(char(*p)[10],intwzxmt[])
{inti,j;
for(i=0;i<4;i++)
for(j=0;p[i][j]!
='\0';j++)
if((p[i][j]>='A')&&(p[i][j]<='Z'))
wzxmt[0]++;
elseif((p[i][j]>='a')&&(p[i][j]<='z'))
wzxmt[1]++;
elseif((p[i][j]>='0')&&(p[i][j]<='9'))
wzxmt[2]++;
elseif(p[i][j]=='')//空格字符的asicc码值为32,''也可写成32
wzxmt[3]++;
else
wzxmt[4]++;
}
9.从键盘输入5个学生的信息(包括姓名,成绩),存到磁盘文件上。
然后再从磁盘文件上读取第1,3,5个学生数据输入计算机,并在屏幕上显示出来。
#include
#include
#defineN5
structStudent
{
charname[10];
intscore;
}stud[N];
voidsave()
{
FILE*fp;
inti;
if((fp=fopen("d:
\\file4.dat","w"))==NULL)
{
printf("cannotopenfile\n");
return;
}
for(i=0;iif(fwrite(&stud[i],sizeof(structStudent),1,fp)!
=1)
printf("filewriteerror\n");
fclose(fp);
}
voidread()
{
FILE*fp=fopen("d:
\\file4.dat","r");
for(inti=0;i{
fseek(fp,i*sizeof(structStudent),0);
fread(&stud[i],sizeof(structStudent),1,fp);
printf("读取的数据为:
%s%d\n",stud[i].name,stud[i].score);
}
fclose(fp);
}
intmain()
{
printf("pleaseenterdataofstudent:
\n");
for(inti=0;i//scanf("%s,%d",stud[i].name,&stud[i].score);
{printf("\n姓名:
");scanf("%s",stud[i].name);
printf("\n成绩:
");scanf("%d",&stud[i].score);}
save();
read();
return0;
}
10.从键盘上读入3个字符串,对它们按字母大小的升序排序,然后把排好序的字符串送到磁盘文件中保存,文件把保存到d盘根目录下,完成write函数。
#include
#include
#include
voidwrite(char(*st)[20],FILE*fp1)
{
inti,j;
chart[20];
for(i=0;i<2;i++)
for(j=0;j<3-i-1;j++)
if(strcmp(st[j],st[j+1])>0)
{
strcpy(t,st[j]);
strcpy(st[j],st[j+1]);
strcpy(st[j+1],t);
}
if((fp1=fopen("d:
\\file.txt","w"))==NULL)
{
printf("can'topenfile\n");
exit(0);
}
for(i=0;i<3;i++)
{
fputs(st[i],fp1);fputs("\n",fp1);
}
fclose(fp1);
}
voidoutput(FILE*fp2)
{
charbuf[30];
if((fp2=fopen("d:
\\file.txt","r"))==NULL)
{printf("can'topenfile\n");
exit(0);
}
while(fgets(buf,20,fp2))
printf("%s",buf);
fclose(fp2);
}
intmain()
{
FILE*fp;
chara[3][20],t[10];
inti,j,k;
printf("Enterthreestrings:
\n");
for(i=0;i<3;i++)
gets(a[i]);
write(a,fp);
output(fp);
return0;
}
11.从键盘输入两个字符串,分别保存在磁盘文件“file1”和“file2”中,完成hebing函数,要求把两个文件中的信息合并(按字母顺序排序),输出到一个新文件“file3”中去。
(file1、file2和file3都存放在d盘根目录下)
#include
#include
voidcreatfile()
{
FILE*fp;
charst[20];
printf("输入第一个字符串:
");
gets(st);
if((fp=fopen("d:
\\file1.txt","w"))==NULL)
{printf("cannotopenfile");
exit(0);
}
fputs(st,fp);
fclose(fp);
printf("\n输入第二个字符串:
");
gets(st);
if((fp=fopen("d:
\\file2.txt","w"))==NULL)
{printf("cannotopenfile");
exit(0);
}
fputs(st,fp);
fclose(fp);
}
voidhebing(charst[],intn)
{
inti,j;
FILE*fp;
chart;
for(i=0;ifor(j=i+1;jif(st[i]>st[j])
{
t=st[i];
st[i]=st[j];
st[j]=t;
}
if((fp=fopen("d:
\\file3.txt","w"))==NULL)
{printf("cannotopenfile");
exit(0);
}
for(i=0;i{
putc(st[i],fp);
}
fclose(fp);
}
voidoutput(FILE*fp1,intn)
{
charch;
if((fp1=fopen("d:
\\file3.txt","r"))==NULL)
{printf("cannotopenfile");
exit(0);
}
printf("\n");
while((ch=fgetc(fp1))!
=EOF)
putchar(ch);
printf("\n");
fclose(fp1);
}
intmain()
{
FILE*fp;
inti,j,n,i1;
charc[100],t,ch;
creatfile();
if((fp=fopen("d:
\\file1.txt","r"))==NULL)
{printf("\ncannotopenfile\n");
exit(0);
}
printf("file1:
\n");
for(i=0;(ch=fgetc(fp))!
=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
i1=i;
if((fp=fopen("d:
\\file2.txt","r"))==NULL)
{
printf("\ncannotopenfile\n");
exit(0);
}
printf("\nfile2:
\n");
for(i=i1;(ch=fgetc(fp))!
=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
hebing(c,i);
printf("\nfile3:
\n");
output(fp,i);
return0;
}
12.写一个函数,完成将任意一个数组中的值按逆序重新存放。
(数组长度不超过6位)
要求:
数组内容的内容输入和重新存放后的输出都要在主函数中实现。
如:
输入内容为5个整数,分别为86541,重新存放后输出为:
theresultis1,4,5,6,8。
#include
#defineN6
voidfun(inta[],intn)
{
intt,i;
for(i=0;i{
t=a[i];
a[i]=a[n-1-i];
a[n-1-i]=t;
}
}
intmain()
{inta[N],i,n;
printf("inputarraylength:
");
scanf("%d",&n);
for(i=0;iscanf("%d",&a[i]);
fun(a,n);
printf("theresultis:
");
for(i=0;iprintf("%d,",a[i]);
return0;
}
13.写一个判断素数的函数,在主函数输入一个整数,是素数时输出yes,否则输出no,结果要求在主函数中输出。
#include
intmain()
{
inta;
intis_prime(int);
scanf("%d",&a);
if(is_prime(a))
printf("yes\n");
else
printf("no\n");
return0;
}
intis_prime(inta)
{
inti;
for(i=2;i{
if(a%i==0)break;
}
return(i>=a);
}
14.用递归调用的方法求n!
。
如:
输入为5,输出为5!
=120。
#include
intmain()
{
intfac(int);
intn;
scanf("%d",&n);
printf("%d!
=%d\n",n,fac(n));
return0;
}
intfac(intn)
{
intc;
if((n==0||n==1))
c=1;
else
c=fac(n-1)*n;
returnc;
}
15.编写一个函数,实现在一个升序排列的整型数组{1,3,5,7,14,23,45,87,155,231};中插入任意一个整数的功能,保持整型数组仍然升序排列,并在main函数中调用此函数,输入数据和最后输出都在主函数中实现。
#include
inta[11]={1,3,5,7,14,23,45,87,155,231};
intmain()
{
intc,i;
voidfun(inta[],intc);
printf("inputaninteger:
");
scanf("%d",&c);
fun(a,c);
for(i=0;i<11;i++)
printf("%d",a[i]);
printf("\n");
return0;
}
voidfun(inta[],intt)
{
inti,j;
if(t>a[9])
a[10]=t;
else
{
for(i=0;i<11;i++)
if(tfor(j=10;j>i;j--)
a[j]=a[j-1];
a[i]=t;
}
}
16.输入两个整数,要求用两个函数求出其最大公约数和最小公倍数,最大公约数和最小公倍数都在主函数中输出。
如:
输入36和60,输出为:
zdgys=12,zxgbs=180
#include
intmain()
{
inti,j,t;
intmax(int,int);
intmin(int,int);
scan