Commit 9f6a4e0a by antsim

pr7

parent 673dbacb
Showing with 221 additions and 0 deletions
M Aivar 16 180 83.1 blond
M Juhan 21 175 71.3 ginger
F Mari 15 159 57.5 blond
M Georg 24 197 91.1 brynett
F Alma 67 171 92.4 ginger
M Urmas 29 176 164.8 kiilas
M Rivo 13 164 62.9 brynett
F Ina 17 188 76.2 brynett
M Mehis 47 178 99.4 kiilas
M Madis 32 185 106.6 ginger
F Julia 24 174 67.7 blond
F Marta 35 161 84.4 kiilas
M Misha 23 166 69.7 brynett
M Valdur 57 174 88.4 blond
F Tiiu 44 179 103.4 kiilas
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 15
#define MAXSIZE_CHAR 20
struct andmestik{
char sugu[2];
char nimi[MAXSIZE_CHAR];
int vanus;
int pikkus;
float kaal;
char juuksed[MAXSIZE_CHAR];
};
int readFile(struct andmestik **isik, FILE *file);
void printMenu(struct andmestik **isik, int kogus, FILE *file);
void printList(struct andmestik **isik, int kogus, FILE *file);
void filterGender(struct andmestik **isik, int kogus, FILE *file);
void filterAge(struct andmestik **isik, int kogus, FILE *file);
void filterWeight(struct andmestik **isik, int kogus, FILE *file);
int main(){
int suurus;
struct andmestik *isik = NULL;
FILE *fileinput; //declare file input
FILE *fileoutput; //declare file outp
fileinput = fopen("/Users/Anastassia/Desktop/andmed.txt", "r");//fopen()
fileoutput = fopen("/Users/Anastassia/Desktop/tulemused.txt", "w");
suurus = readFile(&isik, fileinput);
printMenu(&isik, suurus, fileoutput);
free(isik);
fclose(fileinput);
fclose(fileoutput);
return 0;
}
int readFile(struct andmestik **isik, FILE *file){
int i=0;
struct andmestik *temp = NULL;
if (file == NULL) {
printf("File not found.\n");
}
while(!feof(file)&&(i<MAXSIZE)){
// Allocates the memory
temp = (struct andmestik*) realloc(temp, ((i+1)*sizeof(struct andmestik)));
fscanf(file, "%s %s %d %d %f %s", (temp+i)->sugu,(temp+i)->nimi,&(temp+i)->vanus,&(temp+i)->pikkus,&(temp+i)->kaal,(temp+i)->juuksed);
if(strcmp((temp+i)->juuksed,"kiilas")){
i++;
} else {
temp = (struct andmestik*) realloc(temp, ((i)*sizeof(struct andmestik)));
}
}
//1)
*isik = (struct andmestik*) malloc(i*sizeof(struct andmestik));
//2)
memcpy(*isik,temp,i*sizeof(struct andmestik));
free(temp);
return i;
}
void printMenu(struct andmestik **isik, int kogus, FILE *file){
//1)
struct andmestik *tempStruct = *isik;
int choice;
do{
printf("===========================\n");
printf("(1). View all people unfiltered\n");
printf("(2). View only man or woman\n");
printf("(3). View by (min >==< max) age restrictions\n");
printf("(4). View by weight-index filtering\n");
printf("(0). Exit\n");
printf("===========================\n\n");
printf("Enter your choice Please\n");
scanf("%d", &choice);
switch(choice){
case 1:
printList(&tempStruct, kogus, file);
break;
case 2:
filterGender(&tempStruct, kogus, file);
break;
case 3:
filterAge(&tempStruct, kogus, file);
break;
case 4:
filterWeight(&tempStruct, kogus, file);
break;
case 0:
printf("Goodbye\n");
break;
default: printf("Wrong Choice. Enter again\n");
break;
}
} while(choice != 0);
}
void printList(struct andmestik **isik, int kogus, FILE *file){
struct andmestik *tempStruct = *isik;
for(int i =0;i<kogus;i++){
fprintf(file, "Name: %s\tSugu: %s\t\tVanus: %d\tPikkus: %d\tKaal: %.2f\tJuuksed: %s\n", (tempStruct+i)->nimi,(tempStruct+i)->sugu,(tempStruct+i)->vanus,(tempStruct+i)->pikkus,(tempStruct+i)->kaal,(tempStruct+i)->juuksed);
printf("Name: %s\tSugu: %s\t\tVanus: %d\tPikkus: %d\tKaal: %.2f\tJuuksed: %s\n", (tempStruct+i)->nimi,(tempStruct+i)->sugu,(tempStruct+i)->vanus,(tempStruct+i)->pikkus,(tempStruct+i)->kaal,(tempStruct+i)->juuksed);
}
}
void filterGender(struct andmestik **isik, int kogus, FILE *file){
struct andmestik *tempStruct = *isik;
int choice;
do{
printf("===========================\n");
printf("(1). View only man\n");
printf("(2). View only woman\n");
printf("(0). Back to menu\n");
printf("===========================\n\n");
printf("Enter your choice Please\n");
scanf("%d", &choice);
switch(choice){
case 1:
for(int i=0;i<kogus;i++){
if(!(strcmp((tempStruct+i)->sugu,"m"))||!(strcmp((tempStruct+i)->sugu,"M"))){
fprintf(file, "Sugu: %s\tName: %s\tVanus: %d\tJuuksed: %s\n", "Mees",(tempStruct+i)->nimi,(tempStruct+i)->vanus,(tempStruct+i)->juuksed);
printf("Sugu: %s\tName: %s\tVanus: %d\tJuuksed: %s\n", "Mees",(tempStruct+i)->nimi,(tempStruct+i)->vanus,(tempStruct+i)->juuksed);
}
}
break;
case 2:
for(int i=0;i<kogus;i++){
if(!(strcmp((tempStruct+i)->sugu,"f"))||!(strcmp((tempStruct+i)->sugu,"F"))){
fprintf(file, "Sugu: %s\tName: %s\tVanus: %d\tJuuksed: %s\n", "Naine",(tempStruct+i)->nimi,(tempStruct+i)->vanus,(tempStruct+i)->juuksed);
printf("Sugu: %s\tName: %s\tVanus: %d\tJuuksed: %s\n", "Naine",(tempStruct+i)->nimi,(tempStruct+i)->vanus,(tempStruct+i)->juuksed);
}
}
break;
case 0:
printf("Redirect to main menu\n");
break;
default: printf("Wrong Choice. Enter again\n");
break;
}
} while(choice != 0);
}
void filterAge(struct andmestik **isik, int kogus, FILE *file){
struct andmestik *tempStruct = *isik;
int choice, choice1, choice2;
do{
printf("Enter wanted Min/Max age respectively:\n");
scanf("%d%d", &choice1, &choice2);
if(choice2<choice1){
printf("Wrong Choice. Enter again\n");
}else{
for(int i =0;i<kogus;i++){
if (((tempStruct+i)->vanus>=choice1)&&((tempStruct+i)->vanus<=choice2)){
fprintf(file, "Vanus: %d\tName: %s\tSugu: %s\n", (tempStruct+i)->vanus,(tempStruct+i)->nimi,(tempStruct+i)->sugu);
printf("Vanus: %d\tName: %s\tSugu: %s\n", (tempStruct+i)->vanus,(tempStruct+i)->nimi,(tempStruct+i)->sugu);
}
}
choice = 0;
}
}while(choice != 0);
}
void filterWeight(struct andmestik **isik, int kogus, FILE *file){
struct andmestik *tempStruct = NULL;
tempStruct = (struct andmestik*) malloc(kogus*sizeof(struct andmestik));
memcpy(tempStruct,*isik,kogus*sizeof(struct andmestik));
struct andmestik *temp = NULL;
temp = (struct andmestik*) malloc(sizeof(struct andmestik));
double index1, index2;
for(int i=0;i<kogus;i++){
for(int j=i+1;j<kogus;j++){
index1 = ((tempStruct+i)->kaal)/((((double)(tempStruct+i)->pikkus)/100)*(((double)(tempStruct+i)->pikkus)/100));
index2 = ((tempStruct+j)->kaal)/((((double)(tempStruct+j)->pikkus)/100)*(((double)(tempStruct+j)->pikkus)/100));
if(index1>index2){
memcpy(temp,(tempStruct+i),sizeof(struct andmestik));
memcpy((tempStruct+i),(tempStruct+j),sizeof(struct andmestik));;
memcpy((tempStruct+j), temp,sizeof(struct andmestik));
}
}
}
for(int i=0;i<kogus;i++){
index1 = ((tempStruct+i)->kaal)/((((float)(tempStruct+i)->pikkus)/100)*(((float)(tempStruct+i)->pikkus)/100));
fprintf(file, "KMI: %.2f\tName: %s\tSugu: %s\t\tVanus: %d\n", index1,(tempStruct+i)->nimi,(tempStruct+i)->sugu,(tempStruct+i)->vanus);
printf("KMI: %.2f\tName: %s\tSugu: %s\t\tVanus: %d\n", index1,(tempStruct+i)->nimi,(tempStruct+i)->sugu,(tempStruct+i)->vanus);
}
free(tempStruct);
free(temp);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment