博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
栈的应用——缓冲区计算逆波兰式
阅读量:4171 次
发布时间:2019-05-26

本文共 3511 字,大约阅读时间需要 11 分钟。

栈缓冲法计算数学表达式

问题描述

输入一个字符串(不含括号,只有个位数),输出计算结果

代码

#include 
#include
#include
struct ListNode {
int data; ListNode* Next; ListNode* Last;};struct List {
ListNode* header; ListNode* trailer; int _size;};void CreateList(List* l)//build the list{
l->_size = 0; l->header = (ListNode*)malloc(sizeof(ListNode)); l->trailer = (ListNode*)malloc(sizeof(ListNode)); l->header->Next = l->trailer; l->header->Last = NULL; l->trailer->Last = l->header; l->trailer->Next = NULL;}void InsertList(List* l, int e)//空表,在尾节点前插入{
ListNode* np = (ListNode*)malloc(sizeof(ListNode)); np->data = e; np->Next = l->trailer; np->Last = l->trailer->Last; l->trailer->Last->Next = np; l->trailer->Last = np; l->_size++;}void PushList(List* l, int e)//入栈{
ListNode* np = (ListNode*)malloc(sizeof(ListNode)); np->data = e; np->Last = l->header; np->Next = l->header->Next; np->Next->Last = np; l->header->Next = np; l->_size++;}int PopList(List* l)//出栈/出队{
ListNode* now = l->header->Next; int old = now->data; now->Next->Last = now->Last; l->header->Next = now->Next; free(now); l->_size--; return old;}struct cListNode {
char data; cListNode* Next; cListNode* Last;};struct cList {
cListNode* header; cListNode* trailer; int _size;};void cCreateList(cList* l)//build the list{
l->_size = 0; l->header = (cListNode*)malloc(sizeof(cListNode)); l->trailer = (cListNode*)malloc(sizeof(cListNode)); l->header->Next = l->trailer; l->header->Last = NULL; l->trailer->Last = l->header; l->trailer->Next = NULL;}void cInsertList(cList* l, char e)//空表,在尾节点前插入{
cListNode* np = (cListNode*)malloc(sizeof(cListNode)); np->data = e; np->Next = l->trailer; np->Last = l->trailer->Last; l->trailer->Last->Next = np; l->trailer->Last = np; l->_size++;}void cPushList(cList* l, char e)//入栈{
cListNode* np = (cListNode*)malloc(sizeof(cListNode)); np->data = e; np->Last = l->header; np->Next = l->header->Next; np->Next->Last = np; l->header->Next = np; l->_size++;}char cPopList(cList* l)//出栈/出队{
cListNode* now = l->header->Next; char old = now->data; now->Next->Last = now->Last; l->header->Next = now->Next; free(now); l->_size--; return old;}char orderBetween(char top, char s){
if (((top == '+' || top == '-') && (s == '*' || s == '/'))||((top=='&')&&(s!='&'))) return '<'; else if ((top == '&')&&(s == '&')) return '='; else return '>';}int calcu(int op1, char op, int op2){
if (op == '+') return op1 + op2; else if (op == '-') return op1 - op2; else if (op == '*') return op1*op2; else return op1 / op2;}int main(){
cList formula; cCreateList(&formula); while (1) {
char tmp; scanf_s("%c", &tmp); if (tmp != '\n') cInsertList(&formula, tmp); else break; } cInsertList(&formula, '&'); List opnd;//运算数栈 cList optr;//运算符栈 CreateList(&opnd); cCreateList(&optr); cPushList(&optr, '&'); cListNode* fp = formula.header->Next; while (optr._size !=0 ) {
if ('0' <= fp->data&&fp->data <= '9') {
PushList(&opnd, (int)fp->data - 48); fp = fp->Next; } else switch (orderBetween(optr.header->Next->data, fp->data)) {
case '<': {
cPushList(&optr, fp->data); fp = fp->Next; break; } case '=': {
cPopList(&optr); fp = fp->Next; break; } case '>': {
char op = cPopList(&optr); int popnd2 = PopList(&opnd); int popnd1 = PopList(&opnd); PushList(&opnd, calcu(popnd1, op, popnd2)); break; } default:exit(-1); } } printf_s("%d", PopList(&opnd));}

又水了一篇,等1024勋章,睡觉咯

转载地址:http://nzwai.baihongyu.com/

你可能感兴趣的文章
嵌入式100题(038):HTTPS与HTTP的一些区别
查看>>
嵌入式100题(042):为什么服务端易受到SYN攻击?
查看>>
嵌入式100题(043):什么是四次挥手
查看>>
嵌入式100题(044):为什么客户端最后还要等待2MSL?
查看>>
嵌入式100题(045):为什么建立连接是三次握手,关闭连接确是四次挥手呢?...
查看>>
嵌入式100题(028):static的用法(定义和用途)
查看>>
嵌入式100题(027):char和int之间的转换
查看>>
嵌入式100题(029):const常量和#define的区别(编译阶段、安全性、内存占用等)...
查看>>
嵌入式100题(030):volatile作用和用法
查看>>
嵌入式100题(033):TCP、UDP的优缺点
查看>>
嵌入式100题(035):TCP为什么是可靠连接
查看>>
嵌入式100题(034):TCP UDP适用场景
查看>>
嵌入式100题(70):一个程序从开始运行到结束的完整过程(四个过程)
查看>>
嵌入式100题(71):什么是堆,栈,内存泄漏和内存溢出?
查看>>
嵌入式100题(73):死锁的原因、条件 创建一个死锁,以及如何预防
查看>>
嵌入式100题(60):系统调用的作用
查看>>
C语言基本概念归纳
查看>>
初识单片机
查看>>
在单片机上点亮LED
查看>>
初学定时器
查看>>