Question

In: Computer Science

Write a program to check the mathematical expressions containing the following parentheses for validity:                 (         &n

Write a program to check the mathematical expressions containing the following parentheses for validity:

                (

                [

                {

Hint: {(6 – 2) * 8} is a valid expression. [3 + (6 * 7) } is an invalid expression.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top = -1;
char stack[100];

// function prototypes
void push(char);
void pop();
void find_top();

void main()
{
   int i;
   char a[100];
   printf("enter expression\n");
   scanf("%s", &a);
   for (i = 0; a[i] != '\0';i++)
   {
       if (a[i] == '(')
       {
           push(a[i]);
       }
       else if(a[i]=='{')
{

push(a[i]);
}
else if(a[i]=='[')
{

push(a[i]);
}
       else if (a[i] == ')')
       {
           pop();
       }
       else if(a[i]=='}')
{

pop();
}
else if(a[i]=']')
{

pop();
}
   }
   find_top();
}

// to push elements in stack
void push(char a)
{
   stack[top] = a;
   top++;
}

// to pop elements from stack
void pop()
{
   if (top == -1)
   {
       printf("expression is invalid\n");
       exit(0);
   }
   else
   {
       top--;
   }
}

// to find top element of stack
void find_top()
{
   if (top == -1)
       printf("\nexpression is valid\n");
   else
       printf("\nexpression is invalid\n");
}

------------------------------------------------------------------------------------------------------------------------

out put

enter expression
{3+(2*3))
expression is invalid


Related Solutions

Write a method called isMatch that takes a string of parentheses and check if all parentheses...
Write a method called isMatch that takes a string of parentheses and check if all parentheses match correctly. The parameter string might consist of other characters. Ignore other characters, just check the () {} [] public static boolean isMatch(String expressionLine); Use a JCF ArrayDeque or LinkedList as a stack to store all open or left parentheses. Use following main method to check your method. public static void main(String[] args) { String[] expression = new String[]{"{5*(x+2)+(y-1);}", "32*(20+(x[i]*3)-1", "((){([][])})", "({}((0))", "{([]})", "{}())",...
Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted...
Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted to a file of your choice using a stack with one space between operators and variables (one letter variables) and/or constants (one digit constants). IN PYTHON please
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values to the variables. Print the results. a<b≥c , √a−7 b2 ≠c , d∨e∧f , a<b∨¬d ∧means and, ∨means inclusive or, ¬ means not. b) Write a program that asks a user whether he or she wants to become a Java programmer and determines if the user typed “yes” (Print true if yes and false otherwise.) Don't use the if statement here
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is...
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is provided to the program as a text string. Allowed expression tokens: brackets “(” and “)”, integers like “4”, “15”, addition “+”, subtraction “-”, multiplication “*”, division “/”. Output can be float. Trim spaces from an expression. Brackets should contain at least one operation. Make sure that an expression is validated before it is calculated; reject the invalid expressions with appropriate message. The program must...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
Which of the following mathematical expressions is used to compute the materials usage variance?
Which of the following mathematical expressions is used to compute the materials usage variance? a. (Fixed price × Standard quantity) + (Variable price × Actual quantity) b. (Actual price × Standard quantity) + (Actual quantity × Standard price) c. (Average price × Average quantity) – (Standard price × Standard quantity) d. (Standard price × Actual quantity) – (Standard price × Standard quantity) e. None of these choices are correct.
Write a program in Python to practice evaluating some logical expressions and determine whether the expressions...
Write a program in Python to practice evaluating some logical expressions and determine whether the expressions are equivalent. Read in a 0 or 1 integer value for each of the following variables: A, B, C, and D. Use an if…else statement to change the 0 and 1 integer values into True and False Boolean values. Booleans in Python have to have this capitalization (True/False, not true/false or TRUE/FALSE) and shouldn’t contain double quotes (which makes them a String instead of...
1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number...
1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number of literals: (a) A’C’ + A’BC + B’C (b) (A + B + C)’(ABC)’ (c) ABC’ + AC (d) A’B’D + A’C’D + BD (e) (A’ + B)’(A’ + C’)’(AB’C)’ (f) (AE + A’B’)(C’D’ + CD) + (AC)’ 2. Obtain the truth table of the function F = (AB + C)(B + AC), express the function F in sum-of-minterms and product-of-maxterms forms, and express...
Write the Gauss’s Law for magnetic fields, in details. mathematical expressions (integral form only) and steps....
Write the Gauss’s Law for magnetic fields, in details. mathematical expressions (integral form only) and steps. Explain each symbol, each detail in each of these equations – by words. Draw sketches when they are necessary for your explanation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT