473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Program reading from the keyboard

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

Feb 13 '06 #1
7 2561
* 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?

--
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?
Feb 13 '06 #2

"tiger786" <ya**********************@ssaglobal.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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/ho...t.html#faq-5.2
http://parashift.com/c++-faq-lite/ho...t.html#faq-5.3

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Feb 13 '06 #3
tiger786 wrote:
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
Feb 13 '06 #4
tiger786 wrote:
*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!
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 13 '06 #5
In article <11**********************@f14g2000cwb.googlegroups .com>,
"tiger786" <ya**********************@ssaglobal.com> wrote:
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.

--
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.
Feb 13 '06 #6
"tiger786" writes:
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.}
Feb 13 '06 #7
osmium wrote:
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).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Stu | last post by:
I have the following code in a "C" program. test_prog.c main() { while ( fgets ( buffer, sizeof(buffer), stdin) != NULL) { ..... ..... }
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
0
by: Chris Herring | last post by:
I am writing a WinForms app that will integrate a handheld voting device system into the app. The handhelds communicate to the PC via a USB connection. The USB device has no manufacturer driver,...
7
by: randomtalk | last post by:
hello, recently i am reading "The C Programming Language, Second Edition", and for some reason, the sample program in there doesn't work as expected under GCC.. here is the code: #include...
13
by: John Salerno | last post by:
If I want to write my code in a separate text editor (I like UltraEdit) but then press a single button to have that code run in the IDLE environment, is that possible? I know that you can configure...
1
by: sallyk57 | last post by:
Help with following Programs: Write two programs one where the performance rating here shoud be entered as a int where Excellent =1, Good= 2, Poor=3. an employee who is rated excellent will...
3
by: monkey1001 | last post by:
the program is supposed to calculate home or commercial gas usage and then print the type of customer,present reading,previous reading,cubic used and cost for the month..the user inputs values btw...
2
by: Swan | last post by:
How can I restrict alt+tab and start menu from keyboard while program executing(VB)?I am posting what I tried-- form.frm Option Explicit Private Sub Form_Load() HookKeyboard End Sub...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.