吐血整理:c語言必背18個經典程序!

{ printf("%d ",*p);

fprintf(fp,"%d ",*p); }

system("pause");

fclose(fp);

}

11、

已知數組a中的元素已按由小到大順序排列,以下程序的功能是將輸入的一個數插入數組a中,插入後,數組a中的元素仍然由小到大順序排列*/

main()

{ int a[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

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 the file\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("The new string is----%s\n",str);

fprintf(fp,"The new string is----%s\n",str);

fclose(fp);

}

13、

/*在一個字串s1中查找一子串s2,若存在則返回子串在主串中的起始位置

,不存在則返回-1。*/

main()

{char s1[6]="thisis";char s2[5]="is";

printf("%d\n",search(s1,s2));

system("pause");

}

int search(char s1[],char s2[])

{int i=0,j,len=strlen(s2);

while(s1[i]){

for(j=0;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

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 student a,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

#include

main()

{ char s[100];

int i,j,n;

printf("輸入字符串:\n");

gets(s);

n=strlen(s);

for(i=0,j=n-1;i

if(s[i]!=s[j]) break;

if(i>=j) printf("是迴文串\n");

else printf("不是迴文串\n");

}

17、

/*冒泡排序,從小到大,排序後結果輸出到屏幕及文件myf2.out*/

#include

void fun(int a[],int n)

{int i,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;}

}

main()

{int a[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.out failed!\n");

fun(a,10);

for(i=0;i<10;i++)

{printf("%4d",a[i]);

fprintf(f,"%4d",a[i]);

}

fclose(f);

}

18、

編寫函數countpi,利用公式

吐血整理:c語言必背18個經典程序!

計算π的近似值,當某一項的值小於10-5時,認為達到精度要求,請完善函數。將結果顯示在屏幕上並輸出到文件p7_3.out中。

#include

double countpi(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 the file\n");

exit(0);

}

pi= countpi(eps);

printf("pi=%lf\n",pi);

fprintf(fp,"pi=%lf\n",pi);

fclose(fp);

}


分享到:


相關文章: