注释这段c语言代码 急急急!!!!
#include<cstdio>#include<malloc.h>#defineNULL0typedefstructnode{chardate;structnode*n...
#include<cstdio>
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;
SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
} // 初始化
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
char GetTop(SNode *top){
return (top->next)->date;
}
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';
}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();
PushOptr(top[0],'#');
top[1]=InitStack();
c=getchar();
while(c!='#'||(GetTop(top[0]))!='#'){
if(!In(c)){
PushOpnd(top[1],c);
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);
switch(r){
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}
}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",result-'0');
}
不好意思 就剩下10分了 展开
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;
SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
} // 初始化
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
char GetTop(SNode *top){
return (top->next)->date;
}
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';
}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();
PushOptr(top[0],'#');
top[1]=InitStack();
c=getchar();
while(c!='#'||(GetTop(top[0]))!='#'){
if(!In(c)){
PushOpnd(top[1],c);
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);
switch(r){
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}
}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",result-'0');
}
不好意思 就剩下10分了 展开
3个回答
展开全部
#include<cstdio>
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;//定义给够体变量;
SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
} // 初始化
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出战元素
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出栈元素
char GetTop(SNode *top){
return (top->next)->date;
}返回栈顶元素
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}//给i赋值
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}//给j赋值
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';
}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}//进行数学运算并返回计算结果
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();//创建栈
PushOptr(top[0],'#');
top[1]=InitStack();//栈底元素#
c=getchar();//输入字符
while(c!='#'||(GetTop(top[0]))!='#')//如果输入不是#或不到栈底
{
if(!In(c))//如果c不是#
{
PushOpnd(top[1],c);//将字符c入栈
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);//比较运算级;
switch(r){//根据优先级进行计算或入栈出栈操作
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}
}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",re
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;//定义给够体变量;
SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
} // 初始化
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出战元素
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出栈元素
char GetTop(SNode *top){
return (top->next)->date;
}返回栈顶元素
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}//给i赋值
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}//给j赋值
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';
}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}//进行数学运算并返回计算结果
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();//创建栈
PushOptr(top[0],'#');
top[1]=InitStack();//栈底元素#
c=getchar();//输入字符
while(c!='#'||(GetTop(top[0]))!='#')//如果输入不是#或不到栈底
{
if(!In(c))//如果c不是#
{
PushOpnd(top[1],c);//将字符c入栈
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);//比较运算级;
switch(r){//根据优先级进行计算或入栈出栈操作
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}
}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",re
展开全部
#include<cstdio>
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;//定义给够体变量;
SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
} // 初始化
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出战元素
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出栈元素
char GetTop(SNode *top){
return (top->next)->date;
}返回栈顶元素
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}//给i赋值
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}//给j赋值
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';
}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}//进行数学运算并返回计算结果
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();//创建栈
PushOptr(top[0],'#');
top[1]=InitStack();//栈底元素#
c=getchar();//输入字符
while(c!='#'||(GetTop(top[0]))!='#')//如果输入不是#或不到栈底
{
if(!In(c))//如果c不是#
{
PushOpnd(top[1],c);//将字符c入栈
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);//比较运算级;
switch(r){//根据优先级进行计算或入栈出栈操作
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}
}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",result-'0');
}
该程序是用栈来进行表达式求值
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;//定义给够体变量;
SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
} // 初始化
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出战元素
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}//入栈操作
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}//出栈操作,并返回出栈元素
char GetTop(SNode *top){
return (top->next)->date;
}返回栈顶元素
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}//给i赋值
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}//给j赋值
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';
}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}//进行数学运算并返回计算结果
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();//创建栈
PushOptr(top[0],'#');
top[1]=InitStack();//栈底元素#
c=getchar();//输入字符
while(c!='#'||(GetTop(top[0]))!='#')//如果输入不是#或不到栈底
{
if(!In(c))//如果c不是#
{
PushOpnd(top[1],c);//将字符c入栈
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);//比较运算级;
switch(r){//根据优先级进行计算或入栈出栈操作
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}
}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",result-'0');
}
该程序是用栈来进行表达式求值
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询