Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <math.h>
- typedef struct Stack{ //stack for characters
- double data[255]; //char* array to serve as stack
- int top; //marks where the head is
- }stack;
- void create(stack * s); //initializes stack
- int empty(stack * s); //checks if the stack is empty
- int full(stack * s); //checks if the stack is full
- double pop(stack * s); //pops a double from the stack
- void push(stack * s, double c); //gets a double then places them into the stack
- void process(stack* s, char* postfix);
- double solve(char c, double x, double y);
- double convert(char c); //convert the value from char to double
- int main(){
- int choice; //open a file or not
- stack s; //stack for double inputs
- printf("Open a file? 1/0 ");
- scanf("%d", &choice);
- if(choice == 0){
- int count = 1; //to determine if the user will continue giving statements
- char* postfix;
- create(&s);
- while(count != 0){
- printf("Enter postfix: ");
- scanf("%s", postfix);
- process(&s, postfix);
- printf("Continue? 1/0");
- scanf("%d", &count);
- }
- }
- else if(choice == 1){
- char fn[255]; //file name array
- char buffer[255]; //temporary array for reading lines in the text file to be given
- printf("Input file name: ");
- scanf("%s", fn);
- create(&s);
- FILE *fp;
- fp = fopen(fn, "r"); //opens file that contains multiple postfix commands
- while(!(feof(fp))){ //while not end of file, read contents
- FILE* w;
- w = fopen("output.txt", "w"); //creates an empty txt file
- fclose(w);
- fgets(buffer, sizeof(buffer), (FILE*)fp); //gets the current line (aka the postfix command) and adds to buffer
- process(&s, buffer); //gets the file name of the current loop, the stack, the buffer (postfix), and the number of the current loop
- }
- fclose(fp); //closes the postfix txt
- }
- else
- printf("Give a valid choice.");
- }
- void create(stack * s){
- s -> top = -1; //sets the stack head at -1 thus makes the array inaccessible
- }
- int empty(stack * s){
- if(s->top==-1)
- return 1; //stack is empty
- return 0; //stack is not empty
- }
- int full(stack * s){
- if(s->top==254)//since the data array is only 255 big
- return 1;//stack is full
- return 0;
- }
- double pop(stack * s){
- double x;
- x=s->data[s->top];//gets the value of the data at the current top and assign to x
- s->top=s->top-1; //reduces the current value of top
- return(x);
- }
- void push(stack * s, double c){
- s->top=s->top+1; //moves the top amount to move array position
- s->data[s->top]=c; //assigns the top to the value of c
- }
- void process(stack* s, char* postfix){
- //stuff here
- double x, y;
- double total;
- int i;
- FILE* fp;
- fp = fopen("output.txt", "a");
- fprintf(fp, "%s\n", postfix);
- fclose(fp);
- for(i = 0; i < strlen(postfix); i++){
- if(isalnum(postfix[i])){ //checks if the current postfix character is a number or a letter
- push(s, convert(postfix[i])); //pushes to stack the converted value of the operand
- }
- else{ //if the postfix character is an operand
- if(empty(s)==0){ //checks if the stack is empty
- x = pop(s); //pops the value
- }
- else{ //it means there are no operands anymore to compute
- printf("Form invalid.\n");
- exit(0);
- }
- if(empty(s)==0){ //a repetition of the earlier process but assigned to y
- y = pop(s);
- }
- else{
- printf("Form invalid.\n");
- exit(0);
- }
- total = solve(postfix[i], x, y);
- push(s, total);
- }
- }
- fp = fopen("output.txt", "a");
- total = pop(s); //pops the final pushed value
- if(empty(s)==1){ //if no more operands is left in the stack
- fprintf(fp, "Total is %lf\n", total);
- }
- else{ //something is still in the stack
- fprintf(fp, "Form invalid.\n");
- exit(0);
- }
- fclose(fp);
- }
- double solve(char c, double x, double y){
- FILE* fp;
- fp = fopen("output.txt", "a"); //opens the current loop filename and appends the commands
- char* s;
- switch(c){
- case '+':
- fprintf(fp, "LD %lf\nAD %lf\n", y, x);
- fclose(fp);
- return(y+x);
- break;
- case '-':
- fprintf(fp, "LD %lf\nSB %lf\n", y, x);
- fclose(fp);
- return(y-x);
- break;
- case '*':
- fprintf(fp, "LD %lf\nML %lf\n", y, x);
- fclose(fp);
- return(y*x);
- break;
- case '/':
- fprintf(fp, "LD %lf\nDV %lf\n", y, x);
- fclose(fp);
- return(y/x);
- break;
- case '^':
- fprintf(fp, "LD %lf\nPR %lf\n", x, y);
- fclose(fp);
- return pow(x, y);
- break;
- }
- }
- double convert(char c){ //converts characters to double values
- switch(c){
- case 'A':
- return 1.0;
- break;
- case 'B':
- return 2.0;
- break;
- case 'C':
- return 3.0;
- break;
- case 'D':
- return 4.0;
- break;
- case 'E':
- return 5.0;
- break;
- case 'F':
- return 6.0;
- break;
- case 'G':
- return 7.0;
- break;
- case 'H':
- return 8.0;
- break;
- case 'I':
- return 9.0;
- break;
- case 'J':
- return 10.0;
- break;
- case 'K':
- return 11.0;
- break;
- case 'L':
- return 12.0;
- break;
- case 'M':
- return 13.0;
- break;
- case 'N':
- return 14.0;
- break;
- case 'O':
- return 15.0;
- break;
- case 'P':
- return 16.0;
- break;
- case 'Q':
- return 17.0;
- break;
- case 'R':
- return 18.0;
- break;
- case 'S':
- return 19.0;
- break;
- case 'T':
- return 20.0;
- break;
- case 'U':
- return 21.0;
- break;
- case 'V':
- return 22.0;
- break;
- case 'W':
- return 23.0;
- break;
- case 'X':
- return 24.0;
- break;
- case 'Y':
- return 25.0;
- break;
- case 'Z':
- return 26.0;
- break;
- case 'a':
- return -1.0;
- break;
- case 'b':
- return -2.0;
- break;
- case 'c':
- return -3.0;
- break;
- case 'd':
- return -4.0;
- break;
- case 'e':
- return -5.0;
- break;
- case 'f':
- return -6.0;
- break;
- case 'g':
- return -7.0;
- break;
- case 'h':
- return -8.0;
- break;
- case 'i':
- return -9.0;
- break;
- case 'j':
- return -10.0;
- break;
- case 'k':
- return -11.0;
- break;
- case 'l':
- return -12.0;
- break;
- case 'm':
- return -13.0;
- break;
- case 'n':
- return -14.0;
- break;
- case 'o':
- return -15.0;
- break;
- case 'p':
- return -16.0;
- break;
- case 'q':
- return -17.0;
- break;
- case 'r':
- return -18.0;
- break;
- case 's':
- return -19.0;
- break;
- case 't':
- return -20.0;
- break;
- case 'u':
- return -21.0;
- break;
- case 'v':
- return -22.0;
- break;
- case 'w':
- return -23.0;
- break;
- case 'x':
- return -24.0;
- break;
- case 'y':
- return -25.0;
- break;
- case 'z':
- return -26.0;
- break;
- case '1':
- return 1.0;
- break;
- case '2':
- return 2.0;
- break;
- case '3':
- return 3.0;
- break;
- case '4':
- return 4.0;
- break;
- case '5':
- return 5.0;
- break;
- case '6':
- return 6.0;
- break;
- case '7':
- return 7.0;
- break;
- case '8':
- return 8.0;
- break;
- case '9':
- return 9.0;
- break;
- case '0':
- return 0.0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement