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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #define N_STUDENTS 4 typedef struct student_info { char name[10]; int height; float weight; } student_info; void swap(student_info s[], int i, int j) { student_info tmp; tmp = s[i]; s[i] = s[j]; s[j] = tmp; } void selection_sort_byname(student_info s[], int n) { int i, j, least; for (i = 0; i < n - 1; i++) { least = i; for (j = i + 1; j < n; j++) { if (strcmp(s[j].name, s[least].name) < 0) least = j; } swap(s, i, least); } } int main(void) { int i; student_info *s; s = (student_info *)malloc(sizeof(student_info)*N_STUDENTS); if (s == (student_info *)NULL) { fprintf(stderr, "malloc fail!!!\n"); exit(1); } for(i = 0; i < N_STUDENTS; i++) { printf("À̸§: "); scanf("%s", s[i].name); printf("Å°: "); scanf("%d", &s[i].height); printf("¸ö¹«°Ô: "); scanf("%f", &s[i].weight); } selection_sort_byname(s, N_STUDENTS); for (i = 0; i < N_STUDENTS; i++) { printf("%s %d %f \n", s[i].name, s[i].height, s[i].weight); } free(s); // ¸Þ¸ð¸® ÇÒ´ç ÇØÁ¦ return 0; } | cs |
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 | #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int i, j, *user; int num_computer; srand(time(NULL)); num_computer = rand() % 100 + 1; user = (int *) malloc(sizeof(int)*100); if(user == (int *) NULL) { fprintf(stderr,"malloc fail !!! \n"); exit(1); } printf("---------------------------------------\n"); printf(" UP/DOWN °ÔÀÓ \n"); printf("---------------------------------------\n"); printf(" 1~100 »çÀÌÀÇ ¼ýÀÚ Çϳª¸¦ ¼±ÅÃÇß½À´Ï´Ù \n"); printf(" ¸ÂÃ纸¼¼¿ä~~~ \n"); printf("---------------------------------------\n"); for(i=0; ;i++) { printf("¼ýÀÚ: "); scanf("%d",&user[i]); if(user[i] > num_computer) printf("DOWN \n"); else if(user[i] < num_computer) printf("UP \n"); else { printf("%d¹ø ¸¸¿¡ ¸ÂÃè½À´Ï´Ù.\n",i+1); printf("ÀԷ¼ýÀÚ: "); for(j=0; j<=i ; j++) { printf("%d ",user[j]); } printf("\n"); break; } } free(user); // ¸Þ¸ð¸® ÇÒ´ç ÇØÁ¦ } | cs |