存档

文章标签 ‘矩阵’

矩阵乘法C语言实现

2009年3月20日 Slyar 13 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

这段代码是我刚写的,不是什么技巧性很强的东西,完全都是基础。上午线性代数讲了矩阵乘法,下午没课,寻思着自己也好久没写程序了,不如就拿矩阵乘法开刀,熟悉一下C语言...顺便也可以用来做线性代数的作业...=_=

恩,代码有些长,我只是想多回顾一下以前的知识...这段代码用到了函数、指向指针的指针、函数指针、文件操作...加了注释方便需要的童鞋理解...

代码可能还不完善...以后学得更多了再改吧...

2009.3.23    增加内存释放函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
矩阵乘法C语言实现
Slyar 2009.3.20
*/
 
#include <stdio.h>
#include <stdlib.h>
 
/* 给 int 类型定义别名 datatype */
typedef int datatype;
 
/* 函数声明部分 */
datatype** Create(int m, int n);
void Reset(datatype**, int, int);
void Input(datatype**, int, int);
void Output(datatype**, int, int);
void MatrixMutiply(datatype**, datatype**, datatype**);
void MatrixFree(datatype** , int);
 
/* 定义三个矩阵的行列大小 */
int row_a, col_a;
int row_b, col_b;
int row_c, col_c;
 
/* 定义文件指针 */
FILE *fp;
 
int main()
{
        int i;
        datatype **a, **b, **c;
 
        /* 以只读方式打开输入文件 in.txt */
        if((fp = fopen("in.txt","r")) == NULL)
        {
                printf("Cannot open this file.\n");
                exit(0);
        }
 
        /* 创建并读入矩阵a */
        fscanf(fp,"%d%d", &row_a, &col_a);
        a=Create(row_a, col_a);
        Input(a,row_a, col_a);
 
        /* 创建并读入矩阵b */
        fscanf(fp,"%d%d", &row_b, &col_b);
        b = Create(row_b, col_b);
        Input(b,row_b, col_b);
 
        /* 关闭输入文件 */
        fclose(fp);
 
        /* 以写入方式打开输出文件 out.txt */
        if((fp = fopen("out.txt","w")) == NULL)
        {
                printf("Cannot open this file.\n");
                exit(0);
        }
 
        /* 判断两个矩阵能否相乘 */
        if(col_a == row_b)
        {
                row_c = row_a;
                col_c = col_b;
        }
        else
        {
                fprintf(fp,"Matrix Can't Mutiply !\n");
                exit(0);
        }
 
        /* 创建并初始化结果矩阵c */
        c = Create(row_c, col_c);
        Reset(c, row_c, col_c);
 
        /* 进行矩阵乘法运算 */
        MatrixMutiply(a, b, c);
 
        /* 输出结果矩阵C */
        Output(c, row_c, col_c);
 
        /* 关闭输出文件 */
        fclose(fp);
 
        /* 释放矩阵内存 */
        MatrixFree(a,row_a);
        MatrixFree(b,row_b);
        MatrixFree(c,row_c);
 
        //system("pause");
        return 0;
}
 
/* 为矩阵动态分配内存的函数 */
datatype** Create(int m, int n)
{
        int i;
        datatype **Matrix;
        Matrix = (datatype **) malloc(sizeof(datatype *) * m);
        for(i = 0; i < m; i++)
        {
                Matrix[i] = (datatype *) malloc(sizeof(datatype) * n);
        }
        return Matrix;
}
 
/* 初始化矩阵函数 */
void Reset(datatype** Matrix, int m, int n)
{
        int i,j;
        for(i = 0; i < m; i++)
        {
                for(j = 0; j < n; j++)
                {
                        Matrix[i][j] = 0;
                }
        }
}
 
/* 读入数据函数 */
void Input(datatype** Matrix, int m, int n)
{
        int i,j;
        for(i = 0; i < m; i++)
        {
                for(j = 0; j < n; j++)
                {
                        fscanf(fp,"%d", &Matrix[i][j]);
                }
        }
}
 
/* 输出数据函数 */
void Output(datatype** Matrix, int m, int n)
{
        int i,j;
        for(i = 0; i < m; i++)
        {
                for(j = 0; j < n; j++)
                {
                        fprintf(fp,"%d ", Matrix[i][j]);
                }
                fprintf(fp,"\n");
        }
}
 
/* 矩阵乘法运算函数 */
void MatrixMutiply(datatype** a, datatype** b, datatype** c)
{
        int i,j,k;
        for(i = 0; i < row_c; i++)
        {
                for(j = 0; j < col_c; j++)
                {
                        for(k = 0; k < col_a; k++)
                        {
                                c[i][j] += a[i][k] * b[k][j];
                        }
                }
        }
}
 
/* 释放矩阵内存函数 */
void MatrixFree(datatype** Matrix, int m)
{
        int i;
        for(i = 0; i < m; i++)
        {
                free(Matrix[i]);
        }
        free(Matrix);
}
分类: 编程相关 标签: ,
bnuep:0801010047