git » metnum-tp1.git » master » tree

[master] / calc.cpp

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "aritmetica.hpp"
#include "util.hpp"

int main(int argc, char *argv[])
{
	if (argc != 5) {
		printf("Uso: calc <prec> <nro 1> {+-*/} <nro2>\n");
		return 1;
	}

	int prec = atoi(argv[1]);
	numero a(prec, strtod(argv[2], NULL));
	char op = argv[3][0];
	numero b(prec, strtod(argv[4], NULL));

	printf("a =\t %.60f 0x%" print_64t "x\n", a.to_double(), to_u64(a));
	printf("a =\t %.60f 0x%" print_64t "x\n", b.to_double(), to_u64(b));

	numero res;
	switch (op) {
	case '+':
		res = a + b;
		break;
	case '-':
		res = a - b;
		break;
	case '*':
		res = a * b;
		break;
	case '/':
		res = a / b;
		break;
	default:
		printf("Error: operacion desconocida\n");
		return 1;
	}

	printf("a %c b =\t %.60f 0x%" print_64t "x\n", op, res.to_double(), to_u64(res));

	return 0;
}