程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> // 联赛数据结构 struct League_Match { char name[21]; // 队名 int integral; // 积分 int goal_difference; // 净胜球 int goals; // 进球数 long ranking; // 名次 }; // 函数原型 void Calculate(struct League_Match Data[], int* records); void Sort(struct League_Match Data[], int records); void OutputData(struct League_Match Data[]); // 程序入口 void main(void) { struct League_Match Data[] = { { "China", 2, 1, 5 }, { "U.S.A.", 12, 2, 7 }, { "U.K.", 12, 4, 10 }, { "Japan", 4, 2, 3 }, { "France", 4, 2, 4 }, { "" } }; int records; Calculate(Data, &records); Sort(Data, records); OutputData(Data); _getch(); // 按任意键结束程序 } // 计算名次分数 void Calculate(struct League_Match Data[], int* records) { int count = 0; while (strlen(Data[count].name) > 0) { Data[count].ranking = Data[count].integral * 256 * 256 + Data[count].goal_difference * 256 + Data[count].goals; ++count; } *records = count; } // 排序 void Sort(struct League_Match Data[], int records) { struct League_Match record; for (int i = 0; i < records - 1; ++i) { for (int j = i + 1; j < records; ++j) { if (Data[j].ranking > Data[i].ranking) { memcpy((void *)&record, (void *)&Data[i], sizeof(record)); memcpy((void *)&Data[i], (void *)&Data[j], sizeof(record)); memcpy((void *)&Data[j], (void *)&record, sizeof(record)); } } } } // 输出数据 void OutputData(struct League_Match Data[]) { printf_s("%-20s", "队名"); printf_s("%4s ", "积分"); printf_s("%4s ", "净胜球"); printf_s("%4s", "进球数"); printf_s("\n"); int index = 0; while (strlen(Data[index].name) > 0) { printf_s("%-20s", Data[index].name); printf_s("%4d ", Data[index].integral); printf_s("%4d ", Data[index].goal_difference); printf_s("%4d", Data[index].goals); printf_s("\n"); ++index; } }
[ 本帖最后由 TonyDeng 于 2012-2-29 03:56 编辑 ]
授人以渔,不授人以鱼。