-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortingProducts.c
99 lines (84 loc) · 2.61 KB
/
SortingProducts.c
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
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<string.h>
struct Product {
int Product_Code;
char Name[100];
int Price;
};
void SelectionSort(struct Product A[], int N) {
int largeQ, largeC, index;
char LargeN[100];
int i, j;
for (i = N - 1; i >= 1; i--) {
largeQ = A[0].Price;
index = 0;
largeC = A[0].Product_Code;
strcpy(LargeN, A[0].Name);
for (j = 1; j <= i; j++) {
if (largeQ < A[j].Price) {
largeQ = A[j].Price;
largeC = A[j].Product_Code;
strcpy(LargeN, A[j].Name);
index = j;
}
}
A[index].Price = A[i].Price;
A[index].Product_Code = A[i].Product_Code;
strcpy(A[index].Name, A[i].Name);
A[i].Price = largeQ;
A[i].Product_Code = largeC;
strcpy(A[i].Name, LargeN);
}
}
void Display(struct Product A[], int N, int Choice) {
int i = 0;
if (Choice == 1) {
printf("\nSorted By Price Low to High: \n");
while (i < N) {
printf("%d\t\t%s\t\t%d\n", A[i].Product_Code, A[i].Name, A[i].Price);
i++;
}
} else {
printf("\nSorted By Price High to Low: \n");
i = N - 1;
while (i >= 0) {
printf("%d\t\t%s\t\t%d\n", A[i].Product_Code, A[i].Name, A[i].Price);
i--;
}
}
}
int main() {
int N, Choice, i = 0;
printf("Enter Number of Products Information Needed to Sort: ");
scanf("%d", &N);
getchar();
struct Product A[N];
while (i < N) {
printf("\nEnter Product Code for Product %d: ", i + 1);
scanf("%d", &A[i].Product_Code);
getchar();
printf("Enter Name for Product %d: ", i + 1);
fgets(A[i].Name, sizeof(A[i].Name), stdin);
A[i].Name[strcspn(A[i].Name, "\n")] = '\0';
printf("Enter Price for Product %d: ", i + 1);
scanf("%d", &A[i].Price);
getchar();
i++;
}
do {
printf("\n Enter Sorting Order:\n Enter 1 to Sort Low to High \n Enter 2 to Sort High To Low \n Enter 3 to Exit\n");
printf("\nInput Your Choice: ");
InputChoice:
scanf("%d", &Choice);
if (Choice == 1 || Choice == 2) {
SelectionSort(A, N);
Display(A, N, Choice);
} else if (Choice == 3) {
printf("Exited Program, Thank You");
} else {
printf("\nInvalid Input, Please Input Again !\n");
goto InputChoice;
}
} while (Choice != 3);
return 0;
}