I have an urgent assignment. It would be so great if anyone could help me with the codes of this as I am not able to understand this:
Here's the question:
Enumerations make it possible to define a new type that contains a limited number of values e.g. the TokenType
contains 6 values: EQUALS, ADD, SUBTRACT, NUMBER, WORD, PREV.
We define it in the following way:
typedef enum
EQUALS, ADD, SUBTRACT, NUMBER, WORD, PREV
TokenType;
Enumeration definitions have to be included in header files.
We can treat enumeration values as integer constants e.g.
TokenType en1=ERROR;
int one=9, two=8;
char token[20]=" add ";
if (strstr(token,"add"))
en1=ADD;
switch (en1)
case ADD: return(one+two);
case MULTIPLY: return (one*two);
default: break;
-
Implement the method getNextToken that reads a token from the standard input and determines its type.
The token is stored in the tk parameter. The len specifies the number of elements in the tk array.
TokenType getNextToken(char tk[], int len);
The values should be calculated using the following rules:
“add” or “+” ADD
“equals” or “=” EQUALS,
“subtract” or “-“ SUBTRACT
“prev” or “prev_result PREV
Any floating point or integer number NUMBER
In all other cases WORD
To read values you can use scanf. To limit the length of a token use e.g. code presented during a lecture. - Implement the lineCalc function that gets tokes provided by getNextToken and evaluates their value.
static float prevResult;
char * lineCalc(void)
static char result2Return[20];
float value;
// implementation of the function
sprint(result2Return, “%4.2f”, value); // to convert a number to a string with the specified precision.
return result2Return;
Rules:
All tokens categorized as WORD are ignored by the evaluator.
If more than more than one operator follow each other then last is valid
An expression ends with the EQUALS token.
The value of the PREV token is the value of the prevResult variable.
Examples:
“2 + 7 =“ has the value: “9”
“ 5 - add 8.1 = “ has the value: “13.1”
“ bread 3.5 plus butter 5.1 + milk 2.5 equals ” has the value: “11.1”
“ prev minus 1 = ” has the value: 10.1
- Implement a program that reads tokens from standard input and prints their value on the monitor.
Additions:
Add MULTIPLY and DIVIDE tokens and appropriate operators.
Take into account the priority of operators