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

2024-06-06 09:38

本文主要是介绍简易功能计算器0.1版本(基于双栈),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

                    Lisa


          这个傲娇的计算器叫Lisa。


/*********************************************************
Code writer : EOF
Code file   : stack.h
Code date   : 2014.11.26
Email	    : jasonleaster@gmail.comCode 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      : jasonleaster@gmail.comcode 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))  == 8If 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	    : jasonleaster@gmail.com
Version	    : 0.0Code 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))  == 8If 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 	    : jasonleaster@gmail.com**********************************************************************/
#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: jasonleaster@gmail.com
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: jasonleaster@gmail.com
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:jasonleaster@gmail.com
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,或者指正编码风格不好的地方。





这篇关于简易功能计算器0.1版本(基于双栈)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035746

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.