我的程序有两个功能。一个计算可以分为美元金额的票据类型,另一个用于显示金额。
来自Java背景我不太了解C ++语法,并希望得到一些帮助。
#include <iostream>
#include <iomanip>
using namespace std;
//function declaration
void calcChange(int amount, int* twenties, int* tens, int* fives, int*ones);
void showChange(int amount, int twenties, int tens, int fives, int ones);
int main() {
//Declaration
int amount =0;
//Call to function
calcChange(&twenties, &tens, &fives, &ones);
showChange(twenties,tens,fives,ones);
do {
cout << "Enter amount (or negative to terminate):" << endl;
cin >> amount;
//if loop that ans if 0 is invalid output
if (amount == 0) {
cout << "Invalid dollar amount.\n";
}
//put output here
cout << "Amount " << setw(2) << "Twenties " << setw(2) << "Tens " << setw(5) <<"fives "<< setw(5) <<"Ones "<< endl;
cout << amount << setw(2) << twenties << setw(2) << tens << setw(5) << fives << setw(5) << ones << endl;
}while (amount >= 0);
cout << "Goodbye!";
return 0;
}
//function declarations
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones)
{
while(amount >= 20){
*twenties = amount/20;amount % 20;
amount = *twenties;
twenties++;
}
while(amount >=10){
*tens = amount/10; amount % 10;
amount=*tens;
tens++;
}
while(amount >=5){
*fives = amount/5; amount % 5;
amount = *fives;
fives++;
}
while(amount >=1){
*ones = amount/1; amount % 1;
amount = *ones;
ones++;
}
return;
}
void showChange(int amount, int twenties, int tens, int fives, int ones) {
twenties = twenties;
tens = tens;
fives = fives;
ones = ones;
return;
}
首先,不确定你想用showChange
做什么
功能声明
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones);
函数定义(不是声明)
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones)
{
*twenties = amount / 20; amount %= 20;
*tens = amount / 10; amount %= 10;
*fives = amount / 5; amount %= 5;
*ones = amount / 1; amount %= 1;
return;
}
主功能
int main() {
int amount = 0;
do {
cout << "Enter amount (or negative to terminate):" << endl;
cin >> amount;
if (amount == 0) {
cout << "Invalid dollar amount.\n";
break;
}
int twenties, tens, fives, ones;
calcChange(amount, &twenties, &tens, &fives, &ones);
cout << "Amount " << setw(2) << "Twenties " << setw(2) << "Tens " << setw(5) <<"fives "<< setw(5) <<"Ones "<< endl;
cout << amount << setw(2) << twenties << setw(2) << tens << setw(5) << fives << setw(5) << ones << endl;
} while (amount >= 0);
cout << "Goodbye!";
return 0;
}
大多数正确的代码都在那里,它只需要安排和排序更具可读性。以下是您的问题的MVC示例,如果您对代码中的某些内容有任何具体问题,请询问。
#include <iostream>
#include <iomanip>
using namespace std;
// function declaration
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones);
void showChange(int amount, int twenties, int tens, int fives, int ones);
int main()
{
// Declarations
int amount = 0;
int twenties = 0;
int tens = 0;
int fives = 0;
int ones = 0;
do {
cout << "Enter amount (or negative to terminate) " << endl;
cin >> amount;
// if loop that ans if 0 is invalid output
if (amount == 0) {
cout << "Invalid dollar amount" << endl;
continue;
}
if (amount < 0) {
break;
}
calcChange(amount, &twenties, &tens, &fives, &ones);
// show output here
showChange(amount, twenties, tens, fives, ones);
} while (amount >= 0);
cout << "Goodbye!" << endl;
return 0;
}
void calcChange(int amount, int* twenties, int* tens, int* fives, int* ones) {
*twenties = amount / 20;
amount = amount % 20;
*tens = amount / 10;
amount = amount % 10;
*fives = amount / 5;
amount = amount % 5;
*ones = amount;
return;
}
void showChange(int amount, int twenties, int tens, int fives, int ones) {
cout << "Amount: " << setw(10) << "Twenties: " << setw(10) << "Tens: " << setw(10) << "Fives:" << setw(10) << "Ones:" << setw(10) << endl;
cout << setw(15) << twenties << setw(10) << tens << setw(10) << fives << setw(10) << ones << setw(10) << endl;
return;
}