Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replacing for-loops

Posted on 1997-11-18
5
Medium Priority
?
226 Views
Last Modified: 2010-04-16
I've got to write a program that replaces any for-loops in a program with repeat-loops. The input is a valid Pascal program entered line-by-line (so I'm assuming the conversion will happen after each line entered). Any thoughts, partial algorithms, etc. to get me going in the right direction? We cannot use strings or arrays, just reading in characters.
0
Comment
Question by:chadd082197
5 Comments
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216849
Is this program reading in a text file (for example, something like chadd.pas), or are you typing in at the keyboard?  Also, correct me if I'm not fully comprehending this, but basically is sounds like your program is to replace a statement like:

  for i:=1 to 10 do
    write('Hey maw, get off the dang roof!');

with:

  i:=1;
  repeat
    write('Hey maw, get off the dang roof!');
    inc(i);
  until i>10;

???

Just give me a little clarification, and I should be able to present you with some ideas...  Thanks!
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1216850
please clearify your question
0
 

Expert Comment

by:mccranie
ID: 1216851
If you can't use strings and arrays, you will need to use a (linked) list or stack to allow for nested loops.  The rest is straightforward.

0
 

Author Comment

by:chadd082197
ID: 1216852
The conversion you presented is exactly right..... the intent is to have the user type in each line at the keyboard and have the program perform the conversion line-by-line (after each Return key). If the line doesn't contain a for-statement, it just prints it as is. Also, we're supposed to read each char.
0
 
LVL 2

Accepted Solution

by:
mitchell042997 earned 200 total points
ID: 1216853
OK, here we go...  First you are going to neat a huge repeat ... until someBoolean;  Then, in this huge repeat, you need these statements:

  repeat until keypressed;
  ch:=readkey;

ch is declared as a char up in your variable section.  This will take in a keystroke from the keyboard and save it in ch.  You can then do:

  write(ch);

and do a big case for ch.  When the user types a for loop, they need to have a begin and end too, even if it is just a one line for loop, else you won't know where to put the until ... at.  You will need to store each line in an array or some kind of data structure.  I suggest a simple string (which is an array of characters, obviously).  so, do a var blah:string;

start off with string:='';

then, after each keystroke (if not ENTER), do:  string:=string+ch;  If they type enter, you will need to reset string.  To tell what key they are entering, you will need a case statement, like I said, which looks at the ordinal value (ASCII value) of the key typed.  So, a case ord(ch) of ...  ENTER = 13, characters = 65..81 (?) and something or other to something or other.  In any case, once the user hits return, look at your string, search it for the word "for".  If you find that, you will need to save the variable name used and the limiting values (i.e. "for i:=1 to 10"  save "i","1" and "10").  You will then print out to the screen:

  i:=1;  {or whatever variable := whatever lower limit}
  repeat

then you will let then enter at will until they enter the corresponding end, at which case you'll have it print:

    inc(i);
  until i=10;  {until variable = upper limit}

Of course, a stack would be a useful data structure to use, since nested for loops or if statements using begins and ends could cause your program to return funky results.  It does not sound like a terribly difficult project, but do not get me wrong, it is not terribly simple either.  If you have any further questions just ask...  good luck!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question