简易功能计算器0.1版本(基于双栈)

                    Lisa

这个傲娇的计算器叫Lisa。

/*********************************************************
Code writer : EOF
Code file   : stack.h
Code date   : 2014.11.26
Email	    : [email protected]

Code description:

	Here is a implementation of a naive computer
It based on two stack scheme which was found by Dijkstra.

*********************************************************/
#ifndef _STACK_H
#define _STACK_H 1  

	#define EMPTY     0
	#define NON_EMPTY 1  

	#define ARRAYSIZE 1024

	#include <stdio.h>
	#include <stdlib.h>
	#include <unistd.h>

	struct node
	{
	    int data;
	    struct node* next;
	};  

	int computer(void);

	struct node* creat_stack(void);
	int push_stack(struct node** pp_top,int number);
	int is_empty(struct node* p_node);
	void release_stack(struct node* p_top);

#endif
/*************************************************************
code writer : EOF
Code file   : computer_test.c
code date   : 2014.03.03
e-mail      : [email protected]

code purpose :
		This is just a test code for "Lisa" that
I created. If there is something wrong with my code, please
touche me by e-mail.

#ATTENTION#
	You must input with '(' and ')' for each operator,
otherwise you will be puzzle about the result of output.

Usage:
	(1+2) would output 3
	(1+((2*3)+1))  == 8

If there is something wrong with my code, please touch me by e-mail.

******************************************************************/
#include "stack.h"

int main()
{
	int number = 0;
	printf("Hello ! This is a naive computer."
	       "Her name is 'Lisa' :)\n");

	number = computer();

	printf("The result of your inputed :%d\n",number);

	return 0;
}
/*********************************************************
Code writer : EOF
Code file   : computer.c
Code date   : 2014.11.26
Email	    : [email protected]
Version	    : 0.0

Code description:

code purpose :
	This code is my implementation for function creat_stack.
functin creat_stack would creat a the first node of the stack and
just only the first node. You must know what is stack...

#ATTENTION#
	You must input with '(' and ')' for each operator,
otherwise you will be puzzle about the result of output.

Usage:
	(1+2) would output 3
	(1+((2*3)+1))  == 8

If there is something wrong with my code, please touch me by e-mail.

*********************************************************/
#include "stack.h"

int computer(void)
{
	char string[ARRAYSIZE] = {0};

	/*
	**	According to Dijsktra's Two Stack scheme,
	** we creat two stack.
	**	@p_stack_operand is pointer which point to
	** a stack only for operand in the inputed string.
	** So do @p_stack_operator.
	*/
	struct node* p_stack_operand  = NULL;
	struct node* p_stack_operator = NULL;	

	p_stack_operand  = creat_stack();
	p_stack_operator = creat_stack();

	int  temp      = 0;
	int  result    = 0;
	int  opr_one   = 0;
	int  opr_two   = 0;
	char operator  = 0;

	/*
	** 	Evil --> --> scanf("%s",&string);
	**
	** scanf family is not safe for string which is inputed.
	** I decide to give it up and use system call read().
	*/

	if(read(STDIN_FILENO,string,ARRAYSIZE) < 0)
	{
		printf("system call read() error"
		       " in function %s()\n",__FUNCTION__);
	}

	for(temp = 0;string[temp] != '\n';temp++)
	{
		if(string[temp] == '(')
		{
			continue;
		}

		if(string[temp] == ')')
		{
			opr_one  = pop_stack(p_stack_operand);
			opr_two  = pop_stack(p_stack_operand);

			operator = pop_stack(p_stack_operator);

			switch(operator)
			{
				case '+':
				{
					push_stack(&p_stack_operand,
						   opr_one + opr_two);
					break;
				};

				case '-':
				{
					push_stack(&p_stack_operand,
						   opr_one - opr_two);
					break;
				};

				case '*':
				{
					push_stack(&p_stack_operand,
						   opr_one * opr_two);
					break;
				};

				case '/':
				{
					push_stack(&p_stack_operand,
						   opr_one / opr_two);
					break;
				};

				default:
					goto out;
					result = -1;
					printf("ERROR!undefined "
					       "operator %c\n",operator);

			}
		}

		if(string[temp] >= '0' && string[temp] <= '9')
		{
			push_stack(&p_stack_operand,string[temp] - '0');
			continue;
		}

		if(string[temp] == '+' || string[temp] == '-'||
		   string[temp] == '*' || string[temp] == '/')
		{
			push_stack(&p_stack_operator,string[temp]);
			continue;
		}
	}

	/*
	** Finally, We got the result which is stored in stack--operand
	*/
	result = pop_stack(p_stack_operand);

out:
	release_stack(p_stack_operand);
	release_stack(p_stack_operator);

	return result;
}

/*********************************************************************
code writer : EOF
code date   : 2014.03.03
e-mail 	    : [email protected]

**********************************************************************/
#include "stack.h"

struct node* creat_stack(void)
{
	struct node* p_top = NULL;

	p_top = (struct node*)malloc(sizeof(struct node));

	if(p_top == NULL)
	{
		printf("malloc failed\n");
	}

	p_top->next = NULL;

	while(is_empty(p_top) == NON_EMPTY)
	{
		pop_stack(p_top);
	}

	return p_top;
}
/**********************************************************************
code writer: EOF
code date : 2014.03.03
e-mail: [email protected]
code purpose :
	This code is a implementation for function is_empty
If there is something wrong with my code, please touch me by e-mail.

**********************************************************************/
#include "stack.h"

int is_empty(struct node* p_node)
{
	if(p_node->next == NULL)
	{
		return EMPTY;
	}
	else
	{
		return NON_EMPTY;
	}
}
/*****************************************************************
code writer: EOF
code date: 2014.03.03
e-mail: [email protected]
code purpose :
	This code is a implementation for function pop_stack
If there is something wrong with my code, please touch me by e-mail

*****************************************************************/
#include "stack.h"

int pop_stack(struct node* p_top)
{
	struct node* temp = NULL;

	int number = 0;

	if(is_empty(p_top) == EMPTY)
	{
		printf("empty stack!\nprocess end");
		return EMPTY;
	}
	else
	{
		temp = p_top->next;
		p_top->next = p_top->next->next;
		number = temp->data;
		free(temp);
	}

	return number;
}
/*******************************************************************
code writer : EOF
code date:2014.03.03
e-mail:[email protected]
code purpose :
	This is my implementaion for function push_stack
If there is somrthing wrong with my code, please touche me by e-mail.

*******************************************************************/
#include "stack.h"

int push_stack(struct node** pp_top,int number)
{
	struct node* temp = NULL;
	struct node* new_node = NULL;

	new_node = (struct node*)malloc(sizeof(struct node));

	if(new_node == NULL)
	{
		printf("malloc failed\nprocess end\n");
		return 0;
	}

	new_node->data = number;
	new_node->next = (*pp_top)->next;
	(*pp_top)->next = new_node;
}
#include "stack.h"

void release_stack(struct node* p_top)
{
	while(is_empty(p_top) == NON_EMPTY)
	{
		pop_stack(p_top);
	}
}

测试时,要严格按照输入要求进行测试!

欢迎提出发现的bug,或者指正编码风格不好的地方。

时间: 2024-11-08 05:06:24

简易功能计算器0.1版本(基于双栈)的相关文章

Apache Kafka 0.11版本新功能简介

Apache Kafka近日推出0.11版本.这是一个里程碑式的大版本,特别是Kafka从这个版本开始支持"exactly-once"语义(下称EOS, exactly-once semantics).本文简要介绍一下0.11版本主要的功能变更,下面中的每一项都值得专门写篇文章好好聊聊. 一.修改unclean.leader.election.enabled默认值 Kafka社区终于下定决心要把这个参数的默认值改成false,即不再允许出现unclean leader选举的情况,在正确

.NET Framework 1.1、2.0、3.0、3.5、4.0各版本新增功能

一..NET Framework 1.1版本 1.ASP.NET移动控件 2.ADO.NET的改动 添加System.Data.Odbc命名空间 新增System.Data.OracleClient命名空间供Oracle使用 DataReader对象公开HasRows属性,判断是否有返回行 Connection对象具有EnlistDistributedTransaction,可以在分布式事务中启动手动登记. 3.并发执行 .NET Framework 1.1版本支持并行执行. 4..NET Fr

c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结【转载】

引用:http://blog.csdn.net/attilax/article/details/42014327 c# .net 3.5 4.0 各个版本新特性战略规划总结 1. --------------.Net Framework版本同CLR版本的关系1 2. paip.------------SDK2.0功能-------------2 2.1. 泛型:2 3. --------------sdk3.0  增加了以下功能..2 3.1. LINQ 3 4.  ----------sdk4

开源yYmVc项目 v 0.2 版本介绍

项目地址:https://code.csdn.net/hacke2/yymvc 本版本主要实现以下几点功能: 1.框架入口基于过滤器统一实现,action后缀动态配置 2.action配置模仿struts 3.增加action的辅助类 4.完成模型与视图的分离 ----------- 开源yYmVc项目 v 0.2 版本介绍,布布扣,bubuko.com

INNO SETUP 5.5.0以上版本中文语言包

1 ; *** Inno Setup version 5.5.0+ Chinese messages *** 2 ; 3 ; To download user-contributed translations of this file, go to: 4 ; http://www.jrsoftware.org/is3rdparty.php 5 ; 6 ; Note: When translating this text, do not add periods (.) to the end of

Atitit.c# .net 3.5 4.0 各个版本新特性战略规划总结

Atitit.c# .net 3.5 4.0 各个版本新特性战略规划总结 1. --------------.Net Framework版本同CLR版本的关系 1 2. paip.------------SDK2.0功能------------- 2 2.1. 泛型: 2 3. --------------sdk3.0  增加了以下功能.. 2 3.1. LINQ 3 4.  ----------sdk4.0 新加功能------------ 3 5. ].Net4.5的五项强大新特性 3 5.

基于双TMS320C6678+双XC6VSX315T的6U VPX高速数据处理平台

基于双TMS320C6678+双XC6VSX315T的6U VPX高速数据处理平台 一.板卡概述 板卡由我公司自主研发,基于VPX架构,主体芯片为两片 TI DSP TMS320C6678,两片Virtex-6 XC6VSX315T-ff1156 FPGA,1个RapidIO Switch.FPGA连接FMC子卡.FPGA片外挂接2簇32bit DDRIII SDRAM,最大容量支持2GB.每片FPGA还通过EMIF总线连接一片TMS320C6678型8核心DSP.所有信号处理FPGA与DSP均

恒天云 3.0:打造基于OpenStack的私有云新模式

云计算在当今IT世界中已发展地如火如荼,越来越多的企业利用云计算改造传统的数据中心,简化IT资源的交付模式.云计算是许多开放技术的融合,在这个领域,OpenStack是其中最流行最具影响力的开源IaaS云平台,全世界大批的工程师参与OpenStack各组件的开发,使其社区越来越活跃,发展也越来越快速. OpenStack发展至今已发布9个版本,但许多组件仍存在种种稳定性问题,想在企业内部落地依然困难重重,如: Dashboard组件Horizon项目发展缓慢,产品化不足,用户友好度不高: 尚未与

jeewx-api 1.0.5 版本发布,微信SDK接口封装

JeeWx-api 1.0.5 版本发布,微信SDK接口封装 1.jeewx-api为何诞生 现在微信越来越火,基于微信的公众号和服务号越来越丰富,虽然微信帮助文档已经提供了相关的接口,但是接口比较多,通过代码自己调用比较麻烦,所以为减轻开发者独自创造轮子,将微信API进行了统一封装! 2.作者 : Jeecg开源社区 3.新版本升级功能    一.基础接口         1.获取api_ticket          2.自定义菜单删除接口          3.多媒体素材永久上传接口