Verbesserungsvorschläge für meinen kleinen Rechner

Hi.

Hatte eigentlich schon jemand gesagt, das man keine globalen Variablen verwenden sollte?

C:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>

#define ESC 27

int compute(char op, int lhs, int rhs) {
	switch (op) {
	case '+': return lhs + rhs;
	case '-': return lhs - rhs;
	case '*': return lhs * rhs;
	case '/': 
		assert(rhs != 0);

		return lhs / rhs;

	default:
		assert(("compute: kein gueltiger Operator.", false));
	}
}


int get_operation() {
	while (true) {
		int key = _getch();

		switch (key) {
		case '+':
		case '-':
		case '*':
		case '/':
		case EOF:
			return key;

		case ESC:
			return EOF;
		}
	}
}

int get_left_operand() {
	// TODO: 
	return 5;
}

int get_right_operand() {
	// TODO:
	return 4;
}

int print_result(int lhs, int rhs, char op, int result) {
	printf("%d %c %d = %3d\n", lhs, op, rhs, result);
}

int main(int argc, char *argv[])
{
	int op;

	while ((op = get_operation()) != EOF) {
		int lhs = get_left_operand();
		int rhs = get_right_operand();
		
		int result = compute(op, lhs, rhs);
		
		print_result(lhs, rhs, op, result);
	}
	return 0;
}
Gruß
 
Zurück