通过函数传递数组

Passing Arrays Through Functions

本文关键字:数组 函数      更新时间:2023-10-16

我正在为一个简单的计算器编写一段代码,该计算器将执行几个十进制转换(目前我关心的是1)。基本上,在用户在菜单中进行选择(切换语句)后,我希望有一个单独的函数来执行实际转换(数学)和一个为解决方案保留的函数(输出)。我需要帮助将在mathoption1中创建的二进制数组传递到outputtoption1。代码现在没有编译,我有4个错误与一些函数的参数有关。显然,我迷失了方向,需要一些指导。

#include <iostream>
using namespace std;
void display();
void menu(int &option, int decimal, int binaryarray[]);
void outputoption1(int decimal, int binaryarray[]);
void mathoption1(int &decimal);

int main()
{
    int option;
    int decimal;
    int binaryarray[32];
    display();
    menu(option, decimal, binaryarray);
    return 0;
}
void display()
{
    cout << "Industrial Engineering Decimal Conversion v 1.0n" << endl;
    cout << "Created by: asdf adsfadfn"
         << "t    adsfa adsfadn" << endl;
    cout << "On the next screen, you will choose which operation you want to performn" << endl;
    system("PAUSE");
    cout << "n" << endl;
}
void menu(int &option, int decimal, int binaryarray[])
{
   cout << "Welcome to the IE Decimal Conversion Program!nn"
        << "To choose a conversion, enter a number from the menu belownn"
        << "1) Decimal to Binaryn"
        << "2) Quit the programn" << endl;
   cin >> option;
   switch (option)
   {
   case 1:
       mathoption1(decimal);
       outputoption1(decimal, binaryarray[]);
       break;
   case 2:
       break;
   default:
       cout << "ERROR: Please make a valid selection" << endl;
       menu(option, decimal, binaryarray);
   }
}
void mathoption1(int &decimal)
{
    cout << "Please input the decimal you want to convert to binary/n/n";
    cin >> decimal;
    int x = 0;
    int binaryarray[32];
    while (decimal != 0)
    {
        binaryarray[x] = decimal % 2;
        x++;
        decimal = decimal / 2;
    }
}
void outputoption1(int decimal, int binaryarray[])
{
    int x = 0;
    cout << "Your original decimal value of " << decimal << " is equivalent to the following binary value:n";
    for (int y = x - 1; y >= 0; y--)
    {
        cout << binaryarray[y];
    }
}

如有任何帮助/意见,我们将不胜感激。

不确定如何剪切和粘贴错误,但下面列出了这些错误:语法错误:"]"@第47行

应为表达式@第47行

编辑:代码已更新以更正MENU参数。

函数菜单的原型与实际函数标头不同,您应该更新标头以使其与原型相似,还必须添加&函数中小数前的strong>mathoption1标头

        outputoption1(decimal, binaryarray[]);

移除[]的:只传入变量。

菜单不应该有参数,因为这些东西都不是main需要告诉菜单(甚至知道)的东西。它们可以是本地菜单。

菜单自己调用是不传统的。最好使用循环。

您没有得到任何输出,因为您在outputoption1中的x是0,但您需要在mathoption1中生成的x。此外,因为mathoption1有两个版本的binaryArray:可以共享的参数和不能共享的局部变量。

所以本质上你有参数问题。问问自己每个函数:这个函数到底需要知道什么才能完成它的工作?把它放在()的中间。它为调用程序提供了哪些值(如果有的话)?并将其作为返回类型,或介于()之间。

mathoption1作为convertToBinary会更好。要转换为二进制的函数需要知道什么?十进制数字。它将提供什么调用功能?一个二进制数组,用于与二进制数字的数量进行"与"运算。(请注意,这不会要求用户输入十进制数,而是从参数列表中获取。这比你现在拥有的更好,原因有两个:它更通用(如果你有一个非交互式程序,它会起作用),并且使用函数"convertToBinary"比使用函数"askUserForANumberAndConvertToBinary)更连贯

如果outputtoption1是"printBinary"而不是"printOriginalDecimalValueAndBinary",那么它也可以更简单。所以:如果它是printBinary,它需要知道什么?告诉它。

所以我认为你应该考虑组织和参数。把它连贯起来,然后再回到编译的问题上来。

主要问题是您没有将binaryarray传递到mathoption1,但还有很多其他问题我想解决。

使用bool s表示二进制

  • 数组中的每个元素不需要一个完整的int。二进制是一个布尔值,所以你可以使用bool(就像布尔代数一样)。想想就自然多了

数组的硬编码大小是不可能的

  • 您不应该对数组的大小进行硬编码。通常,您将在C++11中使用宏(#define SIZE)或constexpr

使用函数描述程序的流程

  • 您应该尝试为对读者有意义的函数命名。mathoption1虽然可读,但并没有告诉我它在做什么。CCD_ 11或类似的东西在保持可读性的同时更容易理解

避免using namespace std;

  • 这是不好的做法。不要这么做。有很多充分的理由

其他注意事项

  • 请注意,此转换算法将仅转换正数。你会想处理消极的一面(一两个赞美)
  • 请注意,此转换仅适用于4294967295以下的用户输入。因为数组的大小是32,所以只能寻址到232-1。如果有人输入4294967296,你会过得很糟糕。我会让你想办法处理的。你可能想做更多的错误检查——这太好了。在下面的代码中,我没有做太多的工作来留给您作为练习

您将在下面找到新代码。

#include <iostream>
#define SIZE 32
char display_menu() {
  char choice;
  std::cout << "1. Convert decimal to binary." << std::endl;
  std::cout << "2. Choice 2." << std::endl;
  std::cout << "Q. Quit." << std::endl;
  std::cin >> choice;
  return choice;
}
bool is_not_valid(char choice) {
  choice = toupper(choice);
  return choice != '1' && choice != '2' && choice != 'Q';
}
int get_decimal_from_user() {
  int num;
  std::cout << "Please enter a decimal to be converted to binary: " << std::endl;
  std::cin >> num;
  return num;
}
void compute_binary(int num, bool* binary) {
  for(size_t i = 0; i < SIZE; ++i) {
    binary[SIZE-i-1] = num%2;
    num/=2;
  }
}
void output_binary(int num, bool* binary) {
  std::cout << num << " can be represented in binary as ";
  for(size_t i = 0; i < SIZE; ++i) {
    std::cout << binary[i];
  }
  std::cout << std::endl;
}
void convert() {
  int num = get_decimal_from_user();    
  bool binary[SIZE];
  compute_binary(num, binary);
  output_binary(num, binary);
}
void choice_2() {
  std::cout << "Choice 2" << std::endl;
}
void do_choice(char choice) {
  switch(toupper(choice)) {
    case '1': convert(); break;
    case '2': choice_2(); break;
    case 'Q': return;
    default: break;
  }
} 
int main() {
  char choice;
  do {
    do {
      choice = display_menu();
    } while(is_not_valid(choice));
    do_choice(choice);  
  } while(toupper(choice) != 'Q');
  std::cout << "Goodbye!" << std::endl;
  return 0;
}

@erip-这是我之前想到的。

#include <iostream>
using namespace std;
void display();
void menu(int &option, int &decimal);
void outputoption1(int decimal);
void mathoption1(int decimal, int binaryarray[]);
void restartmenu(char &option2, int option, int decimal);
int decimal;
int x;
char option2;

int main()
{
    int option;

    display();
    menu(option, decimal);
    restartmenu(option2, option, decimal);
    return 0;
}
void display()
{
    cout << "Industrial Engineering Decimal Conversion v 1.0n" << endl;
    cout << "Created by: asdf adsfadfn"
         << "t    adsfa adsfadn" << endl;
    cout << "On the next screen, you will choose which operation you want to performn" << endl;
    system("PAUSE");
    cout << "n" << endl;
}
void menu(int &option, int &decimal)
{
   cout << "Welcome to the IE Decimal Conversion Program!nn"
        << "To choose a conversion, enter a number from the menu belownn"
        << "1) Decimal to Binaryn"
        << "2) Quit the programn" << endl;
   cin >> option;
   switch (option)
   {
   case 1:
       cout << "Please input the decimal you want to convert to binarynn";
       cin >> decimal;
       outputoption1(decimal);
       break;
   case 2:
       break;
   default:
       cout << "ERROR: Please make a valid selection" << endl;
       menu(option, decimal);
   }
}
void mathoption1(int &decimal)
{
    int binaryarray[32];
    int x = 0;
    int number = decimal;
    while (number != 0)
    {
        binaryarray[x] = number % 2;
        x++;
        number = number / 2;
    }
    for (int y = x - 1; y >= 0; y--)
    {
        cout << binaryarray[y];
    }
}
void outputoption1(int decimal)
{
    cout << "Your original decimal value of " << decimal << " is equivalent to the following binary value:n";
    mathoption1(decimal);
}
void restartmenu(char &option2, int option, int decimal)
{
    cout << "Restart Program? Y/N" << endl;
    cin >> option2;
    if (option2 == 'y')
    {
        menu(option, decimal);
    }
    if (option2 == 'n')
    {
        exit(0);
    }
    else
    {
        cout << "ERROR: Make Valid Selection" << endl;
        restartmenu(option2, option, decimal);
    }
}

UPDATE:我找到了一种方法来完成我需要做的事情,而不必通过函数传递数组。