Question

In: Computer Science

(In C) Note: Can you create an example code of these tasks. use any variables you...

(In C)

Note: Can you create an example code of these tasks. use any variables you wish to use.

postfix expressions: (each individual text (T), (F), (NOT), etc is a token)

F
T NOT
T
F NOT
T
T T AND
F
T F NAND

(1) Create stack s.

(2) For each token, x, in the postfix expression:

If x is T or F push it into the stack s. (T = true, F = false)

Else if x is a unary operator

i pop an operand, op1, from s

ii compute x op1 (see unary table below)

   iii push the result into s

Else if x is a binary operator

i pop an operand, op2, from s

ii pop an operand, op1, from s

iii compute op1 op2 x iv push the result into s

op1 NOT

!op1

Binary operations

op1 op2 AND

op1 && op2

op1 op2 NAND

!(op1 && op2)

op1 op2 OR

op1 || op2

op1 op2 NOR

!(op1 || op2)

op1 op2 XOR

op1 ! = op2

op1 op2 COND

!op1 || op2

op1 op2 BICOND

op1 == op2

Solutions

Expert Solution

# your code goes here

static int evaluatePostfix(String exp)
{ //We are assuming True as 1 and False as 0 hence creating a stack of Integer type
//create a stack
Stack<Integer> stack=new Stack<Integer>();
//expression is taken as string ans exp.length() is giving the length of string
// Scan all characters one by one
for(int i=0;i<exp.length();i++)
{
char c=exp.charAt(i); //giving character present at position i
  
// If the scanned character is an operand (number here),
// push it to the stack.
if(Character.isDigit(c))
stack.push(c - '0');
  
// If the scanned character is an unary operator, pop one
// element from stack apply the operator
  
else if(c == "!"){
   int val = stack.pop();
   val = !val;
   stack.push(val);
}
  
/ If the scanned character is a binary operator, pop two
// elements from stack apply the operator
else
{
int val1 = stack.pop();
int val2 = stack.pop();
  
switch(c)
{
case '&&':
stack.push(val2&&val1);
break;
  
case '||':
stack.push(val2 || val1);
break;
  
case '^':
stack.push(val2^val1);
break;
  
case '==':
stack.push(val2==val1);
break;
}
}
}
return stack.pop();   
}


Related Solutions

Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
create and submit your own example of the use of a Hamming code to detect and...
create and submit your own example of the use of a Hamming code to detect and correct a single-bit error.
Can you please code in C the Lagrangian function. If you have any questions please let...
Can you please code in C the Lagrangian function. If you have any questions please let me know.
Use R programming to resolve this; can you please provide details on the code? A) Create...
Use R programming to resolve this; can you please provide details on the code? A) Create a dataframe – comparativeGenomeSize with the following vectors: > organism<-c("Human","Mouse","Fruit Fly", "Roundworm","Yeast") > genomeSizeBP<-c(3000000000,3000000000,135600000,97000000,12100000) > estGeneCount<-c(30000,30000,13061,19099,6034) B) Print the organism and estGeneCount for Human and fruitfly.(1) C) Add a column to this data frame calculating base pairs per gene. To do this, write a function “genedensity” that takes as arguments the genomesizeBP and estimatedGeneCount information, and calculates from this the estimated bp per gene....
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire assignment. There are no more directions to it. Create an array of struct “employee” Fill the array with information read from standard input using C++ style I/O Shuffle the array Select 5 employees from the shuffled array Sort the shuffled array of employees by the alphabetical order of their last Name Print this array using C++ style I/O Random Number Seeding We will make...
Create a C++ project in visual studio. You can use the C++ project that I uploaded...
Create a C++ project in visual studio. You can use the C++ project that I uploaded to complete this project. 1. Write a function that will accept two integer matrices A and B by reference parameters, and two integers i and j as a value parameter. The function will return an integer m, which is the (i,j)-th coefficient of matrix denoted by A*B (multiplication of A and B). For example, if M = A*B, the function will return m, which...
Replace the todo comments with the right code. //Create variables to use later const int TRIG_PIN...
Replace the todo comments with the right code. //Create variables to use later const int TRIG_PIN = 9; const int ECHO_PIN = 10; const int RED_PIN = 3; const int GREEN_PIN = 5; const int BLUE_PIN = 6; float duration, distance_in_cm, distance_in_feet; void setup() { //Setup pins for correct I/O pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); } void loop() { //Generate the ultrasonic waves digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); //Read in the echoed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT