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

相关文章

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java

基于Python实现简易视频剪辑工具

《基于Python实现简易视频剪辑工具》这篇文章主要为大家详细介绍了如何用Python打造一个功能完备的简易视频剪辑工具,包括视频文件导入与格式转换,基础剪辑操作,音频处理等功能,感兴趣的小伙伴可以了... 目录一、技术选型与环境搭建二、核心功能模块实现1. 视频基础操作2. 音频处理3. 特效与转场三、高

Java实现预览与打印功能详解

《Java实现预览与打印功能详解》在Java中,打印功能主要依赖java.awt.print包,该包提供了与打印相关的一些关键类,比如PrinterJob和PageFormat,它们构成... 目录Java 打印系统概述打印预览与设置使用 PageFormat 和 PrinterJob 类设置页面格式与纸张

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

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.