Question: Write a program in Ruby to convert a character representation of a non-negative real number to a double value without using the string-to-double conversions, must

Write a program in Ruby to convert a character representation of a non-negative real number to a double value without using the string-to-double conversions, must do a character-by-character conversion. Basically turn this C++ psudocode into Ruby Code

Token scan_real(char *line, int lookahead) /* scan a real number from input */

{

int pos = lookahead; /* remember start of number */

if (isdigit(line[lookahead])) { /* move from state 1 to state 2 */

while (isdigit(line[++lookahead])) /* stay in state 2 */

;

if (line[lookahead] = = .) { /* move to state 3 */

while (isdigit(line[++lookahead])) /* stay in state 3 */

;

strcpy(token.symbol, line + pos, lookahead pos); /* copy number to token */

}

else

printf(Syntax error: real number doesnt end with decimal point );

}

else if (line[lookahead] = = .) { /* move from state 1 to state 4 */

if (isdigit(line[++lookahead])) /* move from state 4 to state 5 */

same as state 3 above /* stay in state 5 */

else

printf(Syntax error: decimal point must be preceded or followed by digits);

}

else

printf(Syntax error: expected number, found %c , line[lookahead]);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!