简易功能计算器0.1版本(基于双栈)
来源:互联网 发布:远程桌面的端口号 编辑:程序博客网 时间:2024/06/06 04:16
Lisa
这个傲娇的计算器叫Lisa。
/*********************************************************Code writer : EOFCode file : stack.hCode date : 2014.11.26Email : jasonleaster@gmail.comCode description:Here is a implementation of a naive computerIt 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 : EOFCode file : computer_test.ccode date : 2014.03.03e-mail : jasonleaster@gmail.comcode purpose :This is just a test code for "Lisa" that I created. If there is something wrong with my code, pleasetouche 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 : EOFCode file : computer.cCode date : 2014.11.26Email : jasonleaster@gmail.comVersion : 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 : EOFcode date : 2014.03.03e-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: EOFcode date : 2014.03.03e-mail: jasonleaster@gmail.comcode purpose :This code is a implementation for function is_emptyIf 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: EOFcode date: 2014.03.03e-mail: jasonleaster@gmail.comcode purpose :This code is a implementation for function pop_stackIf 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 : EOFcode date:2014.03.03e-mail:jasonleaster@gmail.comcode purpose :This is my implementaion for function push_stackIf 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 0
- 简易功能计算器0.1版本(基于双栈)
- js实现简易计算器功能
- 基于MFC的简易计算器
- 基于JQuery的简易计算器
- 基于javascript的简易计算器
- 简易计算器各响应函数与功能函数
- JAVA学习笔记---简易计算器完整功能
- javascript完成简易计算器的功能
- 基于51单片机的简易计算器
- 基于类实现的简易计算器
- 基于STC89C52单片机实现简易计算器
- 基于angularJs和Bootstrap的简易计算器
- 基于C语言的简易计算器
- 栈完成简易的计算器
- 用栈实现简易计算器
- 简易计算器
- 简易计算器。。。
- 简易计算器
- 用lomboz(Object Web)搭建一个JSP(JAVA WEB)开发平台
- 关于调制的东西
- 构建高性能高并发Java系统
- js中prototype用法(转)
- Spring定时任务的几种实现
- 简易功能计算器0.1版本(基于双栈)
- struts2+hibernate3+spring3整合步骤
- Oracle EBS:常用SQL语句(2)
- undefined reference to `pthread_create'
- windows安装PHP5.4+Apache2.4+Mysql5.5
- 有用的python学习资料link,就两个,推荐!!
- Shell产生随机数或随机字符串
- poj3905 Perfect Election pascal代码
- 开发者必备的十二大Android开发资源