帮忙看一下,为啥结构体里的数值传递不过去,谢谢。
程序要求输入两个整形变量,分别是分子(Numerator)和分母(Denominator),通过void enter(struct Fraction *fraction); void simplify(struct Fraction *fraction); void display(struct Fraction fraction);这三个函数实现输入,简化(2/8变成1/4)和输出。本人调试了很久都只输出原来输入的结果。比如2,8(结果是2/8),4, -16(应该是-1/4, 输出结果是4/-16)。 下面是三个文件(无需main.h即可编译),请大家指点一下,谢谢。// w2.cpp
#include <iostream>
using namespace std;
#include "Fraction.h"
int main() {
struct Fraction fraction;
cout << "Fraction Simplifier" << endl;
cout << "===================" << endl;
enter(&fraction);
simplify(&fraction);
display(fraction);
}
//Fraction.h
struct Fraction{
int num;
int den;
};
void enter(struct Fraction *fraction);
void simplify(struct Fraction *fraction);
void display(struct Fraction fraction);
//Fraction.cpp
#include <iostream>
using namespace std;
#include "Fraction.h"
void enter(struct Fraction *fraction)
{
cout << "Numerator: "<< endl;
cin>>fraction->num;
cout << "Denominator: "<< endl;
cin>>fraction->den;
if (fraction->den==0)
{
cout << "Denominator (not zero): "<< endl;
cin>>fraction->den;
}
}
void simplify(struct Fraction* fraction)
{
int i;
if ((fraction->num>0 && fraction->den>0) || (fraction->num>0 && fraction->den<0)) //ex. 2/4 or 2/-4
{
for (i=fraction->num; i>0; i--)
{
if (fraction->den>0)
{
if (fraction->num%i==0 && fraction->den%i==0)
{
fraction->num==fraction->num/i;
fraction->den==fraction->den/i;
}
}
else
{
if (fraction->num%i==0 && fraction->den%i==0)
{
fraction->num==-1*(fraction->num/i);
fraction->den==-1*(fraction->den/i);
}
}
}
}
else if ((fraction->num<0 && fraction->den>0) || (fraction->num<0 && fraction->den<0))//ex. -2/4 or -2/-4
{
for (i=fraction->num; i<0; i++)
{
if (fraction->den<0)
{
if (fraction->num%i==0 && fraction->den%i==0)
{
fraction->num==fraction->num/i;
fraction->den==fraction->den/i;
}
}
else
{
if (fraction->num%i==0 && fraction->den%i==0)
{
fraction->num==-1*(fraction->num/i);
fraction->den==-1*(fraction->den/i);
}
}
}
}
}
void display(struct Fraction fraction)
{
cout<<endl;
if (fraction.num==0)
cout<<"0"<<endl;
else
cout<<fraction.num<<"/"<<fraction.den<<endl;
}