-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjapa.c
199 lines (175 loc) · 3.96 KB
/
japa.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int valor;
int i;
int j;
} Elemento;
typedef struct {
int i;
int j;
int **m;
int amount;
} Matriz;
void add(Elemento ** temp, int valor, int i, int j, int count){
(*temp)[count].valor = valor;
(*temp)[count].i = i;
(*temp)[count].j = j;
//printf("count = %d\nElemento:\nValor=%d\ni=%d\nj=%d\n", count, (*temp)[count].valor, (*temp)[count].i, (*temp)[count].j);
}
int find(FILE * arquivo){
int count = 0;
int i, j, tami, tamj;
int aux;
fscanf(arquivo, "%d %d\n", &tami, &tamj);
for(i=0; i<tami; i++){
for(j=0; j<tamj; j++){
fscanf(arquivo, "%d ", &aux);
if(aux){
count++;
}
}
}
return count;
}
Elemento * definirElementos(FILE * arquivo, Matriz ** tamanho){
int i, j;
int aux;
int count=0;
Elemento * temp = NULL;
(*tamanho)->amount = find(arquivo);
//printf("amount = %d\n",(*tamanho)->amount);
temp = malloc(sizeof(Elemento) * (*tamanho)->amount);
rewind(arquivo);
fscanf(arquivo, "%d %d\n", &((*tamanho)->i), &((*tamanho)->j));
//printf("tami %d tamj %d\n", (*tamanho)->i, (*tamanho)->j);
for(i=0; i<(*tamanho)->i; i++){
for(j=0; j<(*tamanho)->j; j++){
fscanf(arquivo, "%d ", &aux);
if(aux){
add(&temp, aux, i, j, count);
count++;
}
}
}
return temp;
}
void criaMatriz(Matriz * matriz, Elemento * elementos){
int i;
int j;
matriz->m = malloc(sizeof(int*)*matriz->i);
for(i=0; i<matriz->i; i++){
matriz->m[i] = malloc(sizeof(int)*matriz->j);
}
for(i=0; i<matriz->i; i++){
for(j=0; j<matriz->j; j++){
matriz->m[i][j] = 0;
}
}
for(i=0; i<matriz->amount; i++){
int x = elementos[i].i;
int y = elementos[i].j;
int v = elementos[i].valor;
matriz->m[x][y] = v;
}
for(i=0; i<matriz->i; i++){
for(j=0; j<matriz->j; j++){
printf("%3d ", matriz->m[i][j]);
}
printf("\n");
}
for(i=0; i<matriz->i; i++){
free(matriz->m[i]);
}
free(matriz->m);
}
int menu(char nome[2][50]){
int aux;
system("clear");
printf("O que deseja?\n\n");
printf("\t1: Carregar uma matriz\n");
printf("\t2: Visualizar uma matriz\n");
printf("\t3: Modificar uma matriz\n");
printf("\t4: Salvar uma matriz\n");
printf("\t5: Somar matrizes\n");
printf("\t6: Subtrair matrizes\n");
printf("\t7: Multiplicar matrizes\n");
printf("\t0: Sair\n\n");
if(nome[0][0]){
printf("Arquivos carregados: %s / %s\n\n", nome[0], nome[1]);
}
printf("In: ");
scanf("%d", &aux);
system("clear");
return aux;
}
void carregar(FILE *arquivo[2], char nome[2][50]){
char aux;
printf("Digite o nome do arquivo para ser carregado\nIn: ");
if(!nome[0][0]){
scanf("%s", nome[0]);
if(!(arquivo[0] = fopen(nome[0], "r"))){
printf("Arquivo nao encontrado, ENTER para voltar\n");
getchar();
getchar();
nome[0][0] = '\0';
return;
}
printf("Deseja carregar o segundo arquivo?\nS/N: ");
getchar();
scanf("%c", &aux);
if(aux == 'S' || aux == 's'){
printf("Digite o nome do segundo arquivo\nIn: ");
scanf("%s", nome[1]);
if(!(arquivo[1] = fopen(nome[1], "r"))){
printf("Arquivo nao encontrado, ENTER para voltar\n");
getchar();
getchar();
nome[1][0] = '\0';
return;
}
}else{
return;
}
return;
}if(!nome[1][0]){
scanf("%s", nome[1]);
if(!(arquivo[1] = fopen(nome[1], "r"))){
printf("Arquivo nao encontrado, ENTER para voltar\n");
getchar();
getchar();
nome[1][0] = '\0';
return;
}
}else{
printf("\n\nErro: Todos arquivos lidos, ENTER para continuar\n");
getchar();
getchar();
}
}
int main(int argc, char const *argv[])
{
// if(argc < 3){
// printf("inicie os arquvos\n");
// exit(1);
// }
char nome[2][50] = {"\0", "\0"};
FILE * arquivo[2] = {NULL, NULL};
Matriz * _m = malloc(sizeof(Matriz));
Elemento * posicoes = NULL;
int op;
do{
op = menu(nome);
switch(op){
case 0:
break;
case 1:
carregar(arquivo, nome);
printf("%s\n%s\n", nome[0], nome[1]);
}
}while(op);
//arquivo = fopen(argv[1], "r");
//posicoes = definirElementos(arquivo, &_m);
//criaMatriz(_m, posicoes);
return 0;
}