Created
April 13, 2024 20:00
-
-
Save jorgejr568/d008b9b38cbd1703ed16c521e3b09e2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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