Skip to content

Instantly share code, notes, and snippets.

@jorgejr568
Created April 13, 2024 20:00
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 jorgejr568/d008b9b38cbd1703ed16c521e3b09e2d to your computer and use it in GitHub Desktop.
Save jorgejr568/d008b9b38cbd1703ed16c521e3b09e2d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string>
using namespace std;
struct troco
{
int cedula;
int quantidade;
};
int main()
{
int cedulas[] = {10000, 5000, 2000, 1000, 500, 200, 100, 50, 25, 10, 5, 1};
int cedulasSize = sizeof(cedulas) / sizeof(cedulas[0]);
troco t[cedulasSize];
for (int i = 0; i < cedulasSize; i++)
{
t[i].cedula = cedulas[i];
t[i].quantidade = 0;
}
float valor;
printf("Digite o valor da compra: ");
scanf("%f", &valor);
float pago;
printf("Digite o valor pago: ");
scanf("%f", &pago);
float troco = pago - valor;
int trocoInt = (int)(troco * 100);
int resto = trocoInt;
for (int i = 0; i < cedulasSize; i++)
{
bool temCedula = resto >= cedulas[i];
if (temCedula)
{
t[i].quantidade = resto / cedulas[i];
resto = resto % cedulas[i];
}
}
printf("Troco: %.2f\n", troco);
for (int i = 0; i < cedulasSize; i++)
{
if (t[i].quantidade > 0)
{
string tipoCedula = t[i].cedula >= 100 ? "notas" : "moedas";
printf("%d %s de R$ %.2f\n", t[i].quantidade, tipoCedula.c_str(), t[i].cedula / 100.0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment