求一个比较复杂的C语言程序

四五十行的,不要在教科书上找,急需,跪求!
2025-02-25 20:59:20
推荐回答(3个)
回答1:

#include "stdio.h"
#include "ctype.h"
#include "malloc.h"
#include "stdlib.h"
#define M 100
#define ZERO 0
#define SUCC 1
#define DEFT 0
#define MIN -1
#define MAX 2001
typedef int valuetype;
typedef struct Bnode
{
valuetype data;
int layer ;
struct Bnode *Lson,*Rson;
}Bnode,*Bptr;

void writeT(Bptr root)
{
int first=0,last=1;
Bptr p,q[M];
if(root->data==MIN)p=root->Rson;
else p=root->Lson;
if(p==NULL)
{ printf(" 当前二叉树为空,没有结点。\n");return;}
printf(" 当前二叉树的结点为:\n");
printf(" 层号 当前结点 左儿子 右儿子\n");
p->layer=1;
q[0]=p;
while(first!=last)
{
p=q[first++];
printf("%6d%10d ",p->layer,p->data);
if(p->Lson==NULL)printf("%12c",'\040');
else
{
printf("%12d",p->Lson->data);
p->Lson->layer=p->layer+1;
q[last++]=p->Lson;
}
if(p->Rson!=NULL)
{
printf("%12d",p->Rson->data);
p->Rson->layer=p->layer+1;
q[last++]=p->Rson;
}
printf("\n");
}
}

void inorder(Bptr p)
{
if(!p)return;
inorder(p->Lson);
printf("%5d",p->data);
inorder(p->Rson);
}

void sortT(Bptr root)
{
if(root->data==MIN) inorder(root->Rson);
else inorder(root->Rson);
printf("\n");
}

Bptr search (valuetype x,Bptr p)
{
while (p!=NULL)
{
if(x==p->data)return p;
if(xdata) p=p->Lson;
else p=p->Rson;
}
return NULL;
}

void searchT(Bptr root)
{
int x;
printf("请输入要查找的结点值x>0,x=");
scanf("%d",&x);
if(search(x,root)==NULL)printf("数中没有%d!\n",x);
else printf("%d 已经找到!\n",x);
}

void insert(valuetype x,Bptr &root)
{
Bptr f,p;
f=NULL;p=root;
while(p!=NULL)
{
if(xdata)f=p,p=p->Lson;
else f=p,p=p->Rson;
}
p=new Bnode;
p->data=x;p->Lson=p->Rson=NULL;
if(f==NULL)root=p;
else
if(x<=f->data)f->Lson=p;
else f->Rson=p;
}

void insertT(Bptr p)
{
int x;
printf("请输入要插入的结点的值x>0,x=");
scanf("%d",&x);
insert(x,p);
printf("%d已经被插入了\n",x);
}

Bptr creatST()
{
Bptr root ;valuetype x;
root =NULL;
printf(" 构造初始检索树,请输入元素序列,元素个数不得超过%d,要求:\n",M);
printf("序列以%d或%d开始,以0结束,元素值均为小于%d的正整数\n",MIN,MAX,MAX);
scanf("%d",&x);
while(x!=ZERO)
{
insert(x,root);
scanf("%d",&x);
}
return root;
}

int deleteST(valuetype x,Bptr root)
{
Bptr f,p,s,r;
for (p=root;;)
{
if(p==NULL)return DEFT;
if (x==p->data)break;
if(xdata)
{
f=p;p=p->Rson;
}
else
{
f=p;p=p->Rson;
}
}
if (p->Rson==NULL)
{
if(p==f->Lson)
f->Lson=p->Rson;
else
f->Rson=p->Lson;
free (p);
return SUCC;
}
s=p->Lson;
if (s->Rson==NULL)
{
p->data=s->data;
p->Lson=s->Lson;
free (s);
return SUCC;
}
r=s->Rson;
while (r->Rson!=NULL)
{
s=r;
r=r->Rson;
}
p->data=r->data;
s->Rson=r->Lson;
free (r);
return SUCC;
}

void deleteT(Bptr root)
{
int x;
printf("请输入要删除的结点值x>0,x=");
scanf("%d",&x);
if(deleteST(x,root))
printf("%d 已经被删除!\n",x);
else
printf(" %d不在树中,无法删除!\n",x);
}

char getalpha()
{
char c;
while(1)
{
c=getchar();
if(isalpha(c))
return c;
}
}

void treeT(Bptr root)
{
char c;
printf(" 对检索树可以进行下列操作:\n");
while (1)
{
printf("请输入操作码:查找F/f 插入I/i 删除D/d 显示P/p 结点排序S/s 终止E/e\nC=");
c=getalpha();
switch(c)
{
case 'f':
case 'F': searchT(root);break;
case 'p':
case 'P': writeT(root);break;
case 'i':
case 'I': insertT(root);break;
case 'd':
case 'D': deleteT(root);break;
case 's':
case 'S': sortT(root);break;
case 'e':
case 'E': writeT(root);return;
default:printf("输入的操作码不正确,请重新输入!\n");
continue;
}
}
}

void main()
{
Bptr root;
root=creatST();
treeT(root);
printf("程序结束,再见!\n");
}

回答2:

#include
#include
#define MAXSIZE 30
#define ioptrset 7
typedef struct optrtype
{
char optr;
int priority;
}optrtype;
optrtype optrset[ioptrset]={'+',1,'-',1,'/',2,'*',2,'^',3,'(',4,')',5};
int trans(const char * exp,char postexp[])
{
optrtype st[MAXSIZE];
int top=-1,i=-1,j,find;
while(*exp)
{
find=0;
for(j=0;j{
if(*exp==optrset[j].optr)
{
find=1;
switch(optrset[j].priority)
{
case 1:
case 2:
case 3:
if(top>-1&&st[top].priority>=optrset[j].priority)
{
while(top>-1&&st[top].optr!='('&&st[top].priority>=optrset[j].priority)
postexp[++i]=st[top--].optr;
}//fall through
case 4:
st[++top]=optrset[j];
break;
case 5:
while(st[top].optr!='(')
postexp[++i]=st[top--].optr;
top--;
break;
}
}
if(find)
break;
}
if(find)
{
exp++;
continue;
}
else if((*exp>='0'&&*exp<='9')||*exp=='.')
{
while((*exp>='0'&&*exp<='9')||*exp=='.')
postexp[++i]=*exp++;
postexp[++i]='#';
continue;
}
else
return 0;
}
while(top>-1)
postexp[++i]=st[top--].optr;
postexp[++i]='\0';
return 1;
}
float calculate(const char* postexp)
{
float st[MAXSIZE];
int top=-1,j;
float a,b,d,k,mulnum;
while(*postexp)
{
if(top>=1)
{
a=st[top];
b=st[top-1];
}
switch(*postexp)
{
case '+':
st[--top]=a+b;
break;
case '-':
st[--top]=b-a;
break;
case '*':
st[--top]=a*b;
break;
case '/':
if(a==0)
return -11111;
st[--top]=b/a;
break;
case '^':
st[--top]=pow(b,a);
break;
case '#':
break;
default:
d=0;
k=0;
mulnum=10;
while((*postexp>='0'&&*postexp<='9')||*postexp=='.')
{
while(*postexp>='0'&&*postexp<='9')
{
d*=mulnum;
d+=*postexp++-'0';
}
if(*postexp=='.')
{
mulnum=0.1;
++postexp;
}
while(*postexp>='0'&&*postexp<='9')
{
k+=*postexp++-'0';
k*=mulnum;
}
}
st[++top]=k+d;
break;
}
postexp++;
}
return st[0];
}
int main(void)
{
char exp[MAXSIZE]="1+2.5^(3*4-5)/5";
// char exp[MAXSIZE]="1+2";
char postexp[MAXSIZE];
trans(exp,postexp);
puts(postexp);
printf("the result is%f",calculate(postexp));
getchar();
}

回答3:

#include
void main()
{
int day;
int mouth;
int year;
int i,j,m,n;
int weekday;
long wantyear;
int sum;
printf("请输入你想查询的年份:");
scanf("%d",&wantyear);
printf("********************************************************\n");
printf(" %d年 \n",wantyear);
printf("________________________________________________________\n");
for(year=1;year<=wantyear;year++)
{
if((year%4==0&&year%100!=0)||year%400==0)
sum+=366;
else sum+=365;
}
weekday=sum%7;
do{
for(mouth=1;mouth<=12;mouth++){
printf("%d月 日 一 二 三 四 五 六\n",mouth);
printf(" ");
// for(i=0;i<=weekday;i++)
// printf(" ");
switch(mouth)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;
case 4:case 6:case 9:case 11:day=30;break;
case 2:if((wantyear%4==0&&wantyear%100!=0)||wantyear%400==0) day=29;
else day=28;
break;
}
//printf(" ");
for(i=0;i<=weekday;i++)
printf(" ");

for(j=1,m=weekday;j<=day;j++)
{ m=m++%7;
if((weekday+j)%7==0){

printf("\n");
printf(" ");}
printf("%6d",j);
}
weekday=m;
printf("\n\n\n\n");
}
}
while(mouth<12);
}