#include
#include
#include
#include
using namespace std;
class infix_to_postfix
{
protected:
string exp;
string infixexp;
stack charstack;
bool prcd(char a, char b)
{
return a > b;
}
public:
infix_to_postfix(string str)
{
exp = str;
}
void push_to_stack()
{
for(int i = 0; exp[i]; ++i)
{
if(exp[i] == ' ')
continue;
if(isalnum(exp[i]))
{
infixexp += exp[i];
}
else
{
while(!charstack.empty() && prcd(exp[i], charstack.top()))
{
infixexp += charstack.top();
charstack.pop();
}
charstack.push(exp[i]);
}
}
while(!charstack.empty())
{
infixexp += charstack.top();
charstack.pop();
}
}
string print()
{
return infixexp;
}
};
class evaluate {
public:
evaluate(string str)
{
exp = str;
}
void peform()
{
int res;
for(int i = 0; i < exp.length(); i++)
{
if(isalnum(exp[i]))
{
nums.push(exp[i] - '0');
}
else
{
if(!nums.empty())
{
int a = nums.top();
nums.pop();
int b = nums.top();
nums.pop();
res = doit(b, exp[i], a);
}
nums.push(res);
}
}
}
int retres()
{
return nums.top();
}
protected:
string exp;
stack nums;
private:
int doit(int a, char c, int b)
{
switch(c)
{
case '+':
return a + b;
break;
case '-':
return a - b;
break;
case '*':
return a * b;
break;
case '/':
return a / b;
break;
}
}
};
int main()
{
string infixstr;
infix_to_postfix itop("5 * 2 + 3 * 1 - 9");
itop.push_to_stack();
infixstr = itop.print();
cout << infixstr << endl;
evaluate ev(infixstr);
ev.peform();
int res = ev.retres();
cout << res << endl;
return 0;
}
#include
#include
using namespace std;
class infix_to_postfix
{
protected:
string exp;
string infixexp;
stack
bool prcd(char a, char b)
{
return a > b;
}
public:
infix_to_postfix(string str)
{
exp = str;
}
void push_to_stack()
{
for(int i = 0; exp[i]; ++i)
{
if(exp[i] == ' ')
continue;
if(isalnum(exp[i]))
{
infixexp += exp[i];
}
else
{
while(!charstack.empty() && prcd(exp[i], charstack.top()))
{
infixexp += charstack.top();
charstack.pop();
}
charstack.push(exp[i]);
}
}
while(!charstack.empty())
{
infixexp += charstack.top();
charstack.pop();
}
}
string print()
{
return infixexp;
}
};
class evaluate {
public:
evaluate(string str)
{
exp = str;
}
void peform()
{
int res;
for(int i = 0; i < exp.length(); i++)
{
if(isalnum(exp[i]))
{
nums.push(exp[i] - '0');
}
else
{
if(!nums.empty())
{
int a = nums.top();
nums.pop();
int b = nums.top();
nums.pop();
res = doit(b, exp[i], a);
}
nums.push(res);
}
}
}
int retres()
{
return nums.top();
}
protected:
string exp;
stack
private:
int doit(int a, char c, int b)
{
switch(c)
{
case '+':
return a + b;
break;
case '-':
return a - b;
break;
case '*':
return a * b;
break;
case '/':
return a / b;
break;
}
}
};
int main()
{
string infixstr;
infix_to_postfix itop("5 * 2 + 3 * 1 - 9");
itop.push_to_stack();
infixstr = itop.print();
cout << infixstr << endl;
evaluate ev(infixstr);
ev.peform();
int res = ev.retres();
cout << res << endl;
return 0;
}
Josephus Problem Solvation code in C++
#include
using namespace std;
struct node{
int val;
node *next;
};
void insert_node(int);
void print();
void josephus(int);
node *head,*nptr,*tptr;
int main(){
head = NULL;
int start,n;
cout << "Enter node number: " << endl;
cin >> n;
insert_node(n);
cout << "The persons are:" << endl;
print();
cout << "Enter start number: " << endl;
cin >> start;
josephus(start);
return 0;
}
void josephus(int start){
node *ptr,*qptr;
qptr = ptr = head;
for (int i = 1; i < start; i++){
ptr = ptr->next;
qptr = ptr;
}
while (ptr->next != ptr){
qptr->next = ptr->next->next;
ptr = qptr->next;
qptr = ptr;
}
head = ptr;
cout << "The person survived is :" << ptr->val << endl;
}
void insert_node (int n){
for(int i = 0; i < n; i++){
nptr = new node;
nptr->val = i+1;
nptr->next = NULL;
if (head == NULL){
head = nptr;
tptr = nptr;
}
else{
tptr->next = nptr;
tptr = nptr;
}
}
tptr->next = head;
}
void print(){
tptr = head;
cout << tptr->val << "->";
tptr = tptr->next;
while (head != tptr){
cout << tptr->val << "->";
tptr = tptr->next;
}
cout<< endl;
}
using namespace std;
struct node{
int val;
node *next;
};
void insert_node(int);
void print();
void josephus(int);
node *head,*nptr,*tptr;
int main(){
head = NULL;
int start,n;
cout << "Enter node number: " << endl;
cin >> n;
insert_node(n);
cout << "The persons are:" << endl;
print();
cout << "Enter start number: " << endl;
cin >> start;
josephus(start);
return 0;
}
void josephus(int start){
node *ptr,*qptr;
qptr = ptr = head;
for (int i = 1; i < start; i++){
ptr = ptr->next;
qptr = ptr;
}
while (ptr->next != ptr){
qptr->next = ptr->next->next;
ptr = qptr->next;
qptr = ptr;
}
head = ptr;
cout << "The person survived is :" << ptr->val << endl;
}
void insert_node (int n){
for(int i = 0; i < n; i++){
nptr = new node;
nptr->val = i+1;
nptr->next = NULL;
if (head == NULL){
head = nptr;
tptr = nptr;
}
else{
tptr->next = nptr;
tptr = nptr;
}
}
tptr->next = head;
}
void print(){
tptr = head;
cout << tptr->val << "->";
tptr = tptr->next;
while (head != tptr){
cout << tptr->val << "->";
tptr = tptr->next;
}
cout<< endl;
}