Checking correctness of infix expression in c++ - 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;
}

Related

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

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)

tictaktoe Array bound overflow

I am in need of help for this code that i am working on for a assignment. I am have the issue where if i have any X's on the board that is either in the left 2 columns it will display a X in the row above. I used my debugger and it seems that it is trying to access something outside the array bounds, but it shouldnt be. any advice on how to do this?
#include <iostream>
using namespace std;
void printTTT(char a[3][3]);
void insertX(/*PASS BY REFERENCE*/);
void insertO(char (&arr)[3][3]);
void checkForWin(/*PASS BY REFERENCE*/); // IGNORE THIS FOR NOW
int main() {
char TTTarray[3][3] = { { 'X','-','-' },
{ '-','-','-' },
{ 'X','-','-' } };
//char TTTarray[3][3] = { {'-','X','-'},
// {'-','X','-'},
// {'-','-','O'}};
//char TTTarray[3][3] = { {'-','-','-'},
// {'-','X','-'},
// {'-','O','-'}};
//char TTTarray[3][3] = { {'X','-','X'},
// {'-','-','-'},
// {'O','-','-'}};
//char TTTarray[3][3] = { {'X','-','X'},
// {'O','X','-'},
// {'O','-','O'}};
//insertX(/*CALL*/);
//OR
insertO(TTTarray);
printTTT(TTTarray);
/*****************
I have included the declaratoin of the array, initialized to - for each spot.
The '-' represents an empty position. You should fill it with either a
capital 'O' or a capital 'X'. I have also included a number of initialized arrays
to test; just comment out the ones you don't want for that moment
*****************/
return 0;
}
void printTTT(char a[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << a[i][j];
}
cout << endl;
}
}
void insertX(/*PASS BY REFERENCE*/) {
}
void insertO(char (&arr)[3][3])
{
int x1x;
int x1y;
//int x2x;
//int x2y;
for (int i = 0; i < 3; i++)
{
int go = 0;
for (int j = 0; j < 3; j++)
{
if (arr[i][j] == '-')
{
x1x = i;
x1y = j;
// looking for 2 x's for the block lol
if (x1x == 0 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1x == 1 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1x == 2 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 0 && go == 0)
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 1 && go == 0)
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 2 && go == 0)
181,1-8 83%
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
}
}
}
}
Take a look at these lines from your insertD function:
if (x1x == 0 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
In this case you have checked that x1x is zero, but you haven't checked x1y. So in this case you will go out of bounds if x1y is non-zero.
A couple of lines below you have
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
This will go out of bounds too, when x1y is zero.
You need to add more checks, or rethink the logic.

Infix to Postfix - Deleting Parenthesis

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] != '*')

Entering into the wrong If statement

the program does not enter the if statement it suppose to enter.for example when the sentence1 is oguzhan and the sentence2 is bugrahan for first characters it should enter the first if statement end substitution should be 4 but it doesn't.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cmath>
using namespace std;
int main() {
char sentence1[50];
char sentence2[50];
int m, n, k, l;
int i, j, substitution;
cout << "Enter the first word:" << endl;
cin >> sentence1;
cout << "Enter the second word:" << endl;
cin >> sentence2;
m = strlen(sentence1);
n = strlen(sentence2);
int cost[m + 1][n + 1];
cost[0][0] = 0;
for (i = 1; i < m + 1; i++) {
cost[i][0] = cost[i - 1][0] + 2;
}
for (j = 1; j < n + 1; j++) {
cost[0][j] = cost[0][j - 1] + 2;
}
for (i = 1; i < m + 1; i++) {
for (j = 1; j < n + 1; j++) {
if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' ||
sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' ||
sentence1[i - 1] == 'o') &&
(sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' ||
sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' ||
sentence2[j - 1] != 'o')) {
substitution = 4;
}
if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' ||
sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
sentence1[i - 1] != 'o') &&
(sentence2[j - 1] == 'a' || sentence1[i - 1] != 'u' ||
sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
sentence1[i - 1] != 'o')) {
substitution = 4;
}
if (sentence1[i - 1] == sentence2[j - 1]) {
substitution = 0;
}
if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' ||
sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' ||
sentence1[i - 1] == 'o') &&
(sentence2[j - 1] == 'a' || sentence2[j - 1] == 'u' ||
sentence2[j - 1] == 'e' || sentence2[j - 1] == 'i' ||
sentence2[j - 1] == 'o')) {
substitution = 3;
}
if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' ||
sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
sentence1[i - 1] != 'o') &&
(sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' ||
sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' ||
sentence2[j - 1] != 'o')) {
substitution = 3;
}
cost[i][j] = min(min(cost[i - 1][j] + 2, cost[i][j - 1] + 2),
cost[i - 1][j - 1] + substitution);
}
}
for (i = 0; i < m + 1; i++) {
for (j = 0; j < n + 1; j++) {
cout << cost[i][j] << " ";
}
cout << endl;
}
cout << sentence1[0];
return 0;
}
A condition like: sentence2[j-1]!='a'||sentence2[j-1]!='u' is always true -- no single character can be equal to both a and u, so one of these has to be true.
If you're using !=, it must almost always be joined by &&, not ||, otherwise the result will always be true, regardless of input.