In: Computer Science
C data structure
Initially, we do not need to do any error checking - we can assume that the input postfix expression is syntactically correct.
Input
The input is a string that represents a postfix expression. To simplify the problem, your calculator will be restricted to operating on single-digit non-negative integers. Only the following characters are allowed in the string:
the operators '+', '-'. '*', and '/'
the digits '0' through '9'
the space (blank) character ' '
Example input:
7 8 5 * +
Processing
To handle spaces in the input, need to make one minor addition to the algorithm
if (ch is a blank)
ignore it
end if
Program should ask the user to enter a text file with postfix expression on a single line. we can either read the line one character at a time, or can read the line into a string and then process the string one character at a time. Your program should then print the value of the expression. program should include a loop so that the user can evaluate multiple postfix expressions. Your output might look like:
Enter postfix expression:
5 8 9 + *
The value of the expression is result of postfix
More expressions (Y or N)? Y
Enter postfix expression:
9 5 /
The value of the expression is 1
More expressions (Y or N)? N
program should perform integer calculations only(single digit only). Do not use floating-point (float or double) variables for your calculations.
--------------------------------------------------------------------------------------------------------------------
Second part would be interesting in this part we have to do error checking after calculator thoroughly tested.
Error checking
1). Invalid character in the input expression- if the error occurs, print an error message stating that an invalid character was encountered.
2) Stack is empty when the algorithm needs to remove an operand- this happens when the expression has too many operators or when the operators and operands are not ordered correctly. We call this a malformed expression, if this occurs, print an error message indicating that the expression is malformed.
3). When the loop in the algorithm ends, there should be exactly one value left on the stack and it is the value of input expression. If the input contained too many operands, there will be more than one value left on the stack. If you remove what should be the result of the expression and the stack is not empty, then the expression was malformed and should print error message indicating.
In all cases, if an error occurs do not try to continue processing the input string and do not try to print the value of the expression. Just print the error message. Do not end application- just ask user if he wants to try another expression.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// structure of stack
struct stack{
int *array;
int top;
int capacity;
};
typedef struct stack Stack;
// create an stack of given size
// return pointer to stack
Stack *createStack(int size){
int *array = (int *) malloc(sizeof (int) *
size);
Stack *s = (Stack *) malloc(sizeof(Stack));
s->array = array;
s->top = -1;
s->capacity = size;
}
// check given stack is empty or not
// return 1 if stack is empty
// return 0 otherwise
int isEmpty(const Stack *s){
if (s->top < 0){
return 1;
}
return 0;
}
// set top of the stack to -1
// this mean stack is empty
void clear(Stack *s){
s->top = -1;
}
// check given stack is full or not
// return 1 if stack is full
// return 0 otherwise
int isFull(const Stack *s){
if (s->top+1 == s->capacity){
return 1;
}
return 0;
}
// push given element into stack
void push(Stack *s, int val){
if (!isFull(s)){
s->top++;
s->array[s->top] = val;
}
}
// pop an element from stack
// return popped element
// otherwise throw empty stack error
int pop(Stack *s){
if (!isEmpty(s)){
int top =
s->array[s->top];
s->top--;
return top;
}
}
// solve given postfix expression
// return result of expression
// if given expression is malformed then throw an exception
int solvePostfixExp(Stack *s, char exp[], char error[]){
int i=0;
while(exp[i] != '\0'){
if (isspace(exp[i])){
}else if(exp[i] >= '0'
&& exp[i] <= '9'){
int num = exp[i]
- '0';
push(s,
num);
} else {
char op =
exp[i];
int first =
0;
int second =
0;
int result =
0;
if
(!isEmpty(s)){
first = pop(s);
}else{
strcpy(error, "expression is malformed!");
}
if(!isEmpty(s)){
second = pop(s);
}else{
strcpy(error, "expression is malformed!");
}
if (op ==
'+'){
result = first + second;
} else if(op ==
'-'){
result = second - first;
} else if(op ==
'*'){
result = first * second;
} else if(op ==
'/'){
result = second / first;
} else{
strcpy(error, "Invalid character in input
expression!");
}
push(s, result);
}
i++;
}
int result = pop(s);
if (!isEmpty(s)){
strcpy(error, "Invalid
expression!");
}
return result;
}
// main driver function
int main(){
Stack *s = createStack(200);
char filename[20];
char exp[200];
printf("Enter file name: ");
scanf("%s", filename);
FILE *fp = fopen(filename, "r");
if (fp == NULL){
puts("Invalid filename!");
exit(0);
}
while (fgets(exp, 200, fp) != NULL){
char error[20];
strcpy(error, "");
int result = solvePostfixExp(s, exp, error);
if (strlen(error)== 0)
printf("The value of the expression %s is %d\n", exp,
result);
else
printf("%s\n",
error);
}
}
input.txt:
7 8 5 * +
9 5 /
5 8 9 + *
Output:
Enter file name: input.txt The value of the expression 7 8 5 * + is 47 The value of the expression 9 5 / is 1 The value of the expression 5 8 9 + * is 85