一个c++函数,允许在数据结构中编辑输入
a c++ function that enables input editing in a data structure
我正在设计一个名为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
对象的任务。
-
让你的类尽可能的简单。不要让代码相关的输入和输出流污染它。
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; } };
-
在初始化列表中初始化
class
的成员,而不是在构造函数体中对其赋值。建议使用:
AND() : x(0), y(0) //constructor { }
/
AND() //constructor { x = y = 0; }
-
将读取和写入代码从成员函数移动到非成员函数。不要假设输入/输出流是
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; }
另外,最好确保写/读是这样的,即写出来的内容可以被读回来。
-
创建一个非成员函数,用于读取用户数据和更改对象,而不是将其全部放入成员函数中。
// 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
。这是一个非常不同的话题:) -
将其放在一个或多个函数中测试
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 }
-
从
main
调用test函数void main() { testAND(); return 0; }
相关文章:
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 编辑整个数据结构 c++
- QT编辑自定义数据结构
- 一种单一的数据结构,用于从脚本中读取任意类型的变量,以便在运行时进行检索和编辑
- 一个c++函数,允许在数据结构中编辑输入
- 用于编辑文本的最小cpu和内存开销数据结构