Truth Table help

P

PerritoPerron

//I need a little help with this program, it runs but the output is not
correct.
//Can i get some help here please u fellas???

// This Program asks for user input like 1 && 0 and prints if it is true or
false
// it calculates the input to perform Logical operation from input

#include <iostream>
using namespace std;

// Defines class

class Myclass{
public:
int func();
};

// Function to perform operation

int Myclass::func(){

char x[32] = ""; // array to receive input

cout << "\n\t\t\t T R U T H T A B L E\n" // asks for input
<< "\t\t\t ========= =========\n\n"
<< "Logical operators allow us to form more complex test expressions\n"
<< "for decision and repetition statements. The logical operators
are:\n\n"
<< "1. && (logical and)\n"
<< "2. || (logical or)\n"
<< "3. ! (logical not)\n\n"
<< "Please enter 1 for true, 0 for false with a logical operator\n"
<< "Parentheses may be used to alter the order in which operators are
applied.\n"
<< "Example:\n"
<< "1 && 0 or 1 || 0 ";

cin.getline(x, 32, '\n'); // stores input in variable called x

cout << bool (x) << endl; // prints out true or false value


return 0;
}

// Main function invokes perform operation function

int main(){
Myclass mc;
mc.func();
return 0;
}
 
J

John Harrison

PerritoPerron said:
//I need a little help with this program, it runs but the output is not
correct.
//Can i get some help here please u fellas???

// This Program asks for user input like 1 && 0 and prints if it is true or
false
// it calculates the input to perform Logical operation from input

#include <iostream>
using namespace std;

// Defines class

class Myclass{
public:
int func();
};

// Function to perform operation

int Myclass::func(){

char x[32] = ""; // array to receive input

cout << "\n\t\t\t T R U T H T A B L E\n" // asks for input
<< "\t\t\t ========= =========\n\n"
<< "Logical operators allow us to form more complex test expressions\n"
<< "for decision and repetition statements. The logical operators
are:\n\n"
<< "1. && (logical and)\n"
<< "2. || (logical or)\n"
<< "3. ! (logical not)\n\n"
<< "Please enter 1 for true, 0 for false with a logical operator\n"
<< "Parentheses may be used to alter the order in which operators are
applied.\n"
<< "Example:\n"
<< "1 && 0 or 1 || 0 ";

cin.getline(x, 32, '\n'); // stores input in variable called x

cout << bool (x) << endl; // prints out true or false value


You need to learn how to write a parser. This is not a quick or easy
operation. Do not expect a one line solution like bool(x), we're talking
maybe 40 lines of code, depending on how sophisticated you want to get. You
need to analyse the input data character by character and make choices based
in the input you find.

To be perfectly honest this seems far too difficult for you at your current
level of ability, but if you really want to know how find a book on parsing
and study. For instance Stroustrup has a parser in chapter 6 'The C++
Programming Language', or Avo Sethi and Ullman in chapter 2 of 'Compilers,
Principles, Techniques and Tools'

return 0;
}

// Main function invokes perform operation function

int main(){
Myclass mc;
mc.func();
return 0;
}

john
 
P

PerritoPerron

You right I don't know about parsing..
but I am writting here to get some help and maybe you can lead me on how to
parse..
if you could learned it, so could I.
so can you just give me an example or some guidelines here please.
Thanks John..

----- Original Message -----
From: "John Harrison" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Sunday, July 13, 2003 3:08 PM
Subject: Re: Truth Table help

PerritoPerron said:
//I need a little help with this program, it runs but the output is not
correct.
//Can i get some help here please u fellas???

// This Program asks for user input like 1 && 0 and prints if it is true or
false
// it calculates the input to perform Logical operation from input

#include <iostream>
using namespace std;

// Defines class

class Myclass{
public:
int func();
};

// Function to perform operation

int Myclass::func(){

char x[32] = ""; // array to receive input

cout << "\n\t\t\t T R U T H T A B L E\n" // asks for input
<< "\t\t\t ========= =========\n\n"
<< "Logical operators allow us to form more complex test expressions\n"
<< "for decision and repetition statements. The logical operators
are:\n\n"
<< "1. && (logical and)\n"
<< "2. || (logical or)\n"
<< "3. ! (logical not)\n\n"
<< "Please enter 1 for true, 0 for false with a logical operator\n"
<< "Parentheses may be used to alter the order in which operators are
applied.\n"
<< "Example:\n"
<< "1 && 0 or 1 || 0 ";

cin.getline(x, 32, '\n'); // stores input in variable called x

cout << bool (x) << endl; // prints out true or false value


You need to learn how to write a parser. This is not a quick or easy
operation. Do not expect a one line solution like bool(x), we're talking
maybe 40 lines of code, depending on how sophisticated you want to get. You
need to analyse the input data character by character and make choices based
in the input you find.

To be perfectly honest this seems far too difficult for you at your current
level of ability, but if you really want to know how find a book on parsing
and study. For instance Stroustrup has a parser in chapter 6 'The C++
Programming Language', or Avo Sethi and Ullman in chapter 2 of 'Compilers,
Principles, Techniques and Tools'

return 0;
}

// Main function invokes perform operation function

int main(){
Myclass mc;
mc.func();
return 0;
}

john
 
J

John Harrison

PerritoPerron said:
You right I don't know about parsing..
but I am writting here to get some help and maybe you can lead me on how to
parse..
if you could learned it, so could I.
so can you just give me an example or some guidelines here please.
Thanks John..

The trouble is that it is far too big a subject to deal with in a newsgroup
post. That's why I'd recommend a book.

But you will need to write code something like this (x is the string you've
read in so far)

switch (x)
{
case '0':
// do whatever
break;
case '1':
// do whatever
break;
case '&':
// do whatever
break;
case '|':
// do whatever
break;
case '!':
// do whatever
break;
case '(':
// do whatever
break;
case ')':
// do whatever
break;
}

Basically you have to break down the expression into individual characters,
make decisions based on what you find. Also you need to understand
recursion, because the routines you write will be recursive.

You are not going to figure this out by yourself, even with help from this
newsgroup, no-one could. You need to find a book or a person who can explain
this to you.

john
 
P

PerritoPerron

I know about loops, functions, pointers, arrays and switch,

the point John is that tomorrow I have to turn that sucker in...

I am just putting things together now...

what about to ouput..??

Thanks again John you helped me some..
 
J

John Harrison

PerritoPerron said:
I know about loops, functions, pointers, arrays and switch,

Don't think you'll need pointers.
the point John is that tomorrow I have to turn that sucker in...

Tough, you're not going to get a complete solution, but something will be
better than nothing.
I am just putting things together now...

what about to ouput..??
Huh?


Thanks again John you helped me some..

I'm surprised you're being asked to do this without having had the concepts
explained (or did you just skip those lessons). It seems very difficult.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

I need help 1
Can't solve problems! please Help 0
Lexical Analysis on C++ 1
Need help with this script 4
Help in this program. 2
Help with Loop 0
Help with code plsss 0
Only one table shows up with the information 2

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top