c++编直方图、扇形统计图、条形统计图、折线统计图
时间: 2023-07-03 11:30:34 浏览: 296
直方统计图,扇形统计图,折线统计图
下面给出简单的C++代码实现四种统计图。
1. 直方图
```cpp
#include <iostream>
using namespace std;
int main() {
int data[10];
int max_value = 0;
cout << "请输入10个数据:" << endl;
for (int i = 0; i < 10; i++) {
cin >> data[i];
if (data[i] > max_value) {
max_value = data[i];
}
}
cout << endl;
for (int i = max_value; i > 0; i--) {
for (int j = 0; j < 10; j++) {
if (data[j] >= i) {
cout << "█ ";
} else {
cout << " ";
}
}
cout << endl;
}
for (int i = 0; i < 10; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
```
2. 扇形统计图
```cpp
#include <iostream>
using namespace std;
int main() {
int data[5];
int total = 0;
cout << "请输入5个数据:" << endl;
for (int i = 0; i < 5; i++) {
cin >> data[i];
total += data[i];
}
cout << endl;
for (int i = 0; i < 5; i++) {
int angle = data[i] * 360 / total;
cout << "数据" << i << ":" << string(angle, '*') << endl;
}
return 0;
}
```
3. 条形统计图
```cpp
#include <iostream>
using namespace std;
int main() {
int data[10];
int max_value = 0;
cout << "请输入10个数据:" << endl;
for (int i = 0; i < 10; i++) {
cin >> data[i];
if (data[i] > max_value) {
max_value = data[i];
}
}
cout << endl;
for (int i = max_value; i > 0; i--) {
for (int j = 0; j < 10; j++) {
if (data[j] >= i) {
cout << "█ ";
} else {
cout << " ";
}
}
cout << endl;
}
for (int i = 0; i < 10; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
```
4. 折线统计图
```cpp
#include <iostream>
using namespace std;
int main() {
int data[10];
cout << "请输入10个数据:" << endl;
for (int i = 0; i < 10; i++) {
cin >> data[i];
}
cout << endl;
for (int i = 0; i < 10; i++) {
cout << data[i] << " ";
for (int j = 0; j < data[i]; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
```
注意,这些实现都是比较简单的,只是为了说明实现思路,实际应用中可能需要更多的细节处理。
阅读全文