Arshad Hussain
BAN USER#include <iostream>
#include <list>
#include <stack>
#include <queue>
#include <string>
using namespace std;
int main()
{
char inputSentence[100];
cout<<"Enter any sentence:- \n";
cin.getline(inputSentence,100);
system("cls"); // used to clear the screen, but it is bad practice to call system.... need to figure out some alternative
if(inputSentence[0] == 0)
return 0;
else
cout<<"You Entered : "<<inputSentence;
list<stack<string>> words;
queue<stack<char>*> sentence;
stack<char> * word;
int i =0;
while(inputSentence[i])
{
word = new stack<char>();
while(inputSentence[i] != ' ')
{
word->push(inputSentence[i]);
if(inputSentence[i+1]!=0)
i++;
else
break;
}
i++;
sentence.push(word);
}
string reversedSentence="";
while(!sentence.empty())
{
word=sentence.front();
sentence.pop();
while(!word->empty())
{
reversedSentence +=word->top();
word->pop();
}
delete word;
reversedSentence.append(" ");
}
cout<<"\n\n\n The required reversed sentence is \n"<<reversedSentence.c_str();
}
I think it doesn't make any difference. In fact, the implementation of both similar methods will be common and one in C Class.
Please correct me if I am wrong
Have to do the following modifications:-
1. Semicolon Addition at the end of int n=20;
2. 'f' addition at the print as printf('*');
3. Replacement of '(single quote) to " (Double quote) as printf("*");
Have to do the following modifications:-
1. Semicolon Addition at the end of int n=20;
2. 'f' addition at the print as printf('*');
3. Replacement of '(single quote) to " (Double quote) as printf("*");
I have tried to code for general infix expression evaluation. Tested with the following asthmatic expressions :-
"20+5-(1+2*5+10/5)"
"(20+5-(1+2*5+10/5))+12"
"12+5*4+26 "
int getPrecedence(char c)
{
char oprPrecedence[][2] = {{'-', 0},{'+', 0},{'/', 1 },{'*',2},{'$',3},{'(',0},{')',0}};
int i;
for(i=0; i<7; i++)
{
if(c==oprPrecedence[i][0])
{
i=oprPrecedence[i][1];
break;
}
}
return i;
}
int calculate(char oprator, int operand2,int operand1)
{
int n3 =0;
switch(oprator)
{
case '+':
n3=operand2+operand1;
break;
case '-':
n3=operand2-operand1;
break;
case '/':
n3=operand2/operand1;
break;
case '*':
n3=operand2*operand1;
break;
case '%':
n3=operand2%operand1;
break;
case '$':
n3=pow(operand2*1.0f,operand1);
break;
default:
std::cout<<"Unknown operator";
//*(s+1)='\0'; //exit(1);
}
return n3;
}
//Evaluate Infix Expression
void InfixEvaluationAlgo::EvaluateExpression()
{
bool isNum = false;
while(*s)
{
// skip white spaces if any
if(*s==' ' || *s=='\t')
{
s++;
isNum = false;
continue;
}
// if digit is encountered
if(isdigit(*s))
{
nn=*s-'0';
if(isNum)
{
int digit = Pop();
digit = digit*10 + nn;
Push(digit);
}
else
{
Push(nn);
}
isNum = true;
}else if(*s == '('||*s == ')') //if parenthesis is encountered
{
if(*s == '(')
{
oprStack->Push(*s);
s++;
continue;
}
while(oprStack->stack[oprStack->top] != '(')
{
Push(calculate(oprStack->Pop(), Pop(), Pop()));
}
oprStack->Pop();
}
else//if operator is encountered
{
isNum = false;
if(oprStack->top ==-1)
{
oprStack->Push(*s);
s++;
continue;
}
else if(getPrecedence(oprStack->stack[oprStack->top]) > getPrecedence(*s))
{
Push(calculate(oprStack->Pop(), Pop(), Pop()));
oprStack->Push(*s);
}
else
{
oprStack->Push(*s);
}
}
s++;
}
while(oprStack->top!=-1)
{
Push(calculate(oprStack->Pop(), Pop(), Pop()));
}
}
///Added code to handle ignore/upper case constraint
void Find_Consecutive(char* arr, int len)
{
char str[256] = {0}; //or length * 2
if ( arr == NULL )
return;
if ( len < 2 )
return;
int wptr = 0;
int count = 1;
// * ignore case . out put is always uppercase
char upperArr[256] = {0};
for(int i=0; i<len; i++)
if(arr[i] >97)
*(upperArr+i) =arr[i] - 32;
else
*(upperArr+i) =arr[i];
for (int i = 1; i <= len; i++)
{
//char chr = arr[i] - 32;
if ( upperArr[i] == upperArr[i - 1] )
count++;
else
{
str[wptr++] = upperArr[i - 1];
str[wptr++] = count + 48;
count = 1;
}
}
printf("%s\n", str);
}
list<stack<string>> words; //Not used --- arshad
- Arshad Hussain April 14, 2014