A practical introduction to Pascal programming language - GIARA
A practical introduction to Pascal programming language - GIARA
A practical introduction to Pascal programming language - GIARA
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Data structuresExercise 4.16. Create a string that reads a real number as text (in a string) and s<strong>to</strong>res the value itrepresents in a real variable. Note that the text must represent a valid real number with at leas<strong>to</strong>ne digit and one or no dots <strong>to</strong> separate the integer- and real-valued part.The easiest solution <strong>to</strong> this problem is based on finding the position at which the dot (ifany) appears. Hence, the first part of the program features a while-based search Because ofthe configuration of the subsequent for loops, if no dot was found (i.e. if posDot>length(a))the first for traverses the whole string, and the second is never executed. The use of twodifferent variables for the real and integer parts, plus a third one for the final result is not strictlymanda<strong>to</strong>ry, but enhances the overall comprehensibility of the program.Program 94: Exercise 4.16 (Version A)program c04e16;constN=10;vara:STRING[N];realPart,intPart,r:real;i,posDot:integer;begin(* Reading *)write(’Introduce the real number: ’);readln(a);(* Looking for the dot *)posDot:=1;while( (posDot