Question
Convert query expressions that are in infix notation to postfix expressions. The expressions might contain parentheses and these operators: AND, OR, =, NEVER, ONLY. In
Convert query expressions that are in infix notation to postfix expressions. The expressions might contain parentheses and these operators: AND, OR, =, NEVER, ONLY. In program #2, you will evaluate that postfix expression.
Examples:
# | Infix | Postfix |
1 | SMOKING = N | SMOKING N = |
2 | SMOKING = N AND GENDER = F | SMOKING N = GENDER F = AND |
3 | EXERCISE NEVER YOGA | EXERCISE YOGA NEVER |
4 | SMOKING = N AND ( EXERCISE = HIKE OR EXERCISE = BIKE ) | SMOKING N = EXERCISE HIKE = EXERCISE BIKE = OR AND |
5 | SMOKING = N AND EXERCISE = HIKE OR EXERCISE = BIKE | SMOKING N = EXERCISE HIKE = AND EXERCISE BIKE = OR |
6 | ( BOOK = SCIFI ) | BOOK SCIFI = |
7 | ( ( ( BOOK ONLY SCIFI ) ) ) | BOOK SCIFI ONLY |
Precedence rules
- Evaluate contents in () before others - Highest precedence
- =, NEVER, ONLY
- AND, OR - Lowest precedence
Driver Program and your function in separate files
For this program, I provided a driver program which will invoke your function to convert from infix to postfix. Your code must be placed in a separate file, cs2123p1.c
Why did I provide a driver program?
- Reduces the amount of code you have to write. This allows you to focus on the most important points.
- Teaches you what it is like to use code written by someone else.
- Shows you the value of programming standards.
- Provides a design which makes it easier for you to do this program. It is very helpful to have addOut, categorize, and getToken.
Some Files for your use:
cs2123p1.h - include file for Program #1. Please review this.
cs2123p1Driver.c - driver program that I provided. It has several useful routines to help reduce your effort. Please review this code.
p1Input.txt - input file containing infix expressions, one per text line.
p1Extra.txt - input expressions, one per text line. This is used for extra credit. Note that it also contains the expressions in p1Input.txt.
Your code
- Create your code in cs2123p1.c (not cs2123p1Driver.c). Do not copy the driver code into cs2123p1.c. Based on what the driver calls, you need to create (at least) this function:
int convertToPostfix(char *pszInfix, Out out)
It returns 0 if it converted successfully. Otherwise, it returns a value which indicates an error in the infix data (e.g., missing left paren, missing right paren).
It populates the out array using the addOut function (provided in the driver).
Error Handling
- Your code must handle errors like the following:
- Missing "("
- Missing ")"
- Additional errors are handled for extra credit.
- When an error is encountered, your code for convertToPostfix should return a non-zero value to the driver. Your program must not terminate.
Requirements
- Modularity matters. You probably need to code more than just the convertToPostfix function.
Hint
The functions getToken and categorize are provided in the cs2123p1Driver.c. These can greatly simplify your code.
Meaning of the =, NEVER, and ONLY Operators
- This information is much more important for program #2.
- The "=" operator is interpretted as "at least one". "EXERCISE = HIKE" means at least one of the EXERCISE traits must be HIKE. There can also be other EXERCISEs that are not HIKE for the customer.
- The NEVER operator means that the specified trait must not exist for the customer. If a customer had the traits EXERCISE HIKE and EXERCISE YOGA, do not include the customer in the result when the query specifies EXERCISE NEVER YOGA.
- The ONLY operator means that the specified trait value is the only value allowed for the specified trait type. If the customer has other trait values for the specified trait type, do not include the customer in the result.
Extra Credit (10 pts + 150 / N)
- The following errors must be detected and returned by convertToPostfix:
- Missing "(" -- must also be handled for normal credit
- Missing ")" -- must also be handled for normal credit
- Missing operator
- Missing operand
- You must also print an additional detailed warning message about those last two errors prior to returning (the driver will subsequently print a simple warning message). Some examples:
- Warning: Expected operator, found token operand
- Warning: Expected operator, found (
- Warning: Expected operand, found token operator
- Warning: Expected operand, found )
- Warning: Expected operand at end of expression
What I need assistance on is the part is the Extra credit portion. I can't figure out the Opperand or Opperator warnings.
INPUT FILE
SMOKING = N SMOKING = Y GENDER = F SMOKING = N AND GENDER = F BOOK NEVER SCIFI BOOK ONLY SCIFI SMOKING = N AND ( EXERCISE = HIKE OR EXERCISE = BIKE ) GENDER = F AND EXERCISE NEVER YOGA SMOKING = N AND EXERCISE = HIKE OR EXERCISE = BIKE SMOKING = N AND EXERCISE = HIKE AND EXERCISE = BIKE SMOKING = N AND ( EXERCISE = HIKE OR EXERCISE = BIKE ) AND BOOK = SCIFI GENDER = F AND SMOKING = N OR BOOK = SCIFI AND EXERCISE = HIKE ( ( SMOKING = N ) AND ( BOOK ONLY SCIFI ) AND ( EXERCISE = HIKE ) ) ( BOOK = SCIFI ) ( ( ( BOOK ONLY SCIFI ) ) ) ( ( SMOKING = N ) ( SMOKING = N ) AND ( BOOK ONLY SCIFI ) AND ( EXERCISE = HIKE ) ) ( GENDER = M ) AND EXERCISE = BIKE ) GENDER = F SMOKING = N NEVER BIKE ( GENDER = F ) EXERCISE = BIKE ( SMOKING = ) N BOOK = NEVER SCIFI ( GENDER = F ) ( EXERCISE ONLY BIKE ) ( SMOKING = N ) AND EXERCISE ( ONLY BIKE ) SMOKING = N AND GENDER =
/******************************************************************************
UNABLE TO POST ENTIRE CODE, CODE CAN BE FOUND HERE
https://www.chegg.com/homework-help/questions-and-answers/need-converttopostfix-convert-file-infix-postfix-cs2123p1c-include-include-cs2123p1h-mainc-q17971948
AND HERE
https://www.chegg.com/homework-help/questions-and-answers/convert-expressions-infix-notation-postfix-notation-expressions-might-contain-parentheses--q17964864
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started