您的位置:首页 > 编程语言

第四章计算器程序部分代码,结合练习

2014-01-25 19:28 459 查看
为了练习多文件组织编程,同时为了能和后面学习的内容结合起来,这套程序改了很多次,至今还有部分功能未实现。

这套主要由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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: