第四章计算器程序部分代码,结合练习
2014-01-25 19:28
459 查看
为了练习多文件组织编程,同时为了能和后面学习的内容结合起来,这套程序改了很多次,至今还有部分功能未实现。
这套主要由main.c && atof.c && stack.c && getch.c && getop.c 构成,头文件share.h对所有函数及共享变量或者外部变量声明。
share.h
main.c
atof.c
stack.c
getch.c
getop.c
这套主要由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 stack typedef struct { //definition of stack double data[MAXVOLUME]; //volume of stack int sp; //top pointer of stack } SqStack; void InitStack(SqStack *s); //stack.c bool Push(SqStack *s, double i); //stack.c bool Pop(SqStack *s, double *i); //stack.c bool StackEmpty(SqStack (*s)); //stack.c void PrintTop(SqStack *s); //stack,c double CopyTop(SqStack *s); //stack.c void SwapTop(SqStack *s); //stack.c void SetStackEmpty(SqStack *s); //stack.c int Getch(void); //getch.c void Ungetch(int c); //getch.c int Getop(char s[]); //getop.c double 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-3 int 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; }
相关文章推荐
- OpenGL Super Bible 第四章 Transform 程序绘图部分代码解析
- OpenGL Super Bible 第四章 Transform 程序绘图部分代码解析
- python学习第四章操作列表部分课后练习自己尝试的代码
- 把我写的部分练习程序代码全部奉献了。
- 用js编写的简单的计算器代码程序
- 200行Java代码编写一个计算器程序
- 计算器程序代码(C语言)
- MVC4制作网站教程第四章 部分栏目功能实现代码
- Linux 下C语言学习(四)——变量的类型和程序各部分代码在内存中的位置
- 《APUE》 第一章 图1-7 程序代码中execlp()部分解析
- python编程练习---一行代码实现计算器功能
- python学习第二章变量和简单数据类型的部分课后练习自己尝试的代码
- Visual C# .Net 计算器部分代码
- 编写了一个计算器小程序,作为栈的练习
- 编译:XAML与程序代码结合(转载)
- 【asp.net 】使用js结合hidden控件实现在后台运行一段程序后提示确认,确认通过后继续执行后台代码~~~
- 《算法导论》第六章----优先级队列(代码实现+部分练习)
- 练习 10:Bash:程序退出代码(返回状态)
- Python 练习 06 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释
- python学习第十一章测试代码部分课后练习自己尝试的代码