一、BUG程序
#include <iostream>
#include <string>
class Rectangle {
private:
int width;
int height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
int getArea() {
return width * height;
}
int getPerimeter() {
return 2 * (width + height);
}
void printInfo() {
std::cout << "Rectangle: Width = " << width << ", Height = " << height << std::endl;
}
};
int main() {
bool running = true;
int option;
Rectangle* rectangle = nullptr;
while (running) {
std::cout << "1. Create a rectangle" << std::endl;
std::cout << "2. Set width" << std::endl;
std::cout << "3. Set height" << std::endl;
std::cout << "4. Calculate area" << std::endl;
std::cout << "5. Calculate perimeter" << std::endl;
std::cout << "6. Print rectangle info" << std::endl;
std::cout << "7. Exit" << std::endl;
std::cout << "Enter option: ";
std::cin >> option;
switch (option) {
case 1: {
int w, h;
std::cout << "Enter width: ";
std::cin >> w;
std::cout << "Enter height: ";
std::cin >> h;
rectangle = new Rectangle(w, h);
break;
}
case 2: {
int newWidth;
std::cout << "Enter new width: ";
std::cin >> newWidth;
rectangle->setWidth(newWidth);
break;
}
case 3: {
int newHeight;
std::cout << "Enter new height: ";
std::cin >> newHeight;
rectangle->setHeight(newHeight);
break;
}
case 4: {
int area = rectangle->getArea();
std::cout << "Area: " << area << std::endl;
break;
}
case 5: {
int perimeter = rectangle->getPerimeter();
std::cout << "Perimeter: " << perimeter << std::endl;
break;
}
case 6: {
rectangle->printInfo();
break;
}
case 7: {
running = false;
break;
}
default: {
std::cout << "Invalid option!" << std::endl;
break;
}
}
delete rectangle;
}
return 0;
}
二、分析
1. 内存泄漏:在用户选择创建矩形时,程序会动态分配矩形对象的内存,但在程序结束时没有正确释放内存,导致内存泄漏。
2. 空指针解引用:在用户选择设置宽度或高度时,没有检查是否存在矩形对象的实例,而是直接使用了一个可能为空的指针对象。
3. 逻辑错误:在用户选择设置宽度或高度时,程序应该先检查是否存在矩形对象的实例,再进行相关操作。
4. 默认情况处理:在用户输入无效选项时,程序没有提供相应的处理机制,导致出现未定义的行为。
5. 缺少复制构造函数和赋值运算符重载:矩形类没有实现自定义的复制构造函数和赋值运算符重载,可能导致不正确的对象复制和内存管理问题。
说是还要改成中文…………呵呵
三、修改
#include <iostream>
class Rectangle {
private:
int width;
int height;
public:
Rectangle() {
width = 0;
height = 0;
}
Rectangle(int w, int h) {
width = w;
height = h;
}
Rectangle(const Rectangle& other) {
width = other.width;
height = other.height;
}
~Rectangle() {
// 进行必要的资源清理
}
Rectangle& operator=(const Rectangle& other) {
if (this != &other) {
width = other.width;
height = other.height;
}
return *this;
}
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
int getWidth() const {
return width;
}
int getHeight() const {
return height;
}
int calculateArea() const {
return width * height;
}
int calculatePerimeter() const {
return 2 * (width + height);
}
};
int main() {
Rectangle* rect = nullptr;
int choice, width, height;
while (true) {
std::cout << "1. 创建矩形\n";
std::cout << "2. 设置宽度\n";
std::cout << "3. 设置高度\n";
std::cout << "4. 计算面积\n";
std::cout << "5. 计算周长\n";
std::cout << "6. 退出\n";
std::cout << "请输入选项: ";
std::cin >> choice;
if (choice == 1) {
if (rect != nullptr) {
delete rect;
}
std::cout << "请输入矩形的宽度: ";
std::cin >> width;
std::cout << "请输入矩形的高度: ";
std::cin >> height;
rect = new Rectangle(width, height);
} else if (choice == 2) {
if (rect != nullptr) {
std::cout << "请输入新的宽度: ";
std::cin >> width;
rect->setWidth(width);
} else {
std::cout << "还没有创建矩形!\n";
}
} else if (choice == 3) {
if (rect != nullptr) {
std::cout << "请输入新的高度: ";
std::cin >> height;
rect->setHeight(height);
} else {
std::cout << "还没有创建矩形!\n";
}
} else if (choice == 4) {
if (rect != nullptr) {
std::cout << "矩形的面积为: " << rect->calculateArea() << "\n";
} else {
std::cout << "还没有创建矩形!\n";
}
} else if (choice == 5) {
if (rect != nullptr) {
std::cout << "矩形的周长为: " << rect->calculatePerimeter() << "\n";
} else {
std::cout << "还没有创建矩形!\n";
}
} else if (choice == 6) {
if (rect != nullptr) {
delete rect;
}
break;
} else {
std::cout << "无效的选项,请重新输入。\n";
}
}
return 0;
}
四、结局
啊啊啊啊啊啊啊啊啊啊啊啊!!!!!!!!!