Question

In: Computer Science

C data structure Initially, we do not need to do any error checking - we can...

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.

Solutions

Expert Solution

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


Related Solutions

use c++ (4) Perform error checking for the data point entries. If any of the following...
use c++ (4) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point. If entry has no comma Output: Error: No comma in string. (1 pt) If entry has more than one comma Output: Error: Too many commas in input. (1 pt) If entry after the comma is not an integer Output: Error: Comma not followed by an integer. (2 pts) Ex:...
Question: How can data collection for diseases be improved? What tools do we need? What are...
Question: How can data collection for diseases be improved? What tools do we need? What are medical intelligence and syndromic surveillance, and how are they used or how could they be used?
why do we need financial intermediaries in any economy ? explain clearly
why do we need financial intermediaries in any economy ? explain clearly
Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error checking,...
Give the peek algorithm (code) for a Stack that can hold 10 nodes. Include error checking, and assume that the array theData and the integer top are data members of the class Stack. Give the algorithm for the structure Queue that keeps the front and rear “pointers” from progressing through all of memory (assume the Queue array has n elements).
C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do...
C++, ArrayStack, data structure. Make a main using the following arraystack structure the main most do the following Stack: Stack 1 and Stack 2 most have their own result I need to see the state of the stack1 and stack2 after doing the following operation. (stack1 and stack2 need to have individually there own state result) stack1.push(1) stack1.push(2) stack2.push(3) stack2.push(4) stack1.pop() stackTop = stack2.peek() stack1.push(stackTop) stack1.push(5) stack2.pop() stack2.push(6) ArrayStackInterface.h // Created by Frank M. Carrano and Tim Henry. // Copyright...
Explain briefly the determinants of organizational structure. Why do we need to consider those factors?
Explain briefly the determinants of organizational structure. Why do we need to consider those factors?
Briefly describe the cytoskeletal structure and biological function of motile cilia why do we need to...
Briefly describe the cytoskeletal structure and biological function of motile cilia why do we need to study the bending properties of single cilia?
What do you think of Wall Street (or any financial markets)? Do we need Wall Street?...
What do you think of Wall Street (or any financial markets)? Do we need Wall Street? Why or Why not? Do a bit of research on how the financial markets (in the US, or other countries of your interest) were affected by the Covid-19 since March until now. What is "The Paradox of Thrift"? How does that apply to our current situation?
C++ Assignment: For this assignment, we will model a checking account and savings account, arranging these...
C++ Assignment: For this assignment, we will model a checking account and savings account, arranging these two types of accounts within a simple class hierarchy beneath a base class. By creating a class hierarchy, we will avoid unnecessary code duplication across the derived classes and create an intuitive relationship between them. We will expand on this assignment in a future assignment. Create a class named Account, which will contain data and methods that would naturally be shared by any derived...
C# 1.Visual Studio .NET’s ___________ feature displays all the members in a class a.real-time error checking...
C# 1.Visual Studio .NET’s ___________ feature displays all the members in a class a.real-time error checking b.Quick Info c.outlined code d.Intellisense 2.An algorithm specifies the actions to be executed. True False 3.A(n) _________ occurs when an executed statement does not directly follow the previously executed statement in the written application. a.unordered execution b.transfer of control c.action state d.jump 4.The declarations and statements that compose the method definition are called the __________.    a.method body b.method header c.parameter list d.method name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT