Skip to content

Instantly share code, notes, and snippets.

@markogresak
Created May 23, 2014 18:58
Show Gist options
  • Save markogresak/a920621a2b98f05eecc6 to your computer and use it in GitHub Desktop.
Save markogresak/a920621a2b98f05eecc6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
//----------------struct za linked list-------------
struct node {
char *vpisna;
int naloga;
int vrednost;
};
/**
- errorji za prva dva taga
- popravi structe
-
*/
struct node** insert(struct node **start, int n, char *vpis, char *id,
char *vred);
void readData(FILE *);
int vpisnaPrimerjava(const void *prva, const void *druga);
int main(int argc, char *argv[]) {
FILE *input;
if (argc > 1) {
if (!strcmp(argv[1], "-")) {
readData(stdin);
} else {
input = fopen(argv[1], "r");
readData(input);
}
} else {
readData(stdin);
}
return 0;
}
//---------------------KONEC---------------------------------------------------------------
int vpisnaPrimerjava(const void *prva, const void *druga) {
return strcmp((*(struct node **) prva)->vpisna,
(*(struct node **) druga)->vpisna);
}
struct node* createNew(char *vpis, int id, int vred) {
struct node* new = (struct node*) malloc(sizeof(struct node));
new->vpisna = (char*) malloc(sizeof(char) * (strlen(vpis)));
strcpy(new->vpisna, vpis);
new->naloga = id;
new->vrednost = vred;
return new;
}
struct node** insertInt(struct node **start, int n, char *vpis, int id,
int vred) {
start = (struct node**) realloc(start, sizeof(struct node *) * (n + 1));
start[n] = createNew(vpis, id, vred);
return start;
}
struct node** insert(struct node **start, int n, char *vpis, char *id,
char *vred) {
return insertInt(start, n, vpis, atoi(id), atoi(vred));
}
int nodePosition(struct node **s, int n, char *vs, int id, int p) {
for(int i = 0; i < n; i++) {
if(strcmp(s[i]->vpisna, vs) == 0 && (s[i]->naloga == id || !p)) {
return i;
}
}
return -1;
}
void readData(FILE *input) {
if (input == NULL) {
printf("napaka 1\n");
exit(0);
}
int structCount = 0;
struct node **start = malloc(0);
// char ocene[8] = "<ocene>";
// char oceneE[9] = "/ocene>";
//
// char ocena[8] = "<ocena>";
// char ocenaE[9] = "/ocena>";
char nalogaId[11] = "<nalogaId>"; //1
char nalogaIdE[12] = "/nalogaId>";
char vpisna[17] = "<vpisnaStevilka>"; //2
char vpisnaE[18] = "/vpisnaStevilka>";
char vrednost[11] = "<vrednost>"; //3
char vrednostE[12] = "/vrednost>";
char *string;
char buffer[101];
int ch = 0;
int counter = 0;
int check = 0;
int sCounter = 0;
char *id;
char *vpis;
char *vred;
int checkTAG = 0;
//start copy
while (EOF != (ch = fgetc(input))) {
//berem string do taga
// printf("%c", ch);
if (isspace(ch) == 0) {
buffer[counter] = (char) ch;
//printf("%c", buffer[counter]);
counter++;
}
if (isspace(ch) == 1) {
//printf("space");
}
//scekiram string
if (buffer[counter - 1] == '>' && check == 0) {
buffer[counter] = '\0';
string = malloc(sizeof(char) * (counter + 1));
strcpy(string, buffer);
//printf("\n%s", string);
counter = 0;
memset(buffer, 0, sizeof(char) * 101);
//pogoji za naprej
//nalogaId
if (strcmp(string, nalogaId) == 0) {
check = 1;
}
//vpisna
else if (strcmp(string, vpisna) == 0) {
check = 2;
}
//vrednost
else if (strcmp(string, vrednost) == 0) {
check = 3;
} else if ((strcmp(string, nalogaIdE) == 0 && checkTAG != 1) || //naloga ID tag
(strcmp(string, vpisnaE) == 0 && checkTAG != 2) || //naloga VPISNA tag
(strcmp(string, vrednostE) == 0 && checkTAG != 3)) { //naloga VREDNSOT tag
free(string);
printf("napaka 3\n");
exit(0);
}
free(string);
}
//strcmp za error (ujemanje badgev)
//zapisem ID
if (buffer[counter - 1] == '<' && check == 1) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
id = malloc(sizeof(char) * (counter + 1));
strcpy(id, buffer);
//printf("%s\n", id);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 1;
sCounter++;
}
//zapisem VPISNO
else if (buffer[counter - 1] == '<' && check == 2) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vpis = malloc(sizeof(char) * (counter + 1));
strcpy(vpis, buffer);
//printf("%s\n", vpis);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 2;
sCounter++;
}
//zapisem VREDNOST //NAPAKE!!! spisi majmun :D
else if (buffer[counter - 1] == '<' && check == 3) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vred = malloc(sizeof(char) * (counter + 1));
strcpy(vred, buffer);
//printf("%s\n", vred);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 3;
sCounter++;
}
//struct
if (sCounter == 3) {
start = insert(start, structCount, vpis, id, vred);
structCount++;
// free(vpis);
// free(id);
// free(vred);
sCounter = 0;
}
}
fclose(input);
struct node **tmp = malloc(0);
int tmpCount = 0;
int dupIndex = -1;
// zanka cez vse elemente
for (int i = 0; i < structCount; i++) {
// zanka cez vse elemente pred i-tim elementom
dupIndex = nodePosition(tmp, tmpCount, start[i]->vpisna, start[i]->naloga, 1);
if(dupIndex == -1) {
tmp = insertInt(tmp, tmpCount, start[i]->vpisna, start[i]->naloga, start[i]->vrednost);
tmpCount++;
}
else {
tmp[dupIndex]->vrednost = start[i]->vrednost;
}
}
struct node **result = malloc(0);
int resultCount = 0;
int resultI = -1;
for (int i = 0; i < tmpCount; i++) {
resultI = nodePosition(result, resultCount, tmp[i]->vpisna, tmp[i]->naloga, 0);
if (resultI == -1) {
result = insertInt(result, resultCount, tmp[i]->vpisna, 1, tmp[i]->vrednost);
resultCount++;
} else {
result[resultI]->naloga += 1;
result[resultI]->vrednost += tmp[i]->vrednost;
}
}
qsort(result, resultCount, sizeof(struct node *), vpisnaPrimerjava);
for (int i = 0; i < resultCount; i++) {
printf("%s %d %d\n", result[i]->vpisna, result[i]->naloga,
result[i]->vrednost);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
//----------------struct za linked list-------------
struct node {
char *vpisna;
char *naloga;
char *vrednost;
struct node *next;
};
/**
- errorji za prva dva taga
- popravi structe
-
*/
void swap(struct node *a, struct node *b);
void printList(struct node *start);
void sortList(struct node *start);
void insertAtTheBegin(struct node **start, char *vpis, char*id, char *vred);
void recalulate(struct node *start, struct node **finish, int structCount);
void readData(FILE *);
int main(int argc, char *argv[]) {
int structCount = 0;
printf("Pogon\n");
FILE *input;
struct node *start = NULL;
struct node *finish = NULL;
char *temp;
char ocene[8] = "<ocene>";
char oceneE[9] = "/ocene>";
char ocena[8] = "<ocena>";
char ocenaE[9] = "/ocena>";
char nalogaId[11] = "<nalogaId>"; //1
char nalogaIdE[12] = "/nalogaId>";
char vpisna[17] = "<vpisnaStevilka>"; //2
char vpisnaE[18] = "/vpisnaStevilka>";
char vrednost[11] = "<vrednost>"; //3
char vrednostE[12] = "/vrednost>";
char *stTag;
char *content;
char *enTag;
char *string;
char buffer[101];
int ch = 0;
int counter = 0;
int check = 0;
int sCounter = 0;
char *id;
char *vpis;
char *vred;
int checkTAG = 0;
if (argc > 1) {
if (!strcmp(argv[1], "-")) {
printf("stdin\n");
input = stdin;
temp = malloc(sizeof(char) * 101);
while (scanf(" <%100[^>]>", temp) != EOF) { //while
printf("%s\n", temp);
}
} else {
//----------------------FILE--------------------------------------------------------------
printf("file\n");
//printf("\n%s", argv[1]);
input = fopen(argv[1], "r");
if (input == NULL) {
fprintf(stderr, "Unable to open '%s': %s\n", argv[1],
strerror(errno));
exit(1);
}
//start copy
while (EOF != (ch = fgetc(input))) {
//berem string do taga
//printf("%c", ch);
if (isspace(ch) == 0) {
buffer[counter] = (char) ch;
//printf("%c", buffer[counter]);
counter++;
}
if (isspace(ch) == 1) {
//printf("space");
}
//scekiram string
if (buffer[counter - 1] == '>' && check == 0) {
buffer[counter] = '\0';
string = malloc(sizeof(char) * (counter + 1));
strcpy(string, buffer);
//printf("\n%s", string);
counter = 0;
memset(buffer, 0, sizeof(char) * 101);
//pogoji za naprej
//nalogaId
if (strcmp(string, nalogaId) == 0) {
check = 1;
}
//vpisna
else if (strcmp(string, vpisna) == 0) {
check = 2;
}
//vrednost
else if (strcmp(string, vrednost) == 0) {
check = 3;
} else if ((strcmp(string, nalogaIdE) == 0 && checkTAG != 1)
|| //naloga ID tag
(strcmp(string, vpisnaE) == 0 && checkTAG != 2) || //naloga VPISNA tag
(strcmp(string, vrednostE) == 0 && checkTAG != 3)) { //naloga VREDNSOT tag
free(string);
printf("napaka 3\n");
exit(0);
}
free(string);
}
//strcmp za error (ujemanje badgev)
//zapisem ID
if (buffer[counter - 1] == '<' && check == 1) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
id = malloc(sizeof(char) * (counter + 1));
strcpy(id, buffer);
//printf("%s\n", id);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 1;
sCounter++;
}
//zapisem VPISNO
else if (buffer[counter - 1] == '<' && check == 2) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vpis = malloc(sizeof(char) * (counter + 1));
strcpy(vpis, buffer);
//printf("%s\n", vpis);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 2;
sCounter++;
}
//zapisem VREDNOST //NAPAKE!!! spisi majmun :D
else if (buffer[counter - 1] == '<' && check == 3) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vred = malloc(sizeof(char) * (counter + 1));
strcpy(vred, buffer);
//printf("%s\n", vred);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 3;
sCounter++;
}
//struct
if (sCounter == 3) {
insertAtTheBegin(&start, vpis, id, vred);
structCount++;
free(vpis);
free(id);
free(vred);
sCounter = 0;
}
// if (feof(input)) {
// printf("konec datoteke");
// }
//sortList(start);
//printList(start);
//recalulate(start, &finish, structCount);
//sortList(finish);
//printList(finish);
}
//EO copy
}
} else {
//----------------------STDIN--------------------------------------------------------------
printf("stdin\n");
input = stdin;
//while((temp = readLine(input)) != NULL){ //while
//while((fgets(temp, sizeof(temp), input)) != NULL){
while (!feof(input)) {
//berem string do taga
if (isspace(ch) == 0) {
buffer[counter] = fgetc(input);
counter++;
}
//scekiram string
if (buffer[counter - 1] == '>' && check == 0) {
buffer[counter] = '\0';
string = malloc(sizeof(char) * (counter + 1));
strcpy(string, buffer);
//printf("%s\n", string);
counter = 0;
memset(buffer, 0, 101);
//pogoji za naprej
//nalogaId
if (strcmp(string, nalogaId) == 0) {
check = 1;
}
//vpisna
if (strcmp(string, vpisna) == 0) {
check = 2;
}
//vrednost
if (strcmp(string, vrednost) == 0) {
check = 3;
}
free(string);
}
//strcmp za error (ujemanje badgev)
//zapisem ID
if (buffer[counter - 1] == '<' && check == 1) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
id = malloc(sizeof(char) * (counter + 1));
strcpy(id, buffer);
//printf("%s\n", id);
memset(buffer, 0, 101);
counter = 0;
check = 0;
sCounter++;
}
//zapisem VPISNO
if (buffer[counter - 1] == '<' && check == 2) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vpis = malloc(sizeof(char) * (counter + 1));
strcpy(vpis, buffer);
//printf("%s\n", vpis);
memset(buffer, 0, 101);
counter = 0;
check = 0;
sCounter++;
}
//zapisem VREDNOST //NAPAKE!!! spisi majmun :D
if (buffer[counter - 1] == '<' && check == 3) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vred = malloc(sizeof(char) * (counter + 1));
strcpy(vred, buffer);
//printf("%s\n", vred);
memset(buffer, 0, 101);
counter = 0;
check = 0;
sCounter++;
}
//struct
if (sCounter == 3) {
insertAtTheBegin(&start, vpis, id, vred);
structCount++;
free(vpis);
free(id);
free(vred);
sCounter = 0;
}
}
fclose(input);
//sortList(start);
//printList(start);
recalulate(start, &finish, structCount);
//sortList(finish);
printList(finish);
}
//printf("\n%s","konec");
putchar('\n');
fclose(input);
return 0;
} //fscanf(in, " <%100[^>]>", dataTag)
//---------------------KONEC---------------------------------------------------------------
void printList(struct node *start) {
struct node *temp = start;
printf("\n");
while (temp != NULL) {
printf("%s %s %s\n", temp->vpisna, temp->naloga, temp->vrednost);
temp = temp->next;
}
}
void swap(struct node *a, struct node *b) {
char *tempV = malloc(sizeof(char) * strlen(a->vpisna));
strcpy(tempV, a->vpisna);
strcpy(a->vpisna, b->vpisna);
strcpy(b->vpisna, tempV);
char *tempN = malloc(sizeof(char) * strlen(a->naloga));
strcpy(tempN, a->naloga);
strcpy(a->naloga, b->naloga);
strcpy(b->naloga, tempN);
char *tempVr = malloc(sizeof(char) * strlen(a->vrednost));
strcpy(tempVr, a->vrednost);
strcpy(a->vrednost, b->vrednost);
strcpy(b->vrednost, tempVr);
//printf("\n%s\n", "swap");
free(tempN);
free(tempV);
free(tempVr);
}
void sortList(struct node *start) {
int swapped;
//printf("\n%s\n", "pricenjam urejanje");
struct node *n;
struct node *ln = NULL;
/**
if (n == NULL){
printf("\n%s\n", "null"); //napaka
return;
}
*/
do {
swapped = 0;
n = start;
while (n->next != ln) {
//printf("\n%s\n", "entering loop");
int a = atoi(n->vpisna);
int b = atoi(n->next->vpisna);
if (a > b) {
swap(n, n->next);
swapped = 1;
}
n = n->next;
}
ln = n;
} while (swapped);
}
void recalulate(struct node *start, struct node **finish, int structCount) {
//printf("\n%s\n", "reclalculating");
struct node *temp = start; //zacetek seznama
struct node *tempTwo = *finish; //zacetek novega seznama
int tempC = structCount;
//printf("\n%s %s\n", "Vpisna: ", temp->vpisna);
char *vpisne[tempC];
int x = 0; //stevec vpisnih
int i = 0; //stevec nalog a psoameno vpisno
int j = 0; //tocke
char *naloge[tempC][100]; //tabela IDjev nalog
char *tocke[tempC][100][100]; //tabela tock
int check = 0; //preveri ce Vpisna ze obstaja;
int checkTwo = 0; //preveri ce naloga ID ze obstaja
char *vpisnaSt;
char stNalog[10];
char strTocke[10];
while (temp != NULL) {
//preverim ce vpisna ze obstaja in popravim vnos
check = 0;
for (int y = 0; y < x; y++) {
if (strcmp(temp->vpisna, vpisne[y]) == 0) {
check = 1;
if (check == 1) {
//doda vpis v array nalog
i = 0;
checkTwo = 0;
while (naloge[y][i] != NULL) {
i++;
}
for (int q = 0; q < i; q++) {
//ce taka naloga ze obstaja, primerja tocke, in jih popravi
if (strcmp(temp->naloga, naloge[y][q]) == 0) {
if (atoi(temp->vrednost) < atoi(tocke[y][q][0])) {
tocke[y][q][0] = malloc(
sizeof(char) * sizeof(temp->vrednost));
strcpy(tocke[y][q][0], temp->vrednost);
}
} else { //ce taka naloga se ne obstaja, jo doda
naloge[y][i] = malloc(
sizeof(char) * sizeof(temp->naloga));
strcpy(naloge[y][i], temp->naloga);
tocke[y][i][0] = malloc(
sizeof(char) * sizeof(temp->vrednost));
strcpy(tocke[y][i][0], temp->vrednost); // DODAJANJE TOCK!!!
}
}
//printf("\n%s", "najdena enaka vpisna");
}
}
}
//dodam nov vpis
if (check == 0) {
vpisne[x] = malloc(sizeof(char) * 9);
strcpy(vpisne[x], temp->vpisna);
naloge[x][0] = malloc(sizeof(char) * sizeof(temp->naloga));
strcpy(naloge[x][0], temp->naloga);
tocke[x][0][0] = malloc(sizeof(char) * sizeof(temp->vrednost));
strcpy(tocke[x][0][0], temp->vrednost);
x++;
//printf("\n%s", "dodan nov element");
}
temp = temp->next;
//printf("\n%s", "naslednji element na senamu");
}
//sestavi pravilne vrednosti za koncni list
for (int z = 0; z < x; z++) {
struct node *n = (struct node*) malloc(sizeof(struct node));
//doloci vpisno
n->vpisna = malloc(sizeof(char) * strlen(vpisne[z]));
strcpy(n->vpisna, vpisne[z]);
//printf("\n%s", n->vpisna);
//doloci st. nalog
i = 0;
while (naloge[z][i] != NULL) {
i++;
}
//stNalog = malloc(sizeof(char)*2);
snprintf(stNalog, 10, "%d", i);
n->naloga = malloc(sizeof(char) * strlen(stNalog));
strcpy(n->naloga, stNalog);
//printf("\n%s", stNalog);
//doloci st. tock;
int sumTocke = 0;
for (int k = 0; k < i; k++) {
sumTocke = sumTocke + atoi(tocke[z][k][0]);
}
snprintf(strTocke, 10, "%d", sumTocke);
n->vrednost = malloc(sizeof(char) * strlen(strTocke));
//printf("\n%s", strTocke);
strcpy(n->vrednost, strTocke);
n->next = *finish;
*finish = n;
//printf("%s%s%s\n", vpisnaSt, stNalog, strTocke);
//insertAtTheBegin(&tempTwo, vpisnaSt, stNalog, strTocke);
}
}
void insertAtTheBegin(struct node **start, char *vpis, char *id, char *vred) {
struct node *n = (struct node*) malloc(sizeof(struct node));
n->vpisna = (char*) malloc(sizeof(char) * (strlen(vpis)));
strcpy(n->vpisna, vpis);
printf("\n%s", vpis);
n->naloga = (char*) malloc(sizeof(char) * (strlen(id)));
strcpy(n->naloga, id);
printf("\n%s", id);
n->vrednost = (char*) malloc(sizeof(char) * (strlen(vred)));
strcpy(n->vrednost, vred);
printf("\n%s", vred);
n->next = *start;
*start = n;
printf("\nStruct dodan\n");
}
void readData(FILE *input) {
printf("file\n");
//printf("\n%s", argv[1]);
input = fopen(argv[1], "r");
if (input == NULL) {
fprintf(stderr, "Unable to open '%s': %s\n", argv[1], strerror(errno));
exit(1);
}
//start copy
while (EOF != (ch = fgetc(input))) {
//berem string do taga
//printf("%c", ch);
if (isspace(ch) == 0) {
buffer[counter] = (char) ch;
//printf("%c", buffer[counter]);
counter++;
}
if (isspace(ch) == 1) {
//printf("space");
}
//scekiram string
if (buffer[counter - 1] == '>' && check == 0) {
buffer[counter] = '\0';
string = malloc(sizeof(char) * (counter + 1));
strcpy(string, buffer);
//printf("\n%s", string);
counter = 0;
memset(buffer, 0, sizeof(char) * 101);
//pogoji za naprej
//nalogaId
if (strcmp(string, nalogaId) == 0) {
check = 1;
}
//vpisna
else if (strcmp(string, vpisna) == 0) {
check = 2;
}
//vrednost
else if (strcmp(string, vrednost) == 0) {
check = 3;
} else if ((strcmp(string, nalogaIdE) == 0 && checkTAG != 1) || //naloga ID tag
(strcmp(string, vpisnaE) == 0 && checkTAG != 2) || //naloga VPISNA tag
(strcmp(string, vrednostE) == 0 && checkTAG != 3)) { //naloga VREDNSOT tag
free(string);
printf("napaka 3\n");
exit(0);
}
free(string);
}
//strcmp za error (ujemanje badgev)
//zapisem ID
if (buffer[counter - 1] == '<' && check == 1) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
id = malloc(sizeof(char) * (counter + 1));
strcpy(id, buffer);
//printf("%s\n", id);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 1;
sCounter++;
}
//zapisem VPISNO
else if (buffer[counter - 1] == '<' && check == 2) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vpis = malloc(sizeof(char) * (counter + 1));
strcpy(vpis, buffer);
//printf("%s\n", vpis);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 2;
sCounter++;
}
//zapisem VREDNOST //NAPAKE!!! spisi majmun :D
else if (buffer[counter - 1] == '<' && check == 3) {
buffer[counter - 1] = '\0';
buffer[counter] = '\0';
vred = malloc(sizeof(char) * (counter + 1));
strcpy(vred, buffer);
//printf("%s\n", vred);
memset(buffer, 0, sizeof(char) * 101);
counter = 0;
check = 0;
checkTAG = 3;
sCounter++;
}
//struct
if (sCounter == 3) {
insertAtTheBegin(&start, vpis, id, vred);
structCount++;
free(vpis);
free(id);
free(vred);
sCounter = 0;
}
// if (feof(input)) {
// printf("konec datoteke");
// }
//sortList(start);
//printList(start);
//recalulate(start, &finish, structCount);
//sortList(finish);
//printList(finish);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment