Connecting Tech Pros Worldwide Help | Site Map

Truth Table help

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 03:22 PM
PerritoPerron
Guest
 
Posts: n/a
Default Truth Table help

//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;
}



  #2  
Old July 19th, 2005, 03:22 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Truth Table help


"PerritoPerron" <peter@aol.tv> wrote in message
news:3giQa.72214$hV.4603213@twister.austin.rr.com. ..[color=blue]
> //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[/color]
or[color=blue]
> 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
>[/color]


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'

[color=blue]
>
> return 0;
> }
>
> // Main function invokes perform operation function
>
> int main(){
> Myclass mc;
> mc.func();
> return 0;
> }
>[/color]

john


  #3  
Old July 19th, 2005, 03:22 PM
PerritoPerron
Guest
 
Posts: n/a
Default Re: Truth Table help

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" <john_andronicus@hotmail.com>
Newsgroups: comp.lang.c++
Sent: Sunday, July 13, 2003 3:08 PM
Subject: Re: Truth Table help

[color=blue]
>
> "PerritoPerron" <peter@aol.tv> wrote in message
> news:3giQa.72214$hV.4603213@twister.austin.rr.com. ..[color=green]
> > //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[/color]
> or[color=green]
> > 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[/color][/color]
expressions\n"[color=blue][color=green]
> > << "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
> >[/color]
>
>
> 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.[/color]
You[color=blue]
> need to analyse the input data character by character and make choices[/color]
based[color=blue]
> in the input you find.
>
> To be perfectly honest this seems far too difficult for you at your[/color]
current[color=blue]
> level of ability, but if you really want to know how find a book on[/color]
parsing[color=blue]
> 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'
>
>[color=green]
> >
> > return 0;
> > }
> >
> > // Main function invokes perform operation function
> >
> > int main(){
> > Myclass mc;
> > mc.func();
> > return 0;
> > }
> >[/color]
>
> john
>
>[/color]


  #4  
Old July 19th, 2005, 03:22 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Truth Table help


"PerritoPerron" <peter@aol.tv> wrote in message
news:8bjQa.72224$hV.4610475@twister.austin.rr.com. ..[color=blue]
> 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[/color]
to[color=blue]
> parse..
> if you could learned it, so could I.
> so can you just give me an example or some guidelines here please.
> Thanks John..
>[/color]

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[i])
{
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



  #5  
Old July 19th, 2005, 03:22 PM
PerritoPerron
Guest
 
Posts: n/a
Default Re: Truth Table help

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..



  #6  
Old July 19th, 2005, 03:22 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Truth Table help


"PerritoPerron" <peter@aol.tv> wrote in message
news:TvjQa.72227$hV.4613898@twister.austin.rr.com. ..[color=blue]
> I know about loops, functions, pointers, arrays and switch,
>[/color]

Don't think you'll need pointers.
[color=blue]
> the point John is that tomorrow I have to turn that sucker in...[/color]

Tough, you're not going to get a complete solution, but something will be
better than nothing.
[color=blue]
>
> I am just putting things together now...
>
> what about to ouput..??[/color]

Huh?
[color=blue]
>
> Thanks again John you helped me some..
>[/color]

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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.