用栈实现字符串的算数(无括号基础版)

实现计算出"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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

reloadEarth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值