C语言——整数型加减乘除计算机:
采用逆波兰表达式,栈,队列结合的方式求解:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct seqstack{ //栈定义
char data[MAXSIZE];
int top;
}seqstack,*wstack;
typedef struct intstack{ //栈定义
int data[MAXSIZE];
int top;
}intstack;
typedef struct seq_que{ //队列定义
char data[MAXSIZE];
int front,rear;
}seq_que;
seqstack* init() //栈的初始化
{
seqstack L;
L=(seqstack) malloc(sizeof(seqstack));
if(L)
L->top=-1;
return L;
}
int empty_stack(seqstack *L){ //栈判空操作
if(L->top==-1)
return 1;
else return 0;
}
int push(seqstack *L,char e){ //入栈
if(L->topMAXSIZE-1){
return 0;
}else{
++L->top;
L->data[L->top]=e;
return 1;
}
}
int pop(seqstack *L,char *e){ //出栈
if(L->top-1){
return 0;
}else{
*e=L->data[L->top];
L->top–;
return 1;
}
}
char Get_top(seqstack *L){ //栈的长度
if(L->top==-1){
return 0;
}else{
return L->data[L->top];
}
}
intstack* inits() //栈的初始化
{
intstack *L;
L=(intstack*) malloc(sizeof(intstack));
if(L)
L->top=-1;
return L;
}
int empty_stacks(intstack *L){ //栈判空操作
if(L->top==-1)
return 1;
else return 0;
}
int pushs(intstack *L,int e){ //入栈
if(L->top==MAXSIZE-1){
return 0;
}else{
++L->top;
L->data[L->top]=e;
return 1;
}
}
int pops(intstack *L,int *e){ //出栈
if(L->top==-1){
return 0;
}else{
*e=L->data[L->top];
L->top--;
return 1;
}
}
char Get_tops(intstack *L){ //栈的长度
if(L->top==-1){
return 0;
}else{
return L->data[L->top];
}
}
seq_que *init_que(){ //队列初始化;
seq_que L;
L=(seq_que)malloc(sizeof(seq_que));
L->front=L->rear=-1;
return L;
}
int In_que(seq_que *L,char e){ //入队操作;
if((L->rear+1)%MAXSIZE==L->front){
printf(“队满”);
return -1; //队满不能入队
}else{
L->rear=(L->rear+1)%MAXSIZE;
L->data[L->rear]=e;
return 1;//入队完成;
}
}
int out_que(seq_que *L,char *e){ //出队操作
if(L->rearL->front){
printf(“队空”);
return -1;
}else{
L->front=(L->front+1)%MAXSIZE;
*e=L->data[L->front];
return 1;
}
}
int empty_que(seq_que *L){ //队列判空操作
if(L->frontL->rear){
return 1;
}else{
return 0;
}
}
int main(){
int i=0,d,m,n;
seqstack *s;
intstack *z;
seq_que *k;
k=init_que();
s=init();
z=inits();
char c,e,str[MAXSIZE];
printf(“请输入中缀表达式,以#结束\n”);
scanf("%c",&c);
while(c!=’#’){
while(c>=‘0’&&c<=‘9’){
In_que(k,c);
scanf("%c",&c);
if(c<‘0’||c>‘9’){
In_que(k,’ ');}
}
if(c==')'){
pop(s,&e);
while(e!='('){
In_que(k,e);
pop(s,&e);
}
}
else if(c=='#'){
break;
}else if(c=='-'||c=='+'){
if(empty_stack(s)){
push(s,c);}
else{
do{
pop(s,&e);
if(e=='(')
push(s,e);
else
In_que(k,e);
}while(!empty_stack(s)&&e!='(');
push(s,c);
}
}
else if(c=='/'||c=='*'||c=='('){
push(s,c);
}else{
printf("输入程序错误,请返回\n");
return -1;
}
scanf("%c",&c);
}
while(!empty_stack(s)){
pop(s,&e);
In_que(k,e);
}
while(k->front!=k->rear){
out_que(k,&e);
while(e>='0'&&e<='9'){
str[i++]=e;
str[i]='\0';
out_que(k,&e);
if(e==' '){
d=atoi(str);
pushs(z,d);
i=0;
break;
}
}
switch(e){
case '+':
pops(z,&m);
pops(z,&n);
pushs(z,m+n);
break;
case '-':
pops(z,&m);
pops(z,&n);
pushs(z,n-m);
break;
case '*':
pops(z,&m);
pops(z,&n);
pushs(z,m*n);
break;
case '/':
pops(z,&m);
pops(z,&n);
if(m!=0){
pushs(z,n/m);}
else {
printf("除数不能为零\n");
return -1;
}
break;
}
}
pops(z,&d);
printf("%d\n",d);
return 0;
}