C語言必背18個(gè)經(jīng)典程序
b: ' n
for(1=0i<4:1++)
print(%o6dbliljI
printf("n")
6、/編程打印直角楊輝三角形*
Hint ij a[[6]
for(i=0;i<=5;i+-)
falli=l; a[ol
for(i-2;i<=5+-)
for(=l<=i-1;++)
i][]-a[i-1]j]+a[i-1-1];
for(i0;i<=5;i+-)
{fory-0j<-1j++)
printf("%c4d"a[iI])
7、通過鍵盤輸入3名學(xué)生4門課程的成績(jī),
分別求每個(gè)學(xué)玍的平均成績(jī)和每門課程的平均成績(jī)。
要求所有成績(jī)均放入個(gè)4行5列的數(shù)組中,輸入?yún)纪藬?shù)據(jù)間用空格,不同人月同車
其中最后一列和最后一行分別以每個(gè)學(xué)生的平均成績(jī)、每門課程的平均成績(jī)及班級(jí)總平均分。
include <stdio. h>
include<stdlib.h>
i float a[4][5]
1,J
for(i=0;<3i++)
or
canf(%f", &allin;
for(-0i<3i++)
i sum1=0;
suml+=alJc
ail[4]=sum 1/4
or(=0j<j++)
for(i10;<3;i++
sum2+=aillil
a]]=sum2/3;
f"%6.2f,aj):
8、完善程序,實(shí)現(xiàn)將輸入的字符串反序輸出
如輸入
輸出
共 include< string. h>
printf"Enter a st
(i=0=-1ck2i++j-)
i cl-cli ci-cljj: c lil-cl; j
指針法
har t:
半
j);(+)-t:}
I FILE*Ip
f((fr-fopen("p9 2out","W")--NUL L)
i printf"cannot open the filen);
ntt
n");
invert(str)
printf("n%s”,str)
fclose(fp):
9、下面程序的功能是從字?jǐn)?shù)組s中刪除存放在c中的字符。
maino
i char s[8o
printf("nEntcr a string: )
printf("eNter a character: "
forgj=k=0; s[!=10:j++)
if(sl]!
skI
10、編寫一個(gè) void sort intx,ntn)實(shí)現(xiàn)將x數(shù)組中的n個(gè)數(shù)據(jù)從大到小
排序。n及數(shù)組元素在主函數(shù)中輸入。將結(jié)果顯小在屏幕上并輸出到文件p91out中
include<stdio. h
void sort(int*x, int n)
int i j k
for(i-0
k-i
i(xj」xk」)kj
IFILE
fp=fopen(p9 1.out", w")
printf("Input 10 numbers: ")
for(i=0: 1<10: 1++)
scan
forp<at 10: p++)
fprintf(fp, %d",p):i
fp)
11、已知數(shù)組a中的元素己按由小到大順序排列,以下程序的功能是將輸入的一個(gè)數(shù)插入數(shù)組a中,插入后,數(shù)組a中的元素仍
然曰小到大順序排列*
{ int arlo0.12,1720228,30
a0為工作單元,從a1開始存放數(shù)據(jù)
nt x, ij=6:
/j為元素個(gè)數(shù)
scanf("%od", &x
從最后一個(gè)單元開始
while(a[]>x)
{ai+]ij:i-;}/將比x大的數(shù)往后移動(dòng)一個(gè)位置
a[++i]
j++;
插入x后元素總個(gè)數(shù)增加*
for(i=l i<=j: i++)printf("%8d, a[i]
printi(\n")
12、∧編寫函數(shù) replace( chars, char cl, char c2)實(shí)現(xiàn)將s所指向的字符串中所有字符cl用c2替換,字符串、字符c和c2
均在主函數(shù)中輸入,將原始字符串和替換后的字符串顯示在屏幕上,并輸出到文件pl02.out中
include<stdio h>
replace(char s, char cl, char c2)
i while(sl-\O')
ing
i FILE fp:
har str[100]. a, b;
if(fp-fopen(pl0 2out"," W)=NULL
i printf("cannot open the filein":
i(0)
gets(stI
tf("Enter
scanf(%oc.oc .&a, &
rinf("%s'n", str);
fprintf(fp, %os\n", str);
eplace(str, a, b):
printf("The new string is----%oStn", str);
fprintf(fp. " The new string is----os'n"str)
lose(fp)
13、在一個(gè)字串s1中查找一子串s2,若存在則返回子串在主串中的起始位置
不存在則返回-1。
Char sI[6-
32[5}-"i"
printf("%dk
syste
int search( char sll, char s21)
fint i-0j, len-strlen(s2)
if(s1[i-j]=s2[]) break
ifli=len )return 1
else i-+
eturn -I
14、用指針變量輸出結(jié)構(gòu)體數(shù)組元素。*
struct student
Int num
Int
stu[5={{1001,ihua"F",18},1002, liuxing",M,19},{1003," huanghe",F,19},{1004," fengshou",F,19},{1005," Wangming
8};
printf("Num tName\t\Sex\t")
體月指針變量輸出結(jié)構(gòu)體數(shù)組元素。*
ps<stu+5 ps++)
printf("%d\t%-10s'tit%c\t%od\t'n ps->num, ps>name
ex, ps->age);
月數(shù)組卜標(biāo)法輸出結(jié)構(gòu)體效組元素學(xué)號(hào)和年齡。*
for(-0;<5i+)
printf("%dt%dtn".stu[i]. num, stu l]. age)
5、建立一個(gè)有三個(gè)結(jié)點(diǎn)的簡(jiǎn)單鏈表:*
并 define null0
struct student
int num
char name
struct student * next
void maino
struct student a, b, c, head, "p
anum=1001; a namc="ihua";aage=1l8:/對(duì)結(jié)點(diǎn)成員進(jìn)行賦值/
b nLIm=1002; bname="liuxing; bage=
c num=1003; c name="huangke, cage=18
head-&a
建立鏈表,a為頭結(jié)點(diǎn)*
anext=&b
b next-&c
C next=NULL-
/輸出鏈表
printf(%5d, %oS,%3dnp->num, p->name, p->age)
while(p!-NUL L);
16、*輸入一個(gè)字符串,判斷其是合為回文?;匚淖址侵笍淖蟮接易x和從右到左讀完全相可的字符串。
include <stdio h>
include<string. h>
n
i char s[ 100:
Int 11, n
printi"'輸入字符串:mn");
n=strl
j: i++ j--)
if(s[i]=sui]) break;
if(>=j) printf("是回文串n")
nt("不是回文串n");
17、冒泡排序,從小到大,排序后結(jié)果輸出到屏幕及文什myf2out
include<stdio.h>
void fun(int a l,int n)
1:i
for(=0j≤ij+
if(al]aj+lt=aj; all=a[j+1; aj+1]=t: j
main()
{inta[10-12,45,78,964,10,48,246},n-10
FILE f
if((f-fopcn("myf2 out",W))==NULL)
printf("open file myf2 out failed!\n")
fun(a,10)
10:i++)
tf(%04d, ai]);
94d",a[ij]);
18、編寫函數(shù) counteR,利用公式
≈l+-+×二+-×-×-+
計(jì)算π兇近似值,當(dāng)某一項(xiàng)的值小于103時(shí),認(rèn)為達(dá)到精度要求,請(qǐng)完善圖數(shù)。將結(jié)果顯示在屏暮上并輸出到文件p73.ut
double countpi(double eps)
/eps為允許誤差*
hile(ter
temp temp m/(2 m-1);
return(2s);
main
(FILEp;
ps=le-5, pI;
if((fp=fopen(p7 3out,W )==NULL)
i printf("cannot open the filen")
it(0);
pl-%0
fprintf(fp, pi-%1f\n" pi);
fclose(fp);