1) Add insert and erase methods to the Vector class. The method headers of insert and erase are specified as follows:
iterator insert(iterator pos, const Object& x);
//insert the object x at position pos in a Vector object.
//The method returns a reference to the element inserted at position pos
// in the Vecor object.
iterator erase(iterator pos);
//erase removes the object at the position pos in a Vector object.
//The method returns the the position of the element that followed pos
//prior to the call.
2. (6 points) Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.)
3. (6 points) The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows
template <typename Object>
list<Object> intersection( const list<Object> & L1, const list<Object> & L2);
2.
#include<iostream>
#include<stack>
#include<string>
using namespace std;
string InfixToPostfix(string expression);
int HigherPrecedence(char operator1, char operator2);
bool Operator(char C);
bool Operand(char C);
int main()
{
string expression;
cout<<"Enter Infix Expression \n";
getline(cin,expression);
string postfix = InfixToPostfix(expression);
cout<<"Output =
"<<postfix<<"\n";
}
string InfixToPostfix(string expression)
{
stack<char> S;
string postfix = "";
for(int i = 0;i< expression.length();i++) {
if(expression[i] == ' ' ||
expression[i] == ',') continue;
else
if(Operator(expression[i]))
{
while(!S.empty()
&& S.top() != '(' &&
HigherPrecedence(S.top(),expression[i]))
{
postfix+= S.top();
S.pop();
}
S.push(expression[i]);
}
else
if(Operand(expression[i]))
{
postfix
+=expression[i];
}
else if (expression[i] ==
'(')
{
S.push(expression[i]);
}
else if(expression[i] ==
')')
{
while(!S.empty()
&& S.top() != '(') {
postfix += S.top();
S.pop();
}
S.pop();
}
}
while(!S.empty()) {
postfix += S.top();
S.pop();
}
return postfix;
}
bool Operand(char C)
{
if(C >= '0' && C <= '9') return
true;
if(C >= 'a' && C <= 'z') return
true;
if(C >= 'A' && C <= 'Z') return
true;
return false;
}
bool Operator(char C)
{
if(C == '+' || C == '-' || C == '*' || C == '/' || C==
'$')
return true;
return false;
}
int RightAssociative(char op)
{
if(op == '$') return true;
return false;
}
int GetOperatorWeight(char op)
{
int weight = -1;
switch(op)
{
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
case '$':
weight = 3;
break;
}
return weight;
}
int HigherPrecedence(char op1, char op2)
{
int op1Weight = GetOperatorWeight(op1);
int op2Weight = GetOperatorWeight(op2);
if(op1Weight == op2Weight)
{
if(RightAssociative(op1)) return
false;
else return true;
}
return op1Weight > op2Weight ? true: false;
}
1.
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (3,10);
std::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 20 );
myvector.insert (it,2,30);
it = myvector.begin();
std::vector<int> anothervector (2,40);
myvector.insert
(it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 205,298,350};
myvector.insert (myvector.begin(), myarray, myarray+3);
std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.