在所给线程代码的基础上,修改: (1)一个线程负责数的变化,从0变到4*3.14159,步长为0.02; (2)一个线程负责读取系统时间; (3)一个线程负责将数的正弦、余弦与正要值计算并显示,同时显示系统时间; (4)步长停顿100ms。 要求:用console程序。
时间: 2024-02-21 15:58:44 浏览: 25
以下是修改后的代码:
```c++
#include <iostream>
#include <cmath>
#include <chrono>
#include <thread>
using namespace std;
double num = 0.0;
double sin_num = 0.0, cos_num = 0.0, tan_num = 0.0;
void count_num() {
while (num <= 4 * 3.14159) {
num += 0.02;
this_thread::sleep_for(chrono::milliseconds(100));
}
}
void read_time() {
while (num <= 4 * 3.14159) {
auto now = chrono::system_clock::to_time_t(chrono::system_clock::now());
cout << "系统时间:" << ctime(&now) << endl;
this_thread::sleep_for(chrono::milliseconds(100));
}
}
void calculate_trig() {
while (num <= 4 * 3.14159) {
sin_num = sin(num);
cos_num = cos(num);
tan_num = tan(num);
auto now = chrono::system_clock::to_time_t(chrono::system_clock::now());
cout << "正弦值:" << sin_num << endl;
cout << "余弦值:" << cos_num << endl;
cout << "正切值:" << tan_num << endl;
cout << "系统时间:" << ctime(&now) << endl;
this_thread::sleep_for(chrono::milliseconds(100));
}
}
int main() {
thread t1(count_num);
thread t2(read_time);
thread t3(calculate_trig);
t1.join();
t2.join();
t3.join();
return 0;
}
```
修改后的程序中,我们增加了两个线程,分别用于读取系统时间和计算三角函数值。同时,我们将步长停顿时间设置为100ms,以保证线程之间的同步。最后,我们在主函数中创建三个线程,并等待它们运行结束后再结束程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)