c++ - 坚持在 C++ 中调用一些方法来创建菜单
问题描述
我有一个ComplexesSet
代表一组复数的类。我必须使用这个类来编写一个程序,它从键盘读取复数并创建一个这样的菜单:按 1 在集合中添加一个数字,按 2 从集合中删除一个数字,然后按 0 退出程序。
这是我的程序:头文件
#pragma once
#include <iostream>
using namespace std;
#define DIMMAX 10;
class Complex {
int re, im;
public:
Complex() {
re = im = 0;
}
Complex(int re, int im) {
this->re = re;
this->im = im;
}
void display() {
cout << " (" << re << ", " << im << " ) ";
}
int equal(Complex c2); // check the equality between 2 complex numbers.
void read(); // reads a complex number
};
class ComplexesSet {
Complex* v; // the complex numbera array
int dim; // the maximum dimension of the array
int n; // the current number of complex numbers in the set
public:
ComplexesSet();
ComplexesSet(int d);
~ComplexesSet();
void addNumber(Complex); // add a number in set
void deleteNumber(Complex); // delete a number from set
void displaySet(); // display the set
};
方法文件
#include "multime.h"
#include <iostream>
using namespace std;
int Complex::equal(Complex c2) {
if (this->re == c2.re && this->im == c2.im) {
return 1;
}
else {
return 0;
}
}
void Complex::read() {
cout << "Enter the real part: ";
cin >> this->re;
cout << "Enter the imaginary part: ";
cin >> this->im;
cout << endl;
}
ComplexesSet::ComplexesSet() {
cout << "Set of complexes numbers: ";
dim = DIMMAX;
v = new Complex[dim];
n = 0;
}
ComplexesSet::ComplexesSet(int d) {
cout << "ComplexesSet(" << d << ")";
dim = d;
v = new Complex[dim];
n = 0;
}
ComplexesSet::~ComplexesSet() {
cout << "~ComplexesSet" << endl;
if (v) {
delete[] v;
}
v = nullptr;
dim = -1;
n = -1;
}
void ComplexesSet::addNumber(Complex num) {
int ok = 0;
if (n < dim) {
for (int i = 0; i < n; i++) {
if (v[i].equal(num)) {
ok = 1;
i = n + 1;
}
}
if (ok) {
cout << "This element is already in the set.";
}
else {
v[n] = num;
n++;
}
}
else {
cout << "The set is full.";
}
}
void ComplexesSet::deleteNumber(Complex num) {
int i;
if (n != 0) {
for (i = 0; i < n; i++) {
if (v[i].equal(num)) {
break;
}
else {
cout << "This element does not exists.";
}
}
if (i < n) {
n = n - 1;
for (int j = i; j < n; j++) {
v[j] = v[j + 1];
}
}
}
else {
cout << "The set is empty.";
}
}
void ComplexesSet::displaySet() {
cout << "\n The set: {";
if (n) {
for (int i = 0; i < n; i++) {
v[i].display();
}
}
cout << "}.\n\n";
}
主要文件:
#include "multime.h"
#include <iostream>
using namespace std;
int main() {
ComplexesSet m;
Complex num;
int option;
int x;
do {
cout << endl << "1 - ADD A NUMBER\n 2 - DELETE A NUMBER\n 0 - EXIT THE PROGRAM";
cin >> option;
switch (option) {
case 1:
cout << endl << "Enter the number you want to add: ";
num.read();
//? How to call addNumber method from ComplexesSet class?
// m.displaySet ?;
break;
case 2:
cout << endl << "Enter the number you want to delete: ";
num.read();
//? How to call deleteNumber method from ComplexesSet class?
// m.displaySet ?;
break;
}
} while (option >= 1 && option <= 2);
system("pause");
return 0;
}
我卡在主文件中。如何调用 addNumber 和 deleteNumber 方法以便从集合中添加和删除复数?另外,按“1”或“2”后,在我读取数字后,我需要显示集合。
解决方案
推荐阅读
- sql - Oracle SQL 查询优化 - 基于 varchar 字段获取计数
- c# - asp.net 将记录插入数据库
- r - PAM 算法中初始中心点的选择
- c# - 查找范围之间的 IP(IPv4 和 IPv6)地址
- c# - C#调用Fortran:32位工作,64位无法加载fortran DLL
- java - 尝试两次将同一个孩子写到firebase
- xamarin - 真的可以使用 Xamarin Forms 共享所有视图和视图控制器吗?
- python - 在 Anaconda 环境中使用 Openvino 的 OpenCV 构建
- python - django 应用程序在 heroku TypeError 上崩溃:预期的 str、字节或 os.PathLike 对象,而不是函数
- svn - 每个仓库的 TortoiseSVN 代理设置,而不是全局?