Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- #include <conio.h>
- #define NUM_OF_PEOPLE 5
- #define LENGTH_OF_NAME 20
- #define N 3
- struct GROUP {
- char surname[20];
- int DAT[3];
- int R[3];
- };
- struct People {
- char name[LENGTH_OF_NAME];
- };
- //int compare(char name1[], char name2[]) {
- // for (int i = 0; i < LENGTH_OF_NAME && name1[i] != 0 && name2[i] != 0; ++i) {
- // if (name1[i] > name2[i]) return 0;
- // else if (name1[i] < name2[i]) return 1;
- // }
- // return 0;
- //}
- int recompare(char name1[], char name2[]) {
- for (int i = 0; i < LENGTH_OF_NAME && name1[i] != 0 && name2[i] != 0; i++) {
- int k = 0;
- if (name1[i] == name2[i]) k++;
- if (k == strlen(name2) && k < strlen(name1)) return 1;
- }
- return 0;
- }
- void sort(struct People A[]) {
- struct People temp;
- for (int i = 0; i < NUM_OF_PEOPLE; ++i) {
- for (int j = 0; j < NUM_OF_PEOPLE; ++j) {
- if (strcmp(A[i].name, A[j].name) < 0) {
- temp = A[i];
- A[i] = A[j];
- A[j] = temp;
- }
- }
- }
- }
- void resort(struct People A[]) {
- struct People temp;
- int o = LENGTH_OF_NAME;
- while (o != 0) {
- o--;
- for (int i = 0; i < NUM_OF_PEOPLE - 1; i++) {
- if (recompare(A[i].name, A[i + 1].name)) {
- temp = A[i + 1];
- A[i + 1] = A[i];
- A[i] = temp;
- }
- }
- }
- }
- void main() {
- printf("Enter a names of students: \n");
- struct People A[NUM_OF_PEOPLE];
- for (int i = 0; i < NUM_OF_PEOPLE; ++i) {
- printf("Student number %d: \n", i + 1);
- gets(A[i].name);
- }
- resort(A);
- sort(A);
- //resort(A);
- printf("Sorted people: \n");
- for (int i = 0; i < NUM_OF_PEOPLE; ++i) {
- printf(A[i].name);
- printf("\n");
- }
- printf("\n");
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement