简易功能计算器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

相关文章

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP