问题描述
- c++读取txt文件里的数据,然后保存在二维数组中进行处理
-
我写的程序是把数据自己输入在主函数里,但是如果想实际的应用应该是有一个数据文件,然后提取出数据文件的数据保存在二维数组中才对,而且这个二维数组要根据具体文件的大小定数组的行列数,有谁能帮我做一下吗,谢谢!
#include
#include
#include
using namespace std;
#define M 10//二维数组的行
#define N 6//二维数组的列
class Data
{
double a[M][N];//声明数组
double Max;
double Min;
double r;
public:
Data(double b[M][N])//构造函数
{
int i, j;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
a[i][j] = b[i][j];
}
void Chuzhi();//初值化
void get_Max_Min();//根据公式求出求差序列的两级最大差、最小差
void get_r();//求出各列关联度
void show_Max_Min()
{
cout << "求差序列的两级的最大差是:" << Max << endl << endl;
cout<<"求差序列的两级的最小差是:"<< Min << endl<<endl;} void show()//输出数组 { int i, j; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) cout << a[i][j] << 't'; cout << endl; } }
};
void Data::Chuzhi()//量纲不同时化为无量纲
{
for (int i = M - 1; i >= 0; i--)
for (int j = N - 1; j >= 0; j--)
a[i][j] = a[i][j] / a[0][j];
}void Data::get_Max_Min()
{
double c[M][N - 1];
double t = c[0][0];
for (int i = 0; i < M; i++)
for (int j = 1; j < N; j++)
c[i][j - 1] = fabs(a[i][0] - a[i][j]);//求差序列
//下面的代码可用于输出求差序列
cout << endl;
cout << "求差序列为:" << endl;
int i, j;
for (i = 0; i < M; i++)
{
for (j = 0; j < N - 1; j++)
cout << c[i][j] << 't';
cout << endl;
}
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N - 1; j++)
{
if (c[i][j]>t)
t = c[i][j];
}
}
Max = t;
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N - 1; j++)
{
if (c[i][j] < t)
t = c[i][j];
}
}
Min = t;
}void Data::get_r()
{
double f[M][N];
for (int j = 0; j < N; j++)
for (int i = 0; i < M;i++)
f[i][j] = (Min + 0.5*Max) / (fabs(a[i][0] - a[i][j]) + 0.5*Max);
for (int i = 0; i < M; i++)//输出关联系数序列
{
for (int j = 0; j < N; j++)
cout << f[i][j] << 't';
cout << endl;
}
cout << "下面求出各列的关联度:" << endl<<endl;
double r[N] = { 0 };
double sum[M] = { 0 };
for (int j = 0; j < N; j++)
{
for (int i = 0; i < M; i++)
sum[j] += f[i][j];
}for (int j = 0; j < N; j++) { r[j] = sum[j] / M; cout << "第" << j + 1 << "列与第一列的关联度为:" << r[j] << endl << endl; }
}
int main()
{//将要处理的数据
double b[M][N] = { { 44,5, 35, 60, 110, 130}, { 45,6, 40, 67, 120, 140}, {54, 7, 50, 72, 122, 150},
{ 49,8, 50, 80, 124, 160}, { 43,9, 60, 79, 121, 170}, {54, 20, 60, 90, 122, 156}, { 39,13, 40, 70, 111, 157},
{ 37,27, 65, 83, 130, 189}, { 43,35, 80, 72, 145, 165}, { 34,67, 70, 69, 123, 157} };
Data data(b);//建立对象data
cout << "最初的数据为:" << endl;
data.show();
cout << endl;
data.Chuzhi();
cout << "初始化后数据:" << endl;
data.show();
cout << endl;
data.get_Max_Min();
data.show_Max_Min();
data.get_r();
system("pause");
return 0;
}
解决方案
http://bbs.csdn.net/topics/390189030
解决方案二:
http://tieba.baidu.com/p/2910676684
解决方案三:
http://lzh0859.blog.sohu.com/43892352.html