Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replacing for-loops

Posted on 1997-11-18
5
Medium Priority
?
222 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 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The online market is growing at an unprecedented rate and retail eCommerce sales are expected to reach $4 trillion by 2020. Yet, the profit is not just there for the taking because you have to set yourself apart from the competition.
In this article, I’ll show how research, determination, and use of modern technology helped me solve a DNA mystery.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

705 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