📄 c语言经典程序100例.htm
字号:
"graphics.h"<BR>main()<BR>{int driver,mode,i;<BR>float x0,y0,y1,x1;<BR>float
j=12,k;<BR>driver=VGA;mode=VGAHI;<BR>initgraph(&driver,&mode,"");<BR>setbkcolor(GREEN);<BR>x0=263;y0=263;y1=275;x1=275;<BR>for(i=0;i<=18;i++)<BR>{<BR>setcolor(5);<BR>line(x0,y0,x0,y1);<BR>x0=x0-5;<BR>y0=y0-5;<BR>x1=x1+5;<BR>y1=y1+5;<BR>j=j+10;<BR>}<BR>x0=263;y1=275;y0=263;<BR>for(i=0;i<=20;i++)<BR>{<BR>setcolor(5);<BR>line(x0,y0,x0,y1);<BR>x0=x0+5;<BR>y0=y0+5;<BR>y1=y1-5;<BR>}<BR>}<BR>==============================================================<BR>【程序58】<BR>题目:画图,学用rectangle画方形。 <BR>1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。<BR>2.程序源代码:<BR>#include
"graphics.h"<BR>main()<BR>{int
x0,y0,y1,x1,driver,mode,i;<BR>driver=VGA;mode=VGAHI;<BR>initgraph(&driver,&mode,"");<BR>setbkcolor(YELLOW);<BR>x0=263;y0=263;y1=275;x1=275;<BR>for(i=0;i<=18;i++)<BR>{<BR>setcolor(1);<BR>rectangle(x0,y0,x1,y1);<BR>x0=x0-5;<BR>y0=y0-5;<BR>x1=x1+5;<BR>y1=y1+5;<BR>}<BR>settextstyle(DEFAULT_FONT,HORIZ_DIR,2);<BR>outtextxy(150,40,"How
beautiful it
is!");<BR>line(130,60,480,60);<BR>setcolor(2);<BR>circle(269,269,137);<BR>}<BR>==============================================================<BR>【程序59】<BR>题目:画图,综合例子。
<BR>1.程序分析:<BR>2.程序源代码:<BR># define PAI 3.1415926<BR># define B 0.809<BR>#
include "graphics.h"<BR>#include "math.h"<BR>main()<BR>{<BR>int
i,j,k,x0,y0,x,y,driver,mode;<BR>float
a;<BR>driver=CGA;mode=CGAC0;<BR>initgraph(&driver,&mode,"");<BR>setcolor(3);<BR>setbkcolor(GREEN);<BR>x0=150;y0=100;<BR>circle(x0,y0,10);<BR>circle(x0,y0,20);<BR>circle(x0,y0,50);<BR>for(i=0;i<16;i++)<BR>{<BR> a=(2*PAI/16)*i;<BR> x=ceil(x0+48*cos(a));<BR> y=ceil(y0+48*sin(a)*B);<BR> setcolor(2);
line(x0,y0,x,y);}<BR>setcolor(3);circle(x0,y0,60);<BR>/* Make 0 time normal size
letters */<BR>settextstyle(DEFAULT_FONT,HORIZ_DIR,0);<BR>outtextxy(10,170,"press
a
key");<BR>getch();<BR>setfillstyle(HATCH_FILL,YELLOW);<BR>floodfill(202,100,WHITE);<BR>getch();<BR>for(k=0;k<=500;k++)<BR>{<BR> setcolor(3);<BR> for(i=0;i<=16;i++)<BR> {<BR> a=(2*PAI/16)*i+(2*PAI/180)*k;<BR> x=ceil(x0+48*cos(a));<BR> y=ceil(y0+48+sin(a)*B);<BR> setcolor(2);
line(x0,y0,x,y);<BR> }<BR> for(j=1;j<=50;j++)<BR> {<BR> a=(2*PAI/16)*i+(2*PAI/180)*k-1;<BR> x=ceil(x0+48*cos(a));<BR> y=ceil(y0+48*sin(a)*B);<BR> line(x0,y0,x,y);<BR> }<BR>}<BR>restorecrtmode();<BR>}<BR>==============================================================<BR>【程序60】<BR>题目:画图,综合例子。 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"graphics.h"<BR>#define LEFT 0<BR>#define TOP 0<BR>#define RIGHT 639<BR>#define
BOTTOM 479<BR>#define LINES 400<BR>#define MAXCOLOR 15<BR>main()<BR>{<BR>int
driver,mode,error;<BR>int x1,y1;<BR>int x2,y2;<BR>int
dx1,dy1,dx2,dy2,i=1;<BR>int count=0;<BR>int
color=0;<BR>driver=VGA;<BR>mode=VGAHI;<BR>initgraph(&driver,&mode,"");<BR>x1=x2=y1=y2=10;<BR>dx1=dy1=2;<BR>dx2=dy2=3;<BR>while(!kbhit())<BR>{<BR> line(x1,y1,x2,y2);<BR> x1+=dx1;y1+=dy1;<BR> x2+=dx2;y2+dy2;<BR> if(x1<=LEFT||x1>=RIGHT)<BR> dx1=-dx1;<BR> if(y1<=TOP||y1>=BOTTOM)<BR> dy1=-dy1;<BR> if(x2<=LEFT||x2>=RIGHT)<BR> dx2=-dx2;<BR> if(y2<=TOP||y2>=BOTTOM)<BR> dy2=-dy2;<BR> if(++count>LINES)<BR> {<BR> setcolor(color);<BR> color=(color>=MAXCOLOR)?0:++color;<BR> }<BR>}<BR>closegraph();<BR>}</P>
<P> .:.:经典c程序100例==61--70:.:. 经典c程序100例==61--70<BR>【程序61】<BR>题目:打印出杨辉三角形(要求打印出10行如下图) <BR>1.程序分析:<BR>
1<BR> 1 1<BR> 1 2 1<BR> 1 3 3 1<BR> 1 4 6 4
1<BR> 1 5 10 10 5 1 <BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include
"conio.h"<BR>main()<BR>{<BR>int i,j;<BR>int
a[10][10];<BR>printf("\n");<BR>for(i=0;i<10;i++)<BR>{<BR>a[i][0]=1;<BR>a[i][i]=1;<BR>}<BR>for(i=2;i<10;i++)<BR>for(j=1;j<i;j++)<BR>a[i][j]=a[i-1][j-1]+a[i-1][j];<BR>for(i=0;i<10;i++)<BR>{<BR>for(j=0;j<=i;j++)<BR>printf("%5d",a[i][j]);<BR>printf("\n");<BR>}<BR>getch();
<BR>}<BR>==============================================================<BR>【程序62】<BR>题目:学习putpixel画点。<BR>1.程序分析: <BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>#include "graphics.h"<BR>main()<BR>{<BR>int
i,j,driver=VGA,mode=VGAHI;<BR>initgraph(&driver,&mode,"");<BR>setbkcolor(YELLOW);<BR>for(i=50;i<=230;i+=20)<BR>for(j=50;j<=230;j++)<BR>putpixel(i,j,1);<BR>for(j=50;j<=230;j+=20)<BR>for(i=50;i<=230;i++)<BR>putpixel(i,j,1);<BR>getch();<BR>}<BR>==============================================================<BR>【程序63】<BR>题目:画椭圆ellipse <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "graphics.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
x=260,y=160,driver=VGA,mode=VGAHI;<BR>int num=20,i;<BR>int
top,bottom;<BR>initgraph(&driver,&mode,"");<BR>top=y-30;<BR>bottom=y-30;<BR>for(i=0;i<num;i++)<BR>{<BR>ellipse(x,250,0,360,top,bottom);<BR>top-=5;<BR>bottom+=5;<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序64】<BR>题目:利用ellipse
and rectangle 画图。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include
"graphics.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
driver=VGA,mode=VGAHI;<BR>int i,num=15,top=50;<BR>int
left=20,right=50;<BR>initgraph(&driver,&mode,"");<BR>for(i=0;i<num;i++)<BR>{<BR>ellipse(250,250,0,360,right,left);<BR>ellipse(250,250,0,360,20,top);<BR>rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));<BR>right+=5;<BR>left+=5;<BR>top+=10;<BR>}<BR>getch();<BR>}<BR>==============================================================<BR>【程序65】<BR>题目:一个最优美的图案。 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"graphics.h"<BR>#include "math.h"<BR>#include "dos.h"<BR>#include
"conio.h"<BR>#include "stdlib.h"<BR>#include "stdio.h"<BR>#include
"stdarg.h"</P>
<P>#define MAXPTS 15<BR>#define PI 3.1415926</P>
<P>struct PTS<BR>{<BR>int x,y;<BR>};<BR>double AspectRatio=0.85;<BR>void
LineToDemo(void)<BR>{<BR>struct viewporttype vp;<BR>struct PTS
points[MAXPTS];<BR>int i, j, h, w, xcenter, ycenter;<BR>int radius, angle,
step;<BR>double rads;<BR>printf(" MoveTo / LineTo Demonstration"
);<BR>getviewsettings( &vp );<BR>h = vp.bottom - vp.top;<BR>w = vp.right -
vp.left;<BR>xcenter = w / 2; /* Determine the center of circle */<BR>ycenter = h
/ 2;<BR>radius = (h - 30) / (AspectRatio * 2);<BR>step = 360 / MAXPTS; /*
Determine # of increments */<BR>angle = 0; /* Begin at zero degrees */<BR>for(
i=0 ; i<MAXPTS ; ++i )<BR>{ /* Determine circle intercepts */<BR>rads =
(double)angle * PI / 180.0; /* Convert angle to radians */<BR>points[i].x =
xcenter + (int)( cos(rads) * radius );<BR>points[i].y = ycenter - (int)(
sin(rads) * radius * AspectRatio );<BR>angle += step; /* Move to next increment
*/<BR>}<BR>circle( xcenter, ycenter, radius ); /* Draw bounding circle
*/<BR>for( i=0 ; i<MAXPTS ; ++i )<BR>{ /* Draw the cords to the circle
*/<BR>for( j=i ; j<MAXPTS ; ++j )<BR>{ /* For each remaining intersect
*/<BR>moveto(points[i].x, points[i].y); /* Move to beginning of cord
*/<BR>lineto(points[j].x, points[j].y); /* Draw the cord
*/<BR>}<BR>}<BR>}<BR>main()<BR>{<BR>int
driver,mode;<BR>driver=CGA;mode=CGAC0;<BR>initgraph(&driver,&mode,"");<BR>setcolor(3);<BR>setbkcolor(GREEN);<BR>LineToDemo();<BR>getch();<BR>}<BR>==============================================================<BR>【程序66】<BR>题目:输入3个数a,b,c,按大小顺序输出。 <BR>1.程序分析:利用指针方法。<BR>2.程序源代码:<BR>/*pointer*/<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int n1,n2,n3;<BR>int
*pointer1,*pointer2,*pointer3;<BR>printf("please input 3
number:n1,n2,n3:");<BR>scanf("%d,%d,%d",&n1,&n2,&n3);<BR>pointer1=&n1;<BR>pointer2=&n2;<BR>pointer3=&n3;<BR>if(n1>n2)
swap(pointer1,pointer2);<BR>if(n1>n3)
swap(pointer1,pointer3);<BR>if(n2>n3) swap(pointer2,pointer3);<BR>printf("the
sorted numbers are:%d,%d,%d\n",n1,n2,n3);<BR>getch();<BR>}<BR>swap(p1,p2)<BR>int
*p1,*p2;<BR>{<BR>int
p;<BR>p=*p1;<BR>*p1=*p2;<BR>*p2=p;<BR>}<BR>==============================================================<BR>【程序67】<BR>题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。<BR>1.程序分析:谭浩强的书中答案有问题。 <BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
number[10];<BR>input(number);<BR>max_min(number);<BR>output(number);<BR>getch();<BR>}<BR>input(number)<BR>int
number[10];<BR>{<BR>int
i;<BR>for(i=0;i<9;i++)<BR>scanf("%d,",&number[i]);<BR>scanf("%d",&number[9]);<BR>}<BR>max_min(array)<BR>int
array[10];<BR>{<BR>int *max,*min,k,l;<BR>int
*p,*arr_end;<BR>arr_end=array+10;<BR>max=min=array;<BR>for(p=array+1;p<arr_end;p++)<BR>if(*p>*max)
max=p;<BR>else if(*p<*min)
min=p;<BR>k=*max;<BR>l=*min;<BR>*p=array[0];array[0]=l;l=*p;<BR>*p=array[9];array[9]=k;k=*p;<BR>return;<BR>}<BR>output(array)<BR>int
array[10];<BR>{<BR>int
*p;<BR>for(p=array;p<array+9;p++)<BR>printf("%d,",*p);<BR>printf("%d\n",array[9]);<BR>}<BR>==============================================================<BR>【程序68】<BR>题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数<BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int
number[20],n,m,i;<BR>printf("the total numbers
is:");<BR>scanf("%d",&n);<BR>printf("back
m:");<BR>scanf("%d",&m);<BR>for(i=0;i<n-1;i++)<BR>scanf("%d,",&number[i]);<BR>scanf("%d",&number[n-1]);<BR>move(number,n,m);<BR>for(i=0;i<n-1;i++)<BR>printf("%d,",number[i]);<BR>printf("%d",number[n-1]);<BR>getch();<BR>}<BR>move(array,n,m)<BR>int
n,m,array[20];<BR>{<BR>int
*p,array_end;<BR>array_end=*(array+n-1);<BR>for(p=array+n-1;p>array;p--)<BR>*p=*(p-1);<BR>*array=array_end;<BR>m--;<BR>if(m>0)<BR>move(array,n,m);<BR>}<BR>==============================================================<BR>【程序69】<BR>题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出<BR> 圈子,问最后留下的是原来第几号的那位。<BR>1.
程序分析:<BR>2.程序源代码:<BR>#include "stdio.h"<BR>#include "conio.h"<BR>#define nmax
50<BR>main()<BR>{<BR>int i,k,m,n,num[nmax],*p;<BR>printf("please input the total
of
numbers:");<BR>scanf("%d",&n);<BR>p=num;<BR>for(i=0;i<n;i++)<BR>*(p+i)=i+1;<BR>i=0;<BR>k=0;<BR>m=0;<BR>while(m<n-1)<BR>{<BR>if(*(p+i)!=0)
k++;<BR>if(k==3)<BR>{<BR>*(p+i)=0;<BR>k=0;<BR>m++;<BR>}<BR>i++;<BR>if(i==n)
i=0;<BR>}<BR>while(*p==0) p++;<BR>printf("%d is
left\n",*p);<BR>getch();<BR>}<BR>==============================================================<BR>【程序70】<BR>题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 <BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>main()<BR>{<BR>int len;<BR>char
*str[20];<BR>printf("please input a
string:\n");<BR>scanf("%s",str);<BR>len=length(str);<BR>printf("the string has
%d characters.",len);<BR>getch();<BR>}<BR>length(p)<BR>char *p;<BR>{<BR>int
n;<BR>n=0;<BR>while(*p!='\0')<BR>{<BR>n++;<BR>p++;<BR>}<BR>return n;<BR>}</P>
<P><BR> 经典c程序100例==71--80<BR>【程序71】<BR>题目:编写input()和output()函数输入,输出5个学生的数据记录。<BR>1.程序分析:<BR>2.程序源代码:<BR>#include
"stdio.h"<BR>#include "conio.h"<BR>#define N 5<BR>struct student<BR>{<BR>char
num[6];<BR>char name[8];<BR>int score[4];<BR>}stu[N];<BR>input(stu)<BR>struct
student stu[];<BR>{<BR>int i,j;<BR>for(i=0;i<N;i++)<BR>{<BR>printf("\n please
input %d of %d\n",i+1,N);<BR>printf("num:
");<BR>scanf("%s",stu[i].num);<BR>printf("name:
");<BR>scanf("%s",stu[i].name);<BR>for(j=0;j<3;j++)<BR>{<BR>printf("score
%d.",j+1);<BR>scanf("%d",&stu[i].score[j]);<BR>}<BR>printf("\n");<BR>}<BR>}<BR>print(stu)<BR>struct
student stu[];<BR>{<BR>int i,j;<BR>printf("\nNo. Name Sco1 Sco2
Sco3\n");<BR>for(i=0;i<N;i++)<BR>{<BR>printf("%-6s%-10s",stu[i].num,stu[i].name);<BR>for(j=0;j<3;j++)<BR>printf("%-8d",stu[i].score[j]);<BR>printf("\n");<BR>}<BR>}<BR>main()<BR>{<BR>input();<BR>print();<BR>getch();<BR>}<BR>==============================================================<BR>【程序72】<BR>题目:创建一个链表。<BR>1.程序分析: <BR>2.程序源代码:<BR>/*creat
a list*/<BR>#include "stdlib.h"<BR>#include "stdio.h"<BR>#include
"conio.h"<BR>struct list<BR>{<BR>int data;<BR>struct list
*next;<BR>};<BR>typedef struct list node;<BR>typedef node *link;<BR>void
main()<BR>{<BR>link ptr,head;<BR>int
num,i;<BR>ptr=(link)malloc(sizeof(node));<BR>ptr=head;<BR>printf("please input 5
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -