鱼C论坛

 找回密码
 立即注册
查看: 2624|回复: 1

explain the utility of each function and their relations.

[复制链接]
发表于 2013-12-11 20:07:16 | 显示全部楼层 |阅读模式
3鱼币

Explain the utility of each function in this program and their relation.

#include "stdafx.h"
#include "stdio.h"
struct fraction  
{
    int numerator;
    int denominator;
};
struct fraction create(int, int);
struct fraction add(struct fraction, struct fraction);
struct fraction multiply(struct fraction, struct fraction);
int _tmain(int argc, _TCHAR* argv[])
{
    struct fraction x,y, z;
    int m, n;
    printf("Numerator of fraction 1: ");
    scanf("%d",&m);
    printf("Denomintor of fraction 1: %d/", m);
    scanf("%d",&n);
    x = create(m, n);
    printf("Numeratorof fraction 2: ");
    scanf("%d",&m);
    printf("Denomintorof fraction 2: %d/", m);
    scanf("%d",&n);
    y = create(m, n);
    z = add(x, y);
    printf("%d/%d + %d/%d =%d/%d\n",
        x.numerator,x.denominator,
        y.numerator,y.denominator,
        z.numerator,z.denominator);
    z = multiply(x, y);
    printf("%d/%d × %d/%d =%d/%d\n",
        x.numerator,x.denominator,
        y.numerator,y.denominator,
        z.numerator,z.denominator);
    return 0;
}
struct fraction create(int x, int y)
{
    struct fraction f;
    f.numerator = x;
    f.denominator = y;
    return f;
}
struct fraction add(struct fraction x, struct fraction y)
{
    int m, n, i;
    m = x.numerator *y.denominator + y.numerator * x.denominator;
    n = x.denominator *y.denominator;
    for (i = m; i >1; i--)
        if (n % i == 0&& m % i == 0)
        {
            m /= i;
            n /= i;
        }
    x.numerator = m;
    x.denominator = n;
    return x;
}
struct fraction multiply(struct fraction x, struct fractiony)
{
    int m, n, i;
    m = x.numerator *y.numerator;
    n = x.denominator *y.denominator;
    for (i = m; i >1; i--)
        if (n % i == 0&& m % i == 0)
        {
            m /= i;
            n /= i;
        }
    x.numerator = m;
    x.denominator = n;
    return x;
}  




想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-12-12 22:38:33 | 显示全部楼层
#include "stdafx.h"
#include "stdio.h"
struct fraction  //自定义一个结构体
{
    int numerator;//声明变量 分子
    int denominator;//声明变量 分母
};
struct fraction create(int, int);
struct fraction add(struct fraction, struct fraction);
struct fraction multiply(struct fraction, struct fraction);
//声明三个结构体
int _tmain(int argc, _TCHAR* argv[])
{
    struct fraction x,y,z;
    int m, n;
    printf("Numerator of fraction 1: ");
    scanf("%d",&m);
    printf("Denomintor of fraction 1: %d/", m);
    scanf("%d",&n);     //接收输入的两个数
    x = create(m, n);//create函数是将输入的两个数付给结构体x.m、x.n
    printf("Numeratorof fraction 2: ");
    scanf("%d",&m);
    printf("Denomintorof fraction 2: %d/", m);
    scanf("%d",&n);
    y = create(m, n);
    z = add(x, y);
    printf("%d/%d + %d/%d =%d/%d\n",
        x.numerator,x.denominator,
        y.numerator,y.denominator,
        z.numerator,z.denominator);
    z = multiply(x, y);
    printf("%d/%d × %d/%d =%d/%d\n",
        x.numerator,x.denominator,
        y.numerator,y.denominator,
        z.numerator,z.denominator);
    return 0;
}
struct fraction create(int x, int y)//定义结构体
{
    struct fraction f;
    f.numerator = x;
    f.denominator = y;
    return f;
}
struct fraction add(struct fraction x, struct fraction y)//定义结构体
{
    int m, n, i;
    m = x.numerator *y.denominator + y.numerator * x.denominator;
    n = x.denominator *y.denominator;
    for (i = m; i >1; i--)
        if (n % i == 0&& m % i == 0)
        {
            m /= i;
            n /= i;
        }
                x.numerator = m;
                x.denominator = n;
                return x;
}
struct fraction multiply(struct fraction x, struct fractiony)//定义结构体
{
    int m, n, i;
    m = x.numerator *y.numerator;
    n = x.denominator *y.denominator;
    for (i = m; i >1; i--)
        if (n % i == 0&& m % i == 0)
        {
            m /= i;
            n /= i;
        }
                x.numerator = m;
                x.denominator = n;
                return x;
}


函数具体实现的是什么你自己研究研究看看吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-23 03:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表