C++预学习2

要求:读出指定文本文件中各矩阵,计算多矩阵相乘后结果,并将结果写入另一文本文件中。
文件名任意;如输入文件:input.txt; 输出文件out.txt
输入文件格式:将多个矩阵记录在文件中,多个矩阵以空行分隔。类似:
1 3 2
2 5 4




3
8

12   

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable:4996) 
#define N 10 
typedef struct
{
	int **p;
	int row;
	int col;
}Matrix;

void input_mat(FILE *fp, Matrix *mat, int mat_number);            //输入矩阵
int get_row_col(FILE *fp, Matrix *mat, int len); //获取多个矩阵各自的行数、列数
void delete_mat(Matrix *mat, int mat_number);       //释放给矩阵分配的空间
Matrix get_product(Matrix *mat, int mat_num);
Matrix get_product_2(Matrix mat1, Matrix mat2);
void output_mat(Matrix product, FILE *fp);

int main()
{
	FILE *fp = NULL; //打开文件
	fp = fopen("1.txt", "r");
	if (fp == NULL)
		return -1;
	Matrix mat[N];
	Matrix  product;    // 定义一个一维的结构数组
	int mat_number;
	mat_number = get_row_col(fp, mat, N);
	input_mat(fp, mat, mat_number);
	printf("The mat number in file 1.txt is %d\n", mat_number);
	printf("The final result is: \n");
	product = get_product(mat, mat_number);
	fclose(fp);
	fp = fopen("2.txt", "w");
	if (fp == NULL)
		return -1;
	output_mat(product, fp);
	delete_mat(mat, mat_number);
	delete_mat(&product, 1);
	fclose(fp);
	return 0;
}
void input_mat(FILE *fp, Matrix *mat, int mat_number)
{
	rewind(fp); //将文件指针重新指向一个流的开头搜索
	for (int i = 0; i <mat_number; i++)   //分配内存
	{
		mat[i].p = (int **)malloc(sizeof(int *) * mat[i].row);
		for (int j = 0; j < mat[i].row; j++)
		{
			mat[i].p[j] = (int *)malloc(sizeof(int) * mat[i].col);
			for (int k = 0; k < mat[i].col; k++)
			{
				fscanf(fp, "%d", &mat[i].p[j][k]);
			}
		}
	}
}
int get_row_col(FILE *fp, Matrix *mat, int len)
{
	for (int i = 0; i < len; ++i)
	{
		mat[i].col = 0;
		mat[i].row = 0;
	}
	int ch;
	int state_row = 0;
	int state_col = 0;
	int index_arr = 0;
	while ((ch = fgetc(fp)) != EOF)
	{
		if (ch != '\n' && ch != ' ' && state_col == 0)
		{
			++mat[index_arr].col;
		}
		else if (ch != '\n' && ch != ' ' && state_col == 1)
		{
			state_row = 0;
		}
		else if (ch == '\n' && state_row == 0)
		{
			state_row = 1;
			state_col = 1;
			++mat[index_arr].row;
		}
		else if (ch == '\n' && state_row == 1)
		{
			state_row = 0;
			state_col = 0;
			++index_arr;
		}

	}
	++mat[index_arr].row;



	return index_arr + 1;

}
Matrix get_product(Matrix *mat, int mat_number)
{
	Matrix product = mat[0];
	for (int i = 1; i < mat_number ; i++)
	{
		product = get_product_2(product, mat[i]);
	}
	for (int i = 0; i < product.row; ++i)
	{
		for (int j = 0; j < product.col; ++j)
		{	
			printf("%d", product.p[i][j]);
			printf(" ");
		}
		printf("\n");
	}

	return product;

}

Matrix get_product_2(Matrix mat1, Matrix mat2)
{
	Matrix product;
	product.col = mat2.col;
	product.row = mat1.row;

	product.p = (int **)malloc(sizeof(int *) * product.row);
	for (int i = 0; i < product.row; i++)
	{
		product.p[i] = (int *)malloc(sizeof(int) * product.col);
	}
	for (int i = 0; i < product.row; ++i)
	{
		for (int j = 0; j < product.col; ++j)
		{
			product.p[i][j] = 0;

			for (int k = 0; k < mat1.col; ++k)
			{
				product.p[i][j] += mat1.p[i][k] * mat2.p[k][j];
			}	

		}		
	}
	return product;
}
void output_mat(Matrix product, FILE *fp)
{
	rewind(fp);
	if (fp == NULL)
		exit(0);
	else
		for (int i = 0; i < product.row; i++)
		{
			for (int j = 0; j < product.col; j++)
			{
				//printf("%d", product.p[i][j]);
				fprintf(fp, "%d ", product.p[i][j]);
				fprintf(fp, "%c", ' ');
			}
			fprintf(fp, "%c", '\n');
		}


}
void delete_mat(Matrix *mat, int mat_number)
{
	for (int i = 0; i < mat_number; i++)
	{
		for (int j = 0; j < mat[i].row; j++)
		{
			free(mat[i].p[j]);
			mat[i].p[j] = NULL;
		}
		free(mat[i].p);
		mat[i].p = NULL;
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值