11、输入的一个数插入数组中
已知数组a中的元素已按由小到大顺序排列,以下程序的功能是将输入的一个数插入数组a中,插入后,数组a中的元素仍然由小到大顺序排列
main() { inta[10]={0,12,17,20,25,28,30}; /*a[0]为工作单元,从a[1]开始存放数据*/ int x , i, j=6; /*j为元素个数*/ printf("Enter a number: "); 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,char c1,char c2)实现将s所指向的字符串中所有字符c1用c2替换,字符串、字符c1和c2均在主函数中输入,将原始字符串和替换后的字符串显示在屏幕上,并输出到文件p10_2.out中
#include<stdio.h> replace(char*s,char c1,char c2) {while(*s!='\0') { if(*s==c1) *s=c2; s++; } } main() { FILE *fp; char str[100],a,b; if((fp=fopen("p10_2.out","w"))==NULL) { printf("cannot open thefile\n"); exit(0); } printf("Enter a string:\n"); gets(str); printf("Enter a&&b:\n"); scanf("%c,%c",&a,&b); printf("%s\n",str); fprintf(fp,"%s\n",str); replace(str,a,b); printf("Thenew string is----%s\n",str); fprintf(fp,"Thenew string is----%s\n",str); fclose(fp); }
13、再一个字串中找另一个字串
在一个字串s1中查找一子串s2,若存在则返回子串在主串中的起始位置
,不存在则返回-1。
main() {chars1[6]="thisis";char s2[5]="is"; printf("%d\n",search(s1,s2)); system("pause"); } int search(chars1[],char s2[]) {inti=0,j,len=strlen(s2); while(s1[i]){ for(j=0;j<len;j++) if(s1[i+j]!=s2[j]) break; if(j>=len)return i; else i++; } return -1; }
14、用指针变量输出结构体数组元素。
struct student { int num; char *name; char sex; int age; }stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}}; main() {int i; struct student *ps; printf("Num \tName\t\t\tSex\tAge\t\n"); /*用指针变量输出结构体数组元素。*/ for(ps=stu;ps<stu+5;ps++) 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、建立一个有三个结点的简单链表
#define NULL 0 struct student { int num; char *name; int age ; struct student*next; }; void main() { struct studenta,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<stdio.h> #include<string.h> #include<string.h> main() { char s[100]; int i,j,n; printf("输入字符串:\n"); gets(s); n=strlen(s); for(i=0,j=n-1;i<j;i++,j--) if(s[i]!=s[j]) break; if(i>=j) printf("是回文串\n"); else printf("不是回文串\n"); }
17、冒泡排序
冒泡排序,从小到大,排序后结果输出到屏幕及文件
myf2.out
#include<stdio.h> void fun(inta[],int n) {int i,j,t; for(i=0;i<=n-1;i++) for(j=0;j<i;j++) if(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("open file myf2.outfailed!\n"); fun(a,10); for(i=0;i<10;i++) {printf("%4d",a[i]); fprintf(f,"%4d",a[i]); } fclose(f); }
18、编写函数countpi,利用公式
计算π的近似值,当某一项的值小于10-5时,认为达到精度要求,请完善函数。将结果显示在屏幕上并输出到文件p7_3.out中。
#include<stdio.h> doublecountpi(double eps) /*eps为允许误差*/ { int m=1; double temp=1.0,s=0; while(temp>=eps) { s+=temp; temp=temp*m/(2*m+1); m++; } return(2*s); } main() {FILE *fp; double eps=1e-5,pi; if((fp=fopen("p7_3.out","w"))==NULL) { printf("cannot open thefile\n"); exit(0); } pi= countpi(eps); printf("pi=%lf\n",pi); fprintf(fp,"pi=%lf\n",pi); fclose(fp); }