Solved

Replacing for-loops

Posted on 1997-11-18
5
186 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
Comment Utility
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
Comment Utility
please clearify your question
0
 

Expert Comment

by:mccranie
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now