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
0 1724 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Edward Diener |
last post by:
Is there a way to override the default processing of the assignment operator
for one's own __value types ? I realize I can program my own Assign method,
and provide that for end-users of my class, but I would like to use
internally my own = operator for some of my value types, so I can say "x =
y;" rather than "x.Assign(y);".
The op_Assign...
|
by: Pmb |
last post by:
I've been working on creating a Complex class for my own learning purpose
(learn through doing etc.). I'm once again puzzled about something. I can't
figure out how to overload the assignment operator.
Here's what I'm trying to do. I've defined class Complex as
class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:...
|
by: gorda |
last post by:
Hello,
I am playing around with operator overloading and inheritence,
specifically overloading the + operator in the base class and its
derived class.
The structure is simple: the base class has two int memebers "dataA",
"dataB". The derived class has an additional int member "dataC". I am
simply trying to overload the + operator so that...
|
by: pmatos |
last post by:
Hi all,
I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?
Cheers,
|
by: carlos |
last post by:
Curious:
Why wasnt a primitive exponentiation operator not added to C99?
And, are there requests to do so in the next std revision?
Justification for doing so:
C and C++ are increasingly used in low-level
numerical computations, replacing Fortran in newer projects.
Check, for example, sourceforge.net or freshmeat.net
But neither...
| |
by: karthik |
last post by:
The * operator behaves in 2 different ways. It is used as the value at
address operator as well as the multiplication operator. Does this mean
* is overloaded in c?
|
by: Jerry Fleming |
last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), , ->, =. I wonder
why there is such a restriction.
Some tutorials say that 'new' and 'delete' can only be overloaded with
static member functions,...
|
by: y-man |
last post by:
Hi,
I am trying to get an overloaded operator to work inside the class it
works on. The situation is something like this:
main.cc:
#include "object.hh"
#include "somefile.hh"
object obj, obj2 ;
|
by: sturlamolden |
last post by:
Python allows the binding behaviour to be defined for descriptors,
using the __set__ and __get__ methods. I think it would be a major
advantage if this could be generalized to any object, by allowing the
assignment operator (=) to be overloaded.
One particular use for this would be to implement "lazy evaluation".
For example it would allow...
|
by: Wayne Shu |
last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition).
When I read chapter 11 Operator Overloading, I have two questions.
1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,
operator =, operator, operator(), and operator-must be nonstatic
member function; this ensures that their first operands will be
lvalues". I know that...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |