OOP Assignment
OOP Assignment
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);
return *this;
}
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;
}
}
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.
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)
};
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)
};
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
};
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....
Token *Stack::pop()
{
return st[top--];
}
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...