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
|
#include <stdio.h>
#include <stdlib.h>
int N;
struct person{
char *name;
int id;
int *preference;
};
int search_for_person_with_id(struct person *tab, char *s){
for(int i = 0; i < N; i++)
if(tab[i].name == s)
return tab[i].id;
return -1;
}
void enter_information(struct person *tab){
int i, j;
char *c1 = NULL;
for(i = 0; i < N; i++){
printf("Enter the name of male number %d \n", i+1);
scanf(" %s", tab[i].name);
tab[i].id = i;
}
for(j = 0; j < N; j++){
printf("Enter the name of female number %d \n", j+1);
scanf("\t%s", tab[j+N].name);
tab[j+N].id = j;
}
printf("Now enter the list of male preferences for each female \n ");
for(i = 0; i < N; i++){
printf("- For %s : \n", tab[i].name);
for(j = 0; j < N; j++){
printf(" %d choice \n", j + 1);
//fflush(stdin);
scanf(" %s", c1);
tab[i+N].preference[j] = search_for_person_with_id(tab, c1);
c1 = NULL;
//fflush(stdin);
}
}
printf("Now enter the list of female preferences for each male \n");
for(i = 0; i < N; i++){
printf("- For %s : \n", tab[i].name);
for(j = 0; j < N; j++){
printf(" %d choice \n", j + 1);
scanf(" %s", c1);
tab[i].preference[j] = search_for_person_with_id(tab, c1);
c1 = NULL;
}
}
}
int main(){
int i;
printf("Enter the number of males and females: ");
scanf("%d", &N);
struct person *tab;
tab = malloc(sizeof(struct person)* (2 * N));
for(i = 0; i < 2*N; i++){
tab[i].name = malloc(sizeof(char));
tab[i].preference = malloc(sizeof(int)*N);
}
enter_information(tab);
for(i = 0; i < 2*N; i++){
free(tab[i].name) ;
free(tab[i].preference) ;
}
free(tab);
return 1;
} |
Partager