| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 18521 人关注过本帖
标题:C语言 如何输出一个分数?
只看楼主 加入收藏
yyqd2013
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:35
专家分:154
注 册:2013-5-8
收藏
得分:2 
大致如下:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

typedef struct
{
        long long int   molecular;
        long long int   denominator;
}FRACTION;

int change_to_proper_fraction(FRACTION *fraction);
void fraction_add(FRACTION *frac_sum, FRACTION *frac_num);

main()
{
        FRACTION frac[4];
        int ret = 0;
        long long int quo = 0;
        frac[0].molecular = 1;
        frac[0].denominator = 2;
        frac[1].molecular = 1;
        frac[1].denominator = 3;
        frac[2].molecular = 1;
        frac[2].denominator = 6;
        frac[3].molecular = 0;
        frac[3].denominator = 0;

        if (change_to_proper_fraction(&frac[0])<0 || change_to_proper_fraction(&frac[1])<0
                || change_to_proper_fraction(&frac[2])<0)
        {
                printf("please check the fraction[3]!!!\n");
        }
        else
        {
                fraction_add(&frac[3], &frac[0]);
                fraction_add(&frac[3], &frac[1]);
                fraction_add(&frac[3], &frac[2]);
                if (frac[3].molecular%frac[3].denominator == 0)
                {
                        printf("%lld/%lld+%lld/%lld+%lld/%lld=%lld\n", frac[0].molecular, frac[0].denominator,
                                frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular/frac[3].denominator);
                }
                else
                {
                        printf("%lld/%lld+%lld/%lld+%lld/%lld=%lld/%lld\n", frac[0].molecular, frac[0].denominator,
                                frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular, frac[3].denominator);
                }
        }
        exit(1);
}

int change_to_proper_fraction(FRACTION *fraction)
{
        if (fraction->denominator == 0)
                return -1;

        if (fraction->molecular == 0)
        {
                fraction->denominator = 0;
                return 0;
        }

        if ((fraction->molecular%fraction->denominator) == 0)
        {
                fraction->molecular = fraction->molecular/fraction->denominator;
                fraction->denominator = 1;
                return 0;
        }

        long long int diff = 0;
        long long int larger = 0;
        long long int smaller = 0;
        long long int mole_posi = 0;
        long long int deno_posi = 0;
        mole_posi = fraction->molecular > 0 ? fraction->molecular : (0 - fraction->molecular);
        deno_posi = fraction->denominator > 0 ? fraction->denominator : (0 - fraction->denominator);
        smaller = mole_posi<deno_posi ? mole_posi : deno_posi;
        diff = deno_posi + mole_posi - 2 * smaller;
        larger = diff>smaller ? diff : smaller;
        smaller = diff + smaller - larger;
        while (larger%smaller != 0)
        {
                diff = larger - smaller;
                larger = diff>smaller ? diff : smaller;
                smaller = diff + smaller - larger;
        }

        fraction->molecular = fraction->molecular/smaller;
        fraction->denominator  = fraction->denominator/smaller;
        return 0;
}

void fraction_add(FRACTION *frac_sum, FRACTION *frac_num)
{
        if (frac_sum->molecular == 0)
        {
                frac_sum->molecular = frac_num->molecular;
                frac_sum->denominator  = frac_num->denominator;
                return;
        }
        else if (frac_num->molecular == 0)
        {
                return;
        }
        else
        {
                frac_sum->molecular = frac_sum->molecular*frac_num->denominator + frac_num->molecular*frac_sum->denominator;
                frac_sum->denominator = frac_sum->denominator * frac_num->denominator;
                change_to_proper_fraction(frac_sum);
                return;
        }
}

[ 本帖最后由 yyqd2013 于 2013-5-10 00:30 编辑 ]
2013-05-10 00:25
小男孩的围裙
Rank: 2
等 级:论坛游民
帖 子:15
专家分:32
注 册:2013-4-24
收藏
得分:2 
定义字符串类型char输出,要不就直接printf输出。因为“/”在C语言中表示的是一个字符。
2013-05-10 01:30
梅可伟梅可伟
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:69
专家分:180
注 册:2013-3-11
收藏
得分:2 
如果要得到精确的数,只有把分数化为整数,即每个数乘以所有数分母的最小公倍数,然后把这些全部加起来之后再除以以前所有分母的最小公倍数
即(1+2+3)/6;
2013-05-10 11:44
ustc
Rank: 2
等 级:论坛游民
帖 子:19
专家分:17
注 册:2013-5-7
收藏
得分:2 
看样子lz不是很懂float double。。。
2013-05-10 11:53
yctchxf
Rank: 6Rank: 6
来 自:盐城
等 级:侠之大者
威 望:2
帖 子:176
专家分:454
注 册:2012-4-10
收藏
得分:2 
来学习
2013-05-10 12:09
快速回复:C语言 如何输出一个分数?
数据加载中...
 
   



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

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