这里给大家提供一种适合新手的C语言四则运算的实现(应用中缀转后缀、链栈)。
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
typedef struct node{
int data;
struct node *next;
}LinkStack;
typedef struct NODE{
char sign;
struct NODE *next;
}linkstack;
/建立空栈/
LinkStack *InitStack()
{
LinkStack s;
s=(LinkStack)malloc(sizeof(LinkStack));
s->next=NULL;//栈为空的标志
return s;
}
linkstack *initstack()
{
linkstack s;
s=(linkstack)malloc(sizeof(linkstack));
s->next=NULL;
return s;
}
/判断栈是否为空/
bool Empty(LinkStack *s)
{
return (s->nextNULL);
}
bool empty(linkstack *s)
{
return(s->nextNULL);
}
/入栈/
void Push(LinkStack *s,int element)
{
LinkStack p;
p=(LinkStack)malloc(sizeof(LinkStack));
p->data=element;
p->next=s->next;
s->next=p;
}
void Push2(linkstack *s,char element)
{
linkstack p;
p=(linkstack)malloc(sizeof(linkstack));
p->sign=element;
p->next=s->next;
s->next=p;
}
char Pop2(linkstack *s)
{
char e;
linkstack *p=s->next;
if(s->nextNULL){
printf(“栈为空!\n”);
return 0;
}else{
e=p->sign;
s->next=p->next;//栈顶位置变化
free§;
}
return e;
}
/出栈/
int Pop(LinkStack *s)
{
int e;
LinkStack *p=s->next;
if(s->nextNULL){
printf(“栈为空!\n”);
return 0;
}else{
e=p->data;
s->next=p->next;//栈顶位置变化
free§;
}
return e;
}
char GetTop(linkstack s)//取栈顶元素(但不出栈)
{
if(s->nextNULL){
return;
}else{
return s->next->sign;
}
}
int Prio(char op)//比较算术符号的优先级
{
int priority;
if(op’’||op==’/’){
priority=2;
}else if(op==’+’||op==’-’){
priority=1;
}else if(op==’(’){
priority=0;
}
return priority;
}
void Middle_Transform_Behind(char a,int length)//将中缀表达式转化为后缀表达式
{
char s,x;
int pr;
linkstack head=initstack();
int number=0,count=0;
for(int i=0;i<length;i++){
if((a+i)>=‘1’&&(a+i)<=‘9’){
for(int j=i;(a+j)>=‘0’&&(a+j)<=‘9’;j++){
count++;
number=number10+(a+j)-‘0’;
}
if(count!=1){
i+=count-1;
}
printf("%d “,number);
number=0;count=0;
}else if((a+i)==’+’||(a+i)’-’||*(a+i)’’||(a+i)’/’||*(a+i)’(’){
if(empty(head)){
Push2(head,a[i]);
}else if((a+i)==’’||(a+i)==’/’){
Push2(head,a[i]);
}else if((a+i)’+’||*(a+i)’-’){
x=GetTop(head);
while(Prio(x)>=1){
s=Pop2(head);
printf(”%c “,s);
x=GetTop(head);
}
Push2(head,(a+i));
}else if((a+i)’(’){
Push2(head,(a+i));
}
}else if((a+i)’)’){
s=Pop2(head);
while(s!=’(’&&!empty(head)){
if((s==’+’||s==’-’||s==’*’||s==’/’)&&!empty(head)){
printf(”%c “,s);
}
s=Pop2(head);
}
}
}
while(!empty(head)){ //最后输出栈中所有符号
s=Pop2(head);
printf(”%c “,s);
}
printf(”\n");
}
int main(int argc,char const argv[])
{
int len1;
char array[101];
printf(“请输入一串四则运算表达式:”);
gets(array);
printf("********************转化为后缀表达式为:\n");
len1=strlen(array);
Middle_Transform_Behind(array,len1);
char arr[101];
printf(“请输入一串四则运算的后缀表达式:”);
gets(arr);
LinkStack *top=InitStack();
int len=strlen(arr);
int num=0,x,y;
int cot=0,count=0;
for(int i=0;i<len;i++){
if(arr[i]>='1'&&arr[i]<='9'){
for(int j=i;arr[j]>='0'&&arr[j]<='9';j++){
count++;
cot=num*10;
num=cot+arr[j]-48;
}
if(count!=1){
i+=count-1;
}
Push(top,num);
num=0,count=0;
}else if((arr[i]=='+'||arr[i]=='-'||arr[i]=='*'||arr[i]=='/')&&!Empty(top)){
x=Pop(top);
y=Pop(top);
if(y==0){
printf("输入的表达式有误!");
}else{
switch(arr[i])
{
case '+':
num=x+y;
Push(top,num);num=0;break;
case '-':
num=y-x;
Push(top,num);num=0;break;
case '*':
num=x*y;
Push(top,num);num=0;break;
case '/':
num=y/x;
Push(top,num);num=0;break;
default:
continue;break;
}
}
}
}
int final_result=Pop(top);
printf("最终结果为:%d",final_result);
}