| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 818 人关注过本帖
标题:联赛排名问题
只看楼主 加入收藏
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
程序代码:
#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 编辑 ]

授人以渔,不授人以鱼。
2012-02-29 03:49
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
下面是從外部文件讀入比賽數據的程序代碼:

主程序
程序代码:
#include "League_Match.h"
#include <conio.h>

void main(void)
{
    League_Match theGame;
    if (theGame.Load_Data("Game.TXT") == 0)
    {
        theGame.Calculate();
        theGame.Sort();
        theGame.Output_Data();
    }

    _getch();
}


頭文件(League_Match.h)
程序代码:
#ifndef LEAGUE_MATCH
#define LEAGUE_MATCH

#include <vector>
#include <string>

class League_Match
{
public:

    static struct RECORD
    {
        char name[21];            // 队名
        int integral;            // 积分
        int goal_difference;    // 净胜球
        int goals;                // 进球数
        long ranking;            // 名次
    };

    std::vector<struct RECORD> theRecords;

    errno_t Load_Data(std::string FileName);
    void Calculate(void);
    void Sort(void);
    void Output_Data(void);
};

#endif


類模塊(League_Match.cpp)
程序代码:
#include "League_Match.h"

errno_t League_Match::Load_Data(std::string FileName)
{
    FILE* file;
    errno_t error;

    error = fopen_s(&file, FileName.c_str(), "rt");
    if (error == 0)
    {
        while (!feof(file))
        {
            char buffer[1024];
            if (fgets(buffer, sizeof(buffer) - 1, file) != NULL)
            {
                struct RECORD record;
                char* pStart = strchr(buffer, '\"') + 1;
                char* pEnd = strrchr(buffer, '\"');
                strncpy_s(record.name, pStart, pEnd - pStart);
                record.name[pStart, pEnd - pStart] = '\0';
                if (sscanf_s(pEnd + 1, "%d %d %d", &record.integral, &record.goal_difference, &record.goals) < 3)
                {
                    error = -1;
                    printf_s("数据文件记录错误: Line %d\n", theRecords.size() + 1);
                    break;
                }
                theRecords.push_back(record);
            }
        }
        fclose(file);
    }
    else
    {
        printf_s("数据文件%s不存在,或没有数据!\n", FileName);
    }

    return error;
}

void League_Match::Calculate(void)
{
    for (std::vector<struct RECORD>::iterator index = theRecords.begin(); index != theRecords.end(); ++index)
    {
        (*index).ranking = (*index).integral * 256 * 256 + (*index).goal_difference * 256 + (*index).goals;
    }
}

void League_Match::Sort(void)
{
    struct RECORD record;
    for (std::vector<struct RECORD>::size_type i = 0; i < theRecords.size() - 1; ++i)
    {
        for (std::vector<struct RECORD>::size_type j = i + 1; j < theRecords.size(); ++j)
        {
            if (theRecords[j].ranking > theRecords[i].ranking)
            {
                memcpy((void *)&record, (void *)&theRecords[i], sizeof(record));
                memcpy((void *)&theRecords[i], (void *)&theRecords[j], sizeof(record));
                memcpy((void *)&theRecords[j], (void *)&record, sizeof(record));
            }
        }
    }
}

void League_Match::Output_Data(void)
{
    printf_s("%-20s", "队名");
    printf_s("%4s ", "积分");
    printf_s("%6s ", "净胜球");
    printf_s("%6s", "进球数");
    printf_s("\n");

    for (std::vector<RECORD>::iterator index = theRecords.begin(); index != theRecords.end(); ++index)
    {
        printf_s("%-20s", (*index).name);
        printf_s("%4d ", (*index).integral);
        printf_s("%6d ", (*index).goal_difference);
        printf_s("%6d", (*index).goals);
        printf_s("\n");
    }
}


數據文件(Game.txt)
程序代码:
"China" 2 1 5
"U.S.A." 12 2 7
"U.K." 12 4 10
"Japan" 4 2 3
"France" 4 2 4
"Germany" 10 3 8


授人以渔,不授人以鱼。
2012-02-29 13:26
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:4 
回复 12楼 TonyDeng
南國有佳人,遺世而獨立,一顧傾人城,再顧傾人國。

梅尚程荀
马谭杨奚







                                                       
2012-02-29 19:30
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
我不至於是這樣的禍水吧?

授人以渔,不授人以鱼。
2012-02-29 21:06
zxd675816777
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:252
专家分:631
注 册:2012-2-3
收藏
得分:0 
回复 10楼 a271885843
小朋友,哈喽,你最好一边儿撅着去哈,这种场面不是你应该见到的哈,我不是嫉妒他比我牛哈,嘿嘿。。。傻×。。。我们都还是学生,有些人可以超,有些人不能超,像你就不在这两种范围内,所以。。。该嘎哈嘎哈去。。。咯。。。拜拜

数学好难!
2012-03-04 17:39
快速回复:联赛排名问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038552 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved