C 中使用结构减少有理数的函数

发布于 2024-12-22 11:18:29 字数 865 浏览 2 评论 0原文

#define TRUE 1

#define FALSE 0

struct rational{         
    int numerator;    
    int denominator;    
};       
void reduce(struct rational *inrat,struct rational *outrat)    
{    
    int a,b,rem;        
    if(inrat->numerator>inrat->denominator)      
    {   
        a=inrat->numerator;
        b=inrat->denominator;    
    }        
    else    
    {    
        b=inrat->numerator;
        a=inrat->denominator;
    }
    while(b!=0)
    {
        rem=a%b;
        a=b;
        b=rem;
    }
    outrat->numerator/=a;      
    outrat->denominator/=a;     
}  

好的,这是一个减少有理数的代码。它基于欧几里得算法。
我的问题是,如果所有数据都通过 intrat 存储在变量中(当然在主函数中),那么指向结构 outrat 的指针有什么用呢? 为什么使用了语句outrat->numerator/=a; outrat->分母/=a; 如果实际值是通过intrat指针操作的,为什么要使用outrat

#define TRUE 1

#define FALSE 0

struct rational{         
    int numerator;    
    int denominator;    
};       
void reduce(struct rational *inrat,struct rational *outrat)    
{    
    int a,b,rem;        
    if(inrat->numerator>inrat->denominator)      
    {   
        a=inrat->numerator;
        b=inrat->denominator;    
    }        
    else    
    {    
        b=inrat->numerator;
        a=inrat->denominator;
    }
    while(b!=0)
    {
        rem=a%b;
        a=b;
        b=rem;
    }
    outrat->numerator/=a;      
    outrat->denominator/=a;     
}  

Ok,this a code to reduce a rational number. It's based on Euclid's algorithm.
My question is what's the use of the pointer to the structure outrat ,if all the data is stored in the variables through intrat (in the main function, of course).
Why have used the statement outrat->numerator/=a; outrat->denominator/=a;
if the actual values are manipulated through intrat pointer, why is outrat used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2024-12-29 11:18:29

我对算法不太了解,但是看代码,inrat是输入,outrat是输出有理数。

这些值通过inrat进行操作。 inrat 的值用于计算除数,然后将其应用于 outrat。因此,实际上被操纵的是 outrat 的值。

实际上,为了清楚起见,该函数应该使用 inrat 声明为 const

I don't know much about the algorithm, but looking at the code, inrat is the input and outrat is the output rational number.

The values are not manipulated through inrat. inrat's values are used to calculate a divisor, which is then applied to outrat. So it's outrat's values that are actually manipulated.

Really the function should have been declared with inrat as const for clarity.

彩虹直至黑白 2024-12-29 11:18:29

这是减少有理数的 C++ 代码,在 C 中尝试相同的逻辑:

#include<iostream>
using namespace std;
struct rational {
       int numerator;
       int denominator;
};
/*struct rational *inputrational  : Actual rational number to be reduced
struct rational *outputrational : Variable to store the rational number in its lowest form */
void reduce(struct rational *inputrational, struct rational *outputrational) {
    //Write your solution code below this line
    int n,m,i=2,min;
     n=inputrational->numerator;
     m=inputrational->denominator;
     min=n>m?m:n;
     for(i=m;i>0;i--){
          if(n%i==0 && m%i==0){
             n=n/i;
            m=m/i;
            break;
            }
        }

    outputrational->numerator=n;
    outputrational->denominator=m;


  }

int main(){
    rational n1,s1;
    n1.numerator=13;
    n1.denominator=6;
    reduce(&n1,&s1);
     cout<<s1.numerator<<" "<<s1.denominator;
}

Here is the the C++ code for reducing rational number, try same logic in C:

#include<iostream>
using namespace std;
struct rational {
       int numerator;
       int denominator;
};
/*struct rational *inputrational  : Actual rational number to be reduced
struct rational *outputrational : Variable to store the rational number in its lowest form */
void reduce(struct rational *inputrational, struct rational *outputrational) {
    //Write your solution code below this line
    int n,m,i=2,min;
     n=inputrational->numerator;
     m=inputrational->denominator;
     min=n>m?m:n;
     for(i=m;i>0;i--){
          if(n%i==0 && m%i==0){
             n=n/i;
            m=m/i;
            break;
            }
        }

    outputrational->numerator=n;
    outputrational->denominator=m;


  }

int main(){
    rational n1,s1;
    n1.numerator=13;
    n1.denominator=6;
    reduce(&n1,&s1);
     cout<<s1.numerator<<" "<<s1.denominator;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文