📄 c语言经典程序100例.htm
字号:
<P>/* 如果使用的是TC系列编译器则可能需要添加下句 */<BR>static void dummyfloat(float *x){ float y;
dummyfloat(&y);}</P>
<P>main()<BR>{<BR>float a[3][3],sum=0;<BR>int i,j;<BR>printf("please input
rectangle
element:\n");<BR>for(i=0;i<3;i++)<BR>for(j=0;j<3;j++)<BR>scanf("%f",&a[i][j]);<BR>for(i=0;i<3;i++)<BR>sum=sum+a[i][i];<BR>printf("duijiaoxian
he is
%6.2f",sum);<BR>getch();<BR>}<BR>==============================================================<BR>【程序39】<BR>题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。<BR>1.
程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后<BR> 此元素之后的数,依次后移一个位置。
<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
a[11]={1,4,6,9,13,16,19,28,40,100};<BR>int
temp1,temp2,number,end,i,j;<BR>printf("original array
is:\n");<BR>for(i=0;i<10;i++)<BR>printf("%5d",a[i]);<BR>printf("\n");<BR>printf("insert
a new
number:");<BR>scanf("%d",&number);<BR>end=a[9];<BR>if(number>end)<BR>a[10]=number;<BR>else<BR>{<BR>for(i=0;i<10;i++)<BR>{<BR>if(a[i]>number)<BR>{<BR>temp1=a[i];<BR>a[i]=number;<BR>for(j=i+1;j<11;j++)<BR>{<BR>temp2=a[j];<BR>a[j]=temp1;<BR>temp1=temp2;<BR>}<BR>break;<BR>}<BR>}<BR>}<BR>for(i=0;i<11;i++)<BR>printf("%6d",a[i]);<BR>getch();<BR>}<BR>==============================================================<BR>【程序40】<BR>题目:将一个数组逆序输出。<BR>1.程序分析:用第一个与最后一个交换。<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>#define N 5<BR>main()<BR>{<BR>int
a[N]={9,6,5,4,1},i,temp;<BR>printf("\n original
array:\n");<BR>for(i=0;i<N;i++)<BR>printf("%4d",a[i]);<BR>for(i=0;i<N/2;i++)<BR>{<BR>temp=a[i];<BR>a[i]=a[N-i-1];<BR>a[N-i-1]=temp;<BR>}<BR>printf("\n
sorted
array:\n");<BR>for(i=0;i<N;i++)<BR>printf("%4d",a[i]);<BR>getch();<BR>}</P>
<P><BR> 经典c程序100例==41--50<BR>【程序41】<BR>题目:学习static定义静态变量的用法 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>varfunc()<BR>{<BR>int var=0;<BR>static int
static_var=0;<BR>printf("\40:var equal %d \n",var);<BR>printf("\40:static var
equal %d
\n",static_var);<BR>printf("\n");<BR>var++;<BR>static_var++;<BR>}<BR>void
main()<BR>{<BR>int
i;<BR>for(i=0;i<3;i++)<BR>varfunc();<BR>getch();<BR>}<BR>==============================================================<BR>【程序42】
<BR>题目:学习使用auto定义变量的用法<BR>1.程序分析: <BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
i,num;<BR>num=2;<BR>for(i=0;i<3;i++)<BR>{<BR>printf("\40: The num equal %d
\n",num);<BR>num++;<BR>{<BR>auto int num=1;<BR>printf("\40: The internal block
num equal %d
\n",num);<BR>num++;<BR>}<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序43】<BR>题目:学习使用static的另一用法。 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
i,num;<BR>num=2;<BR>for(i=0;i<3;i++)<BR>{<BR>printf("\40: The num equal %d
\n",num);<BR>num++;<BR>{<BR>static int num=1;<BR>printf("\40:The internal block
num equal
%d\n",num);<BR>num++;<BR>}<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序44】<BR>题目:学习使用external的用法。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>int a,b,c;<BR>void add()<BR>{<BR>int
a;<BR>a=3;<BR>c=a+b;<BR>}<BR>void main()<BR>{<BR>a=b=4;<BR>add();<BR>printf("The
value of c is equal to
%d\n",c);<BR>getch();<BR>}<BR>==============================================================<BR>【程序45】<BR>题目:学习使用register定义变量的方法。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>void main()<BR>{<BR>register int i;<BR>int
tmp=0;<BR>for(i=1;i<=100;i++)<BR>tmp+=i;<BR>printf("The sum is
%d\n",tmp);<BR>getch();<BR>}<BR>==============================================================<BR>【程序46】<BR>题目:宏#define命令练习(1) <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>#define TRUE 1<BR>#define FALSE 0<BR>#define
SQ(x) (x)*(x)<BR>void main()<BR>{<BR>int num;<BR>int again=1;<BR>printf("\40:
Program will stop if input value less than
50.\n");<BR>while(again)<BR>{<BR>printf("\40:Please input
number==>");<BR>scanf("%d",&num);<BR>printf("\40:The square for this
number is %d
\n",SQ(num));<BR>if(num>=50)<BR>again=TRUE;<BR>else<BR>again=FALSE;<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序47】<BR>题目:宏#define命令练习(2)<BR>1.程序分析: <BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include
"conio.h"<BR>/*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/<BR>#define exchange(a,b) {
\<BR>int t;\<BR>t=a;\<BR>a=b;\<BR>b=t;\<BR>}<BR>void main(void)<BR>{<BR>int
x=10;<BR>int y=20;<BR>printf("x=%d;
y=%d\n",x,y);<BR>exchange(x,y);<BR>printf("x=%d;
y=%d\n",x,y);<BR>getch();<BR>}<BR>==============================================================<BR>【程序48】<BR>题目:宏#define命令练习(3) <BR>1.程序分析:<BR>2.程序源代码:<BR>#define
LAG ><BR>#define SMA <<BR>#define EQ ==<BR>#include "stdio.h"<BR>#include
"conio.h"<BR>void main()<BR>{<BR>int i=10;<BR>int j=20;<BR>if(i LAG
j)<BR>printf("\40: %d larger than %d \n",i,j);<BR>else if(i EQ
j)<BR>printf("\40: %d equal to %d \n",i,j);<BR>else if(i SMA
j)<BR>printf("\40:%d smaller than %d \n",i,j);<BR>else<BR>printf("\40: No such
value.\n");<BR>getch();<BR>}<BR>==============================================================<BR>【程序49】<BR>题目:#if
#ifdef和#ifndef的综合应用。<BR>1. 程序分析: <BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include
"conio.h"<BR>#define MAX<BR>#define MAXIMUM(x,y) (x>y)?x:y<BR>#define
MINIMUM(x,y) (x>y)?y:x<BR>void main()<BR>{<BR>int a=10,b=20;<BR>#ifdef
MAX<BR>printf("\40: The larger one is
%d\n",MAXIMUM(a,b));<BR>#else<BR>printf("\40: The lower one is
%d\n",MINIMUM(a,b));<BR>#endif<BR>#ifndef MIN<BR>printf("\40: The lower one is
%d\n",MINIMUM(a,b));<BR>#else<BR>printf("\40: The larger one is
%d\n",MAXIMUM(a,b));<BR>#endif<BR>#undef MAX<BR>#ifdef MAX<BR>printf("\40: The
larger one is %d\n",MAXIMUM(a,b));<BR>#else<BR>printf("\40: The lower one is
%d\n",MINIMUM(a,b));<BR>#endif<BR>#define MIN<BR>#ifndef MIN<BR>printf("\40: The
lower one is %d\n",MINIMUM(a,b));<BR>#else<BR>printf("\40: The larger one is
%d\n",MAXIMUM(a,b));<BR>#endif<BR>getch();<BR>}<BR>==============================================================<BR>【程序50】<BR>题目:#include
的应用练习 <BR>1.程序分析:<BR>2.程序源代码:<BR>test.h 文件如下:<BR>#define LAG ><BR>#define
SMA <<BR>#define EQ ==</P>
<P>主文件如下:<BR>#include "test.h" /*一个新文件50.c,包含test.h*/<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>void main()<BR>{<BR>int i=10;<BR>int
j=20;<BR>if(i LAG j)<BR>printf("\40: %d larger than %d \n",i,j);<BR>else if(i EQ
j)<BR>printf("\40: %d equal to %d \n",i,j);<BR>else if(i SMA
j)<BR>printf("\40:%d smaller than %d \n",i,j);<BR>else<BR>printf("\40: No such
value.\n");<BR>getch();<BR>}</P>
<P><BR> 经典c程序100例==51--60<BR>【程序51】<BR>题目:学习使用按位与 &
。 <BR>1.程序分析:0&0=0; 0&1=0; 1&0=0;
1&1=1<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int
a,b;<BR>a=077;<BR>b=a&3;<BR>printf("\40: The a & b(decimal) is %d
\n",b);<BR>b&=7;<BR>printf("\40: The a & b(decimal) is %d
\n",b);<BR>}<BR>==============================================================<BR>【程序52】<BR>题目:学习使用按位或
| 。<BR>1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1 <BR>2.程序源代码:<BR>#include
"stdio.h"<BR>main()<BR>{<BR>int a,b;<BR>a=077;<BR>b=a|3;<BR>printf("\40: The a
& b(decimal) is %d \n",b);<BR>b|=7;<BR>printf("\40: The a & b(decimal)
is %d
\n",b);<BR>}<BR>==============================================================<BR>【程序53】<BR>题目:学习使用按位异或
^ 。 <BR>1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>main()<BR>{<BR>int a,b;<BR>a=077;<BR>b=a^3;<BR>printf("\40: The a
& b(decimal) is %d \n",b);<BR>b^=7;<BR>printf("\40: The a & b(decimal)
is %d
\n",b);<BR>}<BR>==============================================================<BR>【程序54】<BR>题目:取一个整数a从右端开始的4~7位。<BR>程序分析:可以这样考虑:
<BR>(1)先使a右移4位。<BR>(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)<BR>(3)将上面二者进行&运算。<BR>2.程序源代码:<BR>main()<BR>{<BR>unsigned
a,b,c,d;<BR>scanf("%o",&a);<BR>b=a>>4;<BR>c=~(~0<<4);<BR>d=b&c;<BR>printf("%o\n%o\n",a,d);<BR>}<BR>==============================================================<BR>【程序55】<BR>题目:学习使用按位取反~。 <BR>1.程序分析:~0=1;
~1=0;<BR>2.程序源代码:<BR>#include "stdio.h"<BR>main()<BR>{<BR>int
a,b;<BR>a=234;<BR>b=~a;<BR>printf("\40: The a's 1 complement(decimal) is %d
\n",b);<BR>a=~a;<BR>printf("\40: The a's 1 complement(hexidecimal) is %x
\n",a);<BR>}
<BR>==============================================================<BR>【程序56】<BR>题目:画图,学用circle画圆形。 <BR>1.程序分析:<BR>2.程序源代码:<BR>/*circle*/<BR>#include
"graphics.h"<BR>main()<BR>{int driver,mode,i;<BR>float
j=1,k=1;<BR>driver=VGA;mode=VGAHI;<BR>initgraph(&driver,&mode,"");<BR>setbkcolor(YELLOW);<BR>for(i=0;i<=25;i++)<BR>{<BR>setcolor(8);<BR>circle(310,250,k);<BR>k=k+j;<BR>j=j+0.3;<BR>}<BR>}
<BR>==============================================================<BR>【程序57】<BR>题目:画图,学用line画直线。<BR>1.程序分析: <BR>2.程序源代码:<BR>#include
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -