chap4.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "chap4_calculator.c"
#define abs(x) ((x > 0) ? (x) : -(x))
extern void calculator();
int strindex(char s[], char t[])
{
int i, j, k;
int position = -1;
for(i = 0; s[i] != '\0'; i++)
{
for(j=i,k=0; s[j]!='\0' && t[k]!='\0' && t[k]==s[j];k++,j++);
if(k>0 && t[k]=='\0')
position = i;
}
return position;
}
int _strindex(char s[], char t[])
{
int i, j, k;
for(i = strlen(s)-strlen(t); i>=0; i--)
{
for(j=i,k=0; s[j]!='\0' && t[k]!='\0' && t[k]==s[j];k++,j++);
if(k>0 && t[k]=='\0')
return i;
}
return -1;
}
void evaluated_strindex()
{
char s[] = "afscbajcbai";
char t[] = "cba";
printf("rightmost positon : %d \n",strindex(s, t));
printf("rightmost positon : %d \n",_strindex(s, t));
}
double _atof(char s[])
{
double val, power;
int i, sign, exp;
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;
}
val = sign * val / power;
if(s[i] == 'e' || s[i] == 'E')
{
sign = (s[++i] == '-') ? -1 : 1;
if(s[i] == '-' || s[i] == '+')
i++;
for(exp = 0; isdigit(s[i]); i++)
exp = 10 * exp + (s[i] - '0');
if(sign == -1)
while(exp-- > 0)
val /=10;
else
while(exp-- > 0)
val *= 10;
}
return val;
}
void calculator_()
{
calculator();
}
void itoa(int n, char s[]) {
static int i = 0;
if (n/10)
itoa(n/10, s);
else {
if (n < 0)
s[i++] = '-';
}
s[i++] = abs(n%10) + '0';
s[i] = '\0';
}
void reverse(char s[])
{
void reverse_r(char [], int index, int len);
reverse_r(s, 0, strlen(s));
}
void reverse_r(char s[], int index, int len)
{
int temp, j;
j = len - (index + 1);
if (index < j) {
temp = s[index];
s[index] = s[j];
s[j] = temp;
reverse_r(s, ++index, len);
}
}
#define swap(t, x, y) { t temp;\
temp = x; \
x = y; \
y = temp; \
}
main()
{
calculator_();
}
chap4_calculator.c //handle calculator
#include <stdio.h>
#include "stack.c"
#include "getop.c"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define NUMBER '0'
#define MATHFUN 'n'
extern void push(double);
extern double pop(void);
extern void clear(void);
extern int getop(char []);
extern double mathfun(char []);
void calculator()
{
int type, i, var = 0;
double op2, v;
char strop[100];
double variables[26];
for (i = 0; i < 26; i++)
variables[i] = 0.0;
while ((type = getop_line(strop)) !=EOF) {
switch (type) {
case NUMBER:
push(atof(strop));
break;
case MATHFUN:
mathfun(strop);
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
printf("debug - : \n");
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 > 0.0)
push(pop() / op2);
else
printf("errot : zero divisor \n");
break;
case '%':
op2 = pop();
if (op2 > 0.0)
push((int)pop() % (int)op2);
else
printf("errot : zero divisor \n");
break;
case 'd':
op2 = pop();
push(op2);
printf("ths number top of stack is : %.5g\n", op2);
break;
case 's':
op2 = pop();
double temp = pop();
push(op2);
push(temp);
printf("swap successful \n");
break;
case 'c':
clear();
printf("clear the stack successful \n");
break;
case '=':
if (var >= 'A' && var <= 'Z') {
pop();
variables[var-'A'] = pop();
}
case '\n':
v = pop();
printf("the result is : %g \n", v);
break;
default :
if (type >= 'A' && type <= 'Z')
push(variables[type - 'A']);
else if (type == 'v')
push(v);
else
printf("error input ,unknown command %s\n",strop);
}
var = type;
}
}
double mathfun(char s[])
{
double temp;
if (strcmp(s, "sin") == 0)
push(sin(pop()));
else if (strcmp(s, "exp") == 0)
push(exp(pop()));
else if (strcmp(s, "pow") == 0) {
temp = pop();
push(pow(pop(), temp));
}
else {
printf("mathfun errot : input %s , I can't understading \n ", s);
}
}
getop.c
#pragma once
#include <string.h>
#include <ctype.h>
#include "getch.c"
#define MAXSIZE 100;
#include <stdio.h>
extern int getch(void);
extern void ungetch(int);
extern int _getline(char [], int limit);
int getop(char opstr[])
{
int cr, next;
int i = 0;
while ((cr = getch()) == ' ' || cr == '\t');
if (islower(cr))
{
opstr[i++] = cr;
while (islower(opstr[i++] = cr = getch()));
opstr[i-1] = '\0';
printf("handle the mathfun : %s, lentgh=%d \n", opstr, (int)strlen(opstr));
if (cr != EOF)
ungetch(cr);
if (strlen(opstr) > 1)
return 'n';
else
return cr;
}
if (isdigit(cr)==0 && cr != '.' && cr != '-')
{
opstr[i] = '\0';
return cr;
}
if (cr == '-')
{
if (isdigit(cr = getch()) || cr == '.')
{
opstr[i++] = '-';
opstr[i++] = cr;
cr = getch();
}
else
{
if (cr != EOF)
ungetch(cr);
opstr[i] = '\0';
return '-';
}
}
while(isdigit(cr))
{
opstr[i++] = cr;
if (cr != EOF)
cr = getch();
};
if (cr == '.') {
opstr[i] = '.';
while(isdigit(opstr[++i] = cr = getch()))
;
}
opstr[i] = '\0';
if (cr != EOF)
ungetch(cr);
printf("collect the digit : %s, lentgh=%d \n", opstr, (int)strlen(opstr));
return '0';
}
int li = 0;
char line[100];
int getop_line(char opstr[])
{
int cr, next;
int i = 0;
if (line[li] == '\0') {
if (!_getline(line, 100)) {
opstr[i] = '\0';
return EOF;
}
li = 0;
}
while ((cr = line[li++]) == ' ' || cr == '\t')
;
if (islower(cr))
{
opstr[i++] = cr;
while (islower(opstr[i++] = cr = line[li++]));
opstr[i-1] = '\0';
printf("handle the mathfun : %s, lentgh=%d \n", opstr, (int)strlen(opstr));
if (cr != EOF)
li--;
if (strlen(opstr) > 1)
return 'n';
else
return cr;
}
if (isdigit(cr)==0 && cr != '.' && cr != '-')
{
opstr[i] = '\0';
return cr;
}
if (cr == '-')
{
if (isdigit(cr = line[li++]) || cr == '.')
{
opstr[i++] = '-';
opstr[i++] = cr;
cr = line[li++];
}
else
{
if (cr != EOF)
li--;
opstr[i] = '\0';
return '-';
}
}
while(isdigit(cr))
{
opstr[i++] = cr;
if (cr != EOF)
cr = line[li++];
};
if (cr == '.') {
opstr[i] = '.';
while(isdigit(opstr[++i] = cr = line[li++]))
;
}
opstr[i] = '\0';
if (cr != EOF)
li--;
printf("collect the digit : %s, lentgh=%d \n", opstr, (int)strlen(opstr));
return '0';
}
getch.c
#define BUFFSIZE 100
#pragma once
#include <stdio.h>
#include <string.h>
char buf[BUFFSIZE];
int bufp = 0;
int getch(void)
{
return (bufp > 0)? buf[--bufp]: getchar();
}
void ungetch(int c)
{
if(bufp < BUFFSIZE)
buf[bufp++] = c;
else
printf("ungetch : too many characters \n");
}
void ungets(char s[])
{
int i = 0;
for (i = strlen(s) - 1; i >= 0; i--) {
buf[bufp++] = s[i];
}
}
int _getline(char line[],int lim)
{
int c,i;
for(i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';i++)
line[i] = c;
if(c == '\n')
{
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}
stack.c
#define NUMBER '0'
#define MAXVAL 100
double val[MAXVAL];
int sp=0;
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
}
double pop(void)
{
if (sp > 0)
return val[--sp];
else
printf("pop : the stack is empty \n");
return 0.0;
}
void clear(void)
{
sp =0;
}
getch_4-8.c
#include <stdio.h>
char buf = '\0';
int getch()
{
int c;
if (buf != '0') {
c = buf;
buf = '\0';
}
else
c = getchar();
return c;
}
void ungetch(int cr)
{
if (buf != '\0')
printf("ungetch : too many characters \n ");
else
buf = cr;
}