-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
144 lines (127 loc) · 2.97 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
void fileinput ( char file[], double a[], int *x , int *y);
void fileoutput ( char file[] , double a[] , int m ,int n);
void add( double a[], double b[], double c[],int m, int n);
void print(double a[], int m , int n);
void multiply(double a[], int am ,int an , double b[], int bm ,int bn,double c[] ,int *cm,int *cn);
int main()
{
int am ,an,bm,bn;
double a[1000] = {0} ;
double b[1000] = {0} ;
double c[1000] = {0} ;
int cm, cn;
char file1 [100] ={"H:\\trya.txt"};
char file2 [100] ={"H:\\tryb.txt"};
char file3 [100] ={"H:\\tryc.txt"};
fileinput(file1 , a , &am ,&an);
fileinput(file2 , b , &bm ,&bn);
multiply(a,am,an,b,bm,bn,c,&cm,&cn);
print(c,cm,cn);
fileoutput( file3 , c , cm ,cn);
return 0;
}
void add( double a[],double b[],double c[],int m ,int n)
{
//int i = sizeof(a)/sizeof(a[0]) - 1;
for ( int i = 0 ; i < m ;i++)
{
for ( int j = 0 ; j<n ; j++)
c[i*n+j] = a[i*n+j] + b[i*n+j];
}
}
void print(double a[],int m ,int n)
{
if ( m==0 || n==0 )
printf("\n");
else
{
int i,j;
for ( i = 0; i < m ; i++ )
{
for ( j=0 ; j<n; j++)
{
printf("\t%lf",a[i*n+j]);
}
printf("\n");
}
}
}
void multiply(double a[], int am ,int an , double b[], int bm ,int bn,double c[] ,int *cm,int *cn)
{
if ( an != bm)
{
*cm=0;
*cn=0;
printf("²»ÄܼÆËã");
return;
}
int i ,j;
for ( i = 0 ; i<am ; i++ )
{
for ( j = 0 ; j < bn ; j++)
{
for ( int k =0 ; k < an ; k++)
c[i*bn+j] = c[i*bn+j]+a[i*an+k]*b[k*bn+j];
}
}
*cm = am;
*cn = bn;
}
void fileinput ( char file[], double a[], int *x , int *y)
{
FILE *fp;
char c;
int m = 0 , n=0;
if ( (fp = fopen( file,"r")) == NULL)
{
printf("cannot open the file\n");
exit(0);
}
int i =0;
while( (c = fgetc(fp)) != EOF )
{
if (c!=32 && c !='\n' )
a[i] = a[i]*10+ (c - '0');
if ( c == 32 )
{
i++;
}
if ( c == '\n')
{
i++;
m++;
}
}
n = i/m;
if ( fclose(fp))
{
printf("cannot close this file\n");
exit(0);
}
*x = m;
*y = n;
}
void fileoutput ( char file[] , double a[] , int m ,int n)
{
FILE *fp;
if ( (fp = fopen(file,"w")) == NULL)
{
printf("cannot open the file\n");
exit(0);
}
for ( int i =0 ; i < m*n ;i++)
{
// fputs(gcvt(a[i],10,b), fp );
fprintf(fp, "%lf",a[i]);
fputc ('\t',fp);
if ( (i+1) %n == 0)
fputc('\n',fp);
}
if ( fclose(fp))
{
printf("cannot close this file\n");
exit(0);
}
}