c++ 计算货币的基本程序[已关闭]

f45qwnt8  于 2022-12-05  发布在  其他
关注(0)|答案(2)|浏览(141)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

昨天关门了。
Improve this question
我有一个基本的任务,但不能得到正确的程序。任务是使一个程序,显示最低数额的纸币和硬币必须支付。

#include <iostream>
using namespace std;

int main()
{
    int pari;
    cin >> pari;
    switch (pari)
    {
        case 1: cout << pari/5000 << "*5000" << endl;
        break;
        case 2: cout << pari/1000 << "*1000" << endl;
        break;
        case 3: cout << pari/500 << "*500" << endl;
        break;
        case 4: cout << pari/100 << "*100" << endl;
        break;
        case 5: cout << pari/50 << "*50" << endl;
        break;
        case 6: cout << pari/10 << "*10" << endl;
        break;
        case 7: cout << pari/5 << "*5" << endl;
        break;
        case 8: cout << pari/2 << "*2" << endl;
        break;
        case 9: cout << pari/1 << "*1" << endl;
        break;
        default: cout << "WRONG";
    }
    return 0;
}

例如:
输入:

54321

输出量:

10x5000
4x1000
0x500
3x100
0x50
2x10
0x5
0x2
1x1

我尝试使用switch case和if语句,但没有任何效果。

ssgvzors

ssgvzors1#

要获得所示的输出类型,请使用类似于以下内容的逻辑:

#include <iostream>
using namespace std;

int main()
{
    int pari;
    cin >> pari;

    cout << pari/5000 << "*5000" << endl;
    pari %= 5000;

    cout << pari/1000 << "*1000" << endl;
    pari %= 1000;

    cout << pari/500 << "*500" << endl;
    pari %= 500;

    cout << pari/100 << "*100" << endl;
    pari %= 100;

    cout << pari/50 << "*50" << endl;
    pari %= 50;

    cout << pari/10 << "*10" << endl;
    pari %= 10;

    cout << pari/5 << "*5" << endl;
    pari %= 5;

    cout << pari/2 << "*2" << endl;
    pari %= 2;

    cout << pari/1 << "*1" << endl;

    return 0;
}

Online Demo
这可以简化,如果你把钞票在一个阵列和循环通过它,例如:

#include <iostream>
using namespace std;

int main()
{
    const int bankNotes[] = {5000, 1000, 500, 100, 50, 10, 5, 2, 1};
    const int numBankNotes = sizeof(bankNotes)/sizeof(bankNotes[0]);

    int pari;
    cin >> pari;

    for (int i = 0; i < numBankNotes; ++i) {
        cout << pari/bankNotes[i] << "*" << bankNotes[i] << endl;
        pari %= bankNotes[i];
    }

    return 0;
}

Online Demo

e4yzc0pl

e4yzc0pl2#

我在这里为你写了几个版本。希望它能帮助你理解这个过程。

  • 第1版

这是海军版的。如果我们用手做的话,这就是我们要做的。

int main()
{
  int input_value = 0;
  std::cin >> input_value;         // First we get the input.
                                   // We start with the highest value banknote.

  int       value                = input_value;
  int const number_of_5000_notes = value / 5000; // How many of these notes do 
                                                 // we need?
  value                          = value % 5000; // Now calculate the rest.

  int const number_of_1000_notes = value / 1000; // How many of these notes do 
                                                 // we need? 
  value                          = value % 1000; // Now calculate the rest.
  int const number_of_500_notes  = value / 500;
  value                          = value % 500;
  int const number_of_100_notes  = value / 100;
  value                          = value % 100;
  int const number_of_50_notes   = value / 50;
  value                          = value % 50;
  int const number_of_10_notes   = value / 10;
  value                          = value % 10;
  int const number_of_5_notes    = value / 5;
  value                          = value % 5;
  int const number_of_2_notes    = value / 2;
  value                          = value % 2;
  int const number_of_1_notes    = value;

  // At the end we write the output           
  std::cout << "Input: " << input_value << std::endl;
  std::cout << "Output:" << std::endl;
  std::cout << number_of_5000_notes << " x 5000" << std::endl;
  std::cout << number_of_1000_notes << " x 1000" << std::endl;
  std::cout << number_of_500_notes << " x 500" << std::endl;
  std::cout << number_of_100_notes << " x 100" << std::endl;
  std::cout << number_of_50_notes << " x 50" << std::endl;
  std::cout << number_of_10_notes << " x 10" << std::endl;
  std::cout << number_of_5_notes << " x 5" << std::endl;
  std::cout << number_of_2_notes << " x 2" << std::endl;
  std::cout << number_of_1_notes << " x 1" << std::endl;

  return 0;
}
  • 第二版

这是更高级的版本

int main()
{
  int value = 0;
  std::cin >> value; // Get input

  // Check input
  if (value == 0)
  {
    std::cout << "No value or 0 has been entered";
    return 0;
  }

  // Output on the fly
  std::cout << "Input: " << value << std::endl;
  std::cout << "Output:" << std::endl;

  // loop over a sorted list of banknotes.
  for (auto note_value_ent : {5000, 1000, 500, 100, 50, 10, 5, 2, 1})
  {
    int const number_of_notes = value / note_value_ent;
    value %= note_value_ent;
    std::cout << number_of_notes << " x " << note_value_ent << std::endl;
  }
  return 0;
}

两个版本都给予相同的结果(无效条目除外)。

相关问题