diff options
author | sinanmohd <pcmsinan@gmail.com> | 2022-06-13 11:30:45 +0530 |
---|---|---|
committer | sinanmohd <pcmsinan@gmail.com> | 2022-06-13 11:31:05 +0530 |
commit | c67ff09a3f010b481751a0b6983239217062703d (patch) | |
tree | efcc3ed8cdbcc9571be20b1499e8cb214bf386de | |
parent | 31a036f54543af5c6d9cabea58b021620e888035 (diff) |
add till 4.6
-rw-r--r-- | 4.2.c | 4 | ||||
-rw-r--r-- | 4.3.c | 132 | ||||
-rw-r--r-- | 4.4.c | 178 | ||||
-rw-r--r-- | 4.5.c | 207 | ||||
-rw-r--r-- | 4.6.c | 232 |
5 files changed, 751 insertions, 2 deletions
@@ -71,10 +71,10 @@ atof(const char str[]) if (e_sign < 0) for (j = 0; j < e_power; j++) - power *= 10; + power *= 10.0; else for (j = 0; j < e_power; j++) - power /= 10; + power /= 10.0; return (sign * val / power); } @@ -0,0 +1,132 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <math.h> + +#define MAXOP 100 +#define NUMBER_SIG '0' + +double pop(void); +void push(double f); +char getop(char str[]); + +int +main(void) +{ + char str[MAXOP]; + char type; + double op2; + + while ((type = getop(str)) != EOF) { + switch (type) { + case NUMBER_SIG : + push(atof(str)); + break; + case '+' : + push(pop() + pop()); + break; + case '*' : + push(pop() * pop()); + break; + case '-' : + op2 = pop(); + push(pop() - op2); + break; + case '/' : + if ((op2 = pop()) == 0.0) + printf("Err: deviser cant be zero\n"); + else + push(pop() / op2); + + break; + case '%' : + op2 = pop(); + if (op2 != 0) + push(fmod(pop(), op2)); + else + printf("Err: deviser cant be zero\n"); + + break; + case '\n' : + printf("%.8g\n", pop()); + break; + default : + printf("Err: unknown command\n"); + } + } + + return 0; +} + +#define MAXVAL 100 +double val[MAXVAL]; +int sp = 0; + +double +pop(void) +{ + if (sp > 0) + return val[--sp]; + else { + printf("Err: stack empty\n"); + return 0.0; + } +} + +void +push(double f) +{ + if (sp < MAXVAL) + val[sp++] = f; + else + printf("Err: stack is full\n"); +} + +char getch(void); +void ungetch(char input); + +char +getop(char str[]) +{ + char input; + int i; + + /* ignore blanks */ + while ((input = str[0] = getch()) == ' ' || input == '\t'); + + /* return operator */ + if (!isdigit(input) && input != '.') + return input; + + /* collect digits*/ + for (i = 1; i < MAXOP && (isdigit(input=getch()) || input == '.'); i++) + str[i] = input; + + str[i] = '\0'; + + if (input != EOF) + ungetch(input); + + return NUMBER_SIG; +} + +#define MAXBUFF 100 +char buff[MAXBUFF]; +int bp = 0; + +char +getch(void) +{ + return (bp > 0) ? buff[--bp] : getchar(); +} + +void +ungetch(char input) +{ + if (bp < MAXBUFF) + buff[bp++] = input; + else { + printf("Err: buffer full\n"); + } +} + @@ -0,0 +1,178 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <math.h> + +#define MAXOP 100 +#define NUMBER_SIG '0' + +double pop(void); +void push(double f); +char getop(char str[]); +void ptop(void); +void duplitop(void); +void swaptoptwo(void); +void clearstack(void); + +int +main(void) +{ + char str[MAXOP]; + char type; + double op2; + + while ((type = getop(str)) != EOF) { + switch (type) { + case NUMBER_SIG : + push(atof(str)); + break; + case '+' : + push(pop() + pop()); + break; + case '*' : + push(pop() * pop()); + break; + case '-' : + op2 = pop(); + push(pop() - op2); + break; + case '/' : + if ((op2 = pop()) == 0.0) + printf("Err: deviser cant be zero\n"); + else + push(pop() / op2); + break; + case '%' : + op2 = pop(); + if (op2 != 0) + push(fmod(pop(), op2)); + else + printf("Err: deviser cant be zero\n"); + break; + case '^' : + ptop(); + break; + case '>' : + duplitop(); + break; + case '#' : + swaptoptwo(); + break; + case '<' : + clearstack(); + break; + case '\n' : + printf("%.8g\n", pop()); + break; + default : + printf("Err: unknown command\n"); + } + } + + return 0; +} + +#define MAXVAL 100 +double val[MAXVAL]; +int sp = 0; + +void +ptop(void) +{ + if (sp > 0) + printf("Top of the stack: %.8g\n", val[sp-1]); + else + printf("Err: stack is empty\n"); +} + +void +duplitop(void) +{ + val[sp] = val[sp-1]; + ++sp; +} + +void +swaptoptwo(void) +{ + double temp1 = pop(); + double temp2 = pop(); + + push(temp1); + push(temp2); +} + +void +clearstack(void) +{ + sp = 0; +} + +double +pop(void) +{ + if (sp > 0) + return val[--sp]; + else { + printf("Err: stack empty\n"); + return 0.0; + } +} + +void +push(double f) +{ + if (sp < MAXVAL) + val[sp++] = f; + else + printf("Err: stack is full\n"); +} + +char getch(void); +void ungetch(char input); + +char +getop(char str[]) +{ + char input; + int i; + + /* ignore blanks */ + while ((input = str[0] = getch()) == ' ' || input == '\t'); + + /* return operator */ + if (!isdigit(input) && input != '.') + return input; + + /* collect digits*/ + for (i = 1; i < MAXOP && (isdigit(input=getch()) || input == '.'); i++) + str[i] = input; + + str[i] = '\0'; + + if (input != EOF) + ungetch(input); + + return NUMBER_SIG; +} + +#define MAXBUFF 100 +char buff[MAXBUFF]; +int bp = 0; + +char +getch(void) +{ + return (bp > 0) ? buff[--bp] : getchar(); +} + +void +ungetch(char input) +{ + if (bp < MAXBUFF) + buff[bp++] = input; + else { + printf("Err: buffer full\n"); + } +} + @@ -0,0 +1,207 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <math.h> +#include <string.h> + +#define MAXOP 100 +#define NUMBER_SIG '0' +#define SPECIAL_SIG '1' + +double pop(void); +void push(double f); +char getop(char str[]); +void ptop(void); +void duplitop(void); +void swaptoptwo(void); +void clearstack(void); +void do_special(char str[]); + +int +main(void) +{ + char str[MAXOP]; + char type; + double op2; + + while ((type = getop(str)) != EOF) { + switch (type) { + case NUMBER_SIG : + push(atof(str)); + break; + case SPECIAL_SIG : + do_special(str); + break; + case '+' : + push(pop() + pop()); + break; + case '*' : + push(pop() * pop()); + break; + case '-' : + op2 = pop(); + push(pop() - op2); + break; + case '/' : + if ((op2 = pop()) == 0.0) + printf("Err: deviser cant be zero\n"); + else + push(pop() / op2); + break; + case '%' : + op2 = pop(); + if (op2 != 0) + push(fmod(pop(), op2)); + else + printf("Err: deviser cant be zero\n"); + break; + case '^' : + ptop(); + break; + case '>' : + duplitop(); + break; + case '#' : + swaptoptwo(); + break; + case '<' : + clearstack(); + break; + case '\n' : + printf("%.8g\n", pop()); + break; + default : + printf("Err: unknown command\n"); + } + } + + return 0; +} + +#define MAXVAL 100 +double val[MAXVAL]; +int sp = 0; + +void +do_special(char str[]) +{ + double tempop; + + if (!strcmp(str, "sin")) + push(sin(pop())); + else if (!strcmp(str, "exp")) + push(exp(pop())); + else if (!strcmp(str, "pow")) { + tempop = pop(); + push(pow(pop(), tempop)); + } +} + +void +ptop(void) +{ + if (sp > 0) + printf("Top of the stack: %.8g\n", val[sp-1]); + else + printf("Err: stack is empty\n"); +} + +void +duplitop(void) +{ + val[sp] = val[sp-1]; + ++sp; +} + +void +swaptoptwo(void) +{ + double temp1 = pop(); + double temp2 = pop(); + + push(temp1); + push(temp2); +} + +void +clearstack(void) +{ + sp = 0; +} + +double +pop(void) +{ + if (sp > 0) + return val[--sp]; + else { + printf("Err: stack empty\n"); + return 0.0; + } +} + +void +push(double f) +{ + if (sp < MAXVAL) + val[sp++] = f; + else + printf("Err: stack is full\n"); +} + +char getch(void); +void ungetch(char input); + +char +getop(char str[]) +{ + char input; + int i; + + /* ignore blanks */ + while ((input = str[0] = getch()) == ' ' || input == '\t'); + + if (isalpha(input)) { + i = 1; + while(isalpha(str[i++] = input = getch())); + str[--i] = '\0'; + ungetch(input); + return SPECIAL_SIG; + } + + /* return operator */ + if (!isdigit(input) && input != '.') + return input; + + /* collect digits*/ + for (i = 1; i < MAXOP && (isdigit(input=getch()) || input == '.'); i++) + str[i] = input; + + str[i] = '\0'; + + if (input != EOF) + ungetch(input); + + return NUMBER_SIG; +} + +#define MAXBUFF 100 +char buff[MAXBUFF]; +int bp = 0; + +char +getch(void) +{ + return (bp > 0) ? buff[--bp] : getchar(); +} + +void +ungetch(char input) +{ + if (bp < MAXBUFF) + buff[bp++] = input; + else { + printf("Err: buffer full\n"); + } +} + @@ -0,0 +1,232 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <math.h> +#include <string.h> + +#define MAXOP 100 +#define NUMBER_SIG '0' +#define SPECIAL_SIG '1' +#define VARIABLE_SIG '2' +#define ALPHABET_LEN 26 +#define ON 1 +#define OFF 0 +#define MAXBUFF 100 + +char buff[MAXBUFF]; +double pop(void); +void push(double f); +char getop(char str[]); +void ptop(void); +void duplitop(void); +void swaptoptwo(void); +void clearstack(void); +void do_special(char str[]); +char getch(void); +void ungetch(char input); +double last_print; + +int bp = 0; + +int +main(void) +{ + char str[MAXOP]; + char vars[ALPHABET_LEN]; + char vars_state[ALPHABET_LEN]; + char type; + double op2; + + for (int i = 0; i < ALPHABET_LEN; i++) + vars_state[i] = OFF; + + vars_state[ALPHABET_LEN-1] = '\0'; + + while ((type = getop(str)) != EOF) { + switch (type) { + case NUMBER_SIG : + push(atof(str)); + break; + case SPECIAL_SIG : + do_special(str); + break; + case VARIABLE_SIG : + /* Assuming variabke can only be set once */ + if (vars_state[*str-'a'] == 1) + push(vars[*str-'a']); + else { + vars_state[*str-'a'] = ON; + push(vars[*str - 'a'] = pop()); + } + break; + case '+' : + push(pop() + pop()); + break; + case '*' : + push(pop() * pop()); + break; + case '-' : + op2 = pop(); + push(pop() - op2); + break; + case '/' : + if ((op2 = pop()) == 0.0) + printf("Err: deviser cant be zero\n"); + else + push(pop() / op2); + break; + case '%' : + op2 = pop(); + if (op2 != 0) + push(fmod(pop(), op2)); + else + printf("Err: deviser cant be zero\n"); + break; + case '^' : + ptop(); + break; + case '>' : + duplitop(); + break; + case '#' : + swaptoptwo(); + break; + case '<' : + clearstack(); + break; + case '\n' : + printf("%.8g\n", (last_print = pop())); + break; + default : + printf("Err: unknown command\n"); + } + } + + return 0; +} + +#define MAXVAL 100 +double val[MAXVAL]; +int sp = 0; + +void +do_special(char str[]) +{ + double tempop; + + if (!strcmp(str, "sin")) + push(sin(pop())); + else if (!strcmp(str, "exp")) + push(exp(pop())); + else if (!strcmp(str, "pow")) { + tempop = pop(); + push(pow(pop(), tempop)); + } +} + +void +ptop(void) +{ + if (sp > 0) + printf("Top of the stack: %.8g\n", (last_print = val[sp-1])); + else + printf("Err: stack is empty\n"); +} + +void +duplitop(void) +{ + val[sp] = val[sp-1]; + ++sp; +} + +void +swaptoptwo(void) +{ + double temp1 = pop(); + double temp2 = pop(); + + push(temp1); + push(temp2); +} + +void +clearstack(void) +{ + sp = 0; +} + +double +pop(void) +{ + if (sp > 0) + return val[--sp]; + else { + printf("Err: stack empty\n"); + return 0.0; + } +} + +void +push(double f) +{ + if (sp < MAXVAL) + val[sp++] = f; + else + printf("Err: stack is full\n"); +} + +char +getop(char str[]) +{ + char input; + int i; + + /* ignore blanks */ + while ((input = str[0] = getch()) == ' ' || input == '\t'); + + if (isalpha(input)) { + i = 1; + while(isalpha(str[i++] = input = getch())); + str[--i] = '\0'; + ungetch(input); + + /* all single chars are reserved for variables*/ + if (i == 1) + return VARIABLE_SIG; + + return SPECIAL_SIG; + } + + /* return operator */ + if (!isdigit(input) && input != '.') + return input; + + /* collect digits*/ + for (i = 1; i < MAXOP && (isdigit(input=getch()) || input == '.'); i++) + str[i] = input; + + str[i] = '\0'; + + if (input != EOF) + ungetch(input); + + return NUMBER_SIG; +} + +char +getch(void) +{ + return (bp > 0) ? buff[--bp] : getchar(); +} + +void +ungetch(char input) +{ + if (bp < MAXBUFF) + buff[bp++] = input; + else { + printf("Err: buffer full\n"); + } +} + |