#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAXOP 100
#define NUMBER 0
#define REFRESH -1
#define IDENTIFIE 1
#define MAXNAME 32
#define MAXARR 30
/*Stat creation 2017.11.5 */
struct variables{
char names[MAXNAME];
double val;
};
int getlines(char *s,int);
int charcyp(char *,char *);
void clearemptys2(struct variables *s);
void machfuncs(char *s,struct variables *t);
void variablecyps(struct variables *t,char *s);
void push(double);
double pop(void);
int indexs = 0; /*character string index*/
int pos = 0; /* array index */
struct variables last;
int main(void)
{
int type;
double op2;
char s[MAXOP];
char cyps[MAXOP];
struct variables arrays[MAXARR];
clearemptys2(arrays);
while(getlines(s,MAXOP) > 0)
{
while((type = charcyp(s,cyps)) != REFRESH )
{
switch(type)
{
case NUMBER:
push(atof(cyps));
break;
case IDENTIFIE:
machfuncs(cyps,arrays);
break;
case '+':
push(pop()+pop());
indexs++;
break;
case '*':
push(pop()*pop());
indexs++;
break;
case '-':
op2 = pop();
push(pop()-op2);
indexs++;
break;
case '/':
op2 = pop();
if(op2)
push(pop()/op2);
else
printf("Error:divisior by zero!\n");
indexs++;
break;
case '%':
op2 = pop();
if(op2)
push(fmod(pop(),op2));
else
printf("Error:division by zero!\n");
indexs++;
break;
case '=':
arrays[pos].val = pop();
push(arrays[pos].val);
indexs++;
break;
case '\n':
last.val = pop();
printf("\t%.8g\n",last.val);
indexs++;
break;
case '~':
clearemptys2(arrays);
indexs++;
break;
default:
printf("Error: unknow command %s!\n",cyps);
indexs++;
break;
}
}
indexs = 0;
}
return 0;
}
int getlines(char *s,int lim)
{
int i,c;
i = 0;
while(i < lim -1 && (c = getchar()) != EOF && c != '\n' )
s[i++] = c;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int charcyp(char *s,char *t)
{
int i,c;
i = 0;
while(s[indexs] == ' ' || s[indexs] == '\t' )
indexs++;
if(!isdigit(s[indexs]) && s[indexs] != '.' && s[indexs] != '-')
{
if(isalpha(s[indexs]))
{
while(isalpha(t[i] = s[indexs]))
{
i++;
indexs++;
}
t[i] = '\0';
return IDENTIFIE;
}
if('\0' == s[indexs])
return REFRESH;
return s[indexs];
}
if(s[indexs] == '-')
{
c = indexs;
if(!isdigit(s[++c]) && s[c] != '.')
return s[indexs];
t[i] = s[indexs];
indexs++;
i++;
}
if(isdigit(s[indexs]))
while(isdigit(t[i] = s[indexs]))
{
i++;
indexs++;
}
if(s[indexs] == '.')
{
t[i] = s[indexs];
while(isdigit(t[++i] = s[++indexs]))
;
}
t[i] = '\0';
return NUMBER;
}
void machfuncs(char *s,struct variables *t)
{
double op;
if(!strcmp(s,"sin"))
push(sin(pop()));
else if(!strcmp(s,"cos"))
push(sin(pop()));
else if(!strcmp(s,"exp"))
push(exp(pop()));
else if(!strcmp(s,"pow"))
{
op = pop();
push(pow(pop(),op));
}
else
variablecyps(t,s);
}
void variablecyps(struct variables *t1,char *s1)
{
int i;
i = 0;
while(t1[i].names[0] != '\0' && i < MAXARR)
{
if(!strcmp(t1[i].names,s1))
{
pos = i;
return ;
}
i++;
}
strcpy(t1[i].names,s1);
strcpy(last.names,s1);
pos = i;
}
void clearemptys2(struct variables *s)
{
int i;
for(i = 0; i < MAXNAME; i++)
{
s[i].names[0] = '\0';
s[i].val = 0.0;
}
}
#define MAXSTACK 100
double array[MAXSTACK];
int size = 0;
void push(double x)
{
if(size >= MAXSTACK)
printf("Error: Stack full,can't push %g\n",x);
else
array[size++] = x;
}
double pop(void)
{
if(size > 0)
return array[--size];
else{
printf("error: stack empty!\n");
return 0.0;
}
}
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAXOP 100
#define NUMBER 0
#define REFRESH -1
#define IDENTIFIE 1
#define MAXNAME 32
#define MAXARR 30
/*Stat creation 2017.11.5 */
struct variables{
char names[MAXNAME];
double val;
};
int getlines(char *s,int);
int charcyp(char *,char *);
void clearemptys2(struct variables *s);
void machfuncs(char *s,struct variables *t);
void variablecyps(struct variables *t,char *s);
void push(double);
double pop(void);
int indexs = 0; /*character string index*/
int pos = 0; /* array index */
struct variables last;
int main(void)
{
int type;
double op2;
char s[MAXOP];
char cyps[MAXOP];
struct variables arrays[MAXARR];
clearemptys2(arrays);
while(getlines(s,MAXOP) > 0)
{
while((type = charcyp(s,cyps)) != REFRESH )
{
switch(type)
{
case NUMBER:
push(atof(cyps));
break;
case IDENTIFIE:
machfuncs(cyps,arrays);
break;
case '+':
push(pop()+pop());
indexs++;
break;
case '*':
push(pop()*pop());
indexs++;
break;
case '-':
op2 = pop();
push(pop()-op2);
indexs++;
break;
case '/':
op2 = pop();
if(op2)
push(pop()/op2);
else
printf("Error:divisior by zero!\n");
indexs++;
break;
case '%':
op2 = pop();
if(op2)
push(fmod(pop(),op2));
else
printf("Error:division by zero!\n");
indexs++;
break;
case '=':
arrays[pos].val = pop();
push(arrays[pos].val);
indexs++;
break;
case '\n':
last.val = pop();
printf("\t%.8g\n",last.val);
indexs++;
break;
case '~':
clearemptys2(arrays);
indexs++;
break;
default:
printf("Error: unknow command %s!\n",cyps);
indexs++;
break;
}
}
indexs = 0;
}
return 0;
}
int getlines(char *s,int lim)
{
int i,c;
i = 0;
while(i < lim -1 && (c = getchar()) != EOF && c != '\n' )
s[i++] = c;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int charcyp(char *s,char *t)
{
int i,c;
i = 0;
while(s[indexs] == ' ' || s[indexs] == '\t' )
indexs++;
if(!isdigit(s[indexs]) && s[indexs] != '.' && s[indexs] != '-')
{
if(isalpha(s[indexs]))
{
while(isalpha(t[i] = s[indexs]))
{
i++;
indexs++;
}
t[i] = '\0';
return IDENTIFIE;
}
if('\0' == s[indexs])
return REFRESH;
return s[indexs];
}
if(s[indexs] == '-')
{
c = indexs;
if(!isdigit(s[++c]) && s[c] != '.')
return s[indexs];
t[i] = s[indexs];
indexs++;
i++;
}
if(isdigit(s[indexs]))
while(isdigit(t[i] = s[indexs]))
{
i++;
indexs++;
}
if(s[indexs] == '.')
{
t[i] = s[indexs];
while(isdigit(t[++i] = s[++indexs]))
;
}
t[i] = '\0';
return NUMBER;
}
void machfuncs(char *s,struct variables *t)
{
double op;
if(!strcmp(s,"sin"))
push(sin(pop()));
else if(!strcmp(s,"cos"))
push(sin(pop()));
else if(!strcmp(s,"exp"))
push(exp(pop()));
else if(!strcmp(s,"pow"))
{
op = pop();
push(pow(pop(),op));
}
else
variablecyps(t,s);
}
void variablecyps(struct variables *t1,char *s1)
{
int i;
i = 0;
while(t1[i].names[0] != '\0' && i < MAXARR)
{
if(!strcmp(t1[i].names,s1))
{
pos = i;
return ;
}
i++;
}
strcpy(t1[i].names,s1);
strcpy(last.names,s1);
pos = i;
}
void clearemptys2(struct variables *s)
{
int i;
for(i = 0; i < MAXNAME; i++)
{
s[i].names[0] = '\0';
s[i].val = 0.0;
}
}
#define MAXSTACK 100
double array[MAXSTACK];
int size = 0;
void push(double x)
{
if(size >= MAXSTACK)
printf("Error: Stack full,can't push %g\n",x);
else
array[size++] = x;
}
double pop(void)
{
if(size > 0)
return array[--size];
else{
printf("error: stack empty!\n");
return 0.0;
}
}