-3

我在做这个任务时遇到了麻烦。我只是很难理解,我不完全确定该怎么做。我研究并观看了视频,但无法找到正确的具体信息。这是一堆问题,所以我希望有人不仅可以给我答案,还可以向我解释,以便我有很强的理解:)。以下是问题:

1)在本练习中,我们得到了一些程序代码,它们将接受两个整数作为输入,并评估哪一个拥有更大的值。此评估发生在整个代码的多个位置。编写一个程序可以用来执行相同评估的函数,而不是一遍又一遍地复制代码。首先在代码文件的开头编写一个合适的函数声明。您必须决定您的函数是否会返回一些输出。

2) 编写声明后,继续定义函数,包括将评估两个整数中哪一个最大的适当代码段。如果您之前声明您的函数将返回一个值,请务必在此处定义它将返回的内容。

3) 使用第 (1) 和 (2) 部分的结果来减少主函数中重复代码的数量,方法是将整数比较的多个实例替换为调用您创建的函数的调用。请记住,该函数将需要两个整数作为参数传入,如果您从函数返回一些值,则应该使用它(存储在变量中,输出到屏幕等)。作为一个忠告,只替换一个评估后测试你的函数是否正常工作,不要一次全部替换它们(如果该函数在第一次替换时正常工作,那么它应该适用于其他人)。

4) 由于您创建的函数只比较其参数的值而不写入它们(即更改存储在其中的值),我们应该在函数声明和定义中指定这些参数应该被视为常量。对该功能进行必要的修改并再次测试以验证该功能是否仍然有效。通过尝试在函数中包含会更改变量之一的值的操作(例如 number2 += 10;),确认函数不会让您更改参数的数据

- 这是代码(我为冗长的文字道歉):

#include <iostream>

int main(void)
{
using std::cout;
using std::cin;
using std::endl;

int nNum1 = 10, nNum2 = 11;

cout << "This program will compare two numbers and report which one is larger.\n\n"
    << "Proceeding with evaluation...\n" << endl;

cout << "\nUsing numbers: " << nNum1 << " and " << nNum2 << ", the larger one is: ";
if (nNum1 > nNum2)
    cout << nNum1 << endl;
else if (nNum1 < nNum2)
    cout << nNum2 << endl;
else
    cout << "Neither of them! It's a draw." << endl;

int numberA = 234;
int numberB = 234;
cout << "\nUsing numbers: " << numberA << " and " << numberB << ", the larger one is: ";
if (numberA > numberB)
    cout << numberA << endl;
else if (numberA < numberB)
    cout << numberB << endl;
else
    cout << "Neither of them! It's a draw." << endl;

int one = 'a';
int two = 'A';
cout << "\nUsing numbers: " << one << " and " << two << ", the larger one is: ";
if (one > two)
    cout << one << endl;
else if (one < two)
    cout << two << endl;
else
    cout << "Neither of them! It's a draw." << endl;

cout << "\nUsing numbers: " << 13 << " and " << 84 << ", the larger one is: ";
if (13 > 84)
    cout << 13 << endl;
else if (13 < 84)
    cout << 84 << endl;
else
    cout << "Neither of them! It's a draw." << endl;

int input1 = 0;
int input2 = 0;
cout << "\nPlease enter a number: ";
cin >> input1;
cout << "\nPlease enter a second number: ";
cin >> input2;

cout << "\nUsing numbers: " << input1 << " and " << input2 << ", the larger one is: ";
if (input1 > input2)
    cout << input1 << endl;
else if (input1 < input2)
    cout << input2 << endl;
else
    cout << "Neither of them! It's a draw." << endl;

cout << "\n\tThank you for running me :3\n" << endl;
return 0;

}

4

1 回答 1

0

您基本上必须重构代码以替换main函数中重复的代码部分。

如果您仔细观察,您会看到类似这样的代码重复:

cout << "\nUsing numbers: " << nNum1 << " and " << nNum2 << ", the larger one is: ";
if (nNum1 > nNum2)
    cout << nNum1 << endl;
else if (nNum1 < nNum2)
    cout << nNum2 << endl;
else
    cout << "Neither of them! It's a draw." << endl;

所以把它放到一个函数中:

void CompareNumbers(int nNum1, int nNum2)
{
    cout << "\nUsing numbers: " << nNum1 << " and " << nNum2 << ", the larger one is: ";
    if (nNum1 > nNum2)
        cout << nNum1 << endl;
    else if (nNum1 < nNum2)
        cout << nNum2 << endl;
    else
        cout << "Neither of them! It's a draw." << endl;
}

并在您的主函数中调用它,而不是复制上述代码块。

于 2015-09-13T10:55:10.200 回答