Start New Discussion within our Software Development Community

Hi, I'm working on a simple calculator in Delphi. The assignment is convert C code to Delphi code. My problem is that I've tried for days and I can't come up with something to replace "getchar" from the C code.

This is the code in C:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <dos.h>

char token; //variable token global

//Prototipos de funcion para llamadas recursivas
int exp(void);
int term(void);
int factor(void);

void error(void)
{
	fprintf(stderr, "Error\n");
	exit(1);
}

void match( char expectedToken )
{
	if (token==expectedToken)
		token = getchar();
		else error;
}

main()
{
	int result;
	token = getchar(); //carga token con el primer caracter para busqueda hacia delante
	result = exp();
	if (token == '\n') //verifica el fin de lĂ­nea
		{printf("Result = %d\n", result); }
	else error();
	return 0;
}

int exp(void)
{
	int temp = term();
	while ((token=='+') || (token=='-'))
		switch (token) {
			case '+': match('+');
				temp+=term();
				break;
			case '-': match('-');
				temp-=term();
				break;
		}
	return temp;
}

int term(void)
{
	int temp = factor();
	while (token=='*') {
		match('*');
		temp*=factor();
	}
	return temp;
}

int factor(void)
{
	int temp;
	if (token=='(') {
		match('(');
		temp = exp();
		match(')');
	}
	else if (isdigit(token)) {
		ungetc(token, stdin);
		scanf("%d", &temp);
		token = getchar();
	}
	else error();
	return temp;
}

What I'm asking is if there's something to replace instead of getchar(), and if someone has done this, to please help me with a procedure I can make to getchar() in Delphi :(

You could try using the Windows API to write a getchar function (read 1 char from file handle obtained from GetStdHandle)... otherwise, you'd need interrupts (MS-DOS style getchar...) or a window with a WM_KEY* handler

This question has already been answered. Start a new discussion instead.