Solved

Replacing for-loops

Posted on 1997-11-18
5
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Invest in your employees with these five simple steps to improve employee engagement and retention.
We asked our MSP customer base what their favorite tools were and how they help them serve clients. We focused our questions on favorite tools in the following categories: >PSA tools >RMM tools >Alert management tools >Communication tools and Mo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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