我想知道是否可以仅通过适应printf
进行编译,或者是否需要其他功能。
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int V = 0;
scanf("%d", &V);
printf("NOTAS: \n");
printf("%d Nota(s) de R$ 100.00\n", V / 100);
printf("%d Nota(s) de R$ 50.00\n", V % 100 / 50);
printf("%d Nota(s) de R$ 20.00\n", V % 100 % 50 / 20);
printf("%d Nota(s) de R$ 10.00\n", V % 100 % 50 % 20 / 10);
printf("%d Nota(s) de R$ 5.00\n", V % 100 % 50 % 20 % 10 / 5);
printf("%d Nota(s) de R$ 2.00\n", V % 100 % 50 % 20 % 10 % 5 / 2);
printf("MOEDAS: \n");
printf("%d Moeda(s) de R$ 1.00\n", V % 100 % 50 % 20 % 10 % 2 / 1);
printf("%.2lf Moeda(s) de R$ 0.50\n", V % 100 % 50 % 20 % 10 % 2 % 1 / 0.50);
printf("%.2lf Moeda(s) de R$ 0.25\n", V % 100 % 50 % 20 % 10 % 2 % 1 % 0.50 / 0.25);
return 0;
}
printf("%.2lf Moeda(s) de R$ 0.25\n", V % 100 % 50 % 20 % 10 % 2 % 1 % 0.50 / 0.25);
^^^^^^
不能使用带十进制值的模数,它必须是整数。请注意,
.50
和.25
无法按预期工作,因为如果您输入十进制值,由于V
是int
,它将被截断并存储为整数。您可以做的一件事是分别解析整数值和十进制值,然后从那里获取。
类似:
#include <iostream> #include <sstream> #include <string> int main() { int value[2], i = 0; std::string V, temp; getline(std::cin, V); std::stringstream ss(V); while (getline(ss, temp, '.') && i < 2) //tokenize stream by '.' { value[i++] = std::stoi(temp); //convert to integer } printf("NOTAS: \n"); //you can replace all these with std::cout printf("%d Nota(s) de R$ 100.00\n", value[0] / 100); printf("%d Nota(s) de R$ 50.00\n", value[0] % 100 / 50); printf("%d Nota(s) de R$ 20.00\n", value[0] % 100 % 50 / 20); printf("%d Nota(s) de R$ 10.00\n", value[0] % 100 % 50 % 20 / 10); printf("%d Nota(s) de R$ 5.00\n", value[0] % 100 % 50 % 20 % 10 / 5); printf("%d Nota(s) de R$ 2.00\n", value[0] % 100 % 50 % 20 % 10 % 5 / 2); printf("MOEDAS: \n"); printf("%d Moeda(s) de R$ 1.00\n", value[0] % 100 % 50 % 20 % 10 % 5 % 2); printf("%d Moeda(s) de R$ 0.50\n", value[1] % 100 / 50); printf("%d Moeda(s) de R$ 0.25\n", value[1] % 100 % 50 / 25); return 0; }