코드 ¶
Phase Final
#include <stdio.h> #include <iostream> #include <math.h> int main () { int max = INT_MIN, row, column; bool isInt = true, isSusic = true; char simbol; FILE* fp = fopen("file.txt", "r"); char *string = (char*)malloc(sizeof(char)*1024); char *x = (char*)malloc(sizeof(char)*1024); char *y = (char*)malloc(sizeof(char)*1024); int *num = (int*)malloc(sizeof(int)*row*column); fscanf(fp, "%d %d\n", &row, &column); for(int i = 0; i < row * column; i++) { memset(string, 0, sizeof(char)*1024); memset(x, 0, sizeof(char)*1024); memset(y, 0, sizeof(char)*1024); fscanf(fp, "%s", string); for(int j=0; string[j] != '\0' ; j++) { if(isdigit(string[j])|| (j == 0 && string[j] == '-')) { isInt = true; isSusic = false; continue; } else if(j != 0 && (string[j] == '+' || string[j] == '-' || string[j] == '*')) { simbol = string[j]; int k; for(k=0;k<j;k++) x[k] = string[k]; x[k] = '\0'; for(k=j +1;string[k]!='\0';k++) y[k-(j+1)] = string[k]; y[k] = '\0'; isInt = true; isSusic = true; break; } else { isInt = false; isSusic = false; break; } } if(isSusic) { for(int j=0; y[j] != '\0'; j++) { if(isdigit(y[j]) || (j ==0 && y[j] == '-')) { isInt = true; isSusic = true; continue; } else { isInt = false; isSusic = false; break; } } } if(isSusic) { switch(simbol) { case '+': num[i] = atoi(x) + atoi(y); break; case '-': num[i] = atoi(x) - atoi(y); break; case '*': num[i] = atoi(x) * atoi(y); break; default: printf("ERROR!\n"); num[i] = INT_MIN; break; } } else if(!isInt) { num[i] = INT_MIN; continue; } else { num[i] = atoi(string); } } for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { if(*(num + (i * column) + j) > max) { max = *(num + (i * column) + j); } } if(max == INT_MIN) printf("row %d: %s\n", i +1, "NaN"); else printf("row %d: %d\n", i +1, max); max = INT_MIN; } for(int i = 0; i < column; i++) { for(int j = 0; j < row; j++) { if(*(num + (column * j) + i) > max) { max = *(num + (column * j) + i); } } if(max == INT_MIN) printf("column %d: %s\n", i +1, "NaN"); else printf("column %d: %d\n", i +1, max); max = INT_MIN; } for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { if(*(num + (i * column) + j) > max) { max = *(num + (i * column) + j); } } } if(max == INT_MIN) printf("matrix: %s\n","NaN"); else printf("matrix: %d\n", max); return 0; }