This is my fourth time posting the same question on chegg and i would sincerely
ID: 663180 • Letter: T
Question
This is my fourth time posting the same question on chegg and i would sincerely appreciate if someone could help me here. I am trying to write the code below which was answered by someone here on chegg using boolean method. So basically if there is int(a) or int(e) i would like to use bool a(void) or bool e (void) and the part where i,j,k is used i want to move my string with just s=s.substr(1) and for return 0 i would use return true and for return -1 i would use return false. I am able to understand this code very well but i just need someone to help me implementing this code using boolean function method. I am sure while i am asking to change this to boolean method there will be many things that will be completely changed in the code and i need someone to write the full code. Please do not post partial answers as i am struggling with this since 1 week. Below is the code that i am talking about :
#include
#include
#include
using namespace std;
int A();
int E(); //changing
int P(int k); //changing
int L();
int I(); //not changing
int UI(); //not changing
int UL(); //not hanging
string data;
int i,j;
int main()
{
ifstream infile;
infile.open("input.txt");
while(!infile.eof())
{
i=0;
j=0;
infile >> data;
if(A()!=-1&&i==data.length())
cout<<"The string "<
int A()
{
//cout<<" A()";
if(I()!=-1)
{
i=j;
if(data[i++]=='=')
{
if(E()!=-1)
return 0;
}else
{
i=0;
if(E()!=-1)
return 0;
}
}else
{
i=0;
if(E()!=-1)
return 0;
}
return -1;
}
int E()
{
//cout<<" E()";
if(P(-1)!=-1)
{
if(i==data.length())
return 0;
else
{
if(data[i]=='+'||data[i]=='-'||data[i]=='*'||data[i]=='/')
{
i++;
if(P(1)!=-1)
return 0;
}
}
}
return -1;
}
int P(int k)
{
//cout<<" P()";
if(I()!=-1)
{
if(k==-1)
{
i=j;
return 0;
}
else
{
if(j==data.length())
{
i=j;
return 0;
}
else
return E();
}
}
else if(L()!=-1)
{
if(k==-1)
{
i=j;
return 0;
}
else
{
if(j==data.length())
{
i=j;
return 0;
}
else
return E();
}
}
else if(UI()!=-1)
{
if(k==-1)
{
i=j;
return 0;
}
else
{
if(j==data.length())
{
i=j;
return 0;
}
else
return E();
}
}
else if(UL()!=-1)
{
if(k==-1)
{
i=j;
return 0;
}
else
{
if(j==data.length())
{
i=j;
return 0;
}
else
return E();
}
}
/*else if(P(-1)!=-1)
{
if(data[i]=='+'||data[i]=='-'||data[i]=='*'||data[i]=='/')
{
i++;
if(P(1)!=-1)
return 0;
}
}*/
else if(k==1)
return E();
else
return -1;
return -1;
}
int L()
{
//cout<<" L()";
j=i;
while(j {
if(data[j]>='0'&&data[j]<='9')
j++;
else
break;
}
if(j==i)
return -1;
else
return 0;
}
int I()
{
//cout<<" I()";
j=i;
while(j {
if(data[j]>='a'&&data[j]<='z')
j++;
else
break;
}
if(j==i)
return -1;
else
return 0;
}
int UI()
{
//cout<<" UI()";
j=i;
int k=i;
if(j {
j++;
i=j;
if(I()!=-1)
{
i=j;
return 0;
}
}
i=k;
return -1;
}
int UL()
{
//cout<<" UL()";
j=i;
int k=i;
if(j {
j++;
i=j;
if(L()!=-1)
{
i=j;
return 0;
}
}
i=k;
return -1;
}
Explanation / Answer
As mentioned in the above question ,you want to make changes in two functions i.e. int E() and int P(int k).You want to make the return type of these two functions to the boolean data type.So, i have mentioned only these two functions by changing the code of these two functions.Please refer to the below code:
So firstly include the header file for boolean functions required and some of the mandatory header files required as defined below:
#include<stdio.h>
#include<conio.h>
#include<stdbool.h>
using namespace std;
int A();
bool E(); //changing
bool P(int k); //changing
int L();
int I(); //not changing
int UI(); //not changing
int UL(); //not hanging
string data;
int i,j;
int main()
{
ifstream infile;
infile.open("input.txt");
while(!infile.eof())
{
i=0;
j=0;
infile >> data;
if(A()!=-1&&i==data.length())
cout<<"The string "<
int A()
{
//cout<<" A()";
if(I()!=-1)
{
i=j;
if(data[i++]=='=')
{
if(E()!=-1)
return 0;
}else
{
i=0;
if(E()!=-1)
return 0;
}
}else
{
i=0;
if(E()!=-1)
return 0;
}
return -1;
}
//Changes to the function E()
bool E()
{
//cout<<" E()";
if(P(-1)!=-1)
{
if(i==data.length())
return true;
else
{
if(data[i]=='+'||data[i]=='-'||data[i]=='*'||data[i]=='/')
{
i++;
if(P(1)!=-1)
return ture;
}
}
}
return false;
}
//Changes to the function P()
bool P(int k)
{
//cout<<" P()";
if(I()!=-1)
{
if(k==-1)
{
i=j;
return true;
}
else
{
if(j==data.length())
{
i=j;
return true;
}
else
return E();
}
}
else if(L()!=-1)
{
if(k==-1)
{
i=j;
return true;
}
else
{
if(j==data.length())
{
i=j;
return true;
}
else
return E();
}
}
else if(UI()!=-1)
{
if(k==-1)
{
i=j;
return true;
}
else
{
if(j==data.length())
{
i=j;
return true;
}
else
return E();
}
}
else if(UL()!=-1)
{
if(k==-1)
{
i=j;
return true;
}
else
{
if(j==data.length())
{
i=j;
return true;
}
else
return E();
}
}
/*else if(P(-1)!=-1)
{
if(data[i]=='+'||data[i]=='-'||data[i]=='*'||data[i]=='/')
{
i++;
if(P(1)!=-1)
return true;
}
}*/
else if(k==1)
return E();
else
return false;
return false;
}
int L()
{
//cout<<" L()";
j=i;
while(j {
if(data[j]>='0'&&data[j]<='9')
j++;
else
break;
}
if(j==i)
return -1;
else
return 0;
}
int I()
{
//cout<<" I()";
j=i;
while(j {
if(data[j]>='a'&&data[j]<='z')
j++;
else
break;
}
if(j==i)
return -1;
else
return 0;
}
int UI()
{
//cout<<" UI()";
j=i;
int k=i;
if(j {
j++;
i=j;
if(I()!=-1)
{
i=j;
return 0;
}
}
i=k;
return -1;
}
int UL()
{
//cout<<" UL()";
j=i;
int k=i;
if(j {
j++;
i=j;
if(L()!=-1)
{
i=j;
return 0;
}
}
i=k;
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.