PROGRAM
// Welcome to the world of c++...
// Program to Overload copy construtor and assignment
operator..
// Programmer Name: Hamza Sajid
// Date of Creation: 12th Dec,2023.
// Compilation always starts from the first line of the
program..
// Including all necessary header files
#include <iostream> // For input output
using namespace std; // So that we don't have to write std::
with input output
// Structure for a linked list..
struct link
{
int data;
link *next;
};
// Class performing different operations on linked lists..
class linklist
{
private:
link *first;
public:
linklist() : first(NULL) {} // constructor to the class...
~linklist(); // destructor to the class..
void additem(int d); // Function to add item to link
list..
void display();
// Overloaded copy constructor
linklist(const linklist &other);
// Overloaded assignment operator
linklist &operator=(const linklist &other);
};
// Overloaded copy constructor..
linklist::linklist(const linklist &other)
{
// Copy constructor for deep copy
first = NULL; // Initialize first for the new object
// Traverse the other linked list and copy each element
link *currentOther = other.first;
while (currentOther != NULL)
{
additem(currentOther->data);
currentOther = currentOther->next;
}
}
// Function overloading an assignment operator..
linklist &linklist::operator=(const linklist &other)
{
if (this != &other)
{ // Check for self-assignment
// Clear existing data in the current object
link *current = first;
while (current != NULL)
{
link *temp = current;
current = current->next;
delete temp;
}
// Copy data from the other object
first = NULL;
link *currentOther = other.first;
while (currentOther != NULL)
{
additem(currentOther->data);
currentOther = currentOther->next;
}
}
return *this;
}
// Function to add into a linked list..
void linklist::additem(int d)
{
link *newlink = new link;
newlink->data = d;
newlink->next = NULL; // Set the next pointer of the new
link to NULL
if (first == NULL)
{
// If the list is empty, make the new link the first
link
first = newlink;
}
else
{
// Traverse the list to find the last link
link *current = first;
while (current->next != NULL)
{
current = current->next;
}
// Attach the new link to the last link in the list
current->next = newlink;
}
}
// Function to display the link list...
void linklist::display()
{
link *current = first;
while (current != NULL)
{
cout << endl
<< current->data;
current = current->next;
}
}
// Destructor to destroy the node..
linklist::~linklist()
{
link *current = first;
while (current != NULL)
{
link *temp = current;
current = current->next;
delete temp;
}
}
// End of all member functions definition...
// Start of main Function..
int main()
{
linklist li;
li.additem(12);
li.additem(13);
li.additem(14);
li.additem(15);
li.additem(16);
cout << endl;
// Test the copy constructor and assignment operator
linklist li2 = li; // Overloaded Copy constructor will be
used..
linklist li3;
li3 = li2; // Overloaded Assignment operator will be used..
// Display the Copied Lists Ensuring that both the
overloads work properly...
li2.display();
cout << endl;
li3.display();
cout << endl;
return 0;
}
PROGRAM
// Welcome to the world of c++...
// Program to Modify parse program to evaluate float
expressions
// Programmer Name: Hamza Sajid
// Date of Creation: 12th Dec,2023.
// Compilation always starts from the first line of the
program..
// Including all necessary header files
#include <iostream> // For input output
#include <cstring> // For char string methods like strlen
using namespace std; // So that we don't have to write std::
with input output
const int LEN = 80;
// Start of all classes declaration....
// ------------------------------------------------------------
-------------------------
// Abstract Base class for tokens....
class Token
{
public:
virtual float getNumber() const = 0; // Pure virtual
function for getting the numerical value
virtual char getOperator() const = 0; // Pure virtual
function for getting the operator
virtual ~Token() {} // Virtual destructor
for proper cleanup
};
// Operator class derived from Token
class Operator : virtual public Token
{
private:
char oper; // Operators: +, -, *, /
public:
Operator(char c) : oper(c) {} // Constructor to the class
char getOperator() const; // Implementation of the
getOperator function
float getNumber() const; // Dummy function (not used
for operators)
};
// Number class derived from Token
class Number : virtual public Token
{
private:
float fnum; // The number
public:
Number(float n) : fnum(n) {} // Constructor to the class
float getNumber() const; // Implementation of the
getNumber function
char getOperator() const; // Dummy function (not used
for numbers)
};
// Stack class modified to hold Token pointers
class Stack
{
private:
Token *st[LEN]; // Array of Token pointers
int top; // Index of the top element
public:
Stack(); // Constructor
void push(Token *var); // Function to push a Token pointer
onto the stack
Token *pop(); // Function to pop a Token pointer
from the stack
int gettop() const; // Function to get the index of the
top element
};
// Expression class using polymorphism
class Expression
{
private:
Stack s; // Stack to store Token pointers
char *pStr; // Pointer to the input string
int len; // Length of the input string
public:
Expression(char *ptr) : pStr(ptr), len(strlen(pStr)) {} //
Constructor to class..
void parse(); //
Function to parse the expression input string...
float solve(); //
Function to solve the expression..
};
//-------------------------------------------------------------
---
// End of all classes declarations....
// Start of all classes member functions Definitions...
//
***************************************************************
// Class Operator member functions Definitions..
char Operator::getOperator() const
{
return oper;
}
float Operator::getNumber() const
{
return 0; // Dummy implementation
}
// class Number member functions Definitions..
float Number::getNumber() const
{
return fnum;
}
char Number::getOperator() const
{
return '\0'; // Dummy implementation
}
// class Stack member functions Definitions..
Stack::Stack() : top(0) {}
void Stack::push(Token *var)
{
st[++top] = var;
}
Token *Stack::pop()
{
return st[top--];
}
int Stack::gettop() const
{
return top;
}
// class Expression member function definitions..
void Expression::parse()
{
char ch;
for (int j = 0; j < len; j++)
{
ch = pStr[j];
if (isdigit(ch))
{
// Convert the character to float and save
numerical value
float number = ch - '0';
s.push(new Number(number));
}
else if (ch == '.')
{
// Convert the next character to float (considering
it's a digit)
s.push(new Number(s.pop()->getNumber() + (pStr[++j]
- '0') / 10.0));
}
else if (ch == '+' || ch == '-' || ch == '*' || ch ==
'/')
{
if (s.gettop() == 1)
s.push(new Operator(ch));
else
{
Token *lastval = s.pop();
Token *lastop = s.pop();
if ((ch == '*' || ch == '/') && (lastop-
>getOperator() == '+' || lastop->getOperator() == '-'))
{
s.push(lastop);
s.push(lastval);
}
else
{
float result;
switch (lastop->getOperator())
{
case '+':
result = s.pop()->getNumber() +
lastval->getNumber();
break;
case '-':
result = s.pop()->getNumber() -
lastval->getNumber();
break;
case '*':
result = s.pop()->getNumber() *
lastval->getNumber();
break;
case '/':
result = s.pop()->getNumber() /
lastval->getNumber();
break;
default:
cout << "\nUnknown oper";
exit(1);
}
s.push(new Number(result));
}
s.push(new Operator(ch));
}
}
else
{
cout << "\nUnknown input character";
exit(1);
}
}
}
float Expression::solve()
{
while (s.gettop() > 1)
{
Token *lastval = s.pop();
Token *lastop = s.pop();
float result;
switch (lastop->getOperator())
{
case '+':
result = s.pop()->getNumber() + lastval-
>getNumber();
break;
case '-':
result = s.pop()->getNumber() - lastval-
>getNumber();
break;
case '*':
result = s.pop()->getNumber() * lastval-
>getNumber();
break;
case '/':
result = s.pop()->getNumber() / lastval-
>getNumber();
break;
default:
cout << "\nUnknown operator";
exit(1);
}
s.push(new Number(result));
}
return s.pop()->getNumber();
}
//
***************************************************************
*************
// End of all classes member functions definitions...
// Start of Main function the point from where execution
starts...
int main()
{
char ans;
char string[LEN];
cout << "\nEnter an arithmetic expression"
<< "\nof the form 2+3.4*4/3-2."
<< "\nNo number may have more than one digit."
<< "\nDon't use any spaces or parentheses.";
do
{
cout << "\nEnter expression: ";
cin >> string;
Expression *eptr = new Expression(string);
eptr->parse();
cout << "\nThe numerical value is: " << eptr->solve();
delete eptr;
cout << "\nDo another (Enter y or n)? ";
cin >> ans;
} while (ans == 'y');
return 0;
}
// End of main..
// End of program..
// ------------------------------------------------------------
------------