热门搜索 :
考研考公
您的当前位置:首页正文

C语言小程序

来源:伴沃教育


基础操作

①建立一个工程:文件-新建

②选择 win32 console application (控制台应用程序,左边倒数第三个)命名为工程名称,选择保存位置 ③点击确定,进入下一步

④建立一个空工程,对应其他需要的你可以建立别的工程,点击完成,显示你创建的工程的信息

⑤再已有一个工程的条件下,偶们再建立一个源文件: 文件-新建(ctri+N)出现:

建立源文件,选择“C++ source”,一般都是建立这种文件的(使用在当文件中使用),如果要建立头文件,选择“C/C++ header file”,(适用在多文件工程中使用)命名,文件名称,点击确定,之后:

⑥进入编辑区,在主界面编写代码,如下编写完之后呢。 ⑦可以按编译按钮(向下箭头)调试程序,看看有没有错误,有的话改正,没有的话可以再按链接按钮(双向下箭头)检查连接(多文件工程时常用,检查文件间是否正常连接),最后,点击运行按钮(感叹号)就可以运行了。

⑧运行结果的黑色区域是指DOS窗口,在要选择区域左上角右击鼠标,选择弹出菜单的“标记”,一直鼠标左击一直拖到要选择区域的右下角,放松鼠标左键,敲回车键即可把区域的内容拷贝起来,然后在你的WORD中粘贴即可。

或者直接Print Screen sysRq复制全屏幕 到开始->程序->附件->画图。剪切需要的窗口到Word 就行。

1

热身程序1:1+2+……+100: #include void main() {

int i,sum=0; for(i=1;i<=100;i++) { sum+=i; }

printf(\"%d\\n\}

热身程序2:1*2*……*100: #include void main() { int i; float sum=0; for(i=1;i<=99;i++) sum=sum+i*(i+1); printf(\"%.0f\}

2

程序1:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。 main() {

int i,j,k; printf(\"\\n\");

for(i=1;i<5;i++) /*以下为三重循环*/ for(j=1;j<5;j++) for (k=1;k<5;k++) {

if (i!=k&&i!=j&&j!=k) /*确保i、j、k三位互不相同*/ printf(\"%d,%d,%d\\n\ } }

程序2:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高

于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提

成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;

3

40万到60万之间时高于

40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于

100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?

程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。 main() {

long int i;

int bonus1,bonus2,bonus4,bonus6,bonus10,bonus; scanf(\"%ld\

bonus1=100000*0.1;bonus2=bonus1+100000*0.75; bonus4=bonus2+200000*0.5; bonus6=bonus4+200000*0.3; bonus10=bonus6+400000*0.15; if(i<=100000) bonus=i*0.1; else if(i<=200000)

bonus=bonus1+(i-100000)*0.075; else if(i<=400000)

bonus=bonus2+(i-200000)*0.05;

4

else if(i<=600000)

bonus=bonus4+(i-400000)*0.03; else if(i<=1000000)

bonus=bonus6+(i-600000)*0.015; else

bonus=bonus10+(i-1000000)*0.01; printf(\"bonus=%d\}

程序3 :一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析: #include \"math.h\" main() {

long int i,x,y,z; for (i=1;i<100000;i++)

{ x=sqrt(i+100); /*x为加上100后开方后的结果*/ y=sqrt(i+268); /*y为再加上168后开方后的结果*/ if(x*x==i+100&&y*y==i+268)/*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/

5

printf(\"\\n%ld\\n\ } }

程序4:输入某年某月某日,判断这一天是这一年的第几天? 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。 main() {

int day,month,year,sum,leap;

printf(\"\\nplease input year,month,day\\n\"); scanf(\"%d,%d,%d\switch(month)/*先计算某月以前月份的总天数*/ {

case 1:sum=0;break; case 2:sum=31;break; case 3:sum=59;break; case 4:sum=90;break; case 5:sum=120;break; case 6:sum=151;break; case 7:sum=181;break; case 8:sum=212;break;

6

case 9:sum=243;break; case 10:sum=273;break; case 11:sum=304;break; case 12:sum=334;break;

defaultrintf(\"data error\");break; }

sum=sum+day; /*再加上某天的天数*/

if(year@0==0||(year%4==0&&year0!=0))/*判断是不是闰年*/ leap=1; else leap=0;

if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/ sum++;

printf(\"It is the %dth day.\研发部公共02(yanfabu02) 2013-05-22 08:57:51

程序5:输入三个整数a,b,c,请把这三个数由小到大输出。 #include void main() {

int a,b,c,k;

printf(\"给a,b,c赋值:\\n\");

7

scanf(\"%d%d%d\ if(aprintf(\"%d,%d,%d\\n\}

程序6:用*号输出字母C的图案。

程序分析:可先用<|>*<|>号在纸上写出字母C,再分行输出。 #include \"stdio.h\" main() {

printf(\"Hello C-world!\\n\"); printf(\" ****\\n\"); printf(\" *\\n\"); printf(\" * \\n\"); printf(\" ****\\n\"); }

程序7:输出特殊图案,请在c环境中运行,看一看,Very Beautiful! 程序分析:字符共有256个。不同字符,图形不一样。

8

#include \"stdio.h\" main() {

char a=176,b=219;

printf(\"%c%c%c%c%c\\n\printf(\"%c%c%c%c%c\\n\printf(\"%c%c%c%c%c\\n\printf(\"%c%c%c%c%c\\n\printf(\"%c%c%c%c%c\\n\ 程序8:输出9*9口诀。

程序分析:分行与列考虑,共9行9列,i控制行,j控制列。 #include \"stdio.h\" main() {

int i,j,result; for (i=1;i<10;i++) { for(j=1;j<=i;j++) { result=i*j;

printf(\"%d*%d=%-3d\表示左对齐,占3位*/ }

printf(\"\\n\");/*每一行后换行*/

9

} }

程序9:要求输出国际象棋棋盘。

程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。 #include \"stdio.h\" main() { int i,j;

for(i=0;i<8;i++) {

for(j=0;j<8;j++) if((i+j)%2==0)

printf(\"%c%c\ else

printf(\" \"); printf(\"\\n\"); } }

程序10:打印楼梯,同时在楼梯上方打印两个笑脸。

程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。

10

#include \"stdio.h\" main() { int i,j;

printf(\"\\n\");/*输出两个笑脸*/ for(i=1;i<11;i++) {

for(j=1;j<=i;j++)

printf(\"%c%c\ printf(\"\\n\"); } }

程序11:输入三个整数,输出其中的最大数。 #include void main() {int i,j,k,m; m=i;

if(i11

程序12:求半径为r的圆的面积、周长、圆球的体积。(利用符号常量宏定义) #include #define PI 3.14 void main() {float r,s,c,v;

printf(\"请输入半径值:\\n\"); scanf(\"%f\s=PI*r*r; c=2*PI*r;

v=4.0/30.*PI*r*r*r; printf(\"s=%f\\n\printf(\"c=%f\\n\printf(\"v=%f\\n\}

程序13:小写字母转换成大写字母。 #include void main() {

char c1,c2; c1='a'; c2='b';

12

c1=c1-32; c2=c2-32;

printf(\"%c,%c\\n%d,%d\\n\}

程序14:输入2个角度x,y,计算sin(|x|+|y|)的值。 #include #include #define PI 3.14 void main() {double x,y,z;

printf(\"请输入2个角度:\\n\"); scanf(\"%lf,%lf\ x=x*PI/180.0; y=y*PI/180.0;

z=sin(fabs(x)+fabs(y)); printf(\"z=%lf\\n\ }

程序15:自增自减 #include void main() {int i=5,j=5,p,q; p=(i++)+(i++)+(i++);

13

q=(++j)+(++j)+(++j);

printf(\"%d,%d,%d,%d\\n\}

程序16:赋值表达式中的类型转换 #include void main() {

int i1,i2=15,i3,i4=66; float f1,f2=8.88; char c1='A',c2;

i1=f2;f1=i2;i3=c1;c2=i4;

printf(\"%d,%f,%d,%c\\n\}

#include void main() {float PI=3.14; int s,r=5; s=r*r*PI;

printf(\"s=%d\\n\}

程序17:输入3个双精度数,求他们的平均值并保留一位小数,对小

14

数点后第二位进行四舍五入,最后输出结果。 #include void main() {

double f1,f2,f3,aver; printf(\"input f1,f2,f3:\\n\"); scanf(\"%lf%lf%lf\ aver=(f1+f2+f3)/3;

printf(\"aver=%.1lf\}

程序18:分段函数。 #include void main() {

float x,y;

scanf(\"%f\ if(x>0) y=x-7; if(x==0) y=2; if(x<0) y=3*x*x; printf(\"%.2f\ printf(\"\\n\");

15

}

程序19:点是否在圆环内。 #include void main() {

float x,y; int a,b;

printf(\"给x,y赋值:\\n\"); scanf(\"%f%f\

printf(\"输入圆环的内外半径:\\n\"); scanf(\"%d%d\

if((x*x+y*y)>=a*a&&(x*x+y*y)<=b*b)

printf(\"点(%.2f,%.2f)是圆环内的点。\ else

printf(\"点(%.2f,%.2f)不是圆环内的点。\}

程序20:运输公司对用户计算运费。

#include void main() {

int c,s; float p,w,d,f;

16

scanf(\"%f%f%d\if(s>=3000) c=12; else c=s/250; switch(c) {

case 0:d=0;break; case 1:d=2;break; case 2:

case 3:d=5;break; case 4: case 5: case 6:

case 7:d=8;break; case 8: case 9: case 10:

case 11:d=10;break; case 12:d=15;break; }

f=p*w*s*(1-d/100.0);

17

printf(\"距离为%dkm时的运费是:%.2f元。\\n\}

程序21:求n!

#include void main() {

int n,t=1; long int s=1; scanf(\"%d\ while (t<=n) { s*=t; t++; }

printf(\"n!=%d!=%ld\\n\}

程序22:求平均成绩。

#include void main() {

int n,i,score,sum; float ave;

18

printf(\"please input the number of the class:\"); scanf(\"%d\ i=sum=0; do { i++;

printf(\"请输入第%d同学的成绩:\\n\ scanf(\"%d\ sum+=score; }

while(i程序23:求π。利用π/4=1-1/3+1/5-1/7+…求,直到最后一项的绝对值小于1E-6为止。

#include #include void main() { int s;

19

float t,n,pi; pi=0,s=1,t=1.0,n=1; do { pi+=t; s=-s; n+=2; t=s/n; }

while(fabs(t)>1e-6); pi*=4;

printf(\"pi=%.6f\\n\}

程序24:求e的泰勒级数展开式。

xex=1+x+x2/2!+…+n!+…前n项和。 xn#include void main() {

int n,i; float x,t,s;

20

printf(\"give n,x the value:\\n\"); scanf(\"%d%f\ for (s=t=1.0,i=1;i<=n;i++) {t*=x/i;s+=t;} printf(\"s=%.5f\\n\}

程序25:求和:1-1/2+1/3-1/4+1/5…+1/99-1/100

#include void main() { int i; float t,s;

for(s=0.0,t=1.0,i=1;i<=100;i++) {s+=t/i;t=-t;} printf(\"s=%.5f\\n\}

程序26:用3种循环结构便携求水仙花数。三位数,满足如153=13+53+33的要求。 用while语句来编写:

#include void main() {

21

int n=100,i,j,k; while(n<1000) {

i=n/100;j=(n/10)%10;k=n%10; if(n==i*i*i+j*j*j+k*k*k) printf(\"%6d\\n\ n++; } }

用do-while语句来编写:

#include void main() {int n=100,i,j,k; do {

i=n/100;j=(n/10)%10;k=n%10; if(n==i*i*i+j*j*j+k*k*k) printf(\"%d\\n\ n++; }

while(n<1000); }

22

用for语句来编写:

#include void main() {

int n,i,j,k;

for(n=100;n<1000;n++) {i=n/100;j=(n/10)%10;k=n%10; if(n==i*i*i+j*j*j+k*k*k) printf(\"%d\\n\ } }

程序27:输出4×8的*。

#include void main() {

int i,j;

for(i=1;i<=4;i++) {

for(j=1;j<=8;j++) printf(\"*\"); printf(\"\\n\"); }

23

}

程序28:计算圆面积,直到面积大于100为止,输出此时的半径和面积。

#include #define pi 3.14 void main() { int r; float area; for(r=1;r<=10;r++) {

area=pi*r*r; if(area>100) break; }

printf(\"%d,%f\\n\}

程序29:任意输入10个数,找出其中的最大数和最小数。 因其无法确定,所以设第一个数为最大数、最小数,然后将其余9个数分别与它比较,当前读取的数比最大大则赋给最大值,这是就不用去与最小的比较了,可以结束本次循环,转向判断条件,条件成立,

24

读入下个数据。

#include void main() {

int max,min,x,n;

printf(\"input the first number:\\n\"); scanf(\"%d\ max=min=x;

for(n=2;n<=10;n++) {

printf(\"input the %d number:\\n\ scanf(\"%d\ if(x>max)

{max=x;continue;} if(xprintf(\"%d,%d\\n\ } }

程序30:球从100m高度自由落下,每次落地后反弹到原高度的一半,它第10次落地时,共经过多少米?第10次反弹多高?

#include

25

void main() {

float s,h; int n;

s=100.0;h=s/2; for(n=2;n<=10;n++) {s+=2*h;h=h/2;} printf(\"%f,%f\\n\}

程序31:输入某班n个学生的成绩,分别统计各分数段的人数。

#include void main() {

int a,b,c,score; int i,n; a=b=c=0;

printf(\"the number of the students:\"); scanf(\"%d\ for(i=1;i<=n;i++)

{printf(\"input the grade:\"); scanf(\"%d\ if(score>=80) a++;

26

else if(score>=60) b++; else c++; }

printf(\"the number of 80 gradeup is:%d\\n\ printf(\"the number of 60-80 grade is:%d\\n\ printf(\"he number of failures is:%d\\n\}

程序32:求菲波拉契数列的前20项。即前两项为1,之后每项均为前两项的和。

#include void main() {

long int x1,x2,x3; int n; x1=x2=1;

printf(\"%-6ld %-6ld\ for(n=3;n<=20;n++) {

x3=x1+x2;

printf(\"%-6ld\ if(n%5==0) printf(\"\\n\"); x1=x2;x2=x3;

27

} }

程序33:输出3-100中的所有素数(只能被1和自身整除的数) 。

#include void main() {

int n,i;

for(n=3;n<=100;n++) {

for(i=2;i=n)

printf(\"%d\\ } }

程序34:f(x)=x3-3x-1在区间[0,8]内有一个实根,用二分法求根。 算法简介:从a开始以一个基本步长h分割,若某一步前后b的函数值y0和y1乘积小于0,则此区有根。

#include void main() {

float a,b,c,h,y0,y1,eps,len,root;

28

a=0.0;b=8.0;h=0.1;eps=0.00001; y0=a*a*a-3*a-1;

y1=(a+h)*(a+h)*(a+h)-3*(a+h)-1; while((y10)) {y0=y1;a=a+h;y1=a*a*a-3*a-1;} b=a;a=a-h;c=(a+b)/2; len=h;

while(len>eps) {

y0=a*a*a-3*a-1; y1=b*b*b-3*b-1; if(y0*y1<0) b=c; else a=c; c=(a+b)/2; len=b-a; } root=a;

printf(\"ROOT=%f\\n\}

程序35:牛顿迭代法求根。方程f(x)=x-e取初值x0=-1.

29

x

+2=0,求方程的近似根,

分析:f(x)在xk点附近可用一阶泰勒公式f(x)=f(x0)+f’(x0)(x-x0)来近似,根值可用此方程来近似代求。取f(x)=0得新根xk+1牛顿迭

代公式xk+1=xk-f'(xk),终止条件可设为若相邻两次的迭代根值的f(xk)差小于允许值或函数值小于允许值,则可以终止。对于迭代次数也有限制,若迭代N次不能满足上述条件,则认为迭代失败,N为迭代次数的最大值。

#include #include #define N 10 void main() {

double x0,x1,x2,eps1,eps2,y;/*x0是初值,x1和x2分别是第k次和第k+1次迭代的根*/ int i=0;

x0=-1;eps1=0.000001;eps2=0.000001; x1=x0;

for(i=i;i<=N;i++) {

y=x1-exp(x1)+2;

printf(\"x%d=%lf,f(x%d)=%lf\\n\ x2=x1-(x1-exp(x1)+2)/(1-exp(x1));

30

if((fabs(y)程序36:建立一个数组,并计算一个数组里数值的和。(循环输入并算和,再循环输出)

#include #define N 5 void main() {

float score[N],sum=0.0; int i;

printf(\"input 5 numbers:\"); for(i=0;iscanf(\"%f\ sum+=score[i]; }

for(i=0;i{printf(\"score[%d]=%.2f\\ printf(\"\\nsum=%f\\n\}

31

程序37:用选择法对任意10个数按由小到大方式进行排序。 选择法是最简单、直观的数据排序方法,思路:通过比较及交换,将符合要求的最小的数放在前头,每轮确定一个数;剩下的数依次解决。N个数需要N-1轮才能排完,每轮分别经过N-1,N-2,N-3,…,1次比较。

#include void main() {

int a[5],i,j,k;

printf(\"input 5 integrated number:\"); for(i=0;i<5;i++) scanf(\"%d\ for(i=0;i<5;i++) {

for(j=i+1;j<5;j++) if(a[j]{k=a[j];a[j]=a[i],a[i]=k;} }

printf(\"the sequence of number from the minimum to the maximum is:\\n\"); for(i=0;i<5;i++) printf(\"%d\\

32

printf(\"\\n\"); }

input 5 integrated number:5 6 4 8 7

the sequence of number from the minimum to the maximum is: 4 5 6 7 8 Press any key to continue

程序38:把一个整数依序插入已排序的数组,该数组是从大到小排列的。

分析:设数组有5个元素,待插数x满足a[i]#include void main() {

int i,t,x,a[6];

printf(\"input the numbers of a[] from the maximum to minimum:\"); for(i=0;i<6;i++) scanf(\"%d\

printf(\"input the inserted number:\"); scanf(\"%d\ for(i=0,t=5;i<6;i++) if(x>a[i]) {t=i;break;} for(i=5;i>t;i--)

33

a[i]=a[i-1]; a[t]=x;

for(i=0;i<6;i++) printf(\"%d\\}

input the numbers of a[] from the maximum to minimum:10 9 6 5 3 0 input the inserted number:7

10 9 7 6 5 3 Press any key to continue

程序39:将两个有序的数组合并成一个有序数组。

#include #define M 8 #define N 5 void main() {

int a[M]={3,6,7,9,11,14,18,20}; int b[N]={1,2,13,15,17}; int c[M+N]; int i=0,j=0,k=0; while(i34

{c[k]=b[j];j++;k++;} while(i{c[k]=a[i];i++;k++;} While(j{c[k]=b[j];j++;k++;}

for(i=0;i1 2 3 6 7 9 11 13 14 15 17 18 20 Press any key to continue

程序40:某班有30名学生,在期末考试后,需统计各分数段学生人数,编写程序完成此操作。

分析:定义一维数组存放成绩,依次遍历各元素判断其处的分数段,并将对应的计数器加1,最后输出统计结果。

#include #define N 30 void main() {

float score[N]={0}; int n[5]={0}; int i;

printf(\"input the scores of 30 students:\\n\"); for(i=0;i<30;i++)

35

{scanf(\"%f\ while(score[i]>100||score[i]<0) {printf(\"wrong!\");} }

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

if(score[i]>=90) n[0]++; else if(score[i]>=80) n[1]++; else if(score[i]>=70) n[2]++; else if(score[i]>=60) n[3]++; else n[4]++;

printf(\"the result is :\\n\"); printf(\"the

numbers

of

the

score

%d-%d

is %d\\n\ for(i=1;i<4;i++) printf(\"the

numbers

of

the

score

%d-%d

is %d\\n\

printf(\"the numbers of the failure is %d\\n\}

input the scores of 30 students:

10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 5 84 89 95 93 78 86 85 84 72 66 the result is :

the number of the score 90-100 is 5 the number of the score 80-89 is 7 the number of the score 70-79 is 4 the number of the score 60-69 is 3

36

the number of the failure is 11 Press any key to continue

程序41:计算一个5×5的整数矩阵两条对角线上的数值之和。 分析:奇偶有别哦。

#include #define N 5 void main() {

int i,j,m[N][N],sum=0,n=N; for(i=0;iprintf(\"line %d:\ for(j=0;jscanf(\"%d\ if(i==j) sum+=m[i][j]; if(i+j==N-1) sum+=m[i][j]; } }

for(i=0;ifor(j=0;j37

printf(\"m[%d][%d]=%-3d\\ if(j==4) printf(\"\\n\"); } }

if(n%2) sum-=m[(N-1)/2][(N-1)/2];(奇偶有别哦) printf(\"sum=%d\\n\}

line 0:1 2 3 4 5 line 1:6 7 8 9 10 line 2:11 12 13 14 15 line 3:16 17 18 19 20 line 4:21 22 23 24 25

m[0][0]=1 m[0][1]=2 m[0][2]=3 m[0][3]=4 m[0][4]=5

m[1][0]=6 m[1][1]=7 m[1][2]=8 m[1][3]=9 m[1][4]=10

m[2][0]=11 m[2][1]=12 m[2][2]=13 m[2][3]=14 m[2][4]=15

m[3][0]=16 m[3][1]=17 m[3][2]=18 m[3][3]=19 m[3][4]=20

m[4][0]=21 m[4][1]=22 m[4][2]=23 m[4][3]=24 m[4][4]=25

sum=117

Press any key to continue

程序42:将二维数组的行列互换(转置),存到另一个二维数组中。

#include void main() {

int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

38

int b[4][3],m,n;

printf(\"输出原数组:\\n\"); for(m=0;m<3;m++) {

for(n=0;n<4;n++) {

printf(\"%-3d\ b[n][m]=a[m][n]; }

printf(\"\\n\"); }

printf(\"输出转置后数组:\\n\"); for(m=0;m<4;m++) {

for(n=0;n<3;n++)

printf(\"%-3d\ printf(\"\\n\"); } }

输出原数组: 1 2 3 4 5 6 7 8 9 10 11 12

39

输出转置后数组: 1 5 9 2 6 10 3 7 11 4 8 12

Press any key to continue

程序43:3×4的矩阵,输出最大元素,并指出所在的行号和列号。 分析:首先设矩阵的第一个元素为最大值,分别与其他数比较,找到最大值,记下行列号。

#include void main() {

int a[3][4],i,j,row,col,max; row=col=0;

printf(\"value of array a[][]:\\n\"); for(i=0;i<3;i++) for(j=0;j<4;j++) scanf(\"%d\ max=a[0][0]; for(i=0;i<3;i++) for(j=0;j<4;j++) if(a[i][j]>max) {max=a[i][j]; row=i;col=j; }

40

printf(\"max=%d,row=%d,col=%d\\n\ }

value of arraya[][]:

15 7 4 6 2 5 14 15 18 19 20 4 Max=20,row=2,col=2 Press any key to continue

程序44:函数调用的例子:输入2个整数,输出其中较大的数。

#include int max(int a,int b) {

if(a>b) return a; else return b; }

void main() {

int x,y,z;

printf(\"input 2 numbers:\"); scanf(\"%d %d\ z=max(x,y);

printf(\"maxmum=%d\\n\}

input 2 numbdrs:12 14 maxmum=14

41

Press any key to continue

程序45:求n的n-1次幂,和n-1的n次幂。 发现:实参表达式求值顺序是自右至左。

#include #include

void main() {

int n,j; long int l,m;

printf(\"input integer:\\n\"); scanf(\"%d\ j=n-1; l=pow(n,j);

printf(\"the %dth pow of %d = %ld\\n\ m=pow(j,n);

printf(\"the %dth pow of %d = %ld\\n\ }

Input integer: 4

the 3th pow of 4 = 64 the 4th pow of 3 = 81 Press any key to continue

42

程序46:求给定范围内的素数个数。

#include #include

num_of_primes(int x,int y);//函数原型 void main() {

int a,b,c;

printf(\"input 2 integers:\\n\"); scanf(\"%d%d\

c=num_of_primes(a,b);//调用num_of_primes函数 printf(\"num_of_primes=%d\\n\}

num_of_primes(int x,int y) {

int i,j,k,n=0; if(yif (x<3){n=1;x=2;}//1不作为素数,2作为素数 if (x%2==0) x++; for(i=x;i<=y;i+=2) {

k=sqrt(i);//求出判断素数循环的终值、 for(j=2;j<=k;j++)

43

if(i%j==0) break; if(j>k) n++; }

return n; }

input 2 integers: 15 2

num_of_primes=6

Press any key to continue

程序47:对一个已排序的字符串数组进行反向排序。

#include #include

void sort_string(char str[],int n)//一维数组作为函数参数 {

char ch; int i,j; n=n-1; j=n/2;

for(i=0;i{ //形参数组元素的变化会影响实参数组元素的变化 ch=str[i]; str[i]=str[n]; str[n]=ch;

44

n=n-1; } }

void main() {

char string[30]=\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"; int i=26;

sort_string(string,i); puts(string); }

ZYXWVUTSRQPONMLKJIHGFEDCBA Press any key to continue

程序48:用函数调用求n!

#include

long int f(unsigned int n);//函数原型 void main() { int n; long int sum;

printf(\"input a unsigned interger:\"); scanf(\"%d\ sum=f(n);//函数调用 printf(\"%d!=%ld\\n\

45

}

long int f(unsigned int n)//函数定义 {

long int fac; if(n==1) fac=1; else

fac=f(n-1)*n; return (fac); }

input a unsigned interger: 8 8!=40320

Press any key to continue 程序

49:汉诺(Hanoi)塔问题,在一个塔座1是有若干片盘片,

盘片大小不等,按大盘下小盘上的顺序排列。要把它移动到塔2去,仅依靠一个附加塔3,在每次搬动时,整个移动过程中始终保持每个塔的盘片均为大盘下小盘上的叠放方式。

#include

void hanoi ta(int n,char ta1,char ta2,char ta3);//函数原型 void main() {

46

int n;

printf(\"input the number of diskes:\"); scanf(\"%d\

hanoi ta(n,'1','2','3'); printf(\"\\n\"); }

void hanoi ta(int n,char ta1,char ta2,char ta3) {

if(n==1)

printf(\"%c->%c\ else {

hanoi ta(n-1,ta1,ta3,ta2);//n-1片由ta1移到ta3 printf(\"%c->%c\最下面最大一片盘,直接移

hanoi ta(n-1,ta3,ta2,ta1);//n-1片盘由ta3移到ta2 } }

但是运行有错误!!P144

程序

50:指针的运用。

#include void main()

47

{

int a=50,*p; p=&a;

printf(\"*p=%d\\n\ *p=100;

printf(\"a=%d\\n\}

*p=50 a=100

Press any key to continue 程序51:指针的运用。

#include void main() {

char c='A'; char *p=&c;

printf(\"%c%c\\n\ c='B';

printf(\"%c%c\\n\ *p='C';

printf(\"%c%c\\n\}

AA BB

48

CC

Press any key to continue 程序

52:交换2个元素之值。

#include void swap(int x,int y) {int t;t=x;x=y;y=t;} void main() {int a,b;

a=4,b=6

a=4;b=6; swap(a,b);

printf(\"a=%d,b=%d\\n\}

Press any key to continue

虽然在swap函数中x,y值互换,但由于主函数对该函数的调用是“值传递”,不是“地址传递”,所以该函数不能影响主函数中的a,b值,也达不到交换的目的。

#include void swap(int *x,int *y) {int t;t=*x;*x=*y;*y=t;} void main() {int a,b; a=4;b=6;

swap(&a,&b);

printf(\"a=%d,b=%d\\n\}

a=6,b=4

Press any key to continue

在swap函数中交换了指针变量所指向变量a,b的值。

程序

53:移动指针变量访问数组元素。

#include void main()

49

{

int array[10]={0,1,2,3,4,5,6,7,8,9},i; int *p=&array[0]; for(i=0;i<10;i++) {printf(\"%d\\}

0 1 2 3 4 5 6 7 8 9

Press any key to continue 程序

54:输出数组元素值。

方法1:

#include void main() {

int a[5]={1,2,3,4,5}; int i;

for(i=0;i<5;i++) printf(\"%d\\}

方法2:

#include void main() {

int a[5]={1,2,3,4,5};

50

int i;

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

printf(\"%d\\}

方法3:

#include void main() {

int a[5]={1,2,3,4,5}; int *p;

for(p=a;p<(a+5);p++) printf(\"%d\\}

1 2 3 4 5 Press any key to continue

程序55:用指针法输入输出二维数组各元素。

#include void main() {

int a[3][4],*p; int i,j; p=a[0];

51

printf(\"please input data:\\n\"); for(i=0;i<3;i++) for(j=0;j<4;j++) scanf(\"%d\ p=a[0];

for(i=0;i<3;i++) {

for(j=0;j<4;j++)

printf(\"%-3d\\ printf(\"\\n\"); } }

Please input data: 1 2 3 4 5 6 7 8 9 10 11 12

Press any key to continue

程序56:指向具有m个元素的一维数组的指针变量。

#include void main() {

int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; int i,(*b)[4]; float sum,ave;

52

for(b=a;bfor(i=0;i<4;i++) sum+=*(*b+i); ave=sum/4;

printf(\"ave=%.2f\\n\ }

ave=2.50 ave=6.50 ave=10.50

Press any key to continue

程序57:用选择法对10个整数进行由大到小的排序。(一维数组名作为函数参数)

#include void sort(int [],int); void main() {

int *p,i,a[10];

printf(\"please input data:\\n\"); for(i=0;i<10;i++) scanf(\"%d\

sort(a,10); //函数调用,作为实参的数组名a代表数组的首地址

53

for(p=a;pvoid sort(int x[],int n) {

int *x_end,*y,*p,temp; x_end=x+n;

for(;xfor(y=x+1;y*p) p=y; if(p!=x)

{temp=*x;*x=*p;*p=temp;} } }

Please input data: 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1

Press any key to continue

程序58:利用指针数组输出单位阵。

#include

54

#define N 3 void main() {

int matrix[N][N]={0};//定义矩阵 int *p[N]; //定义整形指针数组 int i,j;

printf(\"%d*%d identity matrix:\\n\ for(i=0;ip[i]=matrix[i]; for(j=0;jif(i==j) p[i][j]=1; printf(\"%-3d\ }

printf(\"\\n\"); } }

3*3 identity matrix: 1 0 0 0 1 0 0 0 1

Press any key to continue

55

程序59:任意输入n个数,找出其中最大数,并输出。

#include #define N 3 void main() {

int f(int,int); int i,a,b;

int (*p)(int x,int y);//定义指向函数的指针变量 printf(\"please input data:\\n\"); scanf(\"%d\

p=f; //给p赋值,使它指向函数 for(i=1;iscanf(\"%d\

a=(*p)(a,b); //通过p调用函数f }

printf(\"the max number is:%d\\n\}

int f(int x,int y) { int z; z=(x>y)?x:y;

56

return (z); }

please input data: 12 9 15

the max number is:15 Press any key to continue

程序60:一个班有若干学生,每个学生有4门课,交互式输入每个学生的各门课程的成绩,找出其中有不及格课程的学生的序号,并输出其所有的课程成绩。用返回指针的函数实现该程序。

#include #define N 3 void main() {

float *search(float(*)[4]),*p,score[N][4]; int i,j;

for(i=0;iprintf(\"please students:\\n\ for(j=0;j<4;j++)

scanf(\"%f\ }

printf(\"the serial number and score of failure students:\\n\");

57

input the scores of %d

for(i=0;ip=search(score+i); if(p==*(score+i)) {

printf(\"NO.%d scores:\ for(j=0;j<4;j++)

printf(\"%5.2f\\ printf(\"\\n\"); } } }

float*search(float (*p)[4]) { int i; float *q=*p; for(i=0;i<4;i++)

if(*(q+i)<60) return *p; return *(p+1); }

please input the scores of 1 students: 90 80 70 60

please input the scores of 2 students: 80 70 60 50

58

please input the scores of 3 students: 45 56 89 79

the serial number and score of failure students: NO.2 scores:80.00 70.00 60.00 50.00 NO.3 scores:45.00 56.00 89.00 79.00 Press any key to continue

程序61:利用格式字符c对字符数组元素逐个输入和输出字符。

#include #define SIZE 10 void main() {

char s[SIZE]; int i,j;

printf(\"please input a string:\\n\");

for(i=0;iscanf(\"%c\ if(s[i]=='\\n') break; }

printf(\"\\n the sequence is:\");

for(j=0;jplease input a string:

59

love

the sequence is:love

Press any key to continue

程序62:利用格式字符s对字符数组整体输入和输出字符串。

#include #define SIZE 10 void main() {

char s[SIZE];

printf(\"please input a string:\\n \"); scanf(\"%s\

printf(\"the string is:\\n\"); printf(\"%s\\n\ }

please input a string: love

the string is: love

Press any key to continue

程序63:利用字符串指针变量输出字符串。

#include void main() {

char*p=\"welcome to study C program language!\";//定义

60

并初始化字符串指针变量

while(*p!='\\0') //移动指针变量输出字符串 {printf(\"%c\ printf(\"\\n\");

p=\"welcome to Wuhan University\"; //将字符串的首地址赋给字符串指针变量

printf(\"%s\\n\输出字符串 }

welcome to study C program language! welcome to Wuhan University Press any key to continue

程序64:编写函数strlink(),连接两个字符串str1和str2,连接后的结果放在str1中。

#include #define SIZE 50

void strlink(char *,char *); void main() {

char str1[SIZE]=\"hello\ printf(\"字符串1:\\n%s\\n\ printf(\"字符串二:\\n%s\\n\ strlink(str1,str2);

printf(\"连接后的新字符串:\\n%s\\n\

61

}

void strlink(char *s,char *t) //字符串指针变量作为函数形参 {

while(*s!='\\0') //不断移动指针s,直到str1的末尾 s++;

while(*t!='\\0') //将t所指向字符串中的有效字符逐个添加到s所指的字符串的末尾 {*s=*t;s++;t++;}

*s='\\0'; //为连接后的新字符串添加字符串结束标志 }

字符串1: hello

字符串二: world!

连接后的新字符串: hello world!

Press any key to continue

62

程序65:比较2个字符串的大小。用strcmp函数。

#include #include void main() {

int flag;

char *str1=\"china\flag=strcmp(str1,str2); if(flag==0)

printf(\"str1=str2\\n\"); else if(flag>0)

printf(\"str1>str2,flag=%d\\n\else

printf(\"str1str1程序66:用strlwr函数转换大写到小写。

如:

#include #include void main()

{ char str[]=\"C PROGRAM LANGUAGE\"; printf(\"%s\\n\}

c program language Press any key to continue

程序67:用strupr函数转换小写到大写。

#include

63

#include void main()

{ char str[]=\" c program language\"; printf(\"%s\\n\ }

C PROGRAM LANGUAGE Press any key to continue

程序68:某班有N名学生,每个学生的数据包括学号、姓名、性别、年龄、平均成绩。要求输入任意一个学号,输出该学生的所有数据。

#include #define N 3

struct student //定义结构体类型 {

long number; char name[16],sex; int age,aver; };

void main() {

int s,t=-1; long xuehao;

struct student stu[N]; //定义结构体数组

printf(\"please input %d students' information as follows:\\n\

printf(\"number name sex age aver\\n\"); for(s=0;sprintf(\"please input %dth student's

64

information:\\n\

scanf(\"%ld%s%*c%c%d%du[s].sex,&stu[s].age,&stu[s].aver); }

printf(\"please input the searched student's number:\\n\"); scanf(\"%ld\

for(s=0;sif(t!=-1) //检索结果

printf(\"%ld %s %c %d %d\\nu[t].sex,stu[t].age,stu[t].aver); else

printf(\"the searched student is not existent!\\n\"); }

please input 3 students' information as follows: number name sex age aver

please input 1th student's information: 1 lf f 27 89

please input 2th student's information: 2 zy m 28 70

please input 3th student's information: 3 hj f 29 90

please input the searched student's number: 3

3 hj f 29 90

Press any key to continue

程序69:运用条件编译的小程序。

#include void main()

65

{

float r=5.5,s; #if defined(PI) s=PI*r*r; #else

#define PI 3.1415926 s=PI*r*r; #endif

printf(\"s=%f\\n\}

s=95.033176

Press any key to continue

程序70:枚举:定义一个描述3种颜色的枚举类型red、blue、green,输出这三种颜色的全部排列结果。

分析:这是3种颜色的全排列问题,用穷举法即可输出三种颜色的全部27种排列结果。(详细分解结果,见P221自己写的笔记)

#include

enum colors{red,blue,green}; void show(enum colors color) {

switch(color) {

case red:printf(\"red\");break; case blue:printf(\"blue\");break; case green:printf(\"green\");break; }

printf(\"\\"); }

void main()

66

{

enum colors c1,c2,c3;

for(c1=red;c1<=green;c1=enum colors(int(c1)+1)) for(c2=red;c2<=green;c2=enum colors(int(c2)+1)) for(c3=red;c3<=green;c3=enum colors(int(c3)+1)) {

} }

red red red red red red red blue red blue red blue red green red green red green blue red blue red blue red blue blue blue blue blue blue blue green blue green blue green green red green red green red green blue green blue green blue green green green green green green

show(c1); show(c2); show(c3); printf(\"\\n\"); red blue green red blue green red blue green red blue green red blue green red blue green red blue green red blue green red blue green

67

Press any key to continue

程序71:统计文件“test”中的字符个数。

#include #include void main() {

FILE *fp; long num=0; char test[20];

if((fp=fopen(\"test\ {

printf(\"can not open this file.\\n\"); exit(0); }

while(fgetc(fp)!=EOF) num++;

printf(\"num=%ld\\n\ fclose(fp); }

can not open this file. Press any key to continue 因为没有文件test给它查>_build,warning: “test”:unreferenced local variable. run的结果如上。

《C语言程序设计》 武汉大学出版社

68

因篇幅问题不能全部显示,请点此查看更多更全内容

Top