设计一个计算机类,具有加减乘除 四个方法,并测试

实现效果:

测试:

实现效果:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你一个基本的思路。 首先,我们需要定义一个链表节点的数据结构,包含两个成员变量:一个是int型的数据值,另一个是指向下一个节点的指针。定义如下: ``` struct Node { int value; Node* next; }; ``` 接下来,我们可以定义一个链表,包含一些常用的链表操作,例如添加节点、删除节点、遍历链表等等。代码如下: ``` class LinkedList { private: Node* head; int length; public: LinkedList() { head = nullptr; length = 0; } ~LinkedList() { Node* current = head; while (current != nullptr) { Node* temp = current; current = current->next; delete temp; } length = 0; } void addNode(int value) { Node* newNode = new Node; newNode->value = value; newNode->next = nullptr; if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } length++; } void deleteNode(int value) { if (head == nullptr) { return; } if (head->value == value) { Node* temp = head; head = head->next; delete temp; length--; return; } Node* current = head; while (current->next != nullptr) { if (current->next->value == value) { Node* temp = current->next; current->next = current->next->next; delete temp; length--; return; } current = current->next; } } void printList() { Node* current = head; while (current != nullptr) { std::cout << current->value << " "; current = current->next; } std::cout << std::endl; } }; ``` 现在我们可以利用这个链表来实现一个链表计算机。我们可以将每个数字作为一个节点存储到链表,然后对链表进行遍历和操作,最终得到计算结果。 下面是一个简单的实现,支持加减乘除四种运算: ``` int calculate(LinkedList& list) { int result = 0; Node* current = list.head; while (current != nullptr) { result += current->value; current = current->next; } return result; } int subtract(LinkedList& list) { int result = list.head->value; Node* current = list.head->next; while (current != nullptr) { result -= current->value; current = current->next; } return result; } int multiply(LinkedList& list) { int result = 1; Node* current = list.head; while (current != nullptr) { result *= current->value; current = current->next; } return result; } int divide(LinkedList& list) { int result = list.head->value; Node* current = list.head->next; while (current != nullptr) { if (current->value == 0) { std::cout << "Cannot divide by zero!" << std::endl; return 0; } result /= current->value; current = current->next; } return result; } int main() { LinkedList list; list.addNode(2); list.addNode(3); list.addNode(4); list.addNode(5); list.addNode(6); std::cout << "Original list: "; list.printList(); int result = calculate(list); std::cout << "Sum: " << result << std::endl; result = subtract(list); std::cout << "Difference: " << result << std::endl; result = multiply(list); std::cout << "Product: " << result << std::endl; result = divide(list); std::cout << "Quotient: " << result << std::endl; return 0; } ``` 当然,这只是一个简单的实现,你可以根据需要添加更多的功能,例如支持括号、浮点数计算等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值