首页 > 解决方案 > 这个函数期望返回什么?

问题描述

程序指定以下内容:

  1. 编写一个程序,将总零钱金额作为整数输入,使用最少的硬币输出零钱,每行一种硬币类型。

  2. 硬币类型有美元、25 美分硬币、10 美分硬币、5 美分硬币和1 美分硬币。

  3. 酌情使用单数和复数硬币名称,例如 1 penny vs. 2 pennies。

  4. 您的程序必须定义并调用以下函数。void ExactChange(int userTotal, vector& coinVals)

  5. coinVals 的位置 0-4 应分别包含美元、25 美分、硬币、镍和便士的数量。

我的代码如下:

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
/*
 1) Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
 2) The coin types are dollars, quarters, dimes, nickels, and pennies.
 3) Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
 
 4) Your program must define and call the following function.
            void ExactChange(int userTotal, vector<int>& coinVals)
 
 5) Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
 */
void ExactChange(int userTotal, vector<int>& coinVals);
const int PENNIES_IN_DOLLAR = 100, PENNIES_IN_QUARTER = 25, PENNIES_IN_DIME = 10, PENNIES_IN_NICKEL = 5;

int main() {
   int userTotal, changeRemaining;
   cin >> userTotal;
    changeRemaining = userTotal;
    
    int dollars = changeRemaining / PENNIES_IN_DOLLAR;
    changeRemaining = changeRemaining % PENNIES_IN_DOLLAR;
    int quarters = changeRemaining / PENNIES_IN_QUARTER;
    changeRemaining = changeRemaining % PENNIES_IN_QUARTER;
    int dimes = changeRemaining / PENNIES_IN_DIME;
    changeRemaining = changeRemaining % PENNIES_IN_DIME;
    int nickels = changeRemaining / PENNIES_IN_NICKEL;
    changeRemaining = changeRemaining % PENNIES_IN_NICKEL;
    int pennies = changeRemaining;
    
    vector<int> changeAmount;
    
    vector<int> coinVals{dollars, quarters, dimes, nickels, pennies};
    changeAmount = coinVals;
    ExactChange(userTotal, changeAmount);
    
  return 0;
}
void ExactChange(int userTotal, vector<int>& coinVals) {
    
    if (userTotal == 0) {
       cout << "no change" << endl;
    }
      if(coinVals.at(0) > 0) {
       cout << coinVals.at(0);
       if(coinVals.at(0) > 1) {
           cout << " dollars" << endl;
       }else {
           cout << " dollar" << endl;
       }
    }
    if(coinVals.at(1) > 0) {
       cout << coinVals.at(1);
       if(coinVals.at(1) > 1) {
           cout << " quarters" << endl;
       } else {
           cout << " quarter" << endl;
       }
    }
    if(coinVals.at(2) > 0) {
       cout << coinVals.at(2);
       if(coinVals.at(2) > 1) {
           cout << " dimes" << endl;
       }else {
           cout << " dime" << endl;
       }
    }
    if(coinVals.at(3) > 0) {
       cout << coinVals.at(3);
       if(coinVals.at(3) > 1) {
           cout << " nickels" << endl;
       }else {
           cout << " nickel" << endl;
       }
    }
    if(coinVals.at(4) > 0) {
       cout << coinVals.at(4);
       if(coinVals.at(4) > 1) {
           cout << " pennies" << endl;
       }else {
           cout << " penny" << endl;
       }
    }
}

但是,zybooks,我们的大学课程做实验室的网站给了我这些消息,表明我的代码存在一些问题:

在此处输入图像描述

我的问题是,这些“消息”是什么意思?我该如何解决这些问题?在我看来,他们是在说函数在给定某个输入的情况下错误地输出了一些东西,但是,他们也没有给我一个正确的输出来比较。

标签: c++

解决方案


您的代码正在获取用户输入,main()在调用之前直接将其手动转换ExactChange(),然后将该转换的结果传递给它以使其按原样ExactChange()显示。

我阅读说明的方式以及屏幕截图显示正在执行的测试的方式,更有可能ExactChange()期望用户输入并将其转换vector为硬币数量作为输出。

ExactChange()这与采用非 const 引用的事实是一致的vector,这意味着它可以自由修改. 如果用于文本输出,则改为采用by const 引用会更有意义,这样它就不能修改,只能查看它。vectorExactChange()vectorvector

如果是这样,那么您的程序将通过为其提供用户定义输入并查找特定文本结果的测试是有意义的,但您的程序将无法通过直接使用特定输入执行ExactChange()vector并查找特定输出的测试。这就是单元测试的重点——直接测试预期行为的功能,而不是测试整个程序。

您的代码可能需要看起来更像以下内容:

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

/*
 1) Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line.
 2) The coin types are dollars, quarters, dimes, nickels, and pennies.
 3) Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
 
 4) Your program must define and call the following function.
            void ExactChange(int userTotal, vector<int>& coinVals)
 
 5) Positions 0-4 of coinVals should contain the number of dollars, quarters, dimes, nickels, and pennies, respectively.
 */

void ExactChange(int userTotal, vector<int>& coinVals);
const int PENNIES_IN_DOLLAR = 100, PENNIES_IN_QUARTER = 25, PENNIES_IN_DIME = 10, PENNIES_IN_NICKEL = 5;

int main() {
   int userTotal;
   cin >> userTotal;
        
   if (userTotal == 0) {
      cout << "no change" << endl;
   }
   else {
      vector<int> coinVals;
      ExactChange(userTotal, coinVals);

      if (coinVals[0] > 0) {
         cout << coinVals[0];
         if (coinVals[0] > 1) {
            cout << " dollars" << endl;
         } else {
            cout << " dollar" << endl;
         }
      }

      if (coinVals[1] > 0) {
         cout << coinVals[1];
         if (coinVals[1] > 1) {
            cout << " quarters" << endl;
         } else {
            cout << " quarter" << endl;
         }
      }

      if (coinVals.at(2) > 0) {
         cout << coinVals[2];
         if (coinVals[2] > 1) {
            cout << " dimes" << endl;
         }else {
            cout << " dime" << endl;
         }
      }

      if (coinVals[3] > 0) {
         cout << coinVals[3];
         if (coinVals[3] > 1) {
            cout << " nickels" << endl;
         }else {
            cout << " nickel" << endl;
         }
      }

      if (coinVals[4] > 0) {
         cout << coinVals[4];
         if (coinVals[4] > 1) {
            cout << " pennies" << endl;
         }else {
            cout << " penny" << endl;
         }
      }
   }

   return 0;
}

void ExactChange(int userTotal, vector<int>& coinVals) {    
    int dollars = userTotal / PENNIES_IN_DOLLAR;
    userTotal %= PENNIES_IN_DOLLAR;
    int quarters = userTotal / PENNIES_IN_QUARTER;
    userTotal %= PENNIES_IN_QUARTER;
    int dimes = userTotal / PENNIES_IN_DIME;
    userTotal %= PENNIES_IN_DIME;
    int nickels = userTotal / PENNIES_IN_NICKEL;
    userTotal %= PENNIES_IN_NICKEL;
    int pennies = userTotal;
    
    coinVals.resize(5);
    coinVals[0] = dollars;
    coinVals[1] = quarters;
    coinVals[2] = dimes;
    coinVals[3] = nickels;
    coinVals[4] = pennies;
}

推荐阅读