-
-
Save passos/738ce9c025c7cfd88e39da50835ea9da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <String.h> | |
#define MAX_LINE_NUM 1000 | |
#define MAX_COLUMN_NUM 30 | |
#define MAX_RECORD_LENGTH 51 | |
#define IDENT_LENGTH 4 | |
void readFile(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int *numOfLine, int *numOfCol); | |
void sumaryStage1(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol); | |
void rowCpy(char tempRow[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], char row[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfCol); | |
void insertionSort(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol, int argc, char *argv[]); | |
void printTable(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol); | |
void printLine(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int lineNum, int numOfCol); | |
void sumaryStage2(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol); | |
int rowCmp(char A[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], char B[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int argc, char *argv[]); | |
int findMaxLength(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int columnNum); | |
int findMaxLength(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int columnNum); | |
void sumaryStage3(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int columnNum, int argc, char *argv[]); | |
void printBrokenLine(int width); | |
void printIndentation(int numOfIndentation); | |
int | |
main(int argc, char *argv[]){ | |
char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH]; | |
int numOfLine = 0; | |
int numOfCol = 0; | |
readFile(A, &numOfLine, &numOfCol); | |
sumaryStage1(A, numOfLine, numOfCol); | |
printf("\n");/**************************************/ | |
if(argc != 1){ | |
insertionSort(A, numOfLine, numOfCol, argc, argv); | |
sumaryStage2(A, numOfLine, numOfCol); | |
}else{ | |
exit(0); | |
} | |
printf("\n");/**************************************/ | |
printTable(A, numOfLine, numOfCol);/*********************************/ | |
printf("\n\n\n"); | |
sumaryStage3(A, numOfLine, atoi(argv[argc-1])-1, argc-1, argv+1); | |
return 0; | |
} | |
void | |
readFile(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int *numOfLine, int *numOfCol){ | |
int i = 0, j = 0; | |
int bufSize = MAX_COLUMN_NUM * MAX_RECORD_LENGTH; | |
char buf[bufSize + 1]; | |
while (i < MAX_LINE_NUM && fgets(buf, bufSize, stdin) != NULL) { | |
j = 0; | |
int k = 0; | |
for (char *p = buf; j < MAX_COLUMN_NUM; p++) { | |
if (*p == '\t') { | |
A[i][j][k] = '\0'; | |
j++; | |
k = 0; | |
} else if (*p == '\n' || *p == '\r') { | |
i++; | |
A[i][j][k] = '\0'; | |
break; | |
} else { | |
A[i][j][k] = *p; | |
k++; | |
if (k > MAX_RECORD_LENGTH - 1) { | |
/* skip to next column */ | |
while (*p != '\n' && *p != '\r' && *p != '\t') p++; | |
} | |
} | |
} | |
} | |
*numOfLine = i; | |
*numOfCol = j + 1; | |
} | |
void | |
sumaryStage1(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol){ | |
printf("Stage 1 Output\n"); | |
printf("input tsv data has %d rows and %d columns\n", numOfLine - 1, numOfCol); | |
printLine(A, numOfLine - 1, numOfCol); | |
} | |
void | |
printLine(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int lineNum, int numOfCol){ | |
printf("row %d is:\n", lineNum); | |
int i; | |
for(i=0; i<numOfCol; i++){ | |
printf("%5d: %-15s\t%-15s\n", i+1, A[0][i], A[lineNum][i]); | |
} | |
} | |
int | |
rowCmp(char A[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], char B[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int argc, char *argv[]){ | |
int i = 1; | |
while(i<argc){ | |
if(strcmp(A[atoi(argv[i])-1], B[atoi(argv[i])-1]) < 0){ | |
return -1; | |
}else if(strcmp(A[atoi(argv[i])-1], B[atoi(argv[i])-1]) > 0){ | |
return 1; | |
}else{ | |
i++; | |
} | |
} | |
return 0; | |
} | |
void | |
rowCpy(char tempRow[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], char row[MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfCol){ | |
int i; | |
for(i=0; i<numOfCol; i++){ | |
strcpy(tempRow[i], row[i]); | |
} | |
} | |
void | |
insertionSort(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol, int argc, char *argv[]){ | |
int i, j; | |
char temp[MAX_COLUMN_NUM][MAX_RECORD_LENGTH]; | |
for(i = 2; i < numOfLine; i++){ | |
rowCpy(temp, A[i], numOfCol); | |
for(j = i - 1; j >= 1 && rowCmp(A[j], temp, argc, argv) > 0; j--){ | |
rowCpy(A[j+1], A[j], numOfCol); | |
} | |
rowCpy(A[j+1], temp, numOfCol); | |
} | |
} | |
void | |
printTable(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol){ | |
printf("******************************************\n"); | |
int i, j; | |
for(i=0; i<numOfLine; i++){ | |
printf("#%2d: ", i); | |
for(j=0; j<numOfCol; j++){ | |
printf("%-15s", A[i][j]); | |
} | |
printf("\n"); | |
} | |
printf("******************************************\n"); | |
} | |
void | |
sumaryStage2(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int numOfCol){ | |
printf("Stage 2 Output\n"); | |
printLine(A, 1, numOfCol); | |
if(numOfLine % 2 == 0){ | |
printLine(A, numOfLine/2, numOfCol); | |
}else{ | |
printLine(A, numOfLine/2+1, numOfCol); | |
} | |
printLine(A, numOfLine - 1, numOfCol); | |
} | |
int | |
findMaxLength(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int columnNum){ | |
int maxlen = (int) strlen(A[0][columnNum]); | |
int i; | |
for(i=1; i<numOfLine; i++){ | |
if(strlen(A[i][columnNum]) > maxlen){ | |
maxlen = (int) strlen(A[i][columnNum]); | |
} | |
} | |
return maxlen; | |
} | |
void | |
sumaryStage3(char A[MAX_LINE_NUM][MAX_COLUMN_NUM][MAX_RECORD_LENGTH], int numOfLine, int columnNum, int argc, char *argv[]){ | |
int line = 0; | |
/* output the first line as column name */ | |
printf("----------------------------\n"); | |
for (int i = 0; i < argc - 1; i++) { | |
int col = atoi(argv[i]) - 1; | |
printf("%*s%s\n", i * IDENT_LENGTH, " ", A[line][col]); | |
} | |
int col = atoi(argv[argc - 1]) - 1; | |
int width = findMaxLength(A, numOfLine, col); | |
printf("%*s%-*s%*s%s\n", (argc - 1) * IDENT_LENGTH, " ", width, A[line][col], IDENT_LENGTH, " ", "Count"); | |
printf("----------------------------\n"); | |
line++; | |
/* group count */ | |
int groupCount = 1; | |
while (line < numOfLine) { | |
/* check next item to see if it is a new group */ | |
int lastItemInGroup = 0; | |
if (line + 1 >= numOfLine) { | |
/* this is the last line */ | |
lastItemInGroup = 1; | |
} else { | |
lastItemInGroup = rowCmp(A[line], A[line + 1], argc, argv); | |
} | |
/* count it if not last item in current group*/ | |
if (!lastItemInGroup) { | |
groupCount++; | |
line++; | |
continue; | |
} | |
/* print group column name */ | |
for (int i = 0; i < argc - 1; i++) { | |
int col = atoi(argv[i]) - 1; | |
/* compare with last group which index is [line - groupCount] */ | |
if (strcmp(A[line - groupCount][col], A[line][col]) != 0) { | |
printf("%*s%s\n", i * IDENT_LENGTH, " ", A[line][col]); | |
} | |
} | |
int col = atoi(argv[argc - 1]) - 1; | |
printf("%*s%-*s%*s%d\n", (argc - 1) * IDENT_LENGTH, " ", width, A[line][col], IDENT_LENGTH, " ", groupCount); | |
groupCount = 1; | |
line++; | |
} | |
} | |
void | |
printBrokenLine(int width){ | |
int i; | |
for(i=0; i<width; i++){ | |
printf("-"); | |
} | |
printf("\n"); | |
} | |
void | |
printIndentation(int numOfIndentation){ | |
int i; | |
for(i=0; i<numOfIndentation; i++){ | |
printf(" "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment