xsomonx 发表于 2013-12-11 20:07:16

explain the utility of each function and their relations.


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;}



maomingkun 发表于 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;
}


函数具体实现的是什么你自己研究研究看看吧
页: [1]
查看完整版本: explain the utility of each function and their relations.