方法1:
#include "stdio.h"
#include "stdlib.h"
void RKT(t,y,n,h,k,z)
int n; /*微分方程组中方程的个数,也是未知函数的个数*/
int k; /*积分的步数(包括起始点这一步)*/
double t; /*积分的起始点t0*/
double h; /*积分的步长*/
double y[]; /*存放n个未知函数在起始点t处的函数值,返回时,其初值在二维数组z的第零列中*/
double z[]; /*二维数组,体积为n x k.返回k个积分点上的n个未知函数值*/
{
extern void Func(); /*声明要求解的微分方程组*/
int i,j,l;
double a[4],*b,*d;
b=malloc(n*sizeof(double)); /*分配存储空间*/
if(b == NULL)
{
printf("内存分配失败\n");
exit(1);
}
d=malloc(n*sizeof(double)); /*分配存储空间*/
if(d == NULL)
{
printf("内存分配失败\n");
exit(1);
}
/*后面应用RK4公式中用到的系数*/
a[0]=h/2.0;
a[1]=h/2.0;
a[2]=h;
a[3]=h;
for(i=0; i<=n-1; i++)
z[i*k]=y[i]; /*将初值赋给数组z的相应位置*/
for(l=1; l<=k-1; l++)
{
Func(y,d);
for (i=0; i<=n-1; i++)
b[i]=y[i];
for (j=0; j<=2; j++)
{
for (i=0; i<=n-1; i++)
{
y[i]=z[i*k+l-1]+a[j]*d[i];
b[i]=b[i]+a[j+1]*d[i]/3.0;
}
Func(y,d);
}
for(i=0; i<=n-1; i++)
y[i]=b[i]+h*d[i]/6.0;
for(i=0; i<=n-1; i++)
z[i*k+l]=y[i];
t=t+h;
}
free(b); /*释放存储空间*/
free(d); /*释放存储空间*/
return;
}
main()
{
int i,j;
double t,h,y[3],z[3][11];
y[0]=-1.0;
y[1]=0.0;
y[2]=1.0;
t=0.0;
h=0.01;
RKT(t,y,3,h,11,z);
printf("\n");
for (i=0; i<=10; i++) /*打印输出结果*/
{
t=i*h;
printf("t=%5.2f\t ",t);
for (j=0; j<=2; j++)
printf("y(%d)=%e ",j,z[j][i]);
printf("\n");
}
}
void Func(y,d)
double y[],d[];
{
d[0]=y[1]; /*y0'=y1*/
d[1]=-y[0]; /*y1'=y0*/
d[2]=-y[2]; /*y2'=y2*/
return;
}
高斯消元法:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
int GS(int,double**,double *,double);
double **TwoArrayAlloc(int,int);
void TwoArrayFree(double **);
void main()
{
int i,n;
double ep,**a,*b;
n = 3;
ep = 1e-4;
a = TwoArrayAlloc(n,n);
b = (double *)calloc(n,sizeof(double));
if(b == NULL)
{
printf("内存分配失败\n");
exit(1);
}
a[0][0]= 2; a[0][1]= 6; a[0][2]=-1;
a[1][0]= 5; a[1][1]=-1; a[1][2]= 2;
a[2][0]=-3; a[2][1]=-4; a[2][2]= 1;
b[0] = -12; b[1] = 29; b[2] = 5;
if(!GS(n,a,b,ep))
{
printf("不可以用高斯消去法求解\n");
exit(0);
}
printf("该方程组的解为:\n");
for(i=0;i<3;i++)
printf("x%d = %.2f\n",i,b[i]);
TwoArrayFree(a);
free(b);
}
int GS(n,a,b,ep)
int n;
double **a;
double *b;
double ep;
{
int i,j,k,l;
double t;
for(k=1;k<=n;k++)
{
for(l=k;l<=n;l++)
if(fabs(a[l-1][k-1])>ep)
break;
else if(l==n)
return(0);
if(l!=k)
{
for(j=k;j<=n;j++)
{
t = a[k-1][j-1];
a[k-1][j-1]=a[l-1][j-1];
a[l-1][j-1]=t;
}
t=b[k-1];
b[k-1]=b[l-1];
b[l-1]=t;
}
t=1/a[k-1][k-1];
for(j=k+1;j<=n;j++)
a[k-1][j-1]=t*a[k-1][j-1];
b[k-1]*=t;
for(i=k+1;i<=n;i++)
{
for(j=k+1;j<=n;j++)
a[i-1][j-1]-=a[i-1][k-1]*a[k-1][j-1];
b[i-1]-=a[i-1][k-1]*b[k-1];
}
}
for(i=n-1;i>=1;i--)
for(j=i+1;j<=n;j++)
b[i-1]-=a[i-1][j-1]*b[j-1];
return(1);
}
double **TwoArrayAlloc(int r,int c)
{
double *x,**y;
int n;
x=(double *)calloc(r*c,sizeof(double));
y=(double **)calloc(r,sizeof(double*));
for(n=0;n<=r-1;++n)
y[n]=&x[c*n];
return (y);
}
void TwoArrayFree(double **x)
{
free(x[0]);
free(x);
}
发表于 2005-09-17 13:23 yingkou的Blog 阅读(3179) | 评论 (4) | 编辑 收藏
拉格朗日插值法:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
double LAG(int,double *,double *,double);
void main()
{
int n;
double *x,*y,t,lag;
t = 0.15;
n = 6;
x = (double*)calloc(n,sizeof(double));
if(x == NULL)
{
printf("内存分配失败\n");
exit(1);
}
y = (double*)calloc(n,sizeof(double));
if(y == NULL)
{
printf("内存分配失败\n");
exit(1);
}
x[0] = 0;
x[1] = 0.1;
x[2] = 0.195;
x[3] = 0.3;
x[4] = 0.401;
x[5] = 0.5;
y[0] = 0.39894;
y[1] = 0.39695;
y[2] = 0.39142;
y[3] = 0.38138;
y[4] = 0.36812;
y[5] = 0.35206;
lag = LAG(n,x,y,t);
printf("拉各朗日插值后得到的结果是:\n");
printf("f(%.2f)=%e\n",t,lag);
free(x);
free(y);
}
double LAG(n,x,y,t)
int n;
double *x;
double *y;
double t;
{
int i,j;
double p,s;
s = 0;
for(i=0;i<n-1;i++)
{
p = 1;
for(j=0;j<n-1;j++)
if(i!=j)
p*=(t-x[j])/(x[i]-x[j]);
s+=p*y[i];
}
return (s);
}
曲线拟合:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
Smooth(double *,double *,double *,int,int,
double *,double *,double *);
void main()
{
int i,n,m;
double *x,*y,*a,dt1,dt2,dt3,b;
n = 20;
m = 6;
b = 0;
/*分别为x,y,a分配存贮空间*/
x = (double *)calloc(n,sizeof(double));
if(x == NULL)
{
printf("内存分配失败\n");
exit (0);
}
y = (double *)calloc(n,sizeof(double));
if(y == NULL)
{
printf("内存分配失败\n");
exit (0);
}
a = (double *)calloc(n,sizeof(double));
if(a == NULL)
{
printf("内存分配失败\n");
exit (0);
}
for(i=1;i<=n;i++)
{
x[i-1]=b+(i-1)*0.1;
/*每隔0.1取一个点,这样连续取n个点*/
y[i-1]=x[i-1]-exp(-x[i-1]);
/*计算x[i-1]点对应的y值作为拟合已知值*/
}
Smooth(x,y,a,n,m,&dt1,&dt2,&dt3); /*调用拟合函数*/
for(i=1;i<=m;i++)
printf("a[%d] = %.10f\n",(i-1),a[i-1]);
printf("拟合多项式与数据点偏差的平方和为:\n");
printf("%.10e\n",dt1);
printf("拟合多项式与数据点偏差的绝对值之和为:\n");
printf("%.10e\n",dt2);
printf("拟合多项式与数据点偏差的绝对值最大值为:\n");
printf("%.10e\n",dt3);
free(x); /*释放存储空间*/
free(y); /*释放存储空间*/
free(a); /*释放存储空间*/
}
Smooth(x,y,a,n,m,dt1,dt2,dt3 )
double *x; /*实型一维数组,输入参数,存放节点的xi值*/
double *y; /*实型一维数组,输入参数,存放节点的yi值*/
double *a; /*双精度实型一维数组,长度为m。返回m一1次拟合多项式的m个系数*/
int n; /*整型变量,输入参数,给定数据点的个数*/
int m; /*整型变量,输入参数,拟合多项式的项数*/
double *dt1; /*实型变量,输出参数,拟合多项式与数据点偏差的平方和*/
double *dt2; /*实型变量,输出参数,拟合多项式与数据点偏差的绝对值之和*/
double *dt3; /*实型变量,输出参数,拟合多项式与数据点偏差的绝对值最大值*/
{
int i,j,k;
double *s,*t,*b,z,d1,p,c,d2,g,q,dt;
/*分别为s,t,b分配存贮空间*/
s = (double *)calloc(n,sizeof(double));
if(s == NULL)
{
printf("内存分配失败\n");
exit (0);
}
t = (double *)calloc(n,sizeof(double));
if(t == NULL)
{
printf("内存分配失败\n");
exit (0);
}
b = (double *)calloc(n,sizeof(double));
if(b == NULL)
{
printf("内存分配失败\n");
exit (0);
}
z = 0;
for(i=1;i<=n;i++)
z=z+x[i-1]/n; /*z为各个x的平均值*/
b[0]=1;
d1=n;
p=0;
c=0;
for(i=1;i<=n;i++)
{
p=p+x[i-1]-z;
c=c+y[i-1];
}
c=c/d1;
p=p/d1;
a[0]=c*b[0];
if(m>1)
{
t[1]=1;
t[0]=-p;
d2=0;
c=0;
g=0;
for(i=1;i<=n;i++)
{
q=x[i-1]-z-p;
d2=d2+q*q;
c=y[i-1]*q+c;
g=(x[i-1]-z)*q*q+g;
}
c=c/d2;
p=g/d2;
q=d2/d1;
d1=d2;
a[1]=c*t[1];
a[0]=c*t[0]+a[0];
}
for(j=3;j<=m;j++)
{
s[j-1]=t[j-2];
s[j-2]=-p*t[j-2]+t[j-3];
if(j>=4)
for(k=j-2;k>=2;k--)
s[k-1]=-p*t[k-1]+t[k-2]-q*b[k-1];
s[0]=-p*t[0]-q*b[0];
d2=0;
c=0;
g=0;
for(i=1;i<=n;i++)
{
q=s[j-1];
for(k=j-1;k>=1;k--)
q=q*(x[i-1]-z)+s[k-1];
d2=d2+q*q;
c=y[i-1]*q+c;
g=(x[i-1]-z)*q*q+g;
}
c=c/d2;
p=g/d2;
q=d2/d1;
d1=d2;
a[j-1]=c*s[j-1];
t[j-1]=s[j-1];
for(k=j-1;k>=1;k--)
{
a[k-1]=c*s[k-1]+a[k-1];
b[k-1]=t[k-1];
t[k-1]=s[k-1];
}
}
*dt1=0;
*dt2=0;
*dt3=0;
for(i=1;i<=n;i++)
{
q=a[m-1];
for(k=m-1;k>=1;k--)
q=q*(x[i-1]-z)+a[k-1];
dt=q-y[i-1];
if(fabs(dt)>*dt3)
*dt3=fabs(dt);
*dt1=*dt1+dt*dt;
*dt2=*dt2+fabs(dt);
}
/*释放存储空间*/
free(s);
free(t);
free(b);
return(1);
}
发表于 2005-09-17 13:21 yingkou的Blog 阅读(8041) | 评论 (14) | 编辑 收藏
辛普生求积分:
#include <stdio.h>
#include <math.h>
double Function(double);
double SIMP1(double,double,int);
double SIMP2(double,double,double);
void main()
{
double a1,b1,eps;
int n1;
double Result1;
double Result2;
a1 = 0.0;
b1 = 0.8;
n1 = 4;
eps = 5e-7;
Result1 = SIMP1(a1,b1,n1);
Result2 = SIMP2(a1,b1,eps);
printf("利用定步长辛普生积分结果为:\n");
printf("I1 = %.10f\n",Result1);
printf("利用变步长辛普生积分结果为:\n");
printf("I2 = %e\n",Result2);
}
double SIMP1(a,b,n)
double a;
double b;
int n;
{
int i;
double h,s;
h=(a-b)/(2*n);
s=0.5*(Function(a)-Function(b));
for(i=1;i<=n;i++)
s+=2*Function(a+(2*i-1)*h)+Function(a+2*i*h);
return((b-a)*s/(3*n));
}
double SIMP2(a,b,eps)
double a;
double b;
double eps;
{
int k,n;
double h,t1,t2,s1,s2,p,x;
n=1;
h=b-a;
t1=h*(Function(a)+Function(b))/2;
s1 = t1;
while(1)
{
p = 0;
for(k=0;k<=n;k++)
{
x = a+(k+0.5)*h;
p+=Function(x);
}
t2=(t1+h*p)/2;
s2=(4*t2-t1)/3;
if(fabs(s2-s1)>=eps)
{
t1=t2;
n=n+n;
h=h/2;
s1=s2;
continue;
}
break;
}
return(s2);
}
double Function(double x)
{
return(cos(x));
}
欧拉方程法:
#include "stdio.h"
#include "stdlib.h"
#include <math.h>
int Func(y,d)
double y[],d[];
{
d[0]=y[1]; /*y0'=y1*/
d[1]=-y[0]; /*y1'=y0*/
d[2]=-y[2]; /*y2'=y2*/
return(1);
}
void Euler1(t,y,n,h,k,z)
int n; /*整型变量,微分方程组中方程的个数,也是未知函数的个数*/
int k; /*整型变量。积分步数(包括起始点这一步)*/
double t; /*双精度实型变量,对微分方程进行积分的起始点t0*/
double h; /*双精度实型变量。积分步长*/
double y[]; /*双精度实型一维数组,长度为n。存放n个未知函数yi在起始点t0处的函数值*/
double z[]; /*双精度实型二维数组,体积为nxk。返回k个积分点(包括起始点)上的未知函数值*/
{
extern int Func();
int i,j;
double *d;
d=malloc(n*sizeof(double));
if(d == NULL)
{
printf("内存分配失败\n");
exit(1);
}
/*将方程组的初值赋给数组z[i*k]*/
for (i=0; i<=n-1; i++)
z[i*k]=y[i];
for (j=1; j<=k-1; j++)
{
Func(y,d); /*求出f(x)*/
for(i=0; i<=n-1; i++)
y[i]=z[i*k+j-1]+h*d[i];
Func(y,d);
for (i=0; i<=n-1; i++)
d[i]=z[i*k+j-1]+h*d[i];
for (i=0; i<=n-1; i++)
{
y[i]=(y[i]+d[i])/2.0;
z[i*k+j]=y[i];
}
}
free(d);
return;
}
void Euler2(t,h,y,n,eps)
int n;
double t,h,eps,y[];
{
int i,j,m;
double hh,p,q,*a,*b,*c,*d;
a=malloc(n*sizeof(double));
b=malloc(n*sizeof(double));
c=malloc(n*sizeof(double));
d=malloc(n*sizeof(double));
hh=h;
m=1;
p=1.0+eps;
for (i=0; i<=n-1; i++) a[i]=y[i];
while (p>=eps)
{
for (i=0; i<=n-1; i++)
{
b[i]=y[i];
y[i]=a[i];
}
for (j=0; j<=m-1; j++)
{
for (i=0; i<=n-1; i++)
c[i]=y[i];
Func(y,d);
for (i=0; i<=n-1; i++)
y[i]=c[i]+hh*d[i];
Func(y,d);
for (i=0; i<=n-1; i++)
d[i]=c[i]+hh*d[i];
for (i=0; i<=n-1; i++)
y[i]=(y[i]+d[i])/2.0;
}
p=0.0;
for (i=0; i<=n-1; i++)
{
q=fabs(y[i]-b[i]);
if (q>p) p=q;
}
hh=hh/2.0; m=m+m;
}
free(a);
free(b);
free(c);
free(d);
return;
}
main()
{
int i,j;
double y[3],z[3][11],t,h,x,eps;
y[0]=-1.0; /*初值y0(0)=-1.0*/
y[1]=0.0; /*初值y1(0)=-1.0*/
y[2]=1.0; /*初值y2(0)=-1.0*/
t=0.0; /*起始点t=0*/
h=0.01; /*步长为0.01*/
eps = 0.00001;
Euler1(t,y,3,h,11,z);
printf("定步长欧拉法结果:\n");
for (i=0; i<=10; i++)
{
x=i*h;
printf("t=%5.2f\t ",x);
for(j=0; j<=2; j++)
printf("y(%d)=%e ",j,z[j][i]);
printf("\n");
}
y[0]=-1.0; /*重新赋初值*/
y[1]=0.0;
y[2]=1.0;
printf("变步长欧拉法结果:\n");
printf("t=%5.2f\t ",t);
for (i=0; i<=2; i++)
printf("y(%d)=%e ",i,y[i]);
printf("\n");
for (j=1; j<=10; j++)
{
Euler2(t,h,y,3,eps);
t=t+h;
printf("t=%5.2f\t ",t);
for (i=0; i<=2; i++)
printf("y(%d)=%e ",i,y[i]);
printf("\n");
}
}
发表于 2005-09-17 13:21 yingkou的Blog 阅读(2837) | 评论 (1) | 编辑 收藏
求解方程:
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>
double Func(double);
int BisectRoot(double,double,double,double,double *,int,int *);
void main()
{
int i,n,m;
double a,b,h,eps,*x;
n = 3; /*方程根的个数的预估值*/
x = (double*)calloc(n,sizeof(double)); /*开辟内存空间*/
if(x == NULL)
{
printf("内存分配失败\n");
exit(1);
}
a = -3; /*区间起始端点*/
b = 7; /*区间终止端点*/
h = 0.1; /*步长*/
eps = 1.e-8; /*要求达到的精度*/
BisectRoot(a,b,h,eps,x,n,&m); /*调用二分法函数*/
printf("y=sin(x)在范围%2.0f和%2.0f之间的根有%d个根\n",a,b,m);
printf("它们分别是:\n");
for(i = 0;i<n;i++)
printf("x[%d] = %e\n",i,x[i]);
free(x); /*释放内存空间*/
}
double Func(double x)
{
return(sin(x));
}
int BisectRoot(a,b,h,eps,x,n,m)
double a; /*实型变量,输入参数,求根区间的起始端点*/
double b; /*实型变量,输入参数,求根区间的终止端点*/
double h; /*利用逐步扫描法确定根位置时的步长*/
double eps; /*实型变量,输入参数,控制精度的参数*/
double *x; /*实型一维数组,输出参数,存放计算得到的数组*/
int n; /*输入参数,区间内方程根的个数的预估值*/
int *m; /*输出参数,实际求得的根的个数*/
{
double z,z0,z1,y,y0,y1;
*m = 0;
z = a;
y = Func(z);
while(1) /*无限循环,直到遇到return或者break语句*/
{/*如果逐步扫描到求根区间的右端点或者得到的根的个数达到预估根的个数*/
if((z>b+h/2)||(*m==n))
return(1);
if(fabs(y)<eps) /*如果当前根z对应的函数f(z)满足精度要求*/
{
*m+=1;
x[*m-1] = z; /*将此时的z值赋值给x数组*/
z+=h/2;
y = Func(z);
continue; /*结束本次循环,即跳过循环体中下面尚未执行
的语句接着进行下一次是否执行循环的判定*/
}
z1 = z+h; /*逐步扫描中小区间的右端点*/
y1 = Func(z1); /*小区间右端点对应的函数值*/
if(fabs(y1)<eps) /*如果右端点恰好满足根的精度要求*/
{
*m+=1;
x[*m-1] = z1;
z = z1+h/2;
y = Func(z);
continue;
}
if(y*y1>0) /*如果对应根乘积大于零,说明该区间内没有根*/
{
y = y1;
z = z1;
continue;
}
while(1) /*如果本while循环执行,说明逐步扫描小区建z和z1间有根*/
{
if(fabs(z1-z)<eps) /*如果满足精度要求*/
{
*m+=1;
x[*m-1]=(z1+z)/2;
z = z1+h/2;
y = Func(z);
break;
}
z0 = (z1+z)/2; /*二分发求根公式*/
y0 = Func(z0);
if(fabs(y0)<eps)
{
*m = *m+1;
x[*m-1] = z0;
z =z0+h/2;
y = Func(z);
break;
}
if(y*y0<0) /*如果乘积小于零,说明根在z和z0之间*/
{
z1 = z0;
y1 = y0;
}
else /*否则根在z0和z1之间*/
{
z = z0;
y = y0;
}
}
}
}
牛顿迭代法:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int Function(double,double *,double *);
int Newton(double *,double,int);
int Function(x,f,dy)
double x;
double *f;
double *dy;
{
*f = x*x*(x-1)-1;
*dy = 3*x*x-2*x;
return(1);
}
int Newton(x,eps,l)
double *x;
double eps;
int l;
{
double f,dy,x1;
Function(*x,&f,&dy);
A: if(fabs(dy) == 0)
{
l = 0;
return (0);
}
x1=*x-f/dy;
Function(x1,&f,&dy);
if(fabs(x1-*x)>=eps||fabs(f)>=eps)
{
l-=1;
*x=x1;
if(l==0)
return(1);
goto A;
}
*x = x1;
return 1;
}
void main()
{
double x,eps;
int l;
eps=1.e-6;
x=1.5;
l=60;
if(!Newton(&x,eps,l))
{
printf("该函数不可以用牛顿跌代法求根!\n");
}
printf("利用牛顿跌代法求的的根为:\n");
printf("x=%.10f\n",x);
}
弦节法:
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>
double Func(double);
int BowRoot(double,double,double,double,double *,int,int *);
void main()
{
int i,n,m;
double a,b,h,eps,*x;
n = 3; /*方程根的个数的预估值*/
x = (double*)calloc(n,sizeof(double)); /*开辟内存空间*/
if(x == NULL)
{
printf("内存分配失败\n");
exit(1);
}
a = -3; /*区间起始端点*/
b = 5; /*区间终止端点*/
h = 1; /*步长*/
eps = 1.e-8; /*要求达到的精度*/
BowRoot(a,b,h,eps,x,n,&m); /*调用二分法函数*/
printf("函数f(x)在范围%2.0f和%2.0f之间的根有%d个根\n",a,b,m);
printf("它们分别是:\n");
for(i = 0;i<n;i++)
printf("x[%d] = %e\n",i,x[i]);
free(x); /*释放内存空间*/
}
double Func(double x)
{
return (x*x*x-3*x*x-6*x+8);
}
int BowRoot(a,b,h,eps,x,n,m)
double a; /*实型变量,输入参数,求根区间的起始端点*/
double b; /*实型变量,输入参数,求根区间的终止端点*/
double h; /*利用逐步扫描法确定根位置时的步长*/
double eps; /*实型变量,输入参数,控制精度的参数*/
double *x; /*实型一维数组,输出参数,存放计算得到的数组*/
int n; /*输入参数,区间内方程根的个数的预估值*/
int *m; /*输出参数,实际求得的根的个数*/
{
double z,z1,z2,y,y1,y2;
*m = 0;
z = a;
y = Func(z);
while(1) /*无限循环,直到遇到return或者break语句*/
{/*如果逐步扫描到求根区间的右端点或者得到的根的个数达到预估根的个数*/
if((z>b+h/2)||(*m==n))
return(1);
if(fabs(y)<eps) /*如果当前根z对应的函数f(z)满足精度要求*/
{
*m+=1;
x[*m-1] = z; /*将此时的z值赋值给x数组*/
z+=h/2;
y = Func(z);
continue; /*结束本次循环,即跳过循环体中下面尚未执行
的语句接着进行下一次是否执行循环的判定*/
}
z1 = z+h; /*逐步扫描中小区间的右端点*/
y1 = Func(z1); /*小区间右端点对应的函数值*/
if(fabs(y1)<eps) /*如果右端点恰好满足根的精度要求*/
{
*m+=1;
x[*m-1] = z1;
z = z1+h/2;
y = Func(z);
continue;
}
if(y*y1>0) /*如果对应根乘积大于零,说明该区间内没有根*/
{
y = y1;
z = z1;
continue;
}
while(1) /*如果本while循环执行,说明逐步扫描小区建z和z1间有根*/
{
if(fabs(z1-z)<eps) /*如果满足精度要求*/
{
*m+=1;
x[*m-1]=(z1+z)/2;
z = z1+h/2;
y = Func(z);
break;
}
y1 = Func(z1); /*弦截法公式*/
y = Func(z);
z2=z1-(y1/(y1-y))*(z1-z);
y2 = Func(z2);
if(fabs(y2)<eps)
{
*m = *m+1;
x[*m-1] = z2;
z =z2+h/2;
y = Func(z);
break;
}
if(y*y2<0) /*如果乘积小于零,说明根在z和z0之间*/
{
z1 = z2;
y1 = y2;
}
else /*否则根在z0和z1之间*/
{
z = z2;
y = y2;
}
}
}
}
发表于 2005-09-17 13:19 yingkou的Blog 阅读(2161) | 评论 (7) | 编辑 收藏
#include <stdio.h>
#include <string.h>
bubble(strings,count)
char *strings;
int count;
{
register int m,n;
register char s;
for(m = 1;m<count;m++)
for(n = count-1;n >= m;--n)
{
if(strings[n-1]>strings[n])
{
s = strings[n-1];
strings[n-1] = strings[n];
strings[n] = s;
}
}
}
int main(void)
{
int count;
char str[200];
printf("请输入字符串:\n");
gets(str);
count = strlen(str);
bubble(str,count);
printf("排序之后的字符串是:\n");
printf("%s.\n",str);
return 0;
}
堆排序:
#include <stdio.h>
#define MARK 0
static a[8] = {MARK,25,4,36,1,60,10,58,};
int count = 1;
void heap(int n);
void adjust(int i,int n);
int main(void)
{
int i;
printf("源数据为:");
for(i = 1;i<8;i++)
printf("%5d",a[i]);
heap(7);
printf("\n排序后的数据为:");
for(i = 1;i<8;i++)
printf("%5d",a[i]);
printf("\n");
return 0;
}
void heap(n)
int n;
{
int i,j,t;
for(i =n/2;i>0;i--)
adjust(i,n);
printf("\n初始化成堆===> ");
for(i = 1;i < 8;i++)
printf("%5d",a[i]);
for(i = n-1;i>0;i--)
{
t = a[i+1];
a[i+1] = a[1];
a[1] = t;
adjust(1,i);
printf("\n第%2d步操作结果===>",count++);
for(j = 1;j<8;j++)
printf("%5d",a[j]);
}
}
void adjust(i,n)
int i,n;
{
int j,k,r,done=0;
k = r = a[i];
j = 2*i;
while((j<=n)&&(done==0))
{
if(j<n)
{
if(a[j]<a[j+1])
j++;
}
if(k>=a[j])
done = 1;
else
{
a[j/2] = a[j];
j = 2* j;
}
}
a[j/2] = r;
}
文件排序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
struct data
{
char name[20];
char school[20];
char city[20];
char province[20];
}info;
struct data addrs[NUM]=
{
"WenHai","BIT","JiLin","JiLin",
"TongWei","BIT","ZhengJiang","JiangSu",
"SunYou","BIT","WeiFang","ShangDong",
"XiaoMing","PKU","TaiYuan","ShanXi"
};
/*对后面要用到的函数进行声明*/
void quick_disk(FILE *fp,int count);
void qs_disk(FILE *fp,int left,int right);
void exchangedata(FILE *fp,long i, long j);
char *get_name(FILE *fp, long rec);
void print_data(struct data *p);
struct data *get_data(FILE *fp,long rec);
int main(void)
{
int i;
FILE *fp; /*文件指针*/
/*以读写方式生成文本文件fp*/
if((fp = fopen("datalist.txt","w+")) == NULL)
{
printf("打开文件失败\n");
exit(1);
}
printf("将未排序的数据写入文件\n");
/*将数组Sdata[NUM]写入文件中*/
fwrite(addrs,sizeof(addrs),1,fp);
/*将文件中的数组Sdata[NUM]依次取出并打印*/
for(i=0;i<NUM;i++)
{
struct data *p;
p = get_data(fp,i); /*得到Sdata[i]的指针*/
print_data(p); /*将结构体Sdata[i]各个成员变量打印出*/
printf("\n");
}
fclose(fp); /*关闭文件指针*/
/*以二进制方式再次打开文件datalist.txt*/
if((fp=fopen("datalist.txt","rb+"))==NULL)
{
printf("不能以读写方式打开文件\n");
exit(1);
}
printf("将文件数据排序\n");
/*调用字符串排序函数将文本中的字符串结构体排序*/
quick_disk(fp,NUM);
printf("排序结束\n");
/*将排序结束后的数组数据打印出来*/
for(i=0;i<4;i++)
{
struct data *p;
p = get_data(fp,i);
print_data(p);
printf("\n");
}
fclose(fp);
return 0;
}
/*应用快速排序方法对字符串进行排序*/
void quick_disk(FILE *fp,int count)
{
qs_disk(fp,0,count-1);
}
/*排序函数*/
void qs_disk(FILE *fp,int left,int right)
{
long int i,j;
char x[30];
i = left;
j = right;
/*比较字符串x为Sdata数组中间一个结构变量的name成员变量*/
strcpy(x,get_name(fp,(long)(i+j)/2));
do
{ /*比较当前结构变量的name成员变量于比较字符串x的大小*/
while((strcmp(get_name(fp,i),x)<0)&&(i<right))
i++;
while((strcmp(get_name(fp,j),x)>0)&&(j>left))
j--;
if(i<=j)
{
exchangedata(fp,i,j); /*交换i和j对应的数据*/
i++;
j--;
}
}while(i<j);
if(left<j) /*反复调用此排序函数,直到j达到数据的最左端*/
qs_disk(fp,left,(int)j);
if(i<right) /*反复调用此排序函数,直到i达到数据的最右端*/
qs_disk(fp,(int)i,right);
}
/*交换i和j在文件中对应的数据*/
void exchangedata(FILE *fp,long i,long j)
{
char a[sizeof(info)],b[sizeof(info)];
fseek(fp,sizeof(info)*i,SEEK_SET); /*找到i在文件中对应的数据位置*/
fread(a,sizeof(info),1,fp); /*将该位置数据读到字符串数组a中*/
fseek(fp,sizeof(info)*j,SEEK_SET); /*找到j在文件中对应的数据位置*/
fread(b,sizeof(info),1,fp); /*将该位置数据读到字符串数组b中*/
fseek(fp,sizeof(info)*j,SEEK_SET); /*找到j在文件中对应的数据位置*/
fwrite(a,sizeof(info),1,fp); /*将刚才读入a中的数据写入到该位置*/
fseek(fp,sizeof(info)*i,SEEK_SET); /*找到i在文件中对应的数据位置*/
fwrite(b,sizeof(info),1,fp); /*将刚才读入b中的数据写入到该位置*/
/*完成文件中i和j处对应数据的交换*/
}
/*得到文件fp中变量rec对应的结构体变量的name成员变量*/
char *get_name(FILE *fp,long rec)
{
struct data *p;
p = &info;
rewind(fp);
/*找到该结构体变量得的位置*/
fseek(fp,rec*sizeof(struct data),SEEK_SET);
/*将其读到指针p*/
fread(p,sizeof(struct data),1L,fp);
return p->name; /*返回该结构体变量的name成员变量*/
}
/*得到文件fp中变量rec对应的结构体变量的指针*/
struct data *get_data(FILE *fp,long rec)
{
struct data *p;
p = &info;
rewind(fp);
/*找到该结构体变量得的位置*/
fseek(fp,rec*sizeof(info),SEEK_SET);
/*将其读到指针p*/
fread(p,sizeof(info),1,fp);
return p; /*返回该结构体指针*/
}
/*将指针p对应的结构体的各个成员变量打印出来*/
void print_data(struct data *p)
{
printf("姓名:%s\n",p->name);
printf("学校:%s\n",p->school);
printf("城市:%s\n",p->city);
printf("省 :%s\n",p->province);
}
链表搜索:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
struct chain
{
char name[20];
char city[20];
char sex[10];
char age[10];
char job[10];
struct chain *next;
};
struct chain *create();
struct chain *SequelSeach(head,name);
void print_data(point);
struct chain Datas[NUM]=
{
"Sun","Weifang","Male","24","student",NULL,
"Tom","Beijing","Male","31","doctor",NULL,
"Marry","Shanghai","Female","19","techer",NULL,
"Willing","Tianjing","Female","21","worker",NULL
};
int main(void)
{
struct chain *head;
struct chain *p;
char name[30];
head = create();
printf("请输入要查找的人名\n");
scanf("%s",name);
p = SequelSeach(head,name);
print_data(p);
return 0;
}
struct chain *create()
{
struct chain *head, *tail, *p;
int i;
head = tail = NULL;
printf("将名单数据输入到链表中:\n");
for(i= 0;i < NUM; i++)
{
p = (struct chain *)malloc (sizeof (struct chain));
strcpy(p->name, Datas[i].name);
strcpy(p->city,Datas[i].city);
strcpy(p->sex,Datas[i].sex);
strcpy(p->age,Datas[i].age);
strcpy(p->job,Datas[i].job);
p->next = NULL;
if(head == NULL)
head = tail = p;
else
tail = tail ->next;
tail ->next = p;
}
return head;
}
struct chain *SequelSeach(head,name)
struct chain *head;
char name[];
{
struct chain *temp;
temp = head;
for(temp=head;temp!=NULL;)
{
if(strcmp(temp->name,name) == 0)
break;
else
temp = temp->next;
}
if(temp ==NULL)
printf("没有查找到该人资料\n");
return temp;
}
void print_data(point)
struct chain *point;
{
if(point ==NULL)
return;
printf("查找结果:\n");
printf(" 姓名:%s\n",point->name);
printf(" 城市:%s\n",point->city);
printf(" 性别:%s\n",point->sex);
printf(" 年龄:%s\n",point->age);
printf(" 工作:%s\n",point->job);
}
二分法查找:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
struct Data
{
char name[20];
char city[20];
char sex[10];
char age[10];
char job[10];
};
struct Data SDatas[NUM]=
{
"Sun","Weifang","Male","24","student",
"Tom","Beijing","Male","31","doctor",
"Marry","Shanghai","Female","19","techer",
"Willing","Tianjing","Female","21","worker"
};
void qs_struct(items,left,right);
void quick_struct(items,count);
int BinarySeach(items,name,n);
void print_data(point);
int main(void)
{
char name[30];
int i;
printf("将原始数据排序\n");
quick_struct(SDatas,NUM);
printf("请输入要查找人的名字:\n");
scanf("%s",name);
i = BinarySeach(SDatas,name,NUM);
if(i == -1)
{
printf("没有查找到该人信息\n");
return 0;
}
printf("查找结果:\n");
print_data(&SDatas[i]);
return 1;
}
void quick_struct(items,count)
struct Data items[];
int count;
{
qs_struct(items,0,count-1);
}
void qs_struct(items,left,right)
struct Data items[];
int left;
int right;
{
register int i,j;
char *x;
struct Data temp;
i = left;
j = right;
x = items[(left+right/2)].name;
do
{
while((strcmp(items[i].name,x)<0)&&(i<right))
i++;
while((strcmp(items[j].name,x)>0)&&(j>left))
j--;
if(i<=j)
{
temp = items[i];
items[i] = items[j];
items[j] = temp;
i++;
j--;
}
}while(i<=j);
if(left<j)
qs_struct(items,left,j);
if(i<right)
qs_struct(items,i,right);
}
int BinarySeach(items,name,n)
struct Data items[];
char name[];
int n;
{
int low,high,mid;
low = 0;
high = n-1;
while(low<=high)
{
mid = (low+high)/2;
if((strcmp(items[mid].name,name)==0))
return mid;
else if((strcmp(items[mid].name,name)<0))
low = mid+1;
else high = mid-1;
}
return -1;
}
void print_data(point)
struct Data *point;
{
if(point ==NULL)
return;
printf(" 姓名:%s\n",point->name);
printf(" 城市:%s\n",point->city);
printf(" 性别:%s\n",point->sex);
printf(" 年龄:%s\n",point->age);
printf(" 工作:%s\n",point->job);
}
树查找:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
struct tree
{
char name[20];
char city[20];
char sex[10];
char age[10];
char job[10];
struct tree *left;
struct tree *right;
};
struct tree Datas[NUM]=
{
"Willing","Tianjing","Female","21","worker",NULL,NULL,
"Tom","Beijing","Male","31","doctor",NULL,NULL,
"Sun","Weifang","Male","24","student",NULL,NULL,
"Marry","Shanghai","Female","19","techer",NULL,NULL
};
struct tree *construct(
struct tree *root,
struct tree *r,
struct tree *Data)
{
if(!r)
{
r = (struct tree *)malloc(sizeof(struct tree));
if(!r)
{
printf("内存分配失败!");
exit(0);
}
r->left = NULL;
r->right = NULL;
strcpy(r->name,Data->name);
strcpy(r->city,Data->city);
strcpy(r->sex,Data->sex);
strcpy(r->age,Data->age);
strcpy(r->job,Data->job);
if(!root)
return r;
if(strcmp(Data->name,root->name)<0)
root->left = r;
else
root->right = r;
return r;
}
if(strcmp(Data->name,r->name)<0)
construct(r,r->left,Data);
else
construct(r,r->right,Data);
return root;
}
struct tree *Search(root,name)
struct tree *root;
char name[];
{
struct tree *p;
if(root == NULL)
printf("该树为空\n");
p = root;
while(strcmp(p->name,name)!=0)
{
if(strcmp(p->name,name)>0)
p = p->left;
else
p = p->right;
if(p == NULL)
break;
}
return(p);
}
void print(struct tree *r)
{
if(!r)
return;
print(r->left);
printf("%s\n",r->name);
print(r->right);
}
void print_currentData(struct tree *point)
{
if(point == NULL)
return;
printf(" 姓名:%s\n",point->name);
printf(" 城市:%s\n",point->city);
printf(" 性别:%s\n",point->sex);
printf(" 年龄:%s\n",point->age);
printf(" 工作:%s\n",point->job);
}
int main(void)
{
int i;
char c[10];
char swap[20];
char name[20];
struct tree *root,*p;
struct tree *temp;
p = NULL;
temp = NULL;
root = NULL;
for(i = 0;i<NUM;i++)
root =construct(root,root,&Datas[i]);
printf("现有人员资料:\n");
print(root);
printf("请输入要查找的人的名字\n");
scanf("%s",name);
p = Search(root,name);
if(p == NULL)
{
printf("没有该人资料\n");
printf("是否要插入该人资料[y/n]\n");
scanf("%s",c);
if(strcmp(c,"y")==0)
{
temp = (struct tree *)malloc(sizeof(struct tree));
if(!temp)
{
printf("内存分配失败!");
exit(0);
}
printf("请输入该人姓名:\n");
scanf("%s",swap);
strcpy(temp->name,swap);
printf("请输入该人所在城市:\n");
scanf("%s",swap);
strcpy(temp->city,swap);
printf("请输入该人性别[Male/Female]:\n");
scanf("%s",swap);
strcpy(temp->sex,swap);
printf("请输入该人年龄:\n");
scanf("%s",swap);
strcpy(temp->age,swap);
printf("请输入该人工作:\n");
scanf("%s",swap);
strcpy(temp->job,swap);
temp->left = NULL;
temp->right = NULL;
root =construct(root,root,temp);
print_currentData(temp);
printf("现有人员资料:\n");
root = root;
print(root);
}
else
return 0;
}
print_currentData(p);
return 1;
}
发表于 2005-09-17 13:18 yingkou的Blog 阅读(1094) | 评论 (0) | 编辑 收藏
上回说了一点C的基础知识,这回说说基本的数据结构:
链表:
#include <stdio.h>
#include <stdlib.h>
struct chain
{
int value;
struct chain *next;
};
struct chain *create()
{
struct chain *head, *tail, *p;
int x,i;
head = tail = NULL;
printf("请输入四个整型数据,然后回车:\n");
for(i= 0;i < 4; i++)
{
scanf("%d",&x);
p = (struct chain *)malloc (sizeof (struct chain));
p->value = x;
p->next = NULL;
if(head == NULL)
head = tail = p;
else
tail = tail ->next = p;
}
return head;
}
struct chain *inlink(head,a,b)
struct chain *head;
int a, b;
{
struct chain *p, *q, *s;
s = (struct chain *)malloc(sizeof(struct chain));
s->value = b;
/*空表插入*/
if(head == NULL)
{
head = s;
s->next = NULL;
}
/*插入s结点作为新表头*/
if(head->value == a)
{
s->next = head;
head = s;
}
else
{
p = head;
/*遍历单链表,寻找数据域值为a的结点*/
while ((p->value != a)&&(p->next != NULL))
{
q = p;
p = p->next;
}
if(p->value == a) //找到数据域为a的结点
{
q->next = s;
s->next = p;
}
/*插入结点s作为表尾*/
else
{
p->next = s;
s->next = NULL;
}
}
return(head);
}
struct chain *dellink(head,a)
struct chain *head;
int a;
{
struct chain *p,*q;
if(head == NULL)
printf("空链表\n");
else if(head ->value == a)
/*链表的第一个结点即为a结点*/
{
p = head;
head = head->next;
}
else
{
p = head;
while ((p->value != a)&&(p->next != NULL))
/*在链表中搜索数据为a的结点*/
{
q = p;
p = p->next;
}
if(p->value != a)
/*在链表中无数据值为a的结点*/
printf("没有要删除的数据 %d\n",a);
else
{
q ->next = p->next;
free(p);
}
}
return(head);
}
void main()
{
struct chain *q,*head;
int a, b;
q = create();
head = q;
while(q) //显示链表
{
printf("%d\n",q->value);
q = q->next;
}
printf("请输入新插入的表元数据位于那个数据之前:");
scanf("%d",&a);
printf("\n 请输入要插入的表元数据: ");
scanf("%d",&b);
q = inlink(head,a,b);
head = q;
while(q) //显示链表
{
printf("%d\n",q->value);
q = q->next;
}
printf("请输入要删除表元的数据: ");
scanf("%d",&a);
q = dellink(head,a);
while(q) //显示链表
{
printf("%d\n",q->value);
q = q->next;
}
}
队列:
#include <stdio.h>
#define Max 100
void SetNull(front, rear)
int *front, *rear;
{
*front = 0;
*rear = 0;
}
int Empty(front,rear)
int *front, *rear;
{
if(*front == *rear)
return(1);
else
return(0);
}
int EnQueue(q,x,front,rear)
int q[];
int x;
int *front,*rear;
{
*rear = (*rear+1) % Max;
if(*front == *rear)
{
printf("队列发生上溢\n");
return(-1);
}
else
{
q[*rear] = x;
return(0);
}
}
int DelQueue(q,y,front,rear)
int q[];
int *y;
int *front,*rear;
{
*front = (*front +1)%Max;
if(*front == *rear)
{
printf("队列发生下溢\n");
return(-1);
}
else
{
*y = q[*front];
return(0);
}
}
void main()
{
int q[Max];
int f = 0, r = 0; /*f和r分别对应队列的头和尾在整个队列存储区域的位置*/
int i,x,m,n;
int a;
SetNull(&f,&r); /*清空队列*/
printf("要输入队列的字符个数:\n");
scanf("%d",&m);
printf("输入队列的整型数据:\n");
for (i=0; i<m; i++)
{
i=i;
scanf("%d",&x);
a = EnQueue(q,x,&f,&r);
if(a == -1)
break;
}
printf("要提出队列的字符个数:");
scanf("%d",&n);
printf("输出从队列中提取的数据:\n");
for (i = 0; i<n; i++)
{
if(DelQueue(q,&x,&f,&r) == -1)
break;
printf("%d\n",x);
}
if(Empty(&f,&r) == 1)
printf("队列为空");
else
printf("队列中还有%d个数据",(m-n));
}
栈:
#include <stdio.h>
#include <stdlib.h>
#define Max 100
int *p;
int *tos;
int *bos;
/*添加一个数据放到堆栈对顶端*/
void push(int i)
{
if(p > bos)
{
printf("堆栈以满\n");
return;
}
*p = i;
p++;
}
/*丛堆栈顶端取出一个数据*/
int pop(void)
{
p--;
if(p < tos)
{
printf("堆栈下溢\n");
return 0;
}
return *p;
}
void main(void)
{
int a,b;
char s[80];
p = (int *)malloc(Max*sizeof(int));
if(!p)
{
printf("分配内存失败");
exit(1);
}
tos = p;
bos = p + Max -1;
printf("请输入第一个数据:\n");
scanf("%d",&a);
push(a);
printf("请输入第二个数据:\n");
scanf("%d",&b);
push(b);
printf("请输入操作符:\n");
scanf("%s",s);
switch (*s)
{
case '+':
a = pop();
b = pop();
printf("结果是a+b = %d\n",(a+b));
push(a+b);
break;
case '-':
a = pop();
b = pop();
printf("结果是a-b = %d\n",(a-b));
push(a-b);
break;
case '*':
a = pop();
b = pop();
printf("结果是a*b = %d\n",(a*b));
push(a*b);
break;
case '/':
a = pop();
b = pop();
printf("结果是a/b = %d\n",(a/b));
push(a/b);
break;
default:
printf("请输入正确操作符\n");
}
}
字符串:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct node
{
char Data[80];
struct node *Next;
}nodetype;
typedef struct head
{
int Num; /*行号*/
int Len; /*改行字符的个数*/
nodetype *Next;
}headtype;
headtype Head[MAX];
void Initial();
int MenuSelect();
void EnterData();
void DeleteLine();
void List();
void ClearBuffer();
main()
{
char choice;
Initial();
while(1)
{
choice = MenuSelect();
switch (choice)
{
case 1:EnterData();
break;
case 2:DeleteLine();
break;
case 3:List();
break;
case 4:exit(0);
}
}
}
void ClearBuffer()
{
while(getchar()!='\n');
}
void Initial()
{
int i;
for(i=0;i<MAX;i++)
{
Head[i].Len=0;
}
}
int MenuSelect()
{
int i;
i=0;
printf(" 1. Enter\n");
printf(" 2. Delete\n");
printf(" 3. List\n");
printf(" 4. Exit\n");
while(i<=0||i>4)
{
printf("请输入菜单选择号\n");
scanf("%d",&i);
ClearBuffer();
}
return(i);
}
void EnterData()
{
nodetype *p,*find();
int i,j,m,LineNumber,k;
char StrBuffer[100];
while(1)
{
printf("输入数据要插入的行号(0~100):\n");
scanf("%d",&LineNumber);
ClearBuffer();
if(LineNumber<0||LineNumber>=MAX)
return;
printf("请输入要插入的数据,以@作为结束符号\n");
i=LineNumber;
Head[i].Num=LineNumber;
Head[i].Next=(nodetype *)malloc(sizeof(nodetype));
p=Head[i].Next;
m=1;
j=-1;
StrBuffer[0]=0;
k=0;
do
{
j++;
if(!StrBuffer[k])
{
scanf("%s",StrBuffer);
k=0;
}
if(j>=80*m)
{
m++;
p->Next=(nodetype *)malloc(sizeof(nodetype));
p=p->Next;
}
p->Data[j%80] = StrBuffer[k++];
}while(p->Data[j%80]!='@');
Head[i].Len = j;
}
}
void DeleteLine()
{
nodetype *p,*q;
int i,j,m,LineNumber;
while(1)
{
printf("输入要删除的行号(0~100):\n");
scanf("%d",&LineNumber);
if(LineNumber<0||LineNumber>=MAX)
return;
i = LineNumber;
p=Head[i].Next;
m=0;
if(Head[i].Len>0)
{
m=(Head[i].Len-1)/80+1; /*查找改行用到几个链表结点*/
}
for(j=0;j<m;j++)
{
q=p->Next;
free(p);
p=q;
}
Head[i].Len=0;
Head[i].Num=0;
}
}
void List()
{
nodetype *p;
int i,j,m,n;
for(i=0;i<MAX;i++)
{
if(Head[i].Len>0)
{
printf("第%d行有数据,它们是:\n",Head[i].Num);
n=Head[i].Len;
m=1;
p=Head[i].Next;
for(j=0;j<n;j++)
if(j>=80*m)
{
p=p->Next;
m++;
}
else
printf("%c",p->Data[j%80]);
printf("\n");
}
}
printf("\n");
}
树:
#include <stdio.h>
#include <stdlib.h>
struct tree
{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root; /*树的第一个结点*/
struct tree *construct(struct tree *root, struct tree *r, char info);
void print(struct tree *r, int l);
int main(void)
{
char s[80];
root = NULL;
do
{
printf("请输入一个字符:");
gets(s);
root = construct(root,root,*s);
}while(*s);
print(root,0);
return 0;
}
struct tree *construct(
struct tree *root,
struct tree *r,
char info)
{
if(!r)
{
r = (struct tree *)malloc(sizeof(struct tree));
if(!r)
{
printf("内存分配失败!");
exit(0);
}
r->left = NULL;
r->right = NULL;
r->info = info;
if(!root)
return r;
if(info < root->info)
root->left = r;
else
root->right = r;
return r;
}
if(info < r->info)
construct(r,r->left,info);
else
construct(r,r->right,info);
return root;
}
void print(struct tree *r, int l)
{
int i;
if(!r)
return;
print(r->left,l+1);
for(i = 0;i < l;++i)
printf(" ");
printf("%c\n",r->info);
print(r->right,l+1);
}