栈-四则运算二(改)

structfun.h文件

//数据结构函数头文件

#include <stdio.h>
#include <iostream>
#include<string>

using std::cout;
using std::cin;
using std::string;

#define MAXSIZE 200
#define OK 1
#define ERROR 0
typedef string ElemType;

//顺序存储结构定义
typedef struct{
	ElemType data[MAXSIZE];
	int lenght;
}SqList;

//定义单链表数据结构(及循环链表)
typedef struct Node{
	ElemType data;
    Node *next;
}Node ,*LinkList;

//双向循环链表结构定义
typedef struct DbNode{
	ElemType data;
	DbNode *prior,*next;
}DBLinklist;//DBLinklist不定义成指针类型,要用到的时候可以加*定义成指针

//静态链表结构定义
typedef struct
	{
	ElemType data;
	int cur;
	}StaticLinkList[MAXSIZE];

//5、栈结构定义
typedef struct stack{
	ElemType data[MAXSIZE];
	int top;
}stackList;



//顺序存储线性表
int InitList(SqList *L);
bool EmptyList(SqList L);
int ClearList(SqList *L);
ElemType GetElem(SqList *L,int i);
int LocaElem(SqList L,ElemType e);
int InsertElem(SqList *L,int i,ElemType e);
ElemType DeleteList(SqList *L,int i);

//单链表结构线性表

//初始化链表L
LinkList InitList();

//在链表前面增加元素(前插法)
int AddBeforeList(LinkList *L,ElemType e);

//在指定i位位置插入元素E
int AddIndexList(LinkList *L,int i,ElemType e);

//在链表前面增加元素(后插法)
int AddAfterList(LinkList *L,ElemType e);

//删除指定i位位置元素
ElemType DelListElem(LinkList *L,int i);

//打印单链表
void PrintLinkList(LinkList *L);

//释放内存空间
int ClearList(LinkList *L);


//-----------------------------------------------------------------
//单链表循环结构链表(数据结构定义与单链表相同,头指针指向尾部,尾指针指向头部)

//初始化循环链表
LinkList  InitLPList();

//插入循环链表元素
int AddListElem(LinkList *L,ElemType e);

//把L2链表并入L1
int UList(LinkList *L1,LinkList *L2);

//------------------------------------------------------------------
//双向循环链表

//创建空双向循环链表
DBLinklist *InitDBList();

//添加结点函数
int AddDBlist(DBLinklist *L,ElemType e);

//打印循环链表
void PrintDBLinkList(DBLinklist *L);

//向前遍历删除L列表中的e元素
int DelDBlist(DBLinklist *L,ElemType e);

//释放列表
int ClearDBList(DBLinklist *L);

//--------------------------------------------------------------------
//静态链表函数
//函数功能:初始化静态链表
int InitSTList(StaticLinkList space);

//函数功能:分配空间,由于静态数组链表不能用指针,所以需要自己编写函数实现功能
int Malloc_SLL(StaticLinkList space);

//函数功能:释放空间.
void Free_SLL(StaticLinkList space,int k);

//函数功能:返回链表长度
int Len_SLL(StaticLinkList space);

//函数功能:插入链表数据
int InsertList(StaticLinkList space,int i,ElemType e);

//查找要删除的位置,位置为游标 i要减去1 从链表尾部获取第一个数据游标,依次遍历
int DeleteList(StaticLinkList space,int i);

//打印列表
int PrintList(StaticLinkList space);

//--------------------------------------------------------------------------
//初始化栈
int InitStack(stackList *L);

//入栈push
int push(stackList *L,ElemType e);

//出栈pop
ElemType pop(stackList *L);

//四则运算优先级
int leve(char c);

//四则元素中缀表达式转换为后缀表达式函数
void BefTansBack(stackList *L1,stackList *L2,string exp );

//string转char函数
char stoc(string s,int i);

//char转string函数
string ctos(char c);

//float转string
string ftos(float f);

structfun.cpp

#include<iostream>
#include<stdio.h>
#include<string>
#include<sstream>


using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::ostringstream;
//数据结构函数库

//1、顺序存储结构定义
typedef struct{
	ElemType data[MAXSIZE];
	int lenght;
}SqList;

//定义双向循环结构链表
typedef struct DbNode{
	ElemType data;
	DbNode *prior,*next;
}DBLinklist;

//5、栈结构定义
typedef struct stack{
	ElemType data[MAXSIZE];
	int top;
}stackList;

//(四)双向循环链表函数

//创建空双向循环链表
DBLinklist *InitDBList()
{
	DbNode *head;
	/*head=(DBLinklist*)malloc(sizeof(DBLinklist));*/
	head=new DbNode;
	head->data="head";
	head->next=head;
	head->prior=head;
	return head;
}

//添加结点函数
int AddDBlist(DBLinklist *L,ElemType e)
{
	//函数功能:添加双向循环结点
	DbNode *adn;
	/*adn=(DBLinklist*)malloc(sizeof(DBLinklist));*/
	adn=new DbNode;
	adn->data=e;
	adn->prior=L->prior;//新增结点的前指针指向链表的头结点前指针(也就是链表的最后元素)
	L->prior->next=adn;//链表的最后元素的next指向新结点
	L->prior=adn;//链表的头结点的前指针prior指向新增结点
	adn->next=L;//新增结点的next指向头结点
	return 1;
	
}

//向前遍历删除L列表中的e元素
int DelDBlist(DBLinklist *L,ElemType e)
{
	//函数功能:向前遍历L链表删除e元素
	DBLinklist *temp;
	temp=L->next;
	while(temp->data!=e)
	{
		temp=temp->next;
	}
	temp->prior->next=temp->prior->next->next;
	temp->next->prior=temp->prior;
	free(temp);
return 1;
}



//删除头节点开始的第i个元素
int DelBeginHeadDBlist()
{
	return 1;
}

//打印循环链表元素
void PrintDBLinkList(DBLinklist *L)
{
	DBLinklist *temp;	
	temp=L;
	printf("循环链表元素: ");
	while(temp->next->data!="head")
	{
		printf("%s,",temp->next->data.c_str());
		temp=temp->next;
	}
}
//释放列表内存
int ClearDBList(DBLinklist *L)
{
	//函数功能:释放内存空间

		&L;
		L;
		free(L);
	    return 1;
}

//(六)栈函数实现

//初始化栈
int InitStack(stackList *L)
{
	for(int i=0;i<MAXSIZE;i++)
		(*L).data[i]="";
	(*L).top=-1;
	return 1;
}

//入栈push
int push(stackList *L,ElemType e)
{
	//
	if((*L).top>MAXSIZE)
		return 0;
	(*L).top=(*L).top+1;
	(*L).data[(*L).top]=e;
	return 1;
}

//出栈pop
ElemType pop(stackList *L)
{
	ElemType e;
	if((*L).top==-1)
		return 0;
	e=(*L).data[(*L).top];
	(*L).data[(*L).top]="";
	(*L).top=(*L).top-1;
	return e;
}

()四则运算

//四则运算优先级
int leve(char s)
{
	//函数功能,获得运算符优先级
	switch(s)
	{
		case '(':
			return 3;
		case  ')':
			return 3;
		case '*':
			return 2;
		case '/':
			return 2;
		case '+':
			return 1;
		case '-':
			return 1;
	}
}

//string转char函数
char stoc(string s,int i)
{
	char c;
	c=s[i];
	return c;
}

//float转string
string ftos(float f)
{
	ostringstream oss;
	oss<<f;
	string str(oss.str());
	return str;
}

//char转string函数
string ctos(char c)
{
	string s;
	s=c;
	return s;
}

//四则元素中缀表达式转换为后缀表达式函数
void BefTansBack(stackList *L1,stackList *L2,string exp )
{
	string temp;
for(int i=0;i<exp.length();i++)
	{
	     ElemType l2;
		if(isdigit(exp[i])!=0||exp[i]=='.')//判断是否是连续数字,为数字直接入栈L1
		{
			while(isdigit(exp[i])!=0||exp[i]=='.')
			{
				temp=temp+exp[i];
				if((i+1)==exp.length()||(isdigit(exp[i+1])==0&&exp[i+1]!='.'))
					break;
				i++;
			}
			push(L1,temp);
			temp="";
		}
		else if(exp[i]=='(')
			push(L2,ctos(exp[i]));
		else if(exp[i]==')')
		{
			l2=(*L2).data[(*L2).top];
			pop(L2);
			while(l2!="(")
			{
				push(L1,l2);
				l2=pop(L2);
				
			}
			//push(&L2,exp[i+1]);
			//i++;

		}
		else if((*L2).top ==-1)//判断L2栈是否为空,为空则符号入栈
			push(L2,ctos(exp[i]));
		else //否则L2栈顶元素,与exp[i]比较优先级,如果exp[i]优先级比较高,入L2栈
		{
			int dd,ee;
			dd=leve(exp[i]);
			ee=leve(stoc((*L2).data[(*L2).top],0));

			/*if(leve(exp[i])>leve(stoc((*L2).data[(*L2).top],0))||(*L2).data[(*L2).top].compare(")"))*/
			if(leve(exp[i])>leve(stoc((*L2).data[(*L2).top],0))||!(*L2).data[(*L2).top].compare(")"))
			{
				push(L2,ctos(exp[i]));
			}
			else  //否则L2元素出栈,入L1,直到优先级比exp[i]高,exp[i]入L2栈
				{
				while(1)
					{
						if((*L2).data[(*L2).top]=="(")
							break;
						push(L1,pop(L2));
						if((*L2).top==-1||leve(exp[i])>=leve(stoc((*L2).data[(*L2).top],0)))
							break;
					}
					push(L2,ctos(exp[i]));
				}

		//	//末尾L2栈全部弹出

		}
				if(i==exp.length()-1)
		{
			while((*L2).top!=-1)
			push(L1,pop(L2));
		}

	}
}

main.cpp

#include<iostream>
#include<stdio.h>
#include<string>
#include"structfun.h"

using std::string;
using std::printf;
using std::scanf;
using std::endl;
using std::to_string;


int main()
{
//栈及四则运算实例
	stackList S1,S2,Sum;
	float ex1,ex2,sum;
	DBLinklist *DL,*p;//双向循环链表
	InitStack(&S1);
	InitStack(&S2);
	InitStack(&Sum);
	DL=InitDBList();//初始化双向链表
	表达式:9+(3-1)*3+10/2
	string exp;
	printf("请输入表达式:");
	cin>>exp;
	BefTansBack(&S1,&S2,exp);

	从S1中取出表达式,存入双向链表
	int i=0;
	while(S1.top!=-1)
	{
		AddDBlist(DL,pop(&S1));
	}
		
	printf("后缀表达式:\n");
	PrintDBLinkList(DL);

	while(DL->prior->data!="head")
	{
		switch(stoc(DL->prior->data,0))
		{
		case'+':
			{
				ex1=atof(pop(&Sum).c_str());
				ex2=atof(pop(&Sum).c_str());
				sum=ex2+ex1;
				//sum=to_string((long long)(ex2+ex1));
				push(&Sum,ftos(sum));
				DL->prior=DL->prior->prior;
				break;
			}
		case'-':
			{
				ex1=atof(pop(&Sum).c_str());
				ex2=atof(pop(&Sum).c_str());
				sum=ex2-ex1;
				//sum=to_string((long long)(ex2+ex1));
				push(&Sum,ftos(sum));
				DL->prior=DL->prior->prior;
				break;
			}
		case'*':
			{
				ex1=atof(pop(&Sum).c_str());
				ex2=atof(pop(&Sum).c_str());
				sum=ex2*ex1;
				//sum=to_string((long long)(ex2+ex1));
				push(&Sum,ftos(sum));
				DL->prior=DL->prior->prior;
				break;
			}
		case'/':
			{
				ex1=atof(pop(&Sum).c_str());
				ex2=atof(pop(&Sum).c_str());
				sum=ex2/ex1;
				//sum=to_string((long long)(ex2+ex1));
				push(&Sum,ftos(sum));
				DL->prior=DL->prior->prior;
				break;
			}
		}
		if(DL->prior->data!="head"&&stoc(DL->prior->data,0)!='+'&&stoc(DL->prior->data,0)!='-'&&stoc(DL->prior->data,0)!='*'&&stoc(DL->prior->data,0)!='/')
		{push(&Sum,DL->prior->data);
		DL->prior=DL->prior->prior;	}	
		}
	printf("表达式计算结果:%s\n",pop(&Sum).c_str());
	

	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值