Connecting Tech Pros Worldwide Help | Site Map

Program reading from the keyboard

tiger786
Guest
 
Posts: n/a
#1: Feb 13 '06
I am new to programming and studying basics in college .Can somebody
write a program on this?

The program should monitor a possibly infinite stream of characters
from the keyboard (standard input). If it detects the sequence "ccc"
it outputs a "0". If it detects the sequence "cdc" it outputs a "1".
DO NOT detect sequences within sequences. The program should exit
cleanly when it detects an End Of Input.

For example:

The following sequence ccdcdcccdccc<End Of Input> would produce the
following result: 100
While the following sequence cccdcdccccddcdcdcb<End Of Input> would
produce the following result: 0101

Alf P. Steinbach
Guest
 
Posts: n/a
#2: Feb 13 '06

re: Program reading from the keyboard


* tiger786:[color=blue]
>
> I am new to programming and studying basics in college .Can somebody
> write a program on this?[/color]

[snip HOMEWORK assignment]

Of course. That's what we're here for. How much are you paying?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sumit Rajan
Guest
 
Posts: n/a
#3: Feb 13 '06

re: Program reading from the keyboard



"tiger786" <yadagirirao.purushothama@ssaglobal.com> wrote in message
news:1139803565.482494.249460@f14g2000cwb.googlegr oups.com...[color=blue]
>I am new to programming and studying basics in college .Can somebody
> write a program on this?
>
> The program should monitor a possibly infinite stream of characters
> from the keyboard (standard input). If it detects the sequence "ccc"
> it outputs a "0". If it detects the sequence "cdc" it outputs a "1".
> DO NOT detect sequences within sequences. The program should exit
> cleanly when it detects an End Of Input.
>[/color]

Perhaps, these may be of interest:
http://parashift.com/c++-faq-lite/ho...t.html#faq-5.2
http://parashift.com/c++-faq-lite/ho...t.html#faq-5.3

Regards,
Sumit.
--
Sumit Rajan <sumitr@msdc.hcltech.com>


Ben Cottrell
Guest
 
Posts: n/a
#4: Feb 13 '06

re: Program reading from the keyboard


tiger786 wrote:[color=blue]
> I am new to programming and studying basics in college .Can somebody
> write a program on this?[/color]

The real question is, can _you_ write a program to do this? have you
made any attempt yet?
[color=blue]
> The program should monitor a possibly infinite stream of characters
> from the keyboard (standard input). If it detects the sequence "ccc"
> it outputs a "0". If it detects the sequence "cdc" it outputs a "1".
> DO NOT detect sequences within sequences. The program should exit
> cleanly when it detects an End Of Input.
>
> For example:
>
> The following sequence ccdcdcccdccc<End Of Input> would produce the
> following result: 100
> While the following sequence cccdcdccccddcdcdcb<End Of Input> would
> produce the following result: 0101[/color]


You could start out with a simple program which reads a line of input
from the keyboard. then you can expand your program to break down and
analyse the input to generate your result
Dietmar Kuehl
Guest
 
Posts: n/a
#5: Feb 13 '06

re: Program reading from the keyboard


tiger786 wrote:[color=blue]
>*The program should monitor a possibly infinite stream of characters
> from the keyboard (standard input). *If it detects the sequence "ccc"
> it outputs a "0". *If it detects the sequence "cdc" it outputs a "1".
> DO NOT detect sequences within sequences. *The program should exit
> cleanly when it detects an End Of Input.[/color]

The others who think that you should do your homework assignment
alone are just spoilsports! The obvious solution to the above
problem is rather simple:

#include <iostream>
#include <iterator>
#include <algorithm>

struct __ {
enum { _0 = 0x636363 };
__(): _() {}
void operator()(unsigned char _1) {
_ = ((_ = (_ & 0xffff) << 8 | _1) == _0 || _ == _0 + 0x100)
&& std::cout << (_0 < _)? 0: _;
}
unsigned long _;
} _;

int main()
{
std::for_each(std::istreambuf_iterator<char>(std:: cin),
std::istreambuf_iterator<char>(), _);
std::cout << "\n";
}

Of course, you should understand the program before handing it in
because it would be a really stupid idea not to do your homework!
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Daniel T.
Guest
 
Posts: n/a
#6: Feb 13 '06

re: Program reading from the keyboard


In article <1139803565.482494.249460@f14g2000cwb.googlegroups .com>,
"tiger786" <yadagirirao.purushothama@ssaglobal.com> wrote:
[color=blue]
> I am new to programming and studying basics in college .Can somebody
> write a program on this?
>
> The program should monitor a possibly infinite stream of characters
> from the keyboard (standard input). If it detects the sequence "ccc"
> it outputs a "0". If it detects the sequence "cdc" it outputs a "1".
> DO NOT detect sequences within sequences. The program should exit
> cleanly when it detects an End Of Input.
>
> For example:
>
> The following sequence ccdcdcccdccc<End Of Input> would produce the
> following result: 100
> While the following sequence cccdcdccccddcdcdcb<End Of Input> would
> produce the following result: 0101[/color]

I would do it this way:

start with:

#include <string>
#include <iostream>

using namespace std;

string examine_sequence( const string& is ) {
string result;
// insert code here
return result;
}

void test_examine_sequence( const char* in, const char* out ) {
string esout = examine_sequence( in );
if ( esout != out ) {
cout << "Error: expected '" << out
<< "' for sequence '" << in
<< "', got '" << esout << "'" << endl;
throw -1;
}
}

int main() {
test_examine_sequence( "ccc", "0" );
test_examine_sequence( "ccdc", "1" );
test_examine_sequence( "ccdcdccc", "10" );
test_examine_sequence( "cccdcdc", "01" );
test_examine_sequence( "ccdcdcccdccc", "100" );
test_examine_sequence( "cccdcdcccc", "010" );
test_examine_sequence( "cccdcdccccddcdcdcb", "0101" );
cout << "everything works" << endl;
}

Run the above, fix any errors until "everything works" appears on the
screen. Then use this main:

int main() {
string s;
getline( cin, s );
cout << examine_sequence( s ) << endl;
}

If you have any trouble with fixing any of the errors. Post your
"examine_sequence" function and the error you are getting, and I'll help
you out.

BTW, this is called "test first design". It would do you well to look it
up.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
osmium
Guest
 
Posts: n/a
#7: Feb 13 '06

re: Program reading from the keyboard


"tiger786" writes:
[color=blue]
>I am new to programming and studying basics in college .Can somebody
> write a program on this?
>
> The program should monitor a possibly infinite stream of characters
> from the keyboard (standard input). If it detects the sequence "ccc"
> it outputs a "0". If it detects the sequence "cdc" it outputs a "1".
> DO NOT detect sequences within sequences. The program should exit
> cleanly when it detects an End Of Input.
>
> For example:
>
> The following sequence ccdcdcccdccc<End Of Input> would produce the
> following result: 100
> While the following sequence cccdcdccccddcdcdcb<End Of Input> would
> produce the following result: 0101[/color]

Wow! What a nasty problem for an introductory course. The logical analysis
that precedes writing a program is the tough part. Be sure you get the
rules firmly in your head before you start. Write several test cases and
decide what the output should be with pencil and paper..

This is a problem from the wonderful world of finite state machines. If
you can find a nice tutorial it would be great, but I didn't find one in a
few moments with Google. It's frustrating to be unable to draw a simple
state machine in ASCII. I found this definition, but a picture would be a
thousand times better.

-----
FSMs are most commonly represented by state diagrams, which are also called
state transition diagrams. The state diagram is basically a directed graph
where each vertex represents a state and each edge represents a transition
between two states.
from http://sakharov.net/fsmtutorial.html
----

Note well, however, that the state usually changes but does not *need* to
change, which conflicts with that definition.
If you can find a tutorial with three or four round circles and some lines
with arrowheads (mostly) connecting the circles you have hit pay dirt.
From reading the responses, I seem to be the only person in these newsgroups
that was born without an innate knowledge of state machines, perhaps you are
one of the lucky ones too..

Fortunately, the problem is simple enough that you can bull ahead without
knowing about state machines if you have to, and I suppose some might
consider using such as overkill..

Come up with some kind of algorithm, and write the code. I visualize the
centerpiece as a switch statement with four values, one for each of the
states. (If I didn't mess up.}


Dietmar Kuehl
Guest
 
Posts: n/a
#8: Feb 14 '06

re: Program reading from the keyboard


osmium wrote:[color=blue]
> This is a problem from the wonderful world of finite state machines. If[/color]

Is it? I saw the state machine sitting there but with the given
spec it can be easily implemented with kind of a minimalist
state machine - if, of course, you would call the following a
use of a state machine at all (it is there, I can see it but it
is, well, not really obvious at all and only a rather special
case state machine):

#include <iostream>
#include <iterator>
#include <algorithm>

struct __ {
enum { _0 = 0x636363 };
__(): _() {}
void operator()(unsigned char _1) {
_ = ((_ = (_ & 0xffff) << 8 | _1) == _0 || _ == _0 + 0x100)
&& std::cout << (_0 < _)? 0: _;
}
unsigned long _;
} _;

int main()
{
std::for_each(std::istreambuf_iterator<char>(std:: cin),
std::istreambuf_iterator<char>(), _);
std::cout << "\n";
}

Of course, even the "switch", although being there, is not at
all that obvious. However, even if this is not the intended
solution: it solves the problem as stated in [nearly] portable
code (the changes to make it portable are rather trivial but
have a serious impact on readability).
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Closed Thread