最近在写一个程序, 要求如下:
1.txt文件中有一个m*n的数字矩阵,
2.txt文件中也有一个m*n的数字矩阵,
编写程序, 创建3.txt文件, 使得其中存储一个m*n的矩阵, 矩阵的元素为对应的1.txt与2.txt中元素的和。
目前, 写了一段程序, 只能实现1*1的矩阵相加, 正在努力完善程序。 程序如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
int count = 0;
long ft1 = 1L;
long ft2 = 1L;
int turn1 = 1;
int turn2 = 1;
int main()
{
FILE *matrix1 = fopen("matrix1.txt", "r");
if (NULL == matrix1)
{
perror("open1");
return 1;
}
FILE *matrix2 = fopen("matrix2.txt", "r");
if (NULL == matrix2)
{
perror("open2");
fclose(matrix1);
return 2;
}
FILE *sum = fopen("sum.txt", "a");
if (NULL == sum)
{
perror("open3");
fclose(matrix1);
fclose(matrix2);
return 3;
}
char butter1[10] = {0};
char butter2[10] = {0};
char butter3[10] = {0};
char vote1[1] = {0};
char vote2[1] = {0};
fseek(matrix1, ft1, SEEK_SET);
int end1 = feof(matrix1);
fseek(matrix2, ft2, SEEK_SET);
int end2 = feof(matrix2);
// while (0 == end1 && 0 == end2)
//{
printf("turn = %d %d\n", turn1, turn2);
printf("ft1 = %ld\n", ft1);
printf("ft2 = %ld\n", ft2);
int length1 = lenth1(matrix1);
int length2 = lenth2(matrix2);
// printf("lenth = %d %d\n", lenth1(matrix1), lenth2(matrix2));
fread(butter1, sizeof(char), length1, matrix1);
fread(butter2, sizeof(char), length2, matrix2);
add(length1, butter1, length2, butter2, butter3);
// fread(butter1, sizeof(char), lenth1(matrix1), matrix1);
// fread(butter2, sizeof(char), lenth2(matrix2), matrix2);
// add(lenth1(matrix1), butter1, lenth2(matrix2), butter2, butter3);
fwrite(butter3, sizeof(char), count, sum);
fwrite(" ", sizeof(char), 1, sum);
memset(butter1, 0, 10);
memset(butter2, 0, 10);
memset(butter3, 0, 10);
printf("\n");
//}
return 0;
}
int lenth1(FILE *file)
{
int nmeb = 0;
char judge[1] = {'0'};
char judge2[1] = {'0'};
// fseek(file, 0, SEEK_SET);
int Boolean;
if (turn1 <= 1)
{
Boolean = fseek(file, 0, SEEK_SET);
}
else
{
Boolean = fseek(file, ft1, SEEK_SET);
//printf("boo = %d\n", Boolean);
}
if (0 == Boolean)
{
fread(judge, sizeof(char), 1, file);
if (' ' == judge[0])
{
fseek(file, 1, SEEK_CUR);
memset(judge, 0, 1);
fread(judge2, sizeof(char), 1, file);
}
while (' ' != judge2[0])
{
nmeb++;
memset(judge2, 0, 1);
fread(judge2, sizeof(char), 1, file);
}
}
ft1 = ftell(file);
turn1++;
fseek(file, 0, SEEK_SET);
return(nmeb);
}
int lenth2(FILE *file)
{
int nmeb = 0;
char judge[1] = {'0'};
char judge2[1] = {'0'};
// fseek(file, 0, SEEK_SET);
int Boolean;
if (turn2 <= 1)
{
Boolean = fseek(file, 0, SEEK_SET);
}
else
{
Boolean = fseek(file, ft2, SEEK_SET);
}
if (0 == Boolean)
{
fread(judge, sizeof(char), 1, file);
if (' ' == judge[0])
{
fseek(file, 1, SEEK_CUR);
memset(judge, 0, 1);
fread(judge2, sizeof(char), 1, file);
}
while (' ' != judge2[0])
{
nmeb++;
memset(judge2, 0, 1);
fread(judge2, sizeof(char), 1, file);
}
}
ft2 = ftell(file);
turn2++;
fseek(file, 0, SEEK_SET);
return(nmeb);
}
int add(int len1, char* pb1, int len2, char* pb2, char* pb3)
{
int a, b, c;
a = int_change(len1, pb1);
b = int_change(len2, pb2);
printf("%d %d\n", a, b);
c = a + b;
printf("%d\n", c);
char_change(c, pb3);
}
int int_change(int len, char* pb)
{
int tmp = len;
int i;
int rv = 0;
for (tmp = len - 1, i = 0; tmp >= 0; tmp--, i++)
{
rv = rv + (pb[i]-'0') * pow(10, tmp);
}
return(rv);
}
int char_change(int c, char* pb3)
{
int tmp = c;
int len = 1;
while (0 != tmp / 10)
{
len++;
tmp = tmp / 10;
}
count = len;
int tmp2 = c;
while (len > 0)
{
pb3[len - 1] = (tmp2 % 10) + '0';
len--;
tmp2 = tmp2 / 10;
}
//printf("pb3: %s", pb3);
}