主题:表达式
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef struct{
char *base;
char *top;
int stacksize;
}sqlist;
char Operate(char a,char thea,char b)
{
int c,d,g;
char h;
c=int(a)-48;
d=int(b)-48;
switch(thea)
{
case '+':g=c+d;break;
case '-':g=c-d;break;
case '*':g=c*d;break;
case '/':g=c/d;break;
}
h=char(g);
return h;
}
int InitStack(sqlist &s)
{
s.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s.base)exit(0);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return 1;
}
char GetTop(sqlist s)
{
char e;
if(s.top==s.base)
return 0;
e=*(s.top-1);
return e;
}
int Push(sqlist s,char e)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(char*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(char));
s.top=s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++=e;
return 1;
}
int Pop(sqlist &s,char &e)
{
if(s.top==s.base)
return 0;
e=*--s.top;
return 1;
}
int In(char b)
{
int i;
char c[7]={'(','+','-','*','/',')','#'};
for(i=0;i<7;i++)
if(b==c[i])
break;
if(i>=7) return 1;
else return 0;
}
char Precede(char e,char c)
{
int a,b;
switch(e){
case '+':case '-':a=3;break;
case '/':case '*':a=2;break;
case '(':a=4;break;
case ')':a=1;break;
}
switch(b){
case '+':case '-':b=3;break;
case '/':case '*':b=2;break;
case '(':b=4;break;
case ')':b=1;break;
}
if(abs(a-b)==3||a==b)
return '=';
if(a>b&&abs(a-b)!=3)
return '>';
if(b>a&&abs(a-b)!=3)
return '<';
}
void main()
{
sqlist OPTR,OPND;
char c,a,x,thea,b;
InitStack(OPTR);Push(OPTR,'#'); /*为了方便操作,先添入'#'作为栈的起始符 */
InitStack(OPND); c=getchar();GetTop(OPTR);
while(c!='#'||a!='#'){
if(!In(c)){ /*如果读入的是数字,直接放入数据栈*/
Push(OPND,c);
c=getchar();
}
else{
switch(Precede(GetTop(OPTR),c)){/*比较读入的运算符与OPTR栈顶元素的优先级大小*/
case '<':
{
Push(OPTR,c);
c=getchar();
break;
}/*如果OPTR栈顶元素的优先级小于c,则c直接放入OPTR*/
case '=':
{
Pop(OPTR,x);
c=getchar();
break;
}/*如果相等则说明是左括号和右括号相遇,则抵消*/
case '>':
{
Pop(OPTR,thea);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,thea,b));
break;
}/*OPTR栈顶元素的优先级大于c,取出OPTR的栈顶运算符thea和OPND中的最顶端两个数,进行
thea运算,并将运算结果放到数据栈OPND中.*/
}//end-switch
}//end-else
}//end-while
while(Pop(OPND,a))
{printf("%c",a);}/*读出数据栈OPND中栈顶元素作为最终结果返回*/
}
编译错误:
D:\mycode\cpp6\cpp1.cpp(38) : error C2143: syntax error : missing ')' before ';'
D:\mycode\cpp6\cpp1.cpp(38) : error C2059: syntax error : ')'
D:\mycode\cpp6\cpp1.cpp(38) : error C2100: illegal indirection
D:\mycode\cpp6\cpp1.cpp(59) : error C2143: syntax error : missing ')' before ';'
D:\mycode\cpp6\cpp1.cpp(59) : error C2143: syntax error : missing ')' before ';'
D:\mycode\cpp6\cpp1.cpp(59) : error C2059: syntax error : ')'
D:\mycode\cpp6\cpp1.cpp(59) : error C2059: syntax error : ')'
D:\mycode\cpp6\cpp1.cpp(59) : error C2100: illegal indirection
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(116) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(116) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(122) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(122) : error C2018: unknown character '0xa1'
Error executing cl.exe.
cpp1.exe - 16 error(s), 0 warning(s)
不知道是指什么了,大虾帮帮忙[em10][em10]
#include<stdlib.h>
#include<math.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
typedef struct{
char *base;
char *top;
int stacksize;
}sqlist;
char Operate(char a,char thea,char b)
{
int c,d,g;
char h;
c=int(a)-48;
d=int(b)-48;
switch(thea)
{
case '+':g=c+d;break;
case '-':g=c-d;break;
case '*':g=c*d;break;
case '/':g=c/d;break;
}
h=char(g);
return h;
}
int InitStack(sqlist &s)
{
s.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s.base)exit(0);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return 1;
}
char GetTop(sqlist s)
{
char e;
if(s.top==s.base)
return 0;
e=*(s.top-1);
return e;
}
int Push(sqlist s,char e)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(char*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(char));
s.top=s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++=e;
return 1;
}
int Pop(sqlist &s,char &e)
{
if(s.top==s.base)
return 0;
e=*--s.top;
return 1;
}
int In(char b)
{
int i;
char c[7]={'(','+','-','*','/',')','#'};
for(i=0;i<7;i++)
if(b==c[i])
break;
if(i>=7) return 1;
else return 0;
}
char Precede(char e,char c)
{
int a,b;
switch(e){
case '+':case '-':a=3;break;
case '/':case '*':a=2;break;
case '(':a=4;break;
case ')':a=1;break;
}
switch(b){
case '+':case '-':b=3;break;
case '/':case '*':b=2;break;
case '(':b=4;break;
case ')':b=1;break;
}
if(abs(a-b)==3||a==b)
return '=';
if(a>b&&abs(a-b)!=3)
return '>';
if(b>a&&abs(a-b)!=3)
return '<';
}
void main()
{
sqlist OPTR,OPND;
char c,a,x,thea,b;
InitStack(OPTR);Push(OPTR,'#'); /*为了方便操作,先添入'#'作为栈的起始符 */
InitStack(OPND); c=getchar();GetTop(OPTR);
while(c!='#'||a!='#'){
if(!In(c)){ /*如果读入的是数字,直接放入数据栈*/
Push(OPND,c);
c=getchar();
}
else{
switch(Precede(GetTop(OPTR),c)){/*比较读入的运算符与OPTR栈顶元素的优先级大小*/
case '<':
{
Push(OPTR,c);
c=getchar();
break;
}/*如果OPTR栈顶元素的优先级小于c,则c直接放入OPTR*/
case '=':
{
Pop(OPTR,x);
c=getchar();
break;
}/*如果相等则说明是左括号和右括号相遇,则抵消*/
case '>':
{
Pop(OPTR,thea);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,thea,b));
break;
}/*OPTR栈顶元素的优先级大于c,取出OPTR的栈顶运算符thea和OPND中的最顶端两个数,进行
thea运算,并将运算结果放到数据栈OPND中.*/
}//end-switch
}//end-else
}//end-while
while(Pop(OPND,a))
{printf("%c",a);}/*读出数据栈OPND中栈顶元素作为最终结果返回*/
}
编译错误:
D:\mycode\cpp6\cpp1.cpp(38) : error C2143: syntax error : missing ')' before ';'
D:\mycode\cpp6\cpp1.cpp(38) : error C2059: syntax error : ')'
D:\mycode\cpp6\cpp1.cpp(38) : error C2100: illegal indirection
D:\mycode\cpp6\cpp1.cpp(59) : error C2143: syntax error : missing ')' before ';'
D:\mycode\cpp6\cpp1.cpp(59) : error C2143: syntax error : missing ')' before ';'
D:\mycode\cpp6\cpp1.cpp(59) : error C2059: syntax error : ')'
D:\mycode\cpp6\cpp1.cpp(59) : error C2059: syntax error : ')'
D:\mycode\cpp6\cpp1.cpp(59) : error C2100: illegal indirection
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(113) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(116) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(116) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(122) : error C2018: unknown character '0xa1'
D:\mycode\cpp6\cpp1.cpp(122) : error C2018: unknown character '0xa1'
Error executing cl.exe.
cpp1.exe - 16 error(s), 0 warning(s)
不知道是指什么了,大虾帮帮忙[em10][em10]