-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIn_Out.c
More file actions
173 lines (150 loc) · 3.46 KB
/
In_Out.c
File metadata and controls
173 lines (150 loc) · 3.46 KB
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
173
#include "In_Out.h"
/**
* Returns the time passed between start and end
*/
double getDifference(struct timeval start, struct timeval end)
{
return (end.tv_sec - start.tv_sec) + 1E-6 * (end.tv_usec - start.tv_usec);
}
/*
*compares two matrices A and B. Returns 1 if they are equal, returns 0 otherwise
*/
int compareMatrices(matrix* A, matrix* B)
{
if (A->rows != B->rows || A->columns != B->columns) return 0;
for (int i = 0; i<(A->rows)*(A->columns); ++i)
{
if (fabs(A->values[i] - B->values[i]) >= EPSILON) return 0;
}
return 1;
}
int parseMatricesPart(FILE* input, matrix* M)
{
//scan dimension
if(fscanf(input,"(%d x %d)", &(M->columns), &M->rows) != 2)
{
// printf("%d %d", M->rows, M->columns);
perror("Bad Dimension Format!");
return 0;
}
M->values = alloc_matrix(M->rows, M->columns);
//scan Matrix values
int c = fgetc(input);
//TODO: ignore every char up to first opening square bracket
if (c != '[') //matrices are enclosed by square brackets
{
perror("Matrices must begin with a '['!.");
return 0;
}
//parsing of matrix values
double val;
for (int i = 0; i < (M->rows)*(M->columns); ++i)
{
if(fscanf(input,"%lf",&val) != 1)
{
perror("Bad Matrix Format!");
return 0;
}
M->values[i] = val;
}
fscanf(input, "%s", &c);
if (c != ']' )
{
perror("Matrices must end with a ']'!");
return 0;
}
return 1;
}
int parseMatrices(char* input, int line, matrix* operandA, matrix* operandB)
{
FILE* in = fopen(input,"r");
if (in == NULL)
{
perror("Could not open Input File!");
return 0;
}
//skip lines
char c = 0;
for (int i = 0; i < line; ++i)
{
c = 0;
while (c!='\n')
{
c = fgetc(in);
}
}
//read the first matrix of the line
if(parseMatricesPart(in, operandA) != 1)
{
fclose(in);
return 0;
}
//return if second matrix shall not be parsed
if (operandB == NULL)
{
fclose(in);
return 1;
}
fscanf(in, "%*[ \t]%c", &c);
if (c == '\n' || c == EOF) //no second matrix in line
{
perror("Missing second Matrix!");
fclose(in);
return 0;
}
//opening bracket means there is a second matrix in this line
if (c == '(')
{
fseek(in,-1,SEEK_CUR);
if(parseMatricesPart(in, operandB) != 1)
{
fclose(in);
return 0;
}
fclose(in);
return 1;
}
//if c is neither newline or opening bracket, theres a character that doesnt belong there
perror("Bad Format!");
return 0;
}
int printMatrix(matrix* M, char* output)
{
int redirect = 0;
int newout, stdoutcpy;
if (strcmp(output,"") != 0) //redirecting standard output to output file
{
redirect = 1;
stdoutcpy = dup(1);
close(1);
//because stdout(which is 1) was closed, the new file gets it's descriptor
if(newout = open(output, O_CREAT|O_APPEND|O_WRONLY, 0777) < 0)
{
perror("open: ");
return 0;
}
// if (dup2(1, newout) < 0)
// {
// perror("Redirecting stdout: ");
// return 0;
// }
// close(newout);
}
printf("(%d x %d)[", M->columns, M->rows);
for (int i = 0; i<(M->rows * M->columns); ++i)
{
printf("%lf ", M->values[i]);
}
printf("]\n");
//reopening standard output
if(redirect)
{
if(dup2(stdoutcpy, 1) < 0)
{
perror("Restoring stdout: ");
return 0;
}
close(stdoutcpy);
}
return 1;
}