C++ 编写一个测试程序,它为数值16创建一个EvenNumber对象,并调用getNext()和getPrevious()函数获取并显示这些数字。
时间: 2023-05-22 20:06:57 浏览: 38
Sure! Here's the code:
```C++
#include <iostream>
class EvenNumber {
private:
int value;
public:
EvenNumber(int value) {
if (value % 2 != 0) {
value += 1;
}
this->value = value;
}
int getNext() {
return this->value + 2;
}
int getPrevious() {
return this->value - 2;
}
};
int main() {
EvenNumber even(16);
std::cout << "Next even number: " << even.getNext() << std::endl;
std::cout << "Previous even number: " << even.getPrevious() << std::endl;
return 0;
}
```
I hope that helps! Let me know if you have any more questions.
相关问题
c++定义用于表示偶数的EvenNumber类,数字该类包含: ■int类型的数据域:value,表示对象存储的整数。(该数据域设为私有类型) ■ 一个无参数构造函数,用于创建默认值为0的EvenNumber对象。 ■ 一个构造函数,用于创建为指定数值的EvenNumber对象。 ■ 成员函数getValue()的函数,用于返回此对象的int值。 ■ 成员函数getNext()的函数,用于返回表示此对象中当前偶数之后的下一个偶数。 ■ 成员函数getPrevious()的函数,用于返回表示此对象中当前偶数之前的前一个偶数。 编写一个测试程序,为数值16创建一个EvenNumber对象,并调用getNext()和getPrevious()函数获取并显示这些数字。 创建一个默认值的EvenNumber对象(无参构造),并调用getNext()和getPrevious()函数获取并显示这些数字。
好的,以下是C++代码实现:
```c++
#include<iostream>
using namespace std;
class EvenNumber {
private:
int value;
public:
EvenNumber() {
value = 0;
}
EvenNumber(int value) {
if (value % 2 != 0) {
value++;
}
this->value = value;
}
int getValue() {
return value;
}
EvenNumber getNext() {
return EvenNumber(value + 2);
}
EvenNumber getPrevious() {
return EvenNumber(value - 2);
}
};
int main() {
EvenNumber en1(16);
cout << "当前偶数为:" << en1.getValue() << endl;
cout << "下一个偶数为:" << en1.getNext().getValue() << endl;
cout << "前一个偶数为:" << en1.getPrevious().getValue() << endl;
EvenNumber en2;
cout << "当前偶数为:" << en2.getValue() << endl;
cout << "下一个偶数为:" << en2.getNext().getValue() << endl;
cout << "前一个偶数为:" << en2.getPrevious().getValue() << endl;
return 0;
}
```
输出结果为:
```
当前偶数为:16
下一个偶数为:18
前一个偶数为:14
当前偶数为:0
下一个偶数为:2
前一个偶数为:-2
```
写一个双向循环链表的Java代码并创建一个DoublyLinkedList对象来验证它
好的,以下是一个双向循环链表的Java代码:
```java
public class DoublyLinkedList {
private Node head;
private Node tail;
public DoublyLinkedList() {
this.head = null;
this.tail = null;
}
public void insertFirst(int data) {
Node newNode = new Node(data);
if (isEmpty()) {
tail = newNode;
} else {
head.setPrev(newNode);
}
newNode.setNext(head);
head = newNode;
head.setPrev(tail);
tail.setNext(head);
}
public void insertLast(int data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
} else {
tail.setNext(newNode);
newNode.setPrev(tail);
}
tail = newNode;
tail.setNext(head);
head.setPrev(tail);
}
public void deleteFirst() {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
head.setPrev(tail);
tail.setNext(head);
}
}
public void deleteLast() {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (head == tail) {
head = null;
tail = null;
} else {
tail = tail.getPrev();
tail.setNext(head);
head.setPrev(tail);
}
}
public boolean isEmpty() {
return head == null;
}
public void printList() {
if (isEmpty()) {
System.out.println("List is empty");
} else {
Node current = head;
do {
System.out.print(current.getData() + " ");
current = current.getNext();
} while (current != head);
System.out.println();
}
}
private class Node {
private int data;
private Node next;
private Node prev;
public Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
}
}
```
这个DoublyLinkedList类包含了双向循环链表的基本操作,如在链表开头和末尾插入节点、删除链表开头和末尾的节点、判断链表是否为空以及打印链表中的节点。注意,在双向循环链表中,头节点的prev指向尾节点,尾节点的next指向头节点。
现在我们可以创建一个DoublyLinkedList对象并使用它的方法来操作链表。例如:
```java
// 创建一个DoublyLinkedList对象
DoublyLinkedList my_list = new DoublyLinkedList();
// 在链表末尾添加节点
my_list.insertLast(1);
my_list.insertLast(2);
my_list.insertLast(3);
// 在链表开头添加节点
my_list.insertFirst(0);
// 打印链表中的所有节点
my_list.printList();
// 删除链表中的一个节点
my_list.deleteLast();
// 再次打印链表中的所有节点
my_list.printList();
```
这样,我们就可以创建、操作和打印一个DoublyLinkedList对象了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)