一个c++函数,允许在数据结构中编辑输入

a c++ function that enables input editing in a data structure

本文关键字:数据结构 编辑 输入 c++ 函数 一个      更新时间:2023-10-16

我正在设计一个名为AND的c++逻辑门数据结构,它有3个功能:

从用户处接受输入1或0的函数。显示与门从输入输出的函数。一个函数,询问用户是否要编辑他们的输入,如果是,则允许他们编辑已经输入的输入。问题是,我不知道如何调用编辑输入的函数。以下是我的代码:

#include<iostream>
using namespace std;
struct AND  //structure called AND
{
   int x, y;
   AND()    //constructor
   {
      x, y = 0;             //declaring variables as inputs
   }
   void inputAND()      //function that takes inputs from the user
   {
      cin >> x;
      cin >> y;
   }
   void outputAND()                       //function that displays output
   {
      if (x == 0 || y == 0)
      {
         cout << "0" << endl;
      }
      else if (x == 1 && y == 1)
      {
         cout << "1" << endl;
      }
   }
   void changeInputAND(AND change[])       //function for changing inputs
   {
      cout << "Do you wish change first input Yes/No:" << endl;       
      string t;
      cin >> t;
      int k;
      if (t == "Yes" || "yes")
      {
         cout << "Enter New Input ";
         cin >> k;
         x = k;
      }
      else
      {
         k = x;
      }
      cout << "Do you wish to change the second input Yes/No:" << endl;
      {
         string s;
         cin >> s;
         int l;
         if (t == "Yes" || "yes")
         {
            cout << "enter new input";
            cin >> l;
            y = l;
         }
      }
   }
};
void main()
{
   AND a1;
   a1.input();   //calling the input function
   a1.output();  //calling the output function
}

改进建议和如何实现编辑AND对象的任务。

  1. 让你的类尽可能的简单。不要让代码相关的输入和输出流污染它。

    struct AND  //structure called AND
    {
       int x, y;
       AND() : x(0), y(0)   //constructor
       {
       }
       void setX(int newX)
       {
          x = newX;
       }
       void setY(int newY)
       {
          y = newY;
       }
    };
    
  2. 在初始化列表中初始化class的成员,而不是在构造函数体中对其赋值。

    建议使用:

    AND() : x(0), y(0)   //constructor
    {
    }
    

    /

    AND()    //constructor
    {
       x = y = 0;
    }
    
  3. 将读取和写入代码从成员函数移动到非成员函数。不要假设输入/输出流是std::cin还是std::cout。驱动程序代码可以选择使用std::cin/std::cout,也可以选择std::ifstream/std::ofstream文件。

    // Write data to a stream
    std::ostream& operator<<(std::ostream& out, AND const& a)
    {
       return out << a.x << " " << a.y;
    }
    // Read data from a stream
    std::istream& operator>>(std::istream& in, AND& a)
    {
       in >> a.x >> a.y;
    }
    

    另外,最好确保写/读是这样的,即写出来的内容可以被读回来。

  4. 创建一个非成员函数,用于读取用户数据和更改对象,而不是将其全部放入成员函数中。

    // Non-member function for changing the object.
    void changeInputAND(AND& a)
    {
       cout << "Do you wish change first input Yes/No:" << endl;       
       string ans;
       cin >> ans;
       if (ans == "Yes" || ans == "yes")
       {
          cout << "Enter New Input ";
          int x;
          cin >> x;
          a.setX(x);
       }
       cout << "Do you wish to change the second input Yes/No:" << endl;
       cin >> ans;
       if (ans == "Yes" || ans == "yes")
       {
          cout << "enter new input";
          int y;
          cin >> y;
          a.setY(y);
       }
    }
    

    顺便说一句,你试图与"Yes""yes"进行比较是有缺陷的。

       if (ans == "Yes" || "yes")
    

    等价于

       if ( (ans == "Yes") || "yes")
    

    总是求值为true。这是一个非常不同的话题:)

  5. 将其放在一个或多个函数中测试AND功能,最好与main不同。

    void testAND()
    {
       AND a1;
       std::cin >> a1;                // calling the input function
       std::cout << a1 << std::endl;  // calling the output function
       changeInputAND(a1);            // function for changing inputs
       std::cout << a1 << std::endl;  // calling the output function again
    }
    
  6. main调用test函数

    void main()
    {
       testAND();
       return 0;
    }