博文
全排列(2011-01-13 22:25:00)
摘要:全排列的程序似乎很基础,不过乍一想还真有点儿想不通,防止忘记,存下来作为积累吧。#include<stdio.h>
#define N 4
void swap(int *first, int *second)
{int temp;
temp = *first;
*first = *second;
*second = temp;
}
void QuanPaiLie(int array[], int start, int end)
{int current;
if(start > end)
{for(current = 0; current <= end; current ++) printf("%d ", array[current]);
printf("\n");
}
else
{for(current = start; current <= end; current ++)
{swap(&array[current], &array[start]);
QuanPaiLie(array, start + 1, end);
swap(&array[current], &array[start]);
}
}
}
void main(void)
{int arraylist[N], iter;
printf("Please input %d numbers for test: ", N);
for(iter = 0; iter < N; iter ++)
sca......
C魔方矩阵(2008-09-04 22:46:00)
摘要:#include<stdio.h>
void main()
{int array[19][19],dimension,first,second,count=1;
printf("Please input the dimension: ");
scanf("%d",&dimension);
first=0;
second=dimension/2;
for(;count<=dimension*dimension;count++)
{array[first][second]=count;
if(count%dimension!=0)
{first=(dimension+first-1)%dimension;
second=(second+1)%dimension;
}
else if(count%dimension==0)first=first+1;
}
printf("\nThe array is:\n\n");
for(first=0;first<dimension;first++)
{for(second=0;second<dimension;second++)
{printf("%4d",array[first][second]);
}
printf("\n");
}
printf("\nEnter Key To Exit......");
getch();
}......
C冒泡排序(2008-09-04 22:45:00)
摘要:#include<stdio.h>
main()
{int array[10],i,j,temp;
printf("Please input 10 numbers to sort:\n");
for(i=0;i<10;i++)
scanf("%d",&array[i]);
for(i=0;i<9;i++)
{for(j=i+1;j<10;j++)
{if(array[j]<array[i])
{temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
printf("The numbers after sorting are:\n");
for(i=0;i<10;i++)
printf("%d ",array[i]);
printf("\n");
getch();
}......
扫描线种子填充算法(2007-12-20 08:01:00)
摘要:这就是扫描线种子填充程序的C实现,运行后将出现一个多边形和一个种子,由于仅是模拟,所以初始化时固定了多边形的位置和种子的位置,可以在代码中修改其位置后重新编译运行,运行时请在main()函数里设置好图形驱动程序路径,因人而异了。
#include<graphics.h>
#include<stdio.h>
#include<malloc.h>
#define bordercolor 15
#define backcolor 0
#define seedcolor 4
#define fillcolor 10
typedef struct Point
{int x;
int y;
}point;
typedef struct Stack
{point *base;
point *top;
}*stack;
void initstack(stack s)
{(*s).base=(point*)malloc(sizeof(point));
if(!(*s).base)exit(1);
(*s).top=(*s).base;
}
void push(stack s,point p)
{*((*s).top)=p;
(*s).top++;
}
int stackempty(stack s)
{if((*s).top==(*s).base)return(1);
else return(0);
}
point pop(stack s)
{(*s).top--;
return(*((*s).top));
}
void drawgraphics()
{rectangle(100,100,539,299);
rectangle(150,150,489,249);
}
point produceseed()
{point p;
p.x=125;
p.y=200;
return(p);
}
void intos......
渔网(2007-12-14 22:44:00)
摘要:给大家看张渔网,运行的时候请设置好图形驱动的路径。
#include <graphics.h>
#include <math.h>
void ellip(int x0,int y0,int a,int b,int dt)
{int x,y,n,i;
float t1,t=0;
t1=dt*0.0174533;
n=360/dt;
moveto(x0+a,y0);
for(i=1;i<n;i++)
{t=t+t1;
x=x0+a*cos(t);
y=y0+b*sin(t);
lineto(x,y);
}
lineto(x0+a,y0);
}
main()
{int i,a=200,x=320,y=200;
int driver=DETECT,mode;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
cleardevice();
setbkcolor(9);
setcolor(4);
for(i=0;i<=200;i=i+10)
{ellip(x,y,a-i,i,10);
}
getch();
closegraph();
}......
C画函数曲线(2007-12-14 22:35:00)
摘要:这是一个函数曲线的描绘问题,可根据注释修改程序,以画出不同的曲线。
#include<graphics.h>
#define y(x) x*x /*在这里修改曲线方程,此处为x的平方*/
#define xs(x) x0+x*s /*这里为x方向的坐标转换*/
#define ys(x) y0-y(x)*s /*这里为y方向的坐标转换*/
main()
{float x0,y0,xl,xr,s,x,dx;
int driver=DETECT,mode;
printf("Please input the x and y of new location:");
scanf("%f %f",&x0,&y0); /*输入要画曲线的坐标系原点坐标*/
printf("Please input the multiply:");
scanf("%f",&s); /*曲线放大倍数,以几十为效果最佳*/
printf("Please input the left edge and the right edge and dx:");
scanf("%f %f %f",&xl,&xr,&dx); /*输入曲线的自变量上下界xl和xr以及自变量的递增步长*/
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
line(x0,getmaxy(),x0,0);
line(0,y0,getmaxx(),y0);
outtextxy(x0,y0,"0,0");
setcolor(14);
while(ys(xl)<0)xl+=dx;
moveto(xs(xl),ys(xl));
for(x=xl;x<=xr;x+=dx)
{if(x>......
C图形学中的错切演示程序(2007-12-14 22:15:00)
摘要:此程序用到两个文件,一个是被调用程序affine.c,另一个是主程序cuoqie.c,在运行前请注意自己设置好图形驱动程序的路径。
affine.c如下:
double sin(),cos();
double xmax=639.0,ymax=399.0;
double f[3][3],xx,yy;
scx(xj)
double xj;
{int x;
x=(int)(xj+xmax/2);
return(x);
}
scy(yj)
double yj;
{int y;
y=(int)(ymax-(yj+(ymax/2)));
return(y);
}
parallel(dx,dy)
double dx,dy;
{f[0][0]=1.0;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=1.0;f[1][2]=0.0;
f[2][0]=dx;f[2][1]=dy;f[2][2]=1.0;
}
rotate(theta)
double theta;
{double th;
th=theta/180*3.1415927;
f[0][0]=cos(th);f[0][1]=sin(th);f[0][2]=0.0;f[1][0]=-sin(th);
f[1][1]=cos(th);f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
scale(s)
double s;
{f[0][0]=s;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=s;f[1][2]=0.0;
f[2][0]=0.0;f[2][1]=0.0;f[2][2]=1.0;
}
taisho_x()
{f[0][0]=1.0;f[0][1]=0.0;f[0][2]=0.0;
f[1][0]=0.0;f[1][1]=-1;f[1][2]=0.0;
f[2][......
C图形学的弱题(2007-11-19 22:33:00)
摘要:以下三个都是很简单的C语言图形学的小程序,可以输出几个不错的图形,作为子函数还可以的,拿出来展览一下,不过运行的时候注意一下initgraph的路径,自己改一下吧。
#include<graphics.h> /*A simplest C graph product,run it directly*/
main()
{int x,driver=DETECT,mode;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
cleardevice();
setbkcolor(9);
for(x=170;x<=470;x=x+30)
{line(170,200,x,100);
line(170,200,x,300);
line(470,200,x,100);
line(470,200,x,300);
}
getch();
closegraph();
}
----------------------------------------------------------------------
#include<graphics.h> /*Input a number less than 50 and than Enter*/
#include<conio.h>
#include<math.h>
#define pi 3.1415926
#define max 50
main()
{int i,j,n,driver=DETECT,mode;
double r=100.0,theta,x[max],y[max];
printf("n(<=50)=");
scanf("%d",&n);
registerbgidriver(EGAVGA_driver);
&nb......
中点画线法(2007-11-19 22:13:00)
摘要:运行之前也须自己设置好initgraph里面的图形驱动路径,如果是绝对路径的话,可以把前面的registerbgidriver(EGAVGA_driver);一句去掉,程序很简单,没什么好说的了。
#include<graphics.h>
main()
{int a,b,d,x0,y0,x1,y1,x,y;
int driver=DETECT,mode;
printf("Please input two point to draw the line between:\n");
printf("x0=");scanf("%d",&x0);
printf("y0=");scanf("%d",&y0);
printf("x1=");scanf("%d",&x1);
printf("y1=");scanf("%d",&y1);
a=y0-y1;b=x1-x0;d=2*a+b;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
for(x=x0,y=y0;x<=x1;)
{putpixel(x,y,14);
if(d>=0)
{x=x+1;
d=d+2*a;
}
else
{x=x+1;
y=y+1;
d=d+2*(a+b);
}
}
getch();
closegraph();
}......
语法分析器(2007-06-23 11:59:00)
摘要:这是一个能识别加法和乘法的语法分析器,例如在提示输入后输入一个算式:3+5*2*(2+1)这类的,那么就会给出匹配过程,并判断是不是成功,请赏析!由于构造了first集,follow集,select集之类,所以比较占篇幅,惭愧!
#include<string.h>
int isterminal(char a)
{if((a=='i')||(a=='+')||(a=='*')||(a=='(')||(a==')')||(a=='$'))
return(1);
else return(0);
}
int notin(char a,char *b)
{char *c;
for(c=b;(*c)!='\0';c++)
{if((*c)==a) break;
}
if((*c)=='\0') return(1);
else return(0);
}
void add(char *g,char *q,char *a)
{char *tem;
tem=a;
for(;(*tem)!='\0';tem++)
{if(notin((*tem),g))
{(*q)=(*tem);
q++;
(*q)='\0';
}
}
}
int maybenull(char E)
{char product[8][5]={"SBA","A+BA","A","BDC","C*DC","C","D(S)","Di"};
int i;
for(i=0;i<8;i++)
{if((product[i][0]==E)&&(product[i][1]=='\0'))
break;
}
if(i<8) return(1);
else if(i>=8) return(0);
}
char......