Program reading from the keyboard

T

tiger786

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
 
A

Alf P. Steinbach

* tiger786:
I am new to programming and studying basics in college .Can somebody
write a program on this?

[snip HOMEWORK assignment]

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

Sumit Rajan

tiger786 said:
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.

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

Regards,
Sumit.
 
B

Ben Cottrell

tiger786 said:
I am new to programming and studying basics in college .Can somebody
write a program on this?

The real question is, can _you_ write a program to do this? have you
made any attempt yet?
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


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
 
D

Dietmar Kuehl

tiger786 said:
 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.

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!
 
D

Daniel T.

"tiger786 said:
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

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

osmium

tiger786 said:
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

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

Dietmar Kuehl

osmium said:
This is a problem from the wonderful world of finite state machines. If

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

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

Members online

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,322
Latest member
ClaritaMcI

Latest Threads

Top