完整word版C++必背18个经典程序.docx
《完整word版C++必背18个经典程序.docx》由会员分享,可在线阅读,更多相关《完整word版C++必背18个经典程序.docx(10页珍藏版)》请在冰豆网上搜索。
完整word版C++必背18个经典程序
c语言入门经典必背18个程序
1、/*输出9*9口诀。
共9行9列,i控制行,j控制列。
*/
#include"stdio.h"
main()
{inti,j,result;
for(i=1;i<10;i++)
{for(j=1;j<10;j++)
{
result=i*j;
printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/
}
printf("\n");/*每一行后换行*/
2、/*古典问题:
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔
子都不死,问每个月的兔子总数为多少?
兔子的规律为数列1,1,2,3,5,8,13,21....*/
longf1,f2;
inti;
f1=f2=1;
for(i=1;i<=20;i++)
{printf("%12ld%12ld",f1,f2);
if(i%2==0)printf("\n");/*控制输出,每行四个*/
f1=f1+f2;/*前两个月加起来赋值给第三个月*/
f2=f1+f2;/*前两个月加起来赋值给第三个月*/
3、/*判断101-200之间有多少个素数,并输出所有素数及素数的个数。
程序分析:
判断素数的方法:
用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
#include"math.h"
intm,i,k,h=0,leap=1;
printf("\n");
for(m=101;m<=200;m++)
{k=sqrt(m+1);
for(i=2;i<=k;i++)
if(m%i==0)
{leap=0;break;}
if(leap)/*内循环结束后,leap依然为1,则m是素数*/
{printf("%-4d",m);h++;if(h%10==0)
leap=1;
printf("\nThetotalis%d",h);
4、/*一个数如果恰好等于它的因子之和,这个数就称为“完数”。
例如6=1+2+3.编程
找出1000以内的所有完数。
staticintk[10];
inti,j,n,s;
for(j=2;j<1000;j++)
n=-1;
s=j;
for(i=1;i{if((j%i)==0){n++;s=s-i;k[n]=i;}}if(s==0){printf("%disawanshu:",j);for(i=0;iprintf("%d,",k[i]);printf("%d\n",k[n]);}}}5、/*下面程序的功能是将一个4 ×4的数组进行逆时针旋转90度后输出,要求原始数组的数据随机输入,新数组以4行4列的方式输出,请在空白处完善程序。*/main(){inta[4][4],b[4][4],i,j;/*a存放原始数组数据,b存放旋转后数组数据*/printf("input16numbers:");/*输入一组数据存放到数组a中,然后旋转存放到b数组中*/for(i=0;i<4;i++)for(j=0;j<4;j++){scanf("%d",&a[i][j]);b[3-j][i]=a[i][j];}printf("arrayb:\n");for(i=0;i<4;i++){for(j=0;j<4;j++)printf("%6d",b[i][j]);printf("\n");}}6、/*编程打印直角杨辉三角形*/main(){inti,j,a[6][6];for(i=0;i<=5;i++){a[i][i]=1;a[i][0]=1;}for(i=2;i<=5;i++)for(j=1;j<=i-1;j++)a[i][j]=a[i-1][j]+a[i-1][j-1];for(i=0;i<=5;i++){for(j=0;j<=i;j++)printf("%4d",a[i][j]);printf("\n");}}7、/*通过键盘输入3名学生4门课程的成绩,分别求每个学生的平均成绩和每门课程的平均成绩。要求所有成绩均放入一个4行5列的数组中,输入时同一人数据间用空格,不同人用回车其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均分。*/#include#includemain(){floata[4][5],sum1,sum2;inti,j;for(i=0;i<3;i++)for(j=0;j<4;j++)scanf("%f",&a[i][j]);for(i=0;i<3;i++){sum1=0;for(j=0;j<4;j++)sum1+=a[i][j];a[i][4]=sum1/4;}for(j=0;j<5;j++){sum2=0;for(i=0;i<3;i++)sum2+=a[i][j];a[3][j]=sum2/3;}for(i=0;i<4;i++){for(j=0;j<5;j++)printf("%6.2f",a[i][j]);printf("\n");}}8、/*完善程序,实现将输入的字符串反序输出,如输入 windows 输出swodniw。*/#includemain(){charc[200],c1;inti,j,k;printf("Enterastring:");scanf("%s",c);k=strlen(c);for(i=0,j=k-1;i{c1=c[i];c[i]=c[j];c[j]=c1;}printf("%s\n",c);}指针法:voidinvert(char*s){inti,j,k;chart;k=strlen(s);for(i=0,j=k-1;i{t=*(s+i);*(s+i)=*(s+j);*(s+j)=t;}}main(){FILE*fp;charstr[200],*p,i,j;if((fp=fopen("p9_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("inputstr:\n");gets(str);printf(“\n%s”,str);fprintf(fp,“%s”,str);invert(str);printf(“\n%s”,str);fprintf(fp,“\n%s”,str);fclose(fp);}9、/*下面程序的功能是从字符数组s中删除存放在c中的字符。*/#includemain(){chars[80],c;intj,k;printf("\nEnterastring:");gets(s);printf("\nEnteracharacter:");c=getchar();for(j=k=0;s[j]!='\0';j++)if(s[j]!=c)s[k++]=s[j];s[k]='\0';printf("\n%s",s);}10、/*编写一个voidsort(int*x,intn)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out中*/#includevoidsort(int*x,intn){inti,j,k,t;for(i=0;i{k=i;for(j=i+1;jif(x[j]>x[k])k=j;if(k!=i){t=x[i];x[i]=x[k];x[k]=t;}}}voidmain(){FILE*fp;int*p,i,a[10];fp=fopen("p9_1.out","w");p=a;printf("Input10numbers:");for(i=0;i<10;i++)scanf("%d",p++);p=a;sort(p,10);for(;p{printf("%d",*p);fprintf(fp,"%d",*p);}system("pause");fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/main(){inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/intx,i,j=6;/*j为元素个数*/printf("Enteranumber:");scanf("%d",&x);a[0]=x;i=j;/*从最后一个单元开始*/while(a[i]>x){a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/a[++i]=x;j++;/*插入x后元素总个数增加*/for(i=1;i<=j;i++)printf("%8d",a[i]);printf("\n");}12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/#includereplace(char*s,charc1,charc2){while(*s!='\0'){if(*s==c1)*s=c2;s++;}}main(){FILE*fp;charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("Enterastring:\n");gets(str);printf("Entera&&b:\n");scanf("%c,%c",&a,&b);printf("%s\n",str);fprintf(fp,"%s\n",str);replace(str,a,b);printf("Thenewstringis----%s\n",str);fprintf(fp,"Thenewstringis----%s\n",str);fclose(fp);}13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/main(){chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));system("pause");}intsearch(chars1[],chars2[]){inti=0,j,len=strlen(s2);while(s1[i]){for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
{if((j%i)==0)
{n++;
s=s-i;
k[n]=i;
if(s==0)
{printf("%disawanshu:
",j);
for(i=0;iprintf("%d,",k[i]);printf("%d\n",k[n]);}}}5、/*下面程序的功能是将一个4 ×4的数组进行逆时针旋转90度后输出,要求原始数组的数据随机输入,新数组以4行4列的方式输出,请在空白处完善程序。*/main(){inta[4][4],b[4][4],i,j;/*a存放原始数组数据,b存放旋转后数组数据*/printf("input16numbers:");/*输入一组数据存放到数组a中,然后旋转存放到b数组中*/for(i=0;i<4;i++)for(j=0;j<4;j++){scanf("%d",&a[i][j]);b[3-j][i]=a[i][j];}printf("arrayb:\n");for(i=0;i<4;i++){for(j=0;j<4;j++)printf("%6d",b[i][j]);printf("\n");}}6、/*编程打印直角杨辉三角形*/main(){inti,j,a[6][6];for(i=0;i<=5;i++){a[i][i]=1;a[i][0]=1;}for(i=2;i<=5;i++)for(j=1;j<=i-1;j++)a[i][j]=a[i-1][j]+a[i-1][j-1];for(i=0;i<=5;i++){for(j=0;j<=i;j++)printf("%4d",a[i][j]);printf("\n");}}7、/*通过键盘输入3名学生4门课程的成绩,分别求每个学生的平均成绩和每门课程的平均成绩。要求所有成绩均放入一个4行5列的数组中,输入时同一人数据间用空格,不同人用回车其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均分。*/#include#includemain(){floata[4][5],sum1,sum2;inti,j;for(i=0;i<3;i++)for(j=0;j<4;j++)scanf("%f",&a[i][j]);for(i=0;i<3;i++){sum1=0;for(j=0;j<4;j++)sum1+=a[i][j];a[i][4]=sum1/4;}for(j=0;j<5;j++){sum2=0;for(i=0;i<3;i++)sum2+=a[i][j];a[3][j]=sum2/3;}for(i=0;i<4;i++){for(j=0;j<5;j++)printf("%6.2f",a[i][j]);printf("\n");}}8、/*完善程序,实现将输入的字符串反序输出,如输入 windows 输出swodniw。*/#includemain(){charc[200],c1;inti,j,k;printf("Enterastring:");scanf("%s",c);k=strlen(c);for(i=0,j=k-1;i{c1=c[i];c[i]=c[j];c[j]=c1;}printf("%s\n",c);}指针法:voidinvert(char*s){inti,j,k;chart;k=strlen(s);for(i=0,j=k-1;i{t=*(s+i);*(s+i)=*(s+j);*(s+j)=t;}}main(){FILE*fp;charstr[200],*p,i,j;if((fp=fopen("p9_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("inputstr:\n");gets(str);printf(“\n%s”,str);fprintf(fp,“%s”,str);invert(str);printf(“\n%s”,str);fprintf(fp,“\n%s”,str);fclose(fp);}9、/*下面程序的功能是从字符数组s中删除存放在c中的字符。*/#includemain(){chars[80],c;intj,k;printf("\nEnterastring:");gets(s);printf("\nEnteracharacter:");c=getchar();for(j=k=0;s[j]!='\0';j++)if(s[j]!=c)s[k++]=s[j];s[k]='\0';printf("\n%s",s);}10、/*编写一个voidsort(int*x,intn)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out中*/#includevoidsort(int*x,intn){inti,j,k,t;for(i=0;i{k=i;for(j=i+1;jif(x[j]>x[k])k=j;if(k!=i){t=x[i];x[i]=x[k];x[k]=t;}}}voidmain(){FILE*fp;int*p,i,a[10];fp=fopen("p9_1.out","w");p=a;printf("Input10numbers:");for(i=0;i<10;i++)scanf("%d",p++);p=a;sort(p,10);for(;p{printf("%d",*p);fprintf(fp,"%d",*p);}system("pause");fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/main(){inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/intx,i,j=6;/*j为元素个数*/printf("Enteranumber:");scanf("%d",&x);a[0]=x;i=j;/*从最后一个单元开始*/while(a[i]>x){a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/a[++i]=x;j++;/*插入x后元素总个数增加*/for(i=1;i<=j;i++)printf("%8d",a[i]);printf("\n");}12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/#includereplace(char*s,charc1,charc2){while(*s!='\0'){if(*s==c1)*s=c2;s++;}}main(){FILE*fp;charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("Enterastring:\n");gets(str);printf("Entera&&b:\n");scanf("%c,%c",&a,&b);printf("%s\n",str);fprintf(fp,"%s\n",str);replace(str,a,b);printf("Thenewstringis----%s\n",str);fprintf(fp,"Thenewstringis----%s\n",str);fclose(fp);}13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/main(){chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));system("pause");}intsearch(chars1[],chars2[]){inti=0,j,len=strlen(s2);while(s1[i]){for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
printf("%d,",k[i]);
printf("%d\n",k[n]);
5、/*下面程序的功能是将一个4 ×4的数组进行逆时针旋转90度后输出,要求原始数组的数据随机输入,新数组以4行4列的
方式输出,
请在空白处完善程序。
{inta[4][4],b[4][4],i,j;/*a存放原始数组数据,b存放旋转后数组数据*/
printf("input16numbers:
");
/*输入一组数据存放到数组a中,然后旋转存放到b数组中*/
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{scanf("%d",&a[i][j]);
b[3-j][i]=a[i][j];
}printf("arrayb:
\n");
{for(j=0;j<4;j++)
printf("%6d",b[i][j]);
6、/*编程打印直角杨辉三角形*/
{inti,j,a[6][6];
for(i=0;i<=5;i++)
{a[i][i]=1;a[i][0]=1;}
for(i=2;i<=5;i++)
for(j=1;j<=i-1;j++)
a[i][j]=a[i-1][j]+a[i-1][j-1];
{for(j=0;j<=i;j++)
printf("%4d",a[i][j]);
printf("\n");}
7、/*通过键盘输入3名学生4门课程的成绩,
分别求每个学生的平均成绩和每门课程的平均成绩。
要求所有成绩均放入一个4行5列的数组中,输入时同一人数据间用空格,不同人用回车
其中最后一列和最后一行分别放每个学生的平均成绩、每门课程的平均成绩及班级总平均分。
#include
{floata[4][5],sum1,sum2;
inti,j;
for(i=0;i<3;i++)
scanf("%f",&a[i][j]);
{sum1=0;
sum1+=a[i][j];
a[i][4]=sum1/4;
for(j=0;j<5;j++)
{sum2=0;
sum2+=a[i][j];
a[3][j]=sum2/3;
}for(i=0;i<4;i++)
{for(j=0;j<5;j++)
printf("%6.2f",a[i][j]);
8、/*完善程序,实现将输入的字符串反序输出,
如输入 windows 输出swodniw。
{charc[200],c1;
inti,j,k;
printf("Enterastring:
scanf("%s",c);
k=strlen(c);
for(i=0,j=k-1;i{c1=c[i];c[i]=c[j];c[j]=c1;}printf("%s\n",c);}指针法:voidinvert(char*s){inti,j,k;chart;k=strlen(s);for(i=0,j=k-1;i{t=*(s+i);*(s+i)=*(s+j);*(s+j)=t;}}main(){FILE*fp;charstr[200],*p,i,j;if((fp=fopen("p9_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("inputstr:\n");gets(str);printf(“\n%s”,str);fprintf(fp,“%s”,str);invert(str);printf(“\n%s”,str);fprintf(fp,“\n%s”,str);fclose(fp);}9、/*下面程序的功能是从字符数组s中删除存放在c中的字符。*/#includemain(){chars[80],c;intj,k;printf("\nEnterastring:");gets(s);printf("\nEnteracharacter:");c=getchar();for(j=k=0;s[j]!='\0';j++)if(s[j]!=c)s[k++]=s[j];s[k]='\0';printf("\n%s",s);}10、/*编写一个voidsort(int*x,intn)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out中*/#includevoidsort(int*x,intn){inti,j,k,t;for(i=0;i{k=i;for(j=i+1;jif(x[j]>x[k])k=j;if(k!=i){t=x[i];x[i]=x[k];x[k]=t;}}}voidmain(){FILE*fp;int*p,i,a[10];fp=fopen("p9_1.out","w");p=a;printf("Input10numbers:");for(i=0;i<10;i++)scanf("%d",p++);p=a;sort(p,10);for(;p{printf("%d",*p);fprintf(fp,"%d",*p);}system("pause");fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/main(){inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/intx,i,j=6;/*j为元素个数*/printf("Enteranumber:");scanf("%d",&x);a[0]=x;i=j;/*从最后一个单元开始*/while(a[i]>x){a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/a[++i]=x;j++;/*插入x后元素总个数增加*/for(i=1;i<=j;i++)printf("%8d",a[i]);printf("\n");}12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/#includereplace(char*s,charc1,charc2){while(*s!='\0'){if(*s==c1)*s=c2;s++;}}main(){FILE*fp;charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("Enterastring:\n");gets(str);printf("Entera&&b:\n");scanf("%c,%c",&a,&b);printf("%s\n",str);fprintf(fp,"%s\n",str);replace(str,a,b);printf("Thenewstringis----%s\n",str);fprintf(fp,"Thenewstringis----%s\n",str);fclose(fp);}13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/main(){chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));system("pause");}intsearch(chars1[],chars2[]){inti=0,j,len=strlen(s2);while(s1[i]){for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
{c1=c[i];c[i]=c[j];c[j]=c1;}
printf("%s\n",c);
指针法:
voidinvert(char*s)
{inti,j,k;
chart;
k=strlen(s);
for(i=0,j=k-1;i{t=*(s+i);*(s+i)=*(s+j);*(s+j)=t;}}main(){FILE*fp;charstr[200],*p,i,j;if((fp=fopen("p9_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("inputstr:\n");gets(str);printf(“\n%s”,str);fprintf(fp,“%s”,str);invert(str);printf(“\n%s”,str);fprintf(fp,“\n%s”,str);fclose(fp);}9、/*下面程序的功能是从字符数组s中删除存放在c中的字符。*/#includemain(){chars[80],c;intj,k;printf("\nEnterastring:");gets(s);printf("\nEnteracharacter:");c=getchar();for(j=k=0;s[j]!='\0';j++)if(s[j]!=c)s[k++]=s[j];s[k]='\0';printf("\n%s",s);}10、/*编写一个voidsort(int*x,intn)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件p9_1.out中*/#includevoidsort(int*x,intn){inti,j,k,t;for(i=0;i{k=i;for(j=i+1;jif(x[j]>x[k])k=j;if(k!=i){t=x[i];x[i]=x[k];x[k]=t;}}}voidmain(){FILE*fp;int*p,i,a[10];fp=fopen("p9_1.out","w");p=a;printf("Input10numbers:");for(i=0;i<10;i++)scanf("%d",p++);p=a;sort(p,10);for(;p{printf("%d",*p);fprintf(fp,"%d",*p);}system("pause");fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/main(){inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/intx,i,j=6;/*j为元素个数*/printf("Enteranumber:");scanf("%d",&x);a[0]=x;i=j;/*从最后一个单元开始*/while(a[i]>x){a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/a[++i]=x;j++;/*插入x后元素总个数增加*/for(i=1;i<=j;i++)printf("%8d",a[i]);printf("\n");}12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/#includereplace(char*s,charc1,charc2){while(*s!='\0'){if(*s==c1)*s=c2;s++;}}main(){FILE*fp;charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("Enterastring:\n");gets(str);printf("Entera&&b:\n");scanf("%c,%c",&a,&b);printf("%s\n",str);fprintf(fp,"%s\n",str);replace(str,a,b);printf("Thenewstringis----%s\n",str);fprintf(fp,"Thenewstringis----%s\n",str);fclose(fp);}13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/main(){chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));system("pause");}intsearch(chars1[],chars2[]){inti=0,j,len=strlen(s2);while(s1[i]){for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
{t=*(s+i);*(s+i)=*(s+j);*(s+j)=t;}
{FILE*fp;
charstr[200],*p,i,j;
if((fp=fopen("p9_2.out","w"))==NULL)
{printf("cannotopenthefile\n");
exit(0);
printf("inputstr:
gets(str);
printf(“\n%s”,str);
fprintf(fp,“%s”,str);
invert(str);
fprintf(fp,“\n%s”,str);
fclose(fp);}
9、/*下面程序的功能是从字符数组s中删除存放在c中的字符。
{chars[80],c;
intj,k;
printf("\nEnterastring:
gets(s);
printf("\nEnteracharacter:
c=getchar();
for(j=k=0;s[j]!
='\0';j++)
if(s[j]!
=c)
s[k++]=s[j];
s[k]='\0';
printf("\n%s",s);
10、/*编写一个voidsort(int*x,intn)实现将x数组中的n个数据从大到小
排序。
n及数组元素在主函数中输入。
将结果显示在屏幕上并输出到文件p9_1.out中*/
voidsort(int*x,intn)
inti,j,k,t;
for(i=0;i{k=i;for(j=i+1;jif(x[j]>x[k])k=j;if(k!=i){t=x[i];x[i]=x[k];x[k]=t;}}}voidmain(){FILE*fp;int*p,i,a[10];fp=fopen("p9_1.out","w");p=a;printf("Input10numbers:");for(i=0;i<10;i++)scanf("%d",p++);p=a;sort(p,10);for(;p{printf("%d",*p);fprintf(fp,"%d",*p);}system("pause");fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/main(){inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/intx,i,j=6;/*j为元素个数*/printf("Enteranumber:");scanf("%d",&x);a[0]=x;i=j;/*从最后一个单元开始*/while(a[i]>x){a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/a[++i]=x;j++;/*插入x后元素总个数增加*/for(i=1;i<=j;i++)printf("%8d",a[i]);printf("\n");}12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/#includereplace(char*s,charc1,charc2){while(*s!='\0'){if(*s==c1)*s=c2;s++;}}main(){FILE*fp;charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("Enterastring:\n");gets(str);printf("Entera&&b:\n");scanf("%c,%c",&a,&b);printf("%s\n",str);fprintf(fp,"%s\n",str);replace(str,a,b);printf("Thenewstringis----%s\n",str);fprintf(fp,"Thenewstringis----%s\n",str);fclose(fp);}13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/main(){chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));system("pause");}intsearch(chars1[],chars2[]){inti=0,j,len=strlen(s2);while(s1[i]){for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
k=i;
for(j=i+1;jif(x[j]>x[k])k=j;if(k!=i){t=x[i];x[i]=x[k];x[k]=t;}}}voidmain(){FILE*fp;int*p,i,a[10];fp=fopen("p9_1.out","w");p=a;printf("Input10numbers:");for(i=0;i<10;i++)scanf("%d",p++);p=a;sort(p,10);for(;p{printf("%d",*p);fprintf(fp,"%d",*p);}system("pause");fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/main(){inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/intx,i,j=6;/*j为元素个数*/printf("Enteranumber:");scanf("%d",&x);a[0]=x;i=j;/*从最后一个单元开始*/while(a[i]>x){a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/a[++i]=x;j++;/*插入x后元素总个数增加*/for(i=1;i<=j;i++)printf("%8d",a[i]);printf("\n");}12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/#includereplace(char*s,charc1,charc2){while(*s!='\0'){if(*s==c1)*s=c2;s++;}}main(){FILE*fp;charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("Enterastring:\n");gets(str);printf("Entera&&b:\n");scanf("%c,%c",&a,&b);printf("%s\n",str);fprintf(fp,"%s\n",str);replace(str,a,b);printf("Thenewstringis----%s\n",str);fprintf(fp,"Thenewstringis----%s\n",str);fclose(fp);}13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/main(){chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));system("pause");}intsearch(chars1[],chars2[]){inti=0,j,len=strlen(s2);while(s1[i]){for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
if(x[j]>x[k])k=j;
if(k!
=i)
t=x[i];
x[i]=x[k];
x[k]=t;
voidmain()
int*p,i,a[10];
fp=fopen("p9_1.out","w");
p=a;
printf("Input10numbers:
for(i=0;i<10;i++)
scanf("%d",p++);
p=a;sort(p,10);
for(;p{printf("%d",*p);fprintf(fp,"%d",*p);}system("pause");fclose(fp);}11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列*/main(){inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/intx,i,j=6;/*j为元素个数*/printf("Enteranumber:");scanf("%d",&x);a[0]=x;i=j;/*从最后一个单元开始*/while(a[i]>x){a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/a[++i]=x;j++;/*插入x后元素总个数增加*/for(i=1;i<=j;i++)printf("%8d",a[i]);printf("\n");}12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/#includereplace(char*s,charc1,charc2){while(*s!='\0'){if(*s==c1)*s=c2;s++;}}main(){FILE*fp;charstr[100],a,b;if((fp=fopen("p10_2.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}printf("Enterastring:\n");gets(str);printf("Entera&&b:\n");scanf("%c,%c",&a,&b);printf("%s\n",str);fprintf(fp,"%s\n",str);replace(str,a,b);printf("Thenewstringis----%s\n",str);fprintf(fp,"Thenewstringis----%s\n",str);fclose(fp);}13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置,不存在则返回-1。*/main(){chars1[6]="thisis";chars2[5]="is";printf("%d\n",search(s1,s2));system("pause");}intsearch(chars1[],chars2[]){inti=0,j,len=strlen(s2);while(s1[i]){for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
{printf("%d",*p);
fprintf(fp,"%d",*p);}
system("pause");
fclose(fp);
11、已知数组a中的元素已按由小到大顺序排列 ,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍
然由小到大顺序排列*/
{inta[10]={0,12,17,20,25,28,30};/*a[0]为工作单元,从a[1]开始存放数据*/
intx,i,j=6;/*j为元素个数*/
printf("Enteranumber:
scanf("%d",&x);
a[0]=x;
i=j;/*从最后一个单元开始*/
while(a[i]>x)
{a[i+1]=a[i];i--;}/*将比x大的数往后移动一个位置*/
a[++i]=x;
j++;/*插入x后元素总个数增加*/
for(i=1;i<=j;i++)printf("%8d",a[i]);
12、/*编写函数replace(char*s,charc1,charc2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2
均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中*/
replace(char*s,charc1,charc2)
{while(*s!
='\0')
{if(*s==c1)
*s=c2;
s++;
charstr[100],a,b;
if((fp=fopen("p10_2.out","w"))==NULL)
exit(0);}
printf("Entera&&b:
scanf("%c,%c",&a,&b);
printf("%s\n",str);fprintf(fp,"%s\n",str);
replace(str,a,b);
printf("Thenewstringis----%s\n",str);
fprintf(fp,"Thenewstringis----%s\n",str);
13、/*在一个字串s1中查找一子串s2 ,若存在则返回子串在主串中的起始位置
,不存在则返回-1。
{chars1[6]="thisis";chars2[5]="is";
printf("%d\n",search(s1,s2));
intsearch(chars1[],chars2[])
{inti=0,j,len=strlen(s2);
while(s1[i]){
for(j=0;jif(s1[i+j]!=s2[j])break;if(j>=len)returni;elsei++;}return-1;}14、/*用指针变量输出结构体数组元素。*/structstudent{intnum;char*name;charsex;intage;}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};main(){inti;structstudent*ps;printf("Num\tName\t\t\tSex\tAge\t\n");/*用指针变量输出结构体数组元素。*/for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
if(s1[i+j]!
=s2[j])break;
if(j>=len)returni;
elsei++;
return-1;
14、/*用指针变量输出结构体数组元素。
structstudent
intnum;
char*name;
charsex;
intage;
}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',
18}};
{inti;
structstudent*ps;
printf("Num\tName\t\t\tSex\tAge\t\n");
/*用指针变量输出结构体数组元素。
for(ps=stu;psprintf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);/*用数组下标法输出结构体数组元素学号和年龄。*/for(i=0;i<5;i++)printf("%d\t%d\t\n",stu[i].num,stu[i].age);}15、/*建立一个有三个结点的简单链表:*/#defineNULL0structstudent{intnum;char*name;intage;structstudent*next;};voidmain(){structstudenta,b,c,*head,*p;a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/b.num=1002;b.name="liuxing";b.age=19;c.num=1003;c.name="huangke";c.age=18;head=&a;/*建立链表,a为头结点*/a.next=&b;b.next=&c;c.next=NULL;p=head;/*输出链表*/do{printf("%5d,%s,%3d\n",p->num,p->name,p->age);p=p->next;}while(p!=NULL);}16、/*输入一个字符串,判断其是否为回文。回文字符串是指从左到右读和从右到左读完全相同的字符串。*/#include#include#includemain(){chars[100];inti,j,n;printf("输入字符串:\n");gets(s);n=strlen(s);for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
printf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);
/*用数组下标法输出结构体数组元素学号和年龄。
for(i=0;i<5;i++)
printf("%d\t%d\t\n",stu[i].num,stu[i].age);
15、/*建立一个有三个结点的简单链表:
*/#defineNULL0
structstudent*next;
};
structstudenta,b,c,*head,*p;
a.num=1001;a.name="lihua";a.age=18;/*对结点成员进行赋值*/
b.num=1002;b.name="liuxing";b.age=19;
c.num=1003;c.name="huangke";c.age=18;
head=&a;/*建立链表,a为头结点*/
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;/*输出链表*/
do{
printf("%5d,%s,%3d\n",p->num,p->name,p->age);
p=p->next;
}while(p!
=NULL);
16、/*输入一个字符串,判断其是否为回文。
回文字符串是指从左到右读和从右到左读完全相同的字符串。
{chars[100];
inti,j,n;
printf("输入字符串:
n=strlen(s);
for(i=0,j=n-1;iif(s[i]!=s[j])break;if(i>=j)printf("是回文串\n");elseprintf("不是回文串\n");}17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/#includevoidfun(inta[],intn){inti,j,t;for(i=0;i<=n-1;i++)for(j=0;jif(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}main(){inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;FILE*f;if((f=fopen("myf2.out","w"))==NULL)printf("openfilemyf2.outfailed!\n");fun(a,10);for(i=0;i<10;i++){printf("%4d",a[i]);fprintf(f,"%4d",a[i]);}fclose(f);}18、编写函数countpi,利用公式⋯+×××+××+×++≈9473523173523152313112π计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。#includedoublecountpi(doubleeps)/*eps为允许误差*/{intm=1;doubletemp=1.0,s=0;while(temp>=eps){s+=temp;temp=temp*m/(2*m+1);m++;}return(2*s);}main(){FILE*fp;doubleeps=1e-5,pi;if((fp=fopen("p7_3.out","w"))==NULL){printf("cannotopenthefile\n");exit(0);}pi=countpi(eps);printf("pi=%lf\n",pi);fprintf(fp,"pi=%lf\n",pi);fclose(fp);}
if(s[i]!
=s[j])break;
if(i>=j)printf("是回文串\n");
elseprintf("不是回文串\n");
17、/*冒泡排序,从小到大,排序后结果输出到屏幕及文件myf2.out*/
voidfun(inta[],intn)
{inti,j,t;
for(i=0;i<=n-1;i++)for(j=0;j
if(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}
{inta[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;
FILE*f;
if((f=fopen("myf2.out","w"))==NULL)
printf("openfilemyf2.outfailed!
fun(a,10);
{printf("%4d",a[i]);
fprintf(f,"%4d",a[i]);
fclose(f);
18、编写函数countpi,利用公式
⋯+×××+××+×++≈
9
4
7
3
5
2
1
π
计算π的近似值,当某一项的值小于10-5
时,认为达到精度要求,请完善函数。
将结果显示在屏幕上并输出到文件p7_3.out中。
doublecountpi(doubleeps)/*eps为允许误差*/
intm=1;
doubletemp=1.0,s=0;
while(temp>=eps)
{s+=temp;
temp=temp*m/(2*m+1);
m++;
return(2*s);
doubleeps=1e-5,pi;
if((fp=fopen("p7_3.out","w"))==NULL)
pi=countpi(eps);
printf("pi=%lf\n",pi);
fprintf(fp,"pi=%lf\n",pi);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1