| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5962 人关注过本帖
标题:Fraction Program...too many functions?分数程序…太多的函数呢?
只看楼主 加入收藏
点线面
Rank: 8Rank: 8
来 自:NO.-1
等 级:蝙蝠侠
帖 子:525
专家分:980
注 册:2011-1-3
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:1 
Fraction Program...too many functions?分数程序…太多的函数呢?
Hi all. I have been working on a program to add fractions.
嗨.我己经设计有添加分数的程序
 I want to eventually +, -, *, and / fractions.
我想弄最后有+,-,*,/分数.
 When I run it all I can do is enter two fractions and nothing else happens.
就是可以运行输入两个数的分数,没有其它事。
 I think I'm messing up with maybe calling too many functions inside each other.
我想调用太多函数在其它地方,搞到很乱
 It feels like I'm messing up with that because I end up with no final n1 and d1 for displayFraction(int n, int d).
觉得弄得乱原因是没有决定displayFraction(int n, int d)的n1和d1结束
If you have the time I would appreciate any and all help. :)
如果你有时间帮助我很感激
Here is the problem:
以下就是问题
Write a function that reads a problem involving two common fractions (such as 2/4 + 5/6).
写包含两个分数的一个函数
 After reading, call a function to perform the indicated operation (just adding right now).
 Pass the numerator and denominator of both fractions to the func that performs the operation (should return num and denom of result through its output parameters). Then display the result as a common fraction.

I have these guidelines:

--foundation: a = bq + r

--1. find gcd of num & denom
2. divid num & denom by gcd
3. use the lcm

--1. divide lcm by denom (multiplier)
2. multiply num & denom by multiplier
3. add num storing in num accumulator
4. copy lcm into denom of accumulator

Code:
--There are specific questions included as comments

 #include<iostream>
#include<cmath>
using namespace std;
void getFraction(int&, int&);
void readFracProblem(int&, int&, int&, int&, char&);    //For later use with +,-,*,/
void add(int&, int&, int, int, int);    //I added the last "int" for my program
int gcd(int, int);
int lcm(int, int, int);        //I added the last "int" for program
void normalizeFraction(int&, int&);
void displayFraction(int, int);
int main(){
    int n1, d1, n2, d2, gcdNumber;

    cout << "Enter a fraction (n / d): ";
    getFraction(n1, d1);
    cout << "Enter a fraction (n / d): ";
    getFraction(n2, d2);

    gcdNumber = gcd(d1, d2);

    add(n1, d1, n2, d2, gcdNumber);
    displayFraction(n1, d1);

    return 0;
}

void getFraction(int& n, int& d){
    char slash;            //Should it be "char = slash;" ?
    cin >> n >> slash >> d;
}

int lcm(int accD, int d, int gcd){
    int lcm;
    lcm = (accD * d) / gcd;
    return lcm;
}

void add(int& accN, int& accD, int n, int d, int gcd){
    int lcmNumber = lcm(accD, d, gcd);    //*I'm not sure where these should go
    int multiplier = lcmNumber / accD;    //*
    accN = accN * multiplier;        //*
    multiplier = lcmNumber / d;        //*
    n = n * multiplier;            //*
    accN = accN + n;            //*
    accD = lcmNumber;            //*I'm not sure where these should go
}

int gcd(int a, int b){
    int remainder, gcdNumber;
    do{
        remainder = a % b;
        gcdNumber = a / b;
    }while(remainder != 0);

    return gcdNumber;
}

void normalizeFraction(int& n, int& d){
    if(d < 0){
        d = -d;
        n = -n;
    }
    int absN = abs(n);
    int gcdNumber = gcd(absN, d);
    n = n / gcdNumber;
    d = d / gcdNumber;
}

void displayFraction(int n, int d){
    normalizeFraction(n, d);        //Does this belong here?
    cout << n << " / " << d << endl;
}
 


I'm still a beginner so.... Thank you for any help. :)
Last edited on Jan 17, 2011 at 10:21am  
Duoas (4712)     Jan 17, 2011 at 9:14pm
It looks to me like you have a pretty good handle on things.
The whole point of functions is to make them useful to do things you would otherwise do repeatedly throughout your code. You are making pretty good use of them.

Don't have external code do stuff your function could, though. For example, you have to calculate the GCD to use your add() function. Don't do that. The add() function should calculate the GCD itself -- it shouldn't need help from the caller to do its job. Likewise with lcm().

I would, however, use a struct to represent a fraction:

 struct fraction
  {
  int n, d;
  fraction( int n = 0, int d = 1 ): n( n ), d( d ) { }
  };
 


Now all your functions should take and return a fraction.

 int gcd( int a, int b );
int lcm( int a, int b );

fraction getFraction();
fraction addFraction( const fraction& a, const fraction& b );
fraction normalizeFraction( const fraction& a );
void displayFraction( const fraction& a );
 
Argh, I just reread your problem statement... Your professor requires you to use individual numerator and denominator arguments with your functions... Alas. Well, good job! Just fix the extra stuff....
 
hottiefee (5)     Jan 18, 2011 at 8:12am
Alright thanks. I'm still working on the gcd(), lcm(), and add() functions. I hope to figure this out soon! :) If you have any other ideas just let me know.  
 
Disch (4231)     Jan 18, 2011 at 8:16am
Argh, I just reread your problem statement... Your professor requires you to use individual numerator and denominator arguments with your functions... Alas


*continues to be amazed at what is taught in programming courses*
 
wolfgang (102)     Jan 18, 2011 at 8:23am
Its funny to me because my professor used fractions to teach structures then classes (with the methods being part of the classes). That was only the intro to classes. Makes me want to go program a class for fractions now.
 
hottiefee (5)     Jan 18, 2011 at 8:43am
Yeah, I'm still new at this. Does my add() function look right? Thats all im confused on now. I just dont see how I have no final answer for my displayFraction() function...
 
hottiefee (5)     Jan 18, 2011 at 9:43am
Okay I changed the gcd() function to:
 int gcd(int a, int b){
    if (b = 0)
        return a;
    else
        return gcd(b, a%b);
}
 


but still nothing happens after the user enters the two fractions. Nothing else is displayed and I don't understand why... :(
 
hottiefee (5)     Jan 18, 2011 at 9:52am
I figured it out.

 Okay I changed the gcd() function to:
[code]
int gcd(int a, int b){
    if (b == 0)      //Had to use assignment char instead of =.
        return a;
    else
        return gcd(b, a%b);
}
 


Thanks all for the help :)
Have a great day!
 
wolfgang (102)     Jan 18, 2011 at 10:31am
How I handled add():

 void simplify_fraction(int &num, int &denom)
{
  int i = 2; // Divisor
  int lowest;

  (num > denom) ? (lowest = denom) : (lowest = num);
  while ( i < (lowest / i) )
  {
    if (((num % i) == 0) && ((denom % i) == 0) ) {
      num     /= i;
      denom   /= i;
      lowest  /= i;
    } // Keeps doing it until its no longer divisible by the current i. So if its even it will strip out all the 2's.
    else
      (i < 3) ? (i += 1) : (i += 2); // Logic. Once no longer even, check all odd divsors.
  }

}

void add_fractions(int num1, int denom1, int num2, int denom2, int &resultnum, int &resultdenom)
{
  resultnum = (num1 * denom2) + (num2 * denom1);
  resultdenom = denom1 * denom2;
  simplify_fraction(resultnum, resultdenom);
}
 
 
搜索更多相关主题的帖子: eventually nothing because calling enter 
2011-01-18 14:23
waterstar
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:984
专家分:2810
注 册:2010-2-12
收藏
得分:5 
I can't understand all.Were you implying that two score summarizations into a score?

冰冻三尺,非一日之寒;士别三日,不足刮目相看!
2011-01-18 19:50
快速回复:Fraction Program...too many functions?分数程序…太多的函数呢?
数据加载中...
 
   



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

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