C語言必背經典程式

2021-10-25 09:54:35 字數 2708 閱讀 8640

1、/輸出99口訣。共9行9列,i控制行,j控制列。/

#include 「stdio.h」

main()

printf("\n");/每一行後換行/}}

2、/*古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?

指標法:

void invert(char s)

}main()

printf(「input str:\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中的字元。/

#include

main()

10、/*編寫乙個void sort(int x,int n)實現將x陣列中的n個資料從大到小

排序。n及陣列元素在主函式中輸入。將結果顯示在螢幕上並輸出到檔案p9_1.out中/

#include

void sort(int *x,int n)}}

void main()

system(「pause」);

fclose(fp);

}11、已知陣列a中的元素已按由小到大順序排列,以下程式的功能是將輸入的乙個數插入陣列a中,插入後,陣列a中的元素仍然由小到大順序排列/

main()

; /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)

/將比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)

}main()

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()

int search(char s1,char s2)

return -1;

}14、/用指標變數輸出結構體陣列元素。/

struct student

stu[5]=,,,,};

main()

15、/建立乙個有三個結點的簡單鍊錶:/

#define null 0

struct student

;void main()

while(p!=null);

}16、/輸入乙個字串,判斷其是否為回文。回文字串是指從左到右讀和從右到左讀完全相同的字串。/

#include

#include

#include

main()

17、/氣泡排序,從小到大,排序後結果輸出到螢幕及檔案myf2.out/

#include

void fun(int a,int n)

}main()

,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++)

fclose(f);

}18、編寫函式countpi,利用公式

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

#include

double countpi(double eps) /eps為允許誤差/

return(2*s);

}main()

pi= countpi(eps);

printf(「pi=%lf\n」,pi);

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

fclose(fp);

}

C語言必背18個經典程式

c語言必背 18個經典程式 1 輸出 9 9口訣。共9行 9列,i控制行,j控制列。include stdio.h main if leap 內迴圈結束後,leap 依然為1,則m 是素數 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 f...

C語言必背18個經典程式

include stdio.h main if leap 內迴圈結束後,leap依然為1,則m是素數 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 include include main printf s n...

c語言必背18個經典程式

1 輸出9 9口訣。共9行9列,i控制行,j控制列。include stdio.h main printf n 每一行後換行 2 古典問題 有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?兔子的規律為數列1,1,2,...