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

相关文章

SpringBoot整合DeepSeek实现AI对话功能

《SpringBoot整合DeepSeek实现AI对话功能》本文介绍了如何在SpringBoot项目中整合DeepSeekAPI和本地私有化部署DeepSeekR1模型,通过SpringAI框架简化了... 目录Spring AI版本依赖整合DeepSeek API key整合本地化部署的DeepSeek

python实现简易SSL的项目实践

《python实现简易SSL的项目实践》本文主要介绍了python实现简易SSL的项目实践,包括CA.py、server.py和client.py三个模块,文中通过示例代码介绍的非常详细,对大家的学习... 目录运行环境运行前准备程序实现与流程说明运行截图代码CA.pyclient.pyserver.py参

Python实现多路视频多窗口播放功能

《Python实现多路视频多窗口播放功能》这篇文章主要为大家详细介绍了Python实现多路视频多窗口播放功能的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下... 目录一、python实现多路视频播放功能二、代码实现三、打包代码实现总结一、python实现多路视频播放功能服务端开

java中不同版本JSONObject区别小结

《java中不同版本JSONObject区别小结》本文主要介绍了java中不同版本JSONObject区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1. FastjsON2. Jackson3. Gson4. org.json6. 总结在Jav

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

使用PyQt实现简易文本编辑器

《使用PyQt实现简易文本编辑器》这篇文章主要为大家详细介绍了如何使用PyQt5框架构建一个简单的文本编辑器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录分析主窗口类 (MyWindow)菜单操作语法高亮 (SyntaxHighlighter)运行程序主要组件代码图示分析实现

5分钟获取deepseek api并搭建简易问答应用

《5分钟获取deepseekapi并搭建简易问答应用》本文主要介绍了5分钟获取deepseekapi并搭建简易问答应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需... 目录1、获取api2、获取base_url和chat_model3、配置模型参数方法一:终端中临时将加

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java CompletableFuture如何实现超时功能

《JavaCompletableFuture如何实现超时功能》:本文主要介绍实现超时功能的基本思路以及CompletableFuture(之后简称CF)是如何通过代码实现超时功能的,需要的... 目录基本思路CompletableFuture 的实现1. 基本实现流程2. 静态条件分析3. 内存泄露 bug