C++ 餐厅计费程序功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26835397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Restaurant Billing Program Function
提问by Doug L.
I need to make a restaurant bill calculator program that allows people to choose from a list of menu items (a function) until they have everything they've wanted to order and then calculate the total when they are finished selecting from a list. Then it takes the amount they tender and subtracts the total plus tax and tip to calculate change.
我需要制作一个餐厅账单计算器程序,该程序允许人们从菜单项(一个功能)列表中进行选择,直到他们拥有想要订购的所有东西,然后在完成从列表中选择后计算总数。然后用他们投标的金额减去总加税和小费来计算变化。
I've found several ideas and similar programs here and on other places but nothing that has given me a good enough idea of how to get this finalized. I have the program coded but I can't figure out how to take the running total and keep accumulating it until the user enters "8". I have a functioning program but it totals after each selection instead of keeping a running total and calcuating it when the user hits the "8" key to end.
我在这里和其他地方找到了几个想法和类似的程序,但没有任何东西让我对如何最终确定它有足够的了解。我对程序进行了编码,但我不知道如何获取运行总数并继续累积直到用户输入“8”。我有一个正常运行的程序,但它在每次选择后进行总计,而不是保持运行总计并在用户点击“8”键结束时进行计算。
Take a look below and see if you can point my in the right direction if you would. Basically this assignment is about functions so we're asked to use functions to display the menu and calculate the total.
看看下面,看看你是否可以指出我的正确方向。基本上这个分配是关于函数的,所以我们被要求使用函数来显示菜单并计算总数。
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
void showMenu();
void showFees(double, int);
int main()
{
int choice; //To Hold Menu Choice
double quantity = 1;
//contants for menu choices
const int hamburgerChoice = 1;
const int hotdogChoice = 2;
const int peanutsChoice = 3;
const int popcornChoice = 4;
const int sodaChoice = 5;
const int chipsChoice = 6;
const int waterChoice = 7;
const int endOrderChoice = 8;
//constants for menu prices
const double hamburger = 6.00;
const double hotdog = 4.50;
const double peanuts = 3.75;
const double popcorn = 5.50;
const double soda = 2.80;
const double chips = 1.00;
const double water = 2.00;
//set precision
cout << fixed << showpoint << setprecision(2);
do
{
//display menu and get user choice
showMenu();
cin >> choice;
//validate choice
while (choice < hamburgerChoice || choice > endOrderChoice)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
//if the user does not want to quit proceed
if (choice != endOrderChoice)
{
//display fees
switch (choice)
{
case hamburgerChoice:
showFees(hamburger, quantity);
break;
case hotdogChoice:
showFees(hotdog, quantity);
break;
case peanutsChoice:
showFees(peanuts, quantity);
break;
case popcornChoice:
showFees(popcorn, quantity);
break;
case sodaChoice:
showFees(soda, quantity);
break;
case chipsChoice:
showFees(chips, quantity);
break;
case waterChoice:
showFees(water, quantity);
break;
}
}
}
while (choice != endOrderChoice);
system("pause");
return 0;
}
//*************************************************************
//Definition of function showMenu which displays the menu **
//*************************************************************
void showMenu()
{
cout << "\n\t\tBaseball Game Snacks" << endl;
cout << "1. Hamburger \t.00"<< endl;
cout << "2. Hotdog \t\t.50" << endl;
cout << "3. Peanuts \t\t.75" << endl;
cout << "4. Popcorn \t\t.50" << endl;
cout << "5. Soda \t\t.80" << endl;
cout << "6. Chips \t\t.00"<< endl;
cout << "7. Water \t\t.00" << endl;
cout << "8. END ORDER" << endl;
cout << "Please enter your menu choice: ";
}
//************************************************************
//Definition of function showFees which caculates the total **
//bill **
//************************************************************
void showFees(double itemCost, int quantity)
{
double amtTendered;
double totalBill = (itemCost * quantity);
double taxRate = .065;
double tipRate = .20;
double tip = (totalBill * tipRate);
double tax = (totalBill * taxRate);
double amountDue = (totalBill + tax + tip);
cout << "Total Bill: $" << totalBill << endl;
cout << "Tax: $" << tax << endl;
cout << "Tip: $" << tip << endl;
cout << "Total Amount Due: $" << amountDue << endl;
cout << "Enter ammount tendered: $";
cin >> amtTendered;
double changeDue = amtTendered - amountDue;
cout << "Change Due: $" << changeDue << endl;
}
回答by Paul92
The "balance" is calculated by the showFees
function. So, your problem is that you need to maintain the state (some data) in showFees
in subsequent calls. The best way you could do this is using OOP. While you are programming in C++ using the procedural paradigm, I will point you some of the solutions available in procedural programming.
“余额”由showFees
函数计算。因此,您的问题是您需要showFees
在后续调用中维护状态(某些数据)。最好的方法是使用 OOP。当您使用过程范式在 C++ 中进行编程时,我将向您指出一些在过程编程中可用的解决方案。
- Global variables
- 全局变量
You could have a global variable to hold the total. This is the simplest, the most intuitive and the worstsolution you could have. Don't.
你可以有一个全局变量来保存总数。这是您可能拥有的最简单、最直观和最糟糕的解决方案。别。
- Static variables
- 静态变量
You could have a static variable in showFees
that stores the current total. Better than a global variable, but still bad.
你可以有一个静态变量showFees
来存储当前的总数。比全局变量好,但仍然不好。
- Store the total in main
- 将总数存储在主
Create a variable that represents the total, initialize it to 0 and create a third argument of showFees
that takes a pointer to a double. This way, the changes done to that variable will remain after the showFees
function call ends. In C++ you can use references also (this is the recommended way in C++).
创建一个代表总数的变量,将其初始化为 0 并创建第三个参数,showFees
该参数采用指向双精度的指针。这样,对该变量所做的更改将在showFees
函数调用结束后保留。在 C++ 中,您也可以使用引用(这是 C++ 中推荐的方法)。
- Improve your program
- 改进你的程序
In programming there is a concept called modularity. Using functions, you don't have duplicate code. But a function should do only one thing, and do it as best as possible. This way, your functions are smaller and easier to manage. In showFees
you do 2 things: compute some financial things and generate output. This should always be separated. The computations, or business logic, should be done in a function (that can work in the way I described above), and the output generation, or visual logic, in another function.
在编程中有一个叫做模块化的概念。使用函数,您没有重复的代码。但是一个函数应该只做一件事,并且尽可能做到最好。这样,您的功能更小且更易于管理。在showFees
你做两件事:计算一些财务方面的事情并产生输出。这应该始终分开。计算或业务逻辑应该在一个函数中完成(可以按照我上面描述的方式工作),而输出生成或视觉逻辑应该在另一个函数中完成。
Of course, this is a small program and the separation that I talk about is probably an overkill. However, you can think at ways to improve your function so that they are as modular as possible.
当然,这是一个小程序,我说的分离可能有点矫枉过正。但是,您可以考虑改进功能的方法,使它们尽可能模块化。