Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Json validator Here is Expression Evaluation problem example (which is similar to json evaluation but in a different context) Json Validator Write a class based

Json validator

image text in transcribed

Here is Expression Evaluation problem example (which is similar to json evaluation but in a different context)

image text in transcribed

Json Validator Write a class based C++ program to validate a Json string, it has only {}, [], and string key and string value. Your program gets a string from the user and uses a pointer-based stack to determine whether its parentheses are properly balanced/matched. Size of characters is less than 100. Assume that operator will enter only [.], {}, :, a-z, ",",". Whitespace is acceptable too. You do not need to check if input Json string has a duplicate key or not. Note that this problem is simplified version of Expression Evaluation problem (pg. 128 and 129), where two stacks are used for operators and operand. A few examples are given below. Enter a json string: { "a":""} True Enter a string: { "a":"b", "x":"y"} True Enter a string: { "a":"b", "x":"y" False Enter a string: { "a": "b" "x": "y" } False Enter a string: { "a":"b", "x": [ { "r":"a" }] } True Enter a string: { "a":"b", "x": [ { "r":"a", "a":"b" }]} True Enter a string: {"x":[{' "a":"b"}],"a":"b","t":[ True "a", "a":"b"}]} Dijkstra's Two-Stack Algorithm for Expression Evaluation public class Evaluate { public static void main(String[] args) { Stack ops = new Stack(); Stack vals = new Stack(); while (!StdIn. isEmpty) { // Read token, push if operator. Strings = StdIn.readString(); if (s.equals("")) else if (s.equals("+")) ops.push(s); else if (s.equals("-")) ops.push(s); else if (s.equals("*")) ops.push(s); else if (s.equals("/")) ops.push(s); else if (s.equals("sort")) ops.push(s); else if (s.equals("") { // Pop, evaluate, and push result if token is ")". String op = ops.pop(); double v = vals.pop(); if (op.equals("+")) v = vals.pop() + V; else if (op.equals("-")) v = vals.pop() else if (op.equals("*")) v = vals.pop() * V; else if (op.equals("/")) v = vals.pop() /v; else if (op.equals("sort")) v = Math. sqrt(); vals.push(); } // Token not operator or paren: push double value. else vals.push(Double.parseDouble(s)); } Stdout.println(vals.pop()); } } V; This Stack client uses two stacks to evaluate arithmetic expressions, illustrating an essential compu- tational process: interpreting a string as a program and executing that program to compute the de- sired result . With generics, we can use the code in a single Stack implementation to implement one stack of String values and another stack of Double values. For simplicity, this code assumes that the expres- % java Evaluate sion is fully parenthesized, with numbers and characters (1 + (( 2 + 3 ) * ( 4 * 5 ) ) ) separated by whitespace. 101.0 % java Evaluate ((1+ sqrt ( 5.0 ) ) / 2.0) 1.618033988749895

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago