- 论坛徽章:
- 2
|
- /* sortbubble.c
- * an example of sorting linked table by bubble mechanism.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define TAX (0.13)
- struct Item
- {
- struct Item *next;
- char name[21];
- int upc;
- double price;
- int isTaxed;
- };
- void prnTitles(void)
- {
- printf("UPC | Name | Price | Tax | Total\n"
- "-----+--------------------+-----------+---------+----------- \n");
- }
- void prnItemRow(struct Item *A)
- {
- printf("%-5d|%-20s|%11.2lf|", A->upc, A->name, A->price);
- if (A->isTaxed)
- printf("%9.2lf|%11.2lf\n", A->price * TAX, A->price * (1 + TAX));
- else
- printf("%9.2lf|%11.2lf\n", 0.0, A->price);
- }
- int readItem(struct Item* Ip, FILE* fptr)
- {
- return fscanf(fptr, "%d,%20[^,],%lf,%d", &Ip->upc, Ip->name, &Ip->price, &Ip->isTaxed)==4;
- }
- struct Item **insertItem(struct Item **io_list_last, struct Item *node)
- {
- node->next = *io_list_last;
- *io_list_last = node;
- return &node->next;
- }
- struct Item *findItem(struct Item *list, int num)
- {
- struct Item *node;
-
- for (node = list; node != NULL; node = node->next) {
- if (node->upc == num) {
- return node;
- }
- }
- return NULL;
- }
- void swapPointer(void **p1, void **p2)
- {
- void *tmp;
-
- tmp = *p1;
- *p1 = *p2;
- *p2 = tmp;
- }
- struct Item **swapItem(struct Item **node_fore, struct Item **node_back)
- {
- if ((*node_fore)->next == *node_back) {
- swapPointer(node_fore, node_back);
- swapPointer(&(*node_fore)->next, &(*node_back)->next);
- return &(*node_fore)->next;
- }else {
- swapPointer(node_fore, node_back);
- swapPointer(&(*node_fore)->next, &(*node_back)->next);
- return node_back;
- }
- }
- int compare_price(const struct Item *item1, const struct Item *item2)
- {
- return item1->price > item2->price;
- }
- int compare_name(const struct Item *item1, const struct Item *item2)
- {
- int len1, len2;
- int len;
- len1 = strlen(item1->name);
- len2 = strlen(item2->name);
- len = min(len1, len2);
- switch (strncmp(item1->name, item2->name, len)) {
- case 1:
- return 1;
- case -1:
- return 0;
- default:
- return len1 > len2;
- }
- }
- void sortItems(struct Item **plist, int (*cmp_func)(const void *item1, const void *item2))
- {
- struct Item **pnext;
- for (; *plist != NULL; plist = &(*plist)->next) {
- for (pnext = &(*plist)->next; *pnext != NULL; pnext = &(*pnext)->next) {
- if ((*cmp_func)(*plist, *pnext)) {
- pnext = swapItem(plist, pnext);
- }
- }
- }
- }
- int main(void)
- {
- struct Item I;
- FILE *fptr;
- struct Item *node;
- struct Item *list, **plast;
- int num;
- list = NULL;
- /* load table from file */
- fptr = fopen("sample\\items.txt", "r");
- if (fptr != NULL) {
- plast = &list;
- while (readItem(&I, fptr)) {
- node = (struct Item *)malloc(sizeof(struct Item));
- if (node == NULL) {
- printf("not enough memory\n");
- break;
- }
- *node = I;
- plast = insertItem(plast, node);
- }
- fclose(fptr);
- }else {
- printf("Could not open the file!\n");
- }
- puts("sort table by price");
- sortItems(&list, compare_price);
- /* print table */
- prnTitles();
- for (node = list; node != NULL; node = node->next) {
- prnItemRow(node);
- }
- puts("sort table by name");
- sortItems(&list, compare_name);
- /* print table */
- prnTitles();
- for (node = list; node != NULL; node = node->next) {
- prnItemRow(node);
- }
- /* search table */
- printf("please enter a UPC number: ");
- scanf("%d",&num);
- node = findItem(list, num);
- if (node != NULL) {
- prnItemRow(node);
- }else {
- printf("the number can not be found\n");
- }
- /* release table */
- while (list != NULL) {
- node = list;
- list = list->next;
- free(node);
- }
- return 0;
- }
复制代码 |
|