C++ 使用读写文件的矩阵乘法

要求:

读出指定文本文件中各矩阵,计算多矩阵相乘后结果,并将结果写入另一文本文件中。

输入文件格式:1.每一个矩阵的列与列之间有一个空格,行与行之间有一个换行符。

                         2.矩阵与矩阵之间有一空行,文件末尾为最后一个矩阵的下一行。

一开始做这个练习的时候为result矩阵分配了所有输入矩阵中行列数最大值对应的内存空间。后来经老师提示发现需要为result矩阵结构分配一个动态的动态内存,即释放再分配问题,因为我们可能不知道矩阵行列数的最大值。所以我修改了之前编写的代码,代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
using namespace std;

struct mat                        //每个矩阵的行数、列数、元素以及下一个矩阵的存储位置
{
	int rows = 0;
	int columns = 0;
	int **a = NULL;
	struct mat *next = NULL;
}result;
void getinfo()                              //获取所有矩阵的行数、列数信息
{
	FILE *fp = NULL;
	char ch;
	int r = 0, c = 0, flag = 0;
	struct mat *p = NULL, *temp = NULL;
	p = &result;
	fp = fopen("d:\\input.txt", "r");        //读文件									  
	do
	{
		ch = fgetc(fp);                      //读文件中的字符
		if (ch >= '0' && ch <= '9')
		{
			flag = 0;
			continue;
		}
		c++;
		if (ch == 10)
		{
			flag++;
			r++;
		}
		if (flag == 2 || ch == EOF)
		{
			temp = (struct mat*)malloc(sizeof(struct mat));    //申请动态内存
			temp->rows = r + 1 - flag;
			temp->columns = (c - 1) / temp->rows;
			temp->next = NULL;
			p->next = temp;
			p = p->next;
			r = 0;
			c = 0;
			flag = 0;
		}
	} while (ch != EOF);
	fclose(fp);
}
void inputdata()                  //向矩阵中输入数据
{
	int flag = 0, num = 0, i, j;
	char ch;
	struct mat *p = NULL;
	FILE *fp = NULL;
	fp = fopen("d:\\input.txt", "r");    //读文件
	p = result.next;                     //p指向第一个矩阵结构体
	while (p != NULL)
	{
		p->a = (int**)malloc(p->rows * sizeof(int*));         //申请动态内存
		for (i = 0; i < p->rows; i++)
		{
			p->a[i] = (int*)malloc(p->columns * sizeof(int));
		}
		for (i = 0; i < p->rows; i++)
		{
			for (j = 0; j < p->columns; j++)
			{
				while (1)
				{
					ch = fgetc(fp);                            //从文件中读取一个字符
					if (ch >= '0' && ch <= '9')
					{
						num = num * 10 + (int)(ch - '0');      //将矩阵中每一个元素的ASCII码转换为整数
						flag = 0;
						continue;
					}
					else
					{
						if (ch == 10)
						{
							flag++;
						}
						if (flag == 2)                         //flag=2,说明此时为一个新矩阵,需要重新循环读取矩阵第一个元素
						{
							flag = 0;
							continue;
						}
						p->a[i][j] = num;
						num = 0;
						break;
					}
				}
			}
		}
		p = p->next;                                        //p指向下一个矩阵结构体
	}
	fclose(fp);
}
void calculate()                                        //计算多矩阵乘法
{
	struct mat *p = NULL, temp;
	int i, j, k;
	p = result.next;
	result.rows = p->rows;                              //第一个矩阵可以直接给result矩阵
	result.columns = p->columns;
	result.a = (int**)malloc(result.rows * sizeof(int*));
	for (i = 0; i < result.rows; i++)
	{
		result.a[i] = (int*)malloc(result.columns * sizeof(int));
	}
	for (i = 0; i < result.rows; i++)
	{
		for (j = 0; j < result.columns; j++)
		{
			result.a[i][j] = p->a[i][j];
		}
	}
	p = p->next;
	while (p != NULL)
	{
		temp.rows = result.rows;                        //把result矩阵复制给temp矩阵
		temp.columns = result.columns;
		temp.a = (int**)malloc(temp.rows * sizeof(int*));
		for (i = 0; i < temp.rows; i++)
		{
			temp.a[i] = (int*)malloc(temp.columns * sizeof(int));
		}
		for (i = 0; i < temp.rows; i++)
		{
			for (j = 0; j < temp.columns; j++)
			{
				temp.a[i][j] = result.a[i][j];
			}
		}
		for (i = 0; i < result.rows; i++)                //释放result矩阵内存
		{
			free(result.a[i]);
		}
		result.columns = p->columns;                     //为result矩阵重新分配内存
		for (i = 0; i < result.rows; i++)
		{
			result.a[i] = (int*)malloc(result.columns * sizeof(int));
		}
		for (i = 0; i < temp.rows; i++)
		{
			for (k = 0; k < p->columns; k++)
			{
				result.a[i][k] = 0;
				for (j = 0; j < temp.columns; j++)
				{
					result.a[i][k] = result.a[i][k] + temp.a[i][j] * p->a[j][k];
				}
			}
		}
		for (i = 0; i < temp.rows; i++)                //释放temp矩阵内存
		{
			free(temp.a[i]);
		}
		free(temp.a);
		p = p->next;
	}
}
void outputdata()                           //将结果矩阵写入文件
{
	FILE *fp;
	fp = fopen("d:\\output.txt", "w");
	for (int i = 0; i < result.rows; i++)
	{
		for (int j = 0; j < result.columns; j++)
		{
			fprintf(fp, "%d\t", result.a[i][j]);
		}
		fprintf(fp, "\n");
	}
	fclose(fp);
}
void freespace()          //释放空间
{
	struct mat *p1 = NULL, *p2 = NULL;
	p1 = result.next;        //p1指向第一个矩阵结构体
	while (p1 != NULL)
	{
		p2 = p1;
		p1 = p1->next;
		for (int i = 0; i < p2->rows; i++)
		{
			free(p2->a[i]);   //先释放结构体中的一维指针
		}
		free(p2->a);		  //后释放结构体中的二维指针
		free(p2);             //最后释放结构体指针
	}
}
int main()
{
	getinfo();
	inputdata();
	calculate();
	outputdata();
	freespace();
	return 0;
}
完毕……


版权声明:本文为qq_35234994原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。