I need to write overloaded operators in C++ by reading their definitions from the input file.how can i write this code.
any idea?
The problem definition is this:
A definition starts with “> defineop”.
op is the name of the operator, which is one of the special characters { ~, !, @, #, $, %, ^, &, _ }
p1 and p2 are distinct lower case characters of English alphabet {a, b, ... , z}
expressionsare C like expressions made up of {p1, p2, (, ), +, ,*, /, defined operators, integers}
Results of 0 op p1 and p1 op 0 are given as expressions.
Precedence of operator is given as “prec = value1”. value1 is a number between 0 and 100. You
will use this precedence value while evaluating the expressions.
By default, * and / has precedence value of 20, + and – has precedence value of 10. Operators with greater precedences are evaluated earlier.
Associativity(*) of the operator is given as “assoc = value2”. value2 is either right or left. If two operators have same precedences, they will have same associativities.
Associativities of +, ,* and / are all left.
Adefinition ends with the keyword end.
> defineop $ ; a $ b = ( a + b ) * a ^ b ; 0 $ a = a + 1 ; a $ 0 = 0 ; prec = 7 ; assoc = left ; end
> defineop ^ ; a ^ b = a ^ ( b – 1 ) * a ; 0 ^ a = 0 ; a ^ 0 = 1 ; prec = 32 ; assoc = right ; end
// be careful! $ used the operator which is defined after its definition.
// For 0 op 0, use 0 op p1.
A unary operator is defined as follows:
> defineop op ; op p1 = expression1 ; op 0 = expression2 ; prec = value ; end
For example,
> defineop ~ ; ~ a = 0 – a ; ~ 0 = 0 ; prec = 45 ; end
After defining operators, some expressions made up of {(, ), +, ,
*, /, defined operators, integers}
are given. You should print the value of these expressions. Each expression is given as:
> evaluate ; expression ; end
for example input file is:
> defineop $ ; a $ b = ( a + b ) * a ^ b ; 0 $ a = a + 1 ; a $ 0 = 0 ; prec = 7 ; assoc = left ; end
> defineop ^ ; a ^ b = a ^ ( b - 1 ) * a ; 0 ^ a = 0 ; a ^ 0 = 1 ; prec = 32 ; assoc = right ; end
> defineop ~ ; ~ a = 0 - a ; ~ 0 = 0 ; prec = 45 ; end
> evaluate ; 3 * ( 2 + 2 ^ 3 ) ; end
> evaluate ; ~ 2 + 3 $ 2 ; end
output is printed as:
30
3