[C++] Given a boolean string, for example: string s = \"T => F ^ (F v F)\"; Plac
ID: 3890976 • Letter: #
Question
[C++]
Given a boolean string, for example: string s = "T => F ^ (F v F)";
Place the elements into the following vector: vector<string> boolSymbols
But the elements must be placed in the order given,element by element, excluding elemtents such as
'=>' which must be placed in the vector altogether in one element.
Heres a string of valid bools: string validBool = { ''T','F','^', 'v', '~', '=','>','<','=','>', '(', ')' };
But remeber '=>' and '<=>' are to remain the together.
So in summary: Go element by element in a given string, and check if the element is '=' or '<' if it is one those check the next element to see if it is '=>' or the next element to check if it is '<=>'. After this place that element in the vector, and if it is '<=>' or '=>' place that entire character in a single element of the vector.
Explanation / Answer
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string s;//variable to store boolean string...
char validBool[] = { 'T','F','^', 'v', '~', '=','>','<','=','>', '(', ')' };
cout<<"Enter boolean string :";//taking input..
getline(cin,s);
vector<string> boolSymbols;//to store symbols of boolean string.
int i;
string n;
for(i=0;s[i]!='';i++)//adding symbols to vector...
{
n="";
n=n+s[i];
if(s[i]=='=')
{
if(s[i+1]=='>')//checking next element
{
i++;
boolSymbols.push_back("=>");//adding to vector...
}
else
{
boolSymbols.push_back(n);//adding to vector...
}
}
else if(s[i]=='<')
{
if(s[i+1]=='='&&s[i+2]=='>')//checking next element
{
i=i+2;
boolSymbols.push_back("<=>");//adding to vector...
}
else
{
boolSymbols.push_back(n); //adding to vector...
}
}
else
{
if(s[i]!=' ')//if it is not a space
boolSymbols.push_back(n); //adding to vector...
}
}
//printing symbols in vector...
cout<<"Symbols: ";
for(i=0;i<boolSymbols.size();i++)
{
cout<<boolSymbols.at(i)<<endl;
}
return 0;
}
output:
Enter boolean string :T => F ^ (F V F)
Symbols:
T
=>
F
^
(
F
V
F
)
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.