Assignment to develop a non-scientific calculator using C Language-81998

For this assignment, you are required to develop a non-scientific calculator using C. Your calculator

should only solve summation, subtraction, division, product, square root and square expressions.

Utilizing the math.h header for your program, your calculator should start by prompting the user to

enter a two-operand mathematical equation (for example 3.74 + 22 or 35.32 – 6.2) just like how you

would using a non-scientific calculator. Your program should then display the correct answer and

the user will have the choice to either continue using the calculator with the current answer to perform

further calculations or end the program. Your program should also have a help function to display

all the functions of the calculator

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<string.h>

#define MAX 512

double memory;

double stack_operend[128];

char stack_op[128];

int operend_top=-1;

int op_top=-1;

void help(){

                printf(“\n”);

                printf(“%-15sExits this program.\n”, “EXIT”);

                printf(“%-15sDisplays information about this program.\n”, “HELP”);

                printf(“%-15sAs part of a mathematical expression the term MEMORY\n”, “MEMORY”);

                printf(“%-15sis substituted by the value stored in memory. Otherwise,\n”,””);

                printf(“%-15sthe value stored in memory is displayed on-screen.\n”, “”);

                printf(“%-15sErases stored in memory and returns calculator to its\n”, “RESET”);

                printf(“%-15sinitial \’start-up\’ mode.\n”, “”);

                printf(“%-15sSaves current answer to memory.\n\n”, “STORE”);

                printf(“%-15s%-15s%-15s\n”, “OPERATOR”, “DESCRIPTION”, “SYNTAX”);

                printf(“%-15s%-15s%-15s\n”, “+”, “addition”, “[a + b|+a]”);

                printf(“%-15s%-15s%-15s\n”, “-“, “subtraction”, “[a – b|-a]”);

                printf(“%-15s%-15s%-15s\n”, “*”, “multiplication”, “[a * b|*a]”);

                printf(“%-15s%-15s%-15s\n”, “/”, “division”, “[a / b|/a]”);

                printf(“%-15s%-15s%-15s\n”, “^”, “sqr(x)”, “a^”);

                printf(“%-15s%-15s%-15s\n”, “#”, “sqrt(x)”, “a^”);

                printf(“Example:\ta^ +b#/ MEMORY (spacing optional)\n”);

}

int op_priority(char op){

        switch(op){

                case ‘+’:

                        return 1;

                case ‘-‘:

                        return 1;

                case ‘*’:

                        return 2;

                case ‘/’:

                        return 2;

                case ‘^’:

                        return 3;

                case ‘#’:

                        return 3;

        }

}

double read_operend(char *Input, int *i, int n)

{

                int j=*i;

                char buf[128];

                while(j<n){

                                if((Input[j] == ‘+’) || (Input[j] == ‘-‘) || (Input[j] == ‘*’) ||

                                                (Input[j] == ‘/’) || (Input[j] == ‘^’) || (Input[j] == ‘#’) || (Input[j] == ‘ ‘))

                                                break;

                                buf[j-*i] = Input[j];

                                j++;

                }

                buf[j-*i]= ‘\0’;

                *i=j;

                if(!strcmp(buf, “MEMORY”) || !(strcmp(buf, “memory”)))

                                return memory;

                return atof(buf);

}

int is_operend(char c){

                if(((c >= 48) && (c <= 57)) || (c == ‘m’) || (c == ‘M’) || (c == ‘.’))

                                return 1;

                return 0;

}

int is_op(char c){

                if((c == ‘+’) || (c == ‘-‘) || (c == ‘*’) || (c == ‘/’) || (c == ‘^’) || (c == ‘#’))

                                return 1;

                return 0;

}

double calop(char op){

                double a,b;

                a = stack_operend[operend_top–];

                if(operend_top == -1)

                                b=0;

                else

                                b = stack_operend[operend_top–];

                switch(op){

                case ‘+’:

                                                return (b+a);

                case ‘-‘:

                                                return (b-a);

                case ‘*’:

                        return (b*a);

                case ‘/’:

                        return (b/a);

                case ‘^’:

                                                if(b != 0)

                                                                stack_operend[++operend_top] = b;

                        return pow(a, 2);

                case ‘#’:

                                                if(b != 0)

                                stack_operend[++operend_top] = b;

                        return sqrt(a);

        }

}

void calc(char done){

                double val=0;

                if(op_top == -1)

                                return;

                if(done == ‘y’){

                                while(op_top != -1){

                                                val = calop(stack_op[op_top–]);

                                                stack_operend[++operend_top] = val;

                                }

                }else{

                                if(op_top < 1)

                                                return;

                                if(op_priority(stack_op[op_top]) > op_priority(stack_op[op_top-1])){

                                                val = calop(stack_op[op_top–]);

                                                stack_operend[++operend_top] = val;

                                }

                }

}

int main(int argc, char *argv[]){

                char Input[512], buf[512];

                char is_answer_exist=’n’;

                double answer=0;

                char is_memory_exist=’n’;

                int i, j, n;

                char last_token;

                printf(“\nType \”HELP\” or enter a mathematical expression\n\n”);

                while(1){

                                printf(“Calc:\\> “);

                                fgets(Input, MAX, stdin);

                                Input[strlen(Input)-1] = ‘\0’;

                                if(!strcmp(Input, “”)){

                                                answer = 0;

                                                printf(“\nType \”HELP\” or enter a mathematical expression\n\n”);

                                                continue;

                                }

                                if(!strcmp(Input, “help”) || !strcmp(Input, “HELP”)){

                                                help();

                                                continue;

                                }

                                if(!strcmp(Input, “store”) || !strcmp(Input, “STORE”)){

                                                if(is_answer_exist == ‘y’){

                                                                memory = answer;

                                                                is_memory_exist=’y’;

                                                                printf(“ANSWER STORED IN MEMORY\n”);

                                                } else{

                                                                printf(“NO ANSWER TO STORE IN MEMORY\n”);

                                                }

                                                continue;

                                }

                                if(!strcmp(Input, “reset”) || !strcmp(Input, “RESET”)){

                                                system(“clear”);

                                                printf(“\nType \”HELP\” or enter a mathematical expression\n\n”);

                                                is_memory_exist=’n’;

                                                continue;

                                }

                                if(!strcmp(Input, “exit”) || !strcmp(Input, “EXIT”)){

                                                printf(“\n\n”);

                                                return 0;

                                }

                                i=0;

                                n = strlen(Input);

                                while(1){

                                                if(is_operend(Input[i])){

                                                                stack_operend[++operend_top] = read_operend(Input, &i, n);

                                                                last_token = ‘e’;

                                                }else if(is_op(Input[i])){

                                                                if(operend_top == -1)

                                                                                stack_operend[++operend_top] = answer;

                                                                stack_op[++op_top] = Input[i];

                                                                i++;

                                                                last_token = ‘o’;

                                                }

                                                if(Input[i] == ‘ ‘){

                                                                if(last_token == ‘o’){

                                                                                char op = stack_op[op_top–];

                                                                                calc(‘y’);

                                                                                stack_op[++op_top] = op;

                                                                }else{

                                                                                calc(‘y’);

                                                                }

                                                                i++;

                                                                continue;

                                                }

                                                if(i == n){

                                                                calc(‘y’);

                                                                answer = stack_operend[operend_top–];

                                                                is_answer_exist = ‘y’;

                                                                break;

                                                }

                                                calc(‘n’);

                                }

                                printf(“Answer:\\> %.6f\n\n”, answer);

                }

}