带状矩阵的存储(c++)

2     1     0     0

3     1     3     0

0     5     2     7

0     0     9     0

这个程序对于三对角矩阵都是有效的,为了精简代码可以考虑用链表的方式动态存储矩阵数据,由于程序已经完成,本次未采用链表,看着代码比较冗长

#include<iostream>

#include<iomanip>

#include<fstream>

#include<string>

#include<cstdlib>

using namespace std;

int main()

{

      fstream matrix;

      int row = 0, colume;      //row表示矩阵的行数,column表示矩阵的列数

      int n;      //矩阵的维数

      int bandwidth;      //bandwidth表示矩阵的带宽

      int j=0;      //提取矩阵三对角的循环控制变量

      string file_number;      //从文件当中提取每一行矩阵

      string p;      //提取每一列矩阵

      int** number;      //用来保存提取的二维数组

      int* band_matrix;      //用来保存带状矩阵的一维数组

      matrix.open("1.txt");      //打开存储带状矩阵的文件

      while (!matrix.eof())      //循环读取矩阵文件当中的每一行

      {

            row++;      //统计矩阵的行数

            colume = 1;            //统计矩阵的列数

            getline(matrix, file_number);      //读取每一行矩阵作为字符串存入file_number变量当中

            

            for (unsigned int i = 0; i < file_number.length(); i++)           

            {

                  if (file_number[i] != ' ')

                  {

                        p += file_number[i];      //读取到非空格字符即是数字,将元素作为字符串存入变量p

                  }

                  if (file_number[i] == ' ')

                  {

                        colume++;      //遇见空格则行数加1

                        cout << p << " ";      //输出前面得到的矩阵元素

                        p = "";      //将变量p重置为空,以便于存储下一个矩阵元素

                  }

            }

            cout << p << '\n';      //由于矩阵文件的每一行末尾使用'\n'结尾的,所以矩阵每行的最后一个元素需要在此单独输出

            p = "";      //将变量p重置为空,以便于存储下一个矩阵元素

      }

      matrix.close();      //关闭存储带状矩阵的文件

      if (row != colume)     

      {

            cout << "请输入方阵,然后重新运行本程序!" << endl;

            system("pause");

            return 0;

      }

      n = row;      //输出矩阵的维数

      cout << "输入矩阵的行数为:" << row << endl;

      cout << "输入矩阵的列数为:" << colume << endl;

      cout << "输入矩阵的维数为:" << n << endl;

      bandwidth = 3 * row - 2;      //计算三对角矩阵的带宽

      

      number = new int*[bandwidth];     

      for (int i = 0; i < bandwidth; i++)

            number[i] = new int[3];

      row = 0;      //重置行

      colume = 0;            //重置列

      matrix.open("1.txt");      //再次打开存储带状矩阵的文件

      while (!matrix.eof())

      {

                  row++;

                  colume = 1;

                  getline(matrix, file_number);

                  for (unsigned int i = 0; i < file_number.length(); i++)

                  {

                        if (file_number[i] != ' ')

                        {

                              p += file_number[i];

                        }

                        if (file_number[i] == ' ')

                        {

                              

                              switch (row - colume)

                              {

                              case 1:case 0:case -1:

                                    number[j][0] = row;

                                    number[j][1] = colume;

                                    number[j][2] = atoi(p.c_str());

                                    j++;

                                    break;

                              }

                              colume++;

                              p = "";

                        }

                  }

                  switch (row - colume)

                  {

                  case 1:case 0:case -1:

                        number[j][0] = row;

                        number[j][1] = colume;

                        number[j][2] = atoi(p.c_str());

                        j++;

                        break;

                  }

            p = "";

      }

      matrix.close();      //关闭存储带状矩阵的文件

      

      band_matrix = new int[bandwidth];      //从堆中申请动态空间用一维数组的方式存储带状元素

      for (int i = 0; i < bandwidth; i++)

      {

            switch (number[i][0] - number[i][1])

            {

            case 1:

                  band_matrix[number[i][0] - 2] = number[i][2];

                  break;

            case 0:

                  band_matrix[n + number[i][0] - 2] = number[i][2];

                  break;

            case -1:

                  band_matrix[2 * n + number[i][0] - 2] = number[i][2];

                  break;

            default:

                  break;

            }

      }

      

      for (int i = 0; i < bandwidth; i++)

      {

            cout << band_matrix[i] << " ";

      }
      cout << endl;

      

      delete[] band_matrix;

      for (int i = 0; i < bandwidth; i++)

      {

            delete[] number[i];

      }

      delete[] number;

      system("pause");

      return 0;

}

带状矩阵的存储(c++)

 

posted @ 2015-11-27 19:31  硫酸亚铜  阅读(701)  评论(0编辑  收藏  举报