实现计算出"3+5*2-1+8/2"的算数结果用栈
首先声明:此题我只是实现了单个数的计算(并未判断括号),如果其中存有”33“类似的数则需要另外添加判断数字的终止的条件;并且主要是为了测试链栈的功能(就干放一个链栈有点没意思嘿嘿owo)
首先声明 我功能直接写主函数去了(懒起来了= =)
声明函数
#ifndef __LINKSTACK__
#define __LINKSTACK__
typedef int datatype;
typedef struct node_t
{
datatype data;
struct node_t *next;
}linkstack_t;
typedef struct charnode_t
{
char data;
struct charnode_t *next;
}charstack_t;
void createlinkstack(linkstack_t **);
int insertlinkstack(linkstack_t **,datatype);
int dellinkstack(linkstack_t**);
int isemptylinkstack(linkstack_t*);
int showlinkstack(linkstack_t*);
int lenlinkstack(linkstack_t *);
void clearlink(linkstack_t **);
void createcharstack(charstack_t**);
int insertcharstack(charstack_t**,char );
int delcharstack(charstack_t**);
char showcharstack(charstack_t*);
int isemptycharstack(charstack_t*);
int lencharstack(charstack_t*);
void clearcharstack(charstack_t**);
int jisuan(int ,int ,char);
#endif
函数功能
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"linkstack.h"
void createlinkstack(linkstack_t **p)
{
*p=NULL;
}
int insertlinkstack(linkstack_t **ptop,datatype data)
{
linkstack_t *pnew=NULL;
pnew=(linkstack_t*)malloc(sizeof(linkstack_t));
if(pnew==NULL)
{
puts("malloc 失败");
return -1;
}
pnew->data=data;
pnew->next=NULL;
pnew->next=*ptop;
*ptop=pnew;
return 0;
}
int isemptylinkstack(linkstack_t* top)
{
return top==NULL;
}
int dellinkstack(linkstack_t** top)
{
int temp;
linkstack_t *pdel=NULL;
if(isemptylinkstack(*top)==1)
{
puts("栈空");
return -1;
}
pdel=*top;
temp=pdel->data;
*top=(*top)->next;
free(pdel);
return temp;
}
int showlinkstack(linkstack_t* top)
{
if(isemptylinkstack(top)==1)
{
puts("栈空");
return -1;
}
return top->data;
}
int lenlinkstack(linkstack_t *p)
{
int len=0;
while(p!=NULL)
{
p=p->next;
len++;
}
return len;
}
void clearlink(linkstack_t **p)
{
linkstack_t *pdel=NULL;
while(*p!=NULL)
{
pdel=*p;
*p=(*p)->next;
free(pdel);
pdel=NULL;
}
(*p)=NULL;
}
/*****************以下为char型**************/
void createcharstack(charstack_t** p)
{
*p=NULL;
}
int insertcharstack(charstack_t** p,char data)
{
charstack_t* pnew=NULL;
pnew=(charstack_t*)malloc(sizeof(charstack_t));
if(pnew==NULL)
{
puts("创建失败");
return -1;
}
pnew->data=data;
pnew->next=NULL;
pnew->next=(*p);
(*p)=pnew;
return 0;
}
int isemptycharstack(charstack_t*p)
{
return p==NULL;
}
int delcharstack(charstack_t** p)
{
charstack_t* pdel;
pdel=NULL;
if(isemptycharstack(*p))
{
puts("栈空");
return -1;
}
pdel=*p;
*p=(*p)->next;
free(pdel);
pdel=NULL;
return 0;
}
char showcharstack(charstack_t *p)
{
return p->data;
}
int lencharstack(charstack_t* p)
{
int len=0;
while(p)
{
p=p->next;
len++;
}
return len;
}
void clearcharstack(charstack_t ** p)
{
while(p)
{
delcharstack(p);
*p=(*p)->next;
}
}
int jisuan(int x,int y,char o)
{
if(o=='+')
return x+y;
if(o=='-')
return x-y;
if(o=='*')
return x*y;
if(o=='/')
return x/y;
return 0;
}
主函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "linkstack.h"
int main(int argc, char *argv[])
{
linkstack_t *top=NULL;
charstack_t *chartop=NULL;
createlinkstack(&top);
createcharstack(&chartop);
int i;
char a[]="3+5*2-1+8/2";
int x,y;
char o;
for(i=0;i<strlen(a);i++)
{
if(a[i]=='*'||a[i]=='/')
{
y=a[i+1]-'0';
x=showlinkstack(top);
dellinkstack(&top);
o=a[i];
i++;
insertlinkstack(&top,jisuan(x,y,o));
}
else if(a[i]=='-')
{
if(chartop!=NULL)
{
if(showcharstack(chartop)=='-')
{
y=showlinkstack(top);
dellinkstack(&top);
x=showlinkstack(top);
dellinkstack(&top);
o=showcharstack(chartop);
delcharstack(&chartop);
printf("%d\n",jisuan(x,y,o));
insertlinkstack(&top,jisuan(x,y,o));
}
}
int j=i+1;
while(a[j]!='+'&&a[j]!='-'&&a[j]!='*'&&a[j]!='/')
{
j++;
if(a[j]=='\0')
{
j=i;
break;
}
}
if(a[j]!='*'&&a[j]!='/')
{
y=a[i+1]-'0';
x=showlinkstack(top);
dellinkstack(&top);
o=a[i];
i++;
printf("%d\n",jisuan(x,y,o));
insertlinkstack(&top,jisuan(x,y,o));
}
else
{
insertcharstack(&chartop,a[i]);
}
}
else if(a[i]=='+')
{
insertcharstack(&chartop,a[i]);
}
else
{
insertlinkstack(&top,(a[i]-'0'));
}
}
while(!isemptycharstack(chartop))
{
y=showlinkstack(top);
dellinkstack(&top);
x=showlinkstack(top);
dellinkstack(&top);
o=showcharstack(chartop);
delcharstack(&chartop);
insertlinkstack(&top,jisuan(x,y,o));
}
printf("%d\n",showlinkstack(top));
return 0;
clearlink(&top);
clearcharstack(&chartop);
}
个人思路,不喜勿喷ouo