初始化以下对象,以this为初始化参数 Algorithm *temperatureKalmanfilter = nullptr; Algorithm *temperatureMedianfilter = nullptr; Algorithm *upPressureKalmanfilter = nullptr; Algorithm *upPressureMedianfilter = nullptr; Algorithm *downPressureKalmanfilter = nullptr; Algorithm *downPressureMedianfilter = nullptr;
时间: 2024-02-05 16:11:54 浏览: 27
以下是使用this作为初始化参数来初始化上述六个对象的代码:
```c++
Algorithm *temperatureKalmanfilter = new Algorithm(this);
Algorithm *temperatureMedianfilter = new Algorithm(this);
Algorithm *upPressureKalmanfilter = new Algorithm(this);
Algorithm *upPressureMedianfilter = new Algorithm(this);
Algorithm *downPressureKalmanfilter = new Algorithm(this);
Algorithm *downPressureMedianfilter = new Algorithm(this);
```
请注意:在使用完这些对象之后,需要调用delete来释放内存。
相关问题
初始化以下对象,以this为初始化参数,名字带Kalmanfilter以KalmanFilter类初始化,带Medianfilter以MedianFilter初始化 Algorithm *temperatureKalmanfilter = nullptr; Algorithm *temperatureMedianfilter = nullptr; Algorithm *upPressureKalmanfilter = nullptr; Algorithm *upPressureMedianfilter = nullptr; Algorithm *downPressureKalmanfilter = nullptr; Algorithm *downPressureMedianfilter = nullptr;
以下是初始化对象的代码:
```cpp
Algorithm *temperatureKalmanfilter = new KalmanFilter(this, "temperature");
Algorithm *temperatureMedianfilter = new MedianFilter(this, "temperature");
Algorithm *upPressureKalmanfilter = new KalmanFilter(this, "upPressure");
Algorithm *upPressureMedianfilter = new MedianFilter(this, "upPressure");
Algorithm *downPressureKalmanfilter = new KalmanFilter(this, "downPressure");
Algorithm *downPressureMedianfilter = new MedianFilter(this, "downPressure");
```
其中,KalmanFilter和MedianFilter是两个继承自Algorithm的子类,它们都带有一个指向父对象的指针和一个用于标识数据类型的字符串。以上代码中的this参数是指向父对象的指针,"temperature"、"upPressure"和"downPressure"是用于标识不同数据类型的字符串。
#include<iostream> #include<algorithm> #include<string> #include<cmath> using namespace std; struct node { char n = 0; node* next = nullptr; }; node* shuru(string a) { node* head = new node{ 0,NULL}; node* p; int m; m= a.length(); p = head; for(int i=m-1;i>=0;i--) { node* q = new node; p->next = q; q->n = a[i]; p = q; q->next = nullptr; } return head; } void show(node* m) { m = m->next;//head是默认值,不show while (m!=nullptr) { cout << m->n; m = m->next; } } node* add(node* a, node* b) { node* add, * head = new node{ 0,NULL }, * tail; node* p, * q; p = a; q = b; tail = head; if ((a->n == '-' && b->n != '-') || (b->n == '-' && a->n != '-')) { } else if (a->n != '-' && b->n != '-') { int carry = 0; while (1) { int val = 0; if (p) { val += p->n - '0'; p = p->next; } if (q) { val += q->n - '0'; q = q->next; } /*if (p && q == nullptr) { val += p->n - '0'; p = p->next; } if (q && p == nullptr) { val += q->n - '0'; q = q->next; }*/ val = val + carry; if (val == 0) break; add = new node{ val % 10+'0' ,NULL}; carry = val / 10; tail->next = add; tail = add; } return head; } else { add = new node{ '-',NULL }; tail->next = add; } } int main() { int a; cin >> a; for (int i = 0; i <a; i++) { string m, n; cin >> m >> n; show(add(shuru(m), shuru(n))); } }为什么输出很奇怪
这段代码的输出很奇怪是因为在输出结果时,没有换行符。可以在 `show` 函数中添加一个换行符,如下所示:
```c++
void show(node* m) {
m = m->next;
while (m != nullptr) {
cout << m->n;
m = m->next;
}
cout << endl; // 添加换行符
}
```
这样就可以在输出结果后换行,使输出结果更加清晰易读。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)