Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiomascarenhas/ad779700d9e7e543b590 to your computer and use it in GitHub Desktop.
Save fabiomascarenhas/ad779700d9e7e543b590 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
Entrada: dinheiro e preço unitário
Saída: no. de bombons e troco
Tenho R$20 (2000) e o bombom custa R$1.50 (150)
Compro 13 (2000 / 150) e tenho R$0.50 (50 cent = 2000 % 150)
Tenho R$5.75 (575) e o bombom custa R$0.35 (35)
Compro 16 (575 / 35) e tenho R$0.15 (15 cent = 575 % 35)
*/
static void bombons_troco(double dinheiro, double preco,
int *bombons, double *troco) {
int cdinh = round(dinheiro * 100);
int cprec = round(preco * 100);
*bombons = cdinh / cprec;
*troco = (cdinh % cprec) / 100.0;
}
/*
Entrada: dinheiro e preço unitário
Saída: quanto falta para comprar sem troco
Tenho R$20, custa $1.5
Recebo R$0.50 de troco, logo falta R$1.00
Tenho R$5.75, custa R$0.35
Recebo R$0.15 de troco, logo falta R$0.20
*/
static double quanto_falta(double dinheiro, double preco) {
int bombons;
double troco;
bombons_troco(dinheiro, preco, &bombons, &troco);
return preco - troco;
}
int main()
{
double dinh, prec, troco;
int bomb;
puts("Entre o dinheiro e preco em reais:");
scanf("%lf %lf", &dinh, &prec);
bombons_troco(dinh, prec, &bomb, &troco);
printf("Compro %d e tenho %.2lf, falta %.2lf\n", bomb, troco,
quanto_falta(dinh, prec));
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment