Infix to Postfix - Deleting Parenthesis - c++

Having trouble getting the parenthesis to pop from the string.
I enter an infix expression such as (A+B)*C and would like to get AB+C*.
Instead, I get (AB)+C*. Any help would be greatly appreciated.
string Postfx::convertToPostfix(Postfx C,string in)
{
stack<char> S;
string postfx = "";
for (int i = 0; i<in.length();i++)
{
if (in[i] == ' ' || in[i] == ',') continue;
else if ((in[i]) == '+' || in[i] == '-' || in[i] == '/' || in[i] == '*')
{
while (!S.empty() && S.top() != '(' && C.precedence(S.top(), in[i]))
{
postfx += S.top();
S.pop();
}
S.push(in[i]);
}
else if ((in[i]) != '+' || in[i] != '-' || in[i] != '/' || in[i] != '*')
{
postfx += in[i];
}
else if (in[i] == '(')
{
S.push(in[i]);
}
else if (in[i] == ')')
{
while (!S.empty() && S.top() != '(')
{
postfx += S.top();
S.pop();
}
S.pop();
}
}
while (!S.empty()) {
postfx += S.top();
S.pop();
}
return postfx;
}

I think that your
else if ((in[i]) != '+' || in[i] != '-' || in[i] != '/' || in[i] != '*')
line catches brackets as well, so
else if (in[i] == '(')
and below never gets executed.
I think you should move
else if ((in[i]) != '+' || in[i] != '-' || in[i] != '/' || in[i] != '*')
to be the last option in the chained if.
Moreover, this if catches absolutely any symbol (because any symbol is either not equal to '+', or not equal to '-'). You need to replace || by &&; but if you will anyway have this if the last in chained if, you need no condition there at all, something like:
if ((in[i] == ' ')|| ...
else if ((in[i] == '+')|| ...
else if (in[i] == '(') ...
else if (in[i] == ')') ...
else postfx += in[i]; // no condition here
P.S. Also if you wrap the initial expression in brackets:
in = "(" + in + ")"
before the loop, you will not need the final while (!S.empty()) loop.

Don't append the parentheses to the output. They're only in the stack to guide your actions. I don't know where you got this code but you need to take a good look at the Dijkstra Shunting-yard algorithm. Your version doesn't match it at several points, and some of it doesn't even make sense:
if (in[i] != '+' || in[i] != '-' || in[i] != '/' || in[i] != '*')

Related

Checking correctness of infix expression in c++

I read an infix expression from the file, how can I check if it is a true or false input? I can only do 1 such case. I can think many situations but I don't know how to check in c++. Everyone please help me. Thanks very much!
int32_t check_error(string& str)
{
unsigned len = str.length();
if (str[0] != '(' || str[len-1] != ')')
{
return -1;
}
for (unsigned i = 3; i < len-3; i++)
{
if (str[i] >='0' && str[i] <= '9' && str[i + 1] = ' ' && str[i + 2] >= '0' && str[i + 2] <= '9')
return -2;
if ((str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '%' || str[i] == '^') && (str[i - 2] < '0' || str[i - 2]>'9' || str[i + 2] < '0' || str[i + 2]>'9'))
return -3;
if (str[i] == ' ' && str[i + 1] == ' ')
return -4;
}
return 0;
}

Why is iteration through a char vector is faster than the iteration through a string in C++

I am practicing a coding challenge where I have to reverse the vowels in a string.
My first approach failed because of exeeding Time limit. Here is my first approach using string iteration to reverse the vowels in a string.
string reverseVowels(string s) {
string str = "";
//storing the vowels from the string into another string
for (auto x : s)
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
str = str + x;
//swapping the vowels
int count = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
{
s[i] = str[count];
count++;
}
}
return s;
}
My second approach using the char vector iteration had passed all the tests. Here is my second approach
class Solution {
public:
string reverseVowels(string s) {
vector<char> v;
//storing the vowels from the string into vector
for (auto x : s)
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
v.push_back(x);
//swapping the vowels
int count = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
{
s[i] = v[count];
count++;
}
}
return s;
}
};
Could you explain why my first method failed the tests but second method passed the tests
Replace str = str + x; with str.push_back(x); or str += x;, and you'll likely see the same performance as with vector.
str = str + x; makes a copy of str, appends the character to that copy, then makes another copy when assigning back to str. As a result, your algorithm is quadratic, for no good reason.
It's because you're doing str = str + x, that creates an unnecessary copy of str, but std::vector::push_back or std::string::push_back appends a character to the vector or string, which is much faster than creating a copy of str.
str = str + x
this creates an additional copy of str while copying.
std::vector::push_back
this straight appends to the vector string

If statement incorrectly recognising spaces in a bracket

I am writing a mini programming language and I am currently working on my first proper command. My command is called 'prout("Sample text")'. When my program sees a space in between the letter t from the word prout and the opening bracket, it outputs an unexpected indent error, which is supposed to happen. What isn't supposed to happen is it recognising spaces in the text that the user wants to output and outputs an unexpected indent error. Does anybody know how to implement a way to stop the program from recognising that spaces in the text that the user wants to output is an unexpected indent error?
Here is the current output:
>>> prout("Hello")
Hello
>>> prout ("Hello")
Error: Unexpected indent //That is supposed to happen
>>> prout("Hello I am a programmer!")
Error: Unexpected indent //That is the problem
I've tried to use the attribute .npos to filter the spaces but this hasn't worked.
#include <iostream>
#include "printoutput.h"
#include "Line.h"
using namespace std;
void printoutput::print(string input) {
int i = 0;
int length = input.length();
if (input.find('(') != input.npos && (input.find(')') != input.npos) && (input.find('\"') != input.npos)) {
for (int i = 0; i <= input.length(); i++) {
char letter = input[i];
if (input.find(' ') != input.npos && (i == 5)) {
cout << "Error: Unexpected indent";
break;
}
if ((letter == 'p') && (i != 0) || (letter == 'r') && (i != 1) || (letter == 'o') && (i != 2) || (letter == 'u') && (i != 3) || (letter == 't') && (i != 4) || (letter == '(') && (i != 5) || (letter == '\"') && (i != 6 && i != input.length() - 2) || (letter == ')') && (i != length - 1)) {
char inputletter = input[i];
cout << inputletter;
}
else if ((i != 0 && (i != 1) && (i != 2) && (i != 3) && (i != 4) && (i != 5)) && (i != 6 && i != length - 2) && (i != length - 1)) {
char inputletter = input[i];
cout << inputletter;
}
}
}
if (input.find('\"') == input.npos) {
cout << "Syntax error: Missing quotation marks";
}
else if (input.find('(') == input.npos || (input.find(')')) == input.npos) {
cout << "Syntax error: Missing parenthesis";
}
cout << endl;
}
I expect the output to show the user's text that they have wanted to output with the spaces that they may have included.
Think about what this line does
if (input.find(' ') != input.npos && (i == 5)) {
If says if the input contains a space and if i equals 5 then output an error. Since i loops through all the indexes of the string this will be true of any string of length at least 5, which contains a space anywhere.
I'm thinking that what you really meant is this
if (input[5] == ' ')
but I'm not really sure.

How can I switch what points on an array equal in c++?

These change the array that I print to the screen. direc is an input before this. However, when array[1][2] == '#', it still moves '#' to array[1][0].
if (array[1][1] == '#' && direc == 'A' || direc == 'a' ) {
array[1][1] = '_';
array[1][0] = '#';
}
else {
if (array[1][1] == '#' && direc == 'D' || direc == 'd' ) {
array[1][1] = '_';
array[1][2] = '#';
}
else {
if (array[1][2] == '#' && direc == 'A' || direc == 'a' ) {
array[1][1] = '#';
array[1][2] = '_';
}
}
}
You can either add parentheses, as already noted in the comments, like this
if (array[1][1] == '#' && (direc == 'A' || direc == 'a' )) {
Alternatively you could use std::tolower in which case you no longer nee
if (array[1][1] == '#' && std::tolower(direc) == 'a') {
You could still add extra parentheses if you are not comfortable that operator && is lower precedence than operator ==
(the full table of operator precedences can be seen here)

Program to count evens odds vowels and consonants in a string

Hello i don't know how to fix my program to find how many evens, odds, vowels and consonants are in my string.
The program compiles and runs but i never get any vowels or consonants and everything else is added in either evens or odds (even characters).
Edit 1: By evens and odds i mean like if the user types in the string John123 i want to find how many characters are vowels in this case 1 the 'o' how many are consonants in this case 3 the 'J', 'h', 'n' how many are evens in this case 1 the '2' and how many are odds in this case 2 the '1' and the '3'.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s1;
int evens = 0; //
int odds = 0; //
int vowels = 0; //
int consonants = 0; //
cout << "Please type in Something: ";
getline(cin, s1);
cout << "You typed: " << s1 << endl;
for(int i = 0; i < s1.size(); ++i)
{
if((s1[i] % 2) == 0 )
{
++evens;
}
else if((s1[i] % 2 ) != 0) // What would an algorithm (formula) be to find when a number is odd without the not-equals != ?
{
++odds;
}
else if(s1[i] == 'A' || 'a' || 'E' || 'e' || 'O' || 'o' || 'I' || 'i' || 'U' || 'u')
{
++vowels;
}
else if(s1[i] == 'Q' || 'W' || 'R' || 'T' || 'Y' || 'P' || 'S' || 'D' || 'F' || 'G' || 'H' || 'J' || 'K' || 'L' || 'Z' || 'X' || 'C' || 'V' || 'B' || 'N' || 'M'||
'q' || 'w' || 'r' || 't' || 'y' || 'p' || 's' || 'd' || 'f' || 'g' || 'h' || 'j' || 'k' || 'l' || 'z' || 'x' || 'c' || 'v' || 'b' || 'n' || 'm')
// I specify every letter so that it dosn't pick whitespaces, symbols etc.
{
++consonants;
}
}
cout << "Evens in string are: " << evens << "\nOdds in string are: " << odds << "\nVowels in string are: " << vowels << "\nConsonants in string are: " << consonants;
return 0;
}
if((s1[i] % 2) == 0 )
This code is not correct. You are modulating the character, which isn't what you want. You want to first convert the character to an integer using atoi or something like that, I can't remember the function. (Just look up string to int c++). Then, once you've converted the character to an integer, then you can modulate it correctly.
Also, your || syntax is totally wrong, as Jongware mentioned. You need to have an == comparison for each character.
Should look like this (very long):
(s1[i] == 'A' || s1[i] == 'a' || s1[i] == 'E' || s1[i] == 'e' || s1[i] == 'O' || s1[i] == 'o' || s1[i] == 'I' || s1[i] == 'i' || s1[i] == 'U' || s1[i] == 'u')
You might want to store s1[i] in a char beforehand so it's more concise.
And you should place your character testing BEFORE the odd/even testing, so that you don't accidentally try and convert h or another non-numeric character to an integer.
A char is a number. For example, a is (on most computers) the ASCII code 97. So either your even test or your odd test will always match. You need to rearrange your if blocks.
Also, your character tests don't do what you intended. s1[i] == 'A' || 'a' tests as true whenever s1[i] is equal to 'A' or whenever 'a' as a number is not zero. But 97 is never zero, so that test will always result in true. You need code more like s1[i] == 'A' || s1[i] == 'a', testing the s1[i] value each time.
(One way to make things simpler: Once you know the character is not a vowel, you can test for an ASCII consonant with ((s1[i] >= 'a' && s1[i] <= 'z') || (s1[i] >= 'A' && s1[i] <= 'Z')).)
You have basically two problems:
Characters are interpreted as numbers, taking into account the character code. When you are checking whether the character is even or odd, you are checking its character code which will always be either event of odd, this is why your code will always enter into the first or second case. You must check whether your character is digit and if so, you must check whether it is even
Your OR is incorrect, see Ruby van Soelen's answer
Suggested, untested code:
for(int i = 0; i < s1.size(); ++i)
{
if (isdigit(s1[i])) {
if ((s1[i] == '0') || (s1[i] == '2') || (s1[i] == '4') || (s1[i] == '6') || (s1[i] == '8'))
{
++evens;
}
else // What would an algorithm (formula) be to find when a number is odd without the not-equals != ?
{
++odds;
}
}
else
{
if(s1[i]=='A' || s1[i]=='a' || s1[i]=='E' || s1[i]=='e' || s1[i]=='O' || s1[i]=='o' || s1[i]=='I' || s1[i]=='i' || s1[i]=='U' || s1[i]=='u')
{
++vowels;
}
else if(s1[i]=='B' || s1[i]=='b' || s1[i]=='C' || s1[i]=='c' || s1[i]=='D' || s1[i]=='d' || s1[i]=='F' || s1[i]=='f' || s1[i]=='G' || s1[i]=='g' || s1[i]=='H' || s1[i]=='h' || s1[i]=='J' || s1[i]=='j' || s1[i]=='K' || s1[i]=='k' || s1[i]=='L' || s1[i]=='l' || s1[i]=='M' || s1[i]=='m' || s1[i]=='N' || s1[i]=='n' || s1[i]=='P' || s1[i]=='p' || s1[i]=='Q' || s1[i]=='q' || s1[i]=='R' || s1[i]=='r' || s1[i]=='S' || s1[i]=='s' || s1[i]=='T' || s1[i]=='t' || s1[i]=='V' || s1[i]=='v' || s1[i]=='X' || s1[i]=='x' || s1[i]=='Y' || s1[i]=='y' || s1[i]=='Z' || s1[i]=='z')
// I specify every letter so that it dosn't pick whitespaces, symbols etc.
{
++consonants;
}
}
}
Your || syntax is making no sence, what you currently have is something like this
else if(s1[i] == 'A' || 'a' || 'E' || 'e' || 'O' || 'o' || 'I' || 'i' || 'U' || 'u')
while it should be like this:
else if(s1[i]=='A' || s1[i]=='a' || s1[i]=='E' || s1[i]=='e' || s1[i]=='O' || s1[i]=='o' || s1[i]=='I' || s1[i]=='i' || s1[i]=='U' || s1[i]=='u')
Expression in the else-if statement will be always equal to true
else if(s1[i] == 'A' || 'a' || 'E' || 'e' || 'O' || 'o' || 'I' || 'i' || 'U' || 'u')
becuase it is equivalent to
( s1[i] == 'A' ) || 'a' || 'B' /*...*/
where 'a' as it is not equal to zero evaluates to true.
So you have to write at least like
s1[i] == 'A' || s1[i] == 'a' || s1[i] == 'B' /*...*/
Also take into account that for example character 'B' though is not a digit but its code is even. So you will get a wrong result if will start checks from statement
if((s1[i] % 2) == 0 )
You should check at first whether the character is a digit. Also you could define an array of vowels and consonants and use standard function strchr that to check whether a character is a vowel or consonant.
For example
#include <iostream>
#include <string>
#include <cctype>
//...
const char *alpha_vowels = "AEIOU";
const char &alpha_consonants = "BCD...";
//...
if ( std::isdigit( s[i] ) )
{
s[i] % 2 == 0 ? ++evens : ++odds;
}
else if ( std::strchr( alpha_vowels, std::toupper( ( unsigned char )s[i] ) ) != nullptr )
{
++vowels;
}
else if ( std::strchr( alpha_consonants, std::toupper( ( unsigned char )s[i] ) ) != nullptr )
{
++consonants;
}
In addition to the other answers, here's a simpler way to accomplish the same thing.
std::set<char> vs{'a', 'o', 'u', 'e', 'i'};
for (auto&& c : s1) {
if (std::isdigit(c))
if ((c - '0') % 2 == 0) // Trick to convert numeric char -> int.
++evens;
else
++odds;
else if (std::isalpha(c))
if (vs.count(std::tolower(c)))
++vowels;
else
++consonants;
}