第四章计算器程序部分代码,结合练习
来源:互联网 发布:小鸭淘宝客助手 编辑:程序博客网 时间:2024/05/22 14:45
为了练习多文件组织编程,同时为了能和后面学习的内容结合起来,这套程序改了很多次,至今还有部分功能未实现。
这套主要由main.c && atof.c && stack.c && getch.c && getop.c 构成,头文件share.h对所有函数及共享变量或者外部变量声明。
share.h
#ifndef SHARE_H_INCLUDED#define SHARE_H_INCLUDED#define MAXOP 100 // the max length of character#define NUMBER '0' //label a digit#define BUFFERSIZE 100 //the max size of input buffer#define MAXVOLUME 1000 //maximun dept of stacktypedef struct { //definition of stack double data[MAXVOLUME]; //volume of stack int sp; //top pointer of stack} SqStack;void InitStack(SqStack *s); //stack.cbool Push(SqStack *s, double i); //stack.cbool Pop(SqStack *s, double *i); //stack.cbool StackEmpty(SqStack (*s)); //stack.cvoid PrintTop(SqStack *s); //stack,cdouble CopyTop(SqStack *s); //stack.cvoid SwapTop(SqStack *s); //stack.cvoid SetStackEmpty(SqStack *s); //stack.cint Getch(void); //getch.cvoid Ungetch(int c); //getch.cint Getop(char s[]); //getop.cdouble Atof(char s[]); //atof.c#endif // SHARE_H_INCLUDED
main.c
#include <stdio.h>#include <math.h>#include <stdlib.h>#include <stdbool.h>#include <ctype.h>#include "share.h"#define MAXOP 100 // the max length of character//practic 4-3int main(void) { SqStack val; int type; double op1; double op2; char s[MAXOP]; //char sAfterTransfrom[MAXOP]; InitStack(&val); while ((type = Getop(s)) != EOF) { switch (type) { case NUMBER : Push(&val, Atof(s)); PrintTop(&val); break; case '+' : Pop(&val, &op2); Pop(&val, &op1); Push(&val, op1 + op2); break; case '*' : Pop(&val, &op2); Pop(&val, &op1); Push(&val, op1 * op2); break; case '-' : Pop(&val, &op2); Pop(&val, &op1); Push(&val, op1 - op2); break; case '/' : Pop(&val, &op2); Pop(&val, &op1); if (op2 != 0.0) Push(&val, op1 / op2); else printf("error: zero divisor\n"); break; case '%' : //*********practic 4-3******** Pop(&val, &op2); Pop(&val, &op1); if (op2 != 0.0) Push(&val, (double)((int)op1 % (int)op2)); else printf("error: zero divisor\n"); break; case '\n' : Pop(&val, &op2); printf("\t%.8g\n", op2); break; case '\0' : Pop(&val, &op2); printf("\t%.8g\n", op2); break; default : printf("error: unknown command %s\n", s); break; } } return 0;}
atof.c
#include <stdio.h>#include <ctype.h>#include <math.h>#include <stdbool.h>#include "share.h"double Atof(char s[]) { double val, power; int i, sign, index; char index_sign = '+'; for (i = 0; isspace(s[i]); i++) {} sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') i++; for (val = 0.0; isdigit(s[i]); i++) val = 10.0 * val + (s[i] - '0'); if (s[i] == '.') i++; for (power = 1.0; isdigit(s[i]); i++) { val = 10.0 * val + (s[i] - '0'); power *= 10.0; } if (s[i] == 'e' || s[i] == 'E') { ++i; if ( !(isdigit(s[i])) ) { index_sign = (s[i] == '-') ? '-' : '+'; ++i; } for (index = 0; isdigit(s[i]); i++) index = 10 * index + (s[i] - '0'); if (index_sign == '+') return sign * val / power * pow(10.0, index); else return sign * val / power / pow(10.0, index); } return sign * val / power;}
stack.c
#include <stdio.h>#include <stdbool.h>#include "share.h"void InitStack(SqStack *s) { //initialization of stack (*s).sp = -1;}bool Push(SqStack *s, double i) {//push element of stack if ((*s).sp < MAXVOLUME) { (*s).data[++(*s).sp] = i; return true; } else { printf("stack is full!\n"); return false; }}bool Pop(SqStack *s,double *i) {//pop element of stack if ((*s).sp >= 0) { *i = (*s).data[(*s).sp--]; return true; } else { printf("\nstack is empty!\n"); return false; }}bool StackEmpty(SqStack (*s)) { //checks if stack is empty if ((*s).sp == -1) return true; return false;}//****************4-4**********void PrintTop(SqStack *s) { printf("the Top of Stack is %6.3f\n", (*s).data[(*s).sp]);}//***************4-4***********double CopyTop(SqStack *s) { return (*s).data[(*s).sp];}//*****************4-4************void SwapTop(SqStack *s) { char temp_1; char temp_2; temp_1 = (*s).data[((*s).sp)--]; temp_2 = (*s).data[(*s).sp]; (*s).data[(*s).sp] = temp_1; ++((*s).sp); (*s).data[(*s).sp] = temp_2;}//***********************4-4*************void SetStackEmpty(SqStack *s) {// set current stack as empty stack if ((*s).sp == -1) return; while ((*s).sp != -1) { (*s).data[(*s).sp--] = '\0'; }}/*int main(void) { SqStack s; int input_temp; int output_temp; InitStack(&s); while ((input_temp = getchar()) != EOF) { if(Push(&s, input_temp)) continue; else break; } while (Pop(&s, &output_temp)) { putchar(output_temp); }}*/
getch.c
#include <stdio.h>#include <stdbool.h>#include "share.h"char buf[BUFFERSIZE]; //buffer use for store data of input.int bufp = 0; //point free position in buffer.int Getch(void) { return (bufp > 0) ? buf[--bufp] : getchar();}void Ungetch(int c) { if (bufp >= BUFFERSIZE) printf("Ungetch: too many characters\n"); else buf[bufp++] = c; //printf("Ungetch: %s\n", buf);}
getop.c
#include <stdio.h>#include <ctype.h>#include <stdbool.h>#include "share.h"int Getop(char s[]) { int i = 0; int c; int c_next; while ((s[0] = c = Getch()) == ' ' || c == '\t') continue; //printf("******"); if (c == '\0') printf("\\0\n"); s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-') { return c; } /* if (c == '+' || c == '-' || c == '*' || c == '%' || c == '/' || c == '\n') return c; */ if (c == '-') {//4-3 while ((c_next = Getch()) == ' ' || c_next == '.') continue; if (c_next == '\n') Ungetch(c_next); if (isdigit(c_next)) { c = c_next; s[++i] = c; } else { return c; } } if (isdigit(c)) { while (isdigit(s[++i] = c = Getch())) { continue; } } if (c == '.') while (isdigit(s[++i] = c = Getch())) continue; s[i] = '\0'; if (c != EOF) { Ungetch(c); } return NUMBER;}
0 0
- 第四章计算器程序部分代码,结合练习
- 第四部分 程序编译与代码优化
- OpenGL Super Bible 第四章 Transform 程序绘图部分代码解析
- 笔记:深入理解JVM 第四部分 程序编译及代码优化 (第10、11章)
- 代码大全第二部分 (包含第四章)
- 《JQuery基础教程》第四版课后练习代码--第四章
- 指针练习-简易计算器程序
- 指针练习-简易计算器程序
- 简易计算器代码示例【部分】
- 第四课 计算器代码的编写
- 第四天笔记和计算器关键代码
- 《JQuery基础教程》第四版课后练习代码--第三章
- 《JQuery基础教程》第四版课后练习代码--第五章
- 《JQuery基础教程》第四版课后练习代码--第六章
- 【算法导论(第三版)】第四章部分习题代码
- Visual C# .Net 计算器部分代码
- 第四章例题练习
- 第四章 编程练习
- Oracle存储过程读取文件例子--UTL_FILE
- Java String类
- CL邀请码(后记)
- 浏览器兼容性问题解决方案之CSS——已在IE、FF、Chrome测试
- 《C专家编程》勘误表
- 第四章计算器程序部分代码,结合练习
- [BFS]HOJ2979Escape from Pyramids
- zoj 3471 Most Powerful (状态压缩dp)
- ORA-27102(转载)
- 6174问题。
- The connection to adb is down, and a severe error has occured.错误解决
- 对excel的导出,使用jxt
- [MacOSX]_[Mac Book电脑的快捷键*长期更新]
- hdu 2594 Simpsons’ Hidden Talents(KMP)