c++数据结构-二维数据的统计
功能要求
读取文件中的二维数组并作以下统计
- 每行和每列的最大数、最小数、平均数、中位数
- 所有元素的最大数、最小数、平均数、中位数
代码参考
头文件Arr2D.h
#include <string>
using namespace std;
constexpr auto MAXSIZE = 1000;
// 二维数组类
class Arr2D {
private:
int array[MAXSIZE];
int row, col, size;
string file;
public:
Arr2D(string f);
int getRow() { return row; }
int getCol() { return col; }
int getSize() { return size; }
int getVal(int r,int c);
void show();
};
// 统计类
class Stat {
private:
Arr2D *arr;
int row, col;
public:
Stat(Arr2D a);
int max();
int rowMax(int r);
void rowMaxShow();
int colMax(int c);
void colMaxShow();
int min();
int rowMin(int r);
void rowMinShow();
int colMin(int c);
void colMinShow();
int mean();
int rowMean(int r);
void rowMeanShow();
int colMean(int c);
void colMeanShow();
int median();
int rowMedian(int r);
void rowMedianShow();
int colMedian(int c);
void colMedianShow();
friend void selectSort(int nums[], int size);
};
源码文件Arr2D.cpp
#include <iostream>
#include <fstream>
#include "Arr2D.h"
using namespace std;
Arr2D::Arr2D(string f)
{
file = f;
ifstream ifs(file);
int r, c;
ifs >> r >> c;
row = r; col = c; size = r * c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
ifs >> array[i*c + j];
}
}
}
int Arr2D::getVal(int r, int c)
{
return array[r*col + c];
}
void Arr2D::show()
{
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
cout << getVal(i, j) << " ";
cout << endl;
}
}
Stat::Stat(Arr2D a)
{
arr = &a;
row = arr->getRow();
col = arr->getCol();
}
int Stat::max()
{
int res = -(1 << 31);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (res < arr->getVal(i, j)) {
res = arr->getVal(i, j);
}
}
}
return res;
}
int Stat::rowMax(int r)
{
int res = -(1 << 31);
for (int j = 0; j < col; j++) {
if (res < arr->getVal(r, j)) {
res = arr->getVal(r, j);
}
}
return res;
}
void Stat::rowMaxShow()
{
for (int i = 0; i < row; i++)
cout << rowMax(i) << "\t";
cout << endl;
}
int Stat::colMax(int c)
{
int res = -(1 << 31);
for (int i = 0; i < row; i++) {
if (res < arr->getVal(i, c)) {
res = arr->getVal(i, c);
}
}
return res;
}
void Stat::colMaxShow()
{
for (int i = 0; i < col; i++)
cout << colMax(i) << "\t";
cout << endl;
}
int Stat::min()
{
int res = 1 << 31 - 1;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (res > arr->getVal(i, j)) {
res = arr->getVal(i, j);
}
}
}
return res;
}
int Stat::rowMin(int r)
{
int res = 1 << 31 - 1;
for (int j = 0; j < col; j++) {
if (res > arr->getVal(r, j)) {
res = arr->getVal(r, j);
}
}
return res;
}
void Stat::rowMinShow()
{
for (int i = 0; i < row; i++)
cout << rowMin(i) << "\t";
cout << endl;
}
int Stat::colMin(int c)
{
int res = 1 << 31 - 1;
for (int i = 0; i < row; i++) {
if (res > arr->getVal(i, c)) {
res = arr->getVal(i, c);
}
}
return res;
}
void Stat::colMinShow()
{
for (int i = 0; i < col; i++)
cout << colMin(i) << "\t";
cout << endl;
}
int Stat::mean()
{
int sum = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
sum += arr->getVal(i, j);
}
}
return sum/arr->getSize();
}
int Stat::rowMean(int r)
{
int sum = 0;
for (int j = 0; j < col; j++) {
sum += arr->getVal(r, j);
}
return sum / col;
}
void Stat::rowMeanShow()
{
for (int i = 0; i < row; i++)
cout << rowMean(i) << "\t";
cout << endl;
}
int Stat::colMean(int c)
{
int sum = 0;
for (int i = 0; i < row; i++) {
sum += arr->getVal(i, c);
}
return sum / row;
}
void Stat::colMeanShow()
{
for (int i = 0; i < col; i++)
cout << colMean(i) << "\t";
cout << endl;
}
void selectSort(int nums[], int size)
{
int minIndex, n = size;
for (int i = 0; i < n; i++) {
minIndex = i;
for (int j = i + 1; j < n; j++) {
if (nums[minIndex] > nums[j]) {
minIndex = j;
}
}
int tmp = nums[i];
nums[i] = nums[minIndex];
nums[minIndex] = tmp;
}
}
int Stat::median()
{
int arrTemp[MAXSIZE];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arrTemp[i * col + j] = arr->getVal(i, j);
}
}
int size = row * col;
selectSort(arrTemp, size);
if ((size&1) == 1) {
return arrTemp[size / 2];
}
return (arrTemp[size/2-1]+arrTemp[size/2])/2;
}
int Stat::rowMedian(int r)
{
int arrTemp[MAXSIZE];
for (int i = 0; i < col; i++) {
arrTemp[i] = arr->getVal(r, i);
}
int size = col;
selectSort(arrTemp, size);
if ((size & 1) == 1) {
return arrTemp[size / 2];
}
return (arrTemp[size / 2 - 1] + arrTemp[size / 2]) / 2;
}
void Stat::rowMedianShow()
{
for (int i = 0; i < row; i++)
cout << rowMedian(i) << "\t";
cout << endl;
}
int Stat::colMedian(int c)
{
int arrTemp[MAXSIZE];
for (int i = 0; i < row; i++) {
arrTemp[i] = arr->getVal(i, c);
}
int size = row;
selectSort(arrTemp, size);
if ((size & 1) == 1) {
return arrTemp[size / 2];
}
return (arrTemp[size / 2 - 1] + arrTemp[size / 2]) / 2;
}
void Stat::colMedianShow()
{
for (int i = 0; i < col; i++)
cout << colMedian(i) << "\t";
cout << endl;
}
主程序main.cpp
#include <iostream>
#include "Arr2D.h"
using namespace std;
void menu0()
{
cout << "1.统计最大值" << endl;
cout << "2.统计最小值" << endl;
cout << "3.统计平均值" << endl;
cout << "4.统计中位数" << endl;
cout << "0.退出" << endl;
}
void menu1()
{
cout << "1.全局统计" << endl;
cout << "2.按行统计" << endl;
cout << "3.按列统计" << endl;
cout << "0.退出" << endl;
}
int main()
{
Arr2D arr("in.txt");
cout << "读入的二维数组:" << endl;
arr.show();
Stat st(arr);
int opr,way,nm;
do
{
menu0();
cin >> opr;
switch (opr)
{
case 1:
menu1();
cin >> way;
switch (way)
{
case 1:
cout << "全局最大值:" << st.max() << endl;
cout << "每行:\n"; st.rowMaxShow();
cout << "每列:\n"; st.colMaxShow();
break;
case 2:
cout << "请输入:";
cin >> nm;
cout << st.rowMax(nm) << endl;
break;
case 3:
cout << "请输入:";
cin >> nm;
cout << st.colMax(nm) << endl;
break;
default:
break;
}
break;
case 2:
menu1();
cin >> way;
switch (way)
{
case 1:
cout << "全局平均值:" << st.min() << endl;
cout << "每行:\n"; st.rowMinShow();
cout << "每列:\n"; st.colMinShow();
break;
case 2:
cout << "请输入:";
cin >> nm;
cout << st.rowMin(nm) << endl;
break;
case 3:
cout << "请输入:";
cin >> nm;
cout << st.colMin(nm) << endl;
break;
default:
break;
}
break;
case 3:
menu1();
cin >> way;
switch (way)
{
case 1:
cout << "全局最小值:" << st.mean() << endl;
cout << "每行:\n"; st.rowMeanShow();
cout << "每列:\n"; st.colMeanShow();
break;
case 2:
cout << "请输入:";
cin >> nm;
cout << st.rowMean(nm) << endl;
break;
case 3:
cout << "请输入:";
cin >> nm;
cout << st.colMean(nm) << endl;
break;
default:
break;
}
break;
case 4:
menu1();
cin >> way;
switch (way)
{
case 1:
cout << "全局最小值:" << st.median() << endl;
cout << "每行:\n"; st.rowMedianShow();
cout << "每列:\n"; st.colMedianShow();
break;
case 2:
cout << "请输入:";
cin >> nm;
cout << st.rowMedian(nm) << endl;
break;
case 3:
cout << "请输入:";
cin >> nm;
cout << st.colMedian(nm) << endl;
break;
default:
break;
}
break;
default:
break;
}
cout << "--------------------------------------------------------------" << endl;
} while (opr != 0);
return 0;
}