| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 474 人关注过本帖, 1 人收藏
标题:使用class编译一个分数计算器的问题
只看楼主 加入收藏
ws01274696
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-3-23
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:3 
使用class编译一个分数计算器的问题
为何最后屏幕上无法显示计算结果?是class中constructor运用错误的问题吗?请详示,谢谢

#include <iostream>
using namespace std;

class Fraction
{
private:
    int top;       //Numerator
    int bottom;    //Denominator
    Fraction F1();
    Fraction F2();
public:
    Fraction()  //default constructor
    {   
        top = 0;
        bottom = 1;
    }
    Fraction(int T, int B) //constructor with both numerator and denominator
    {
        top = T;
        if (B==0)
        {
            cout<<"Invalid number"<<endl;
            exit(0);
        }
        else
            bottom = B;
    }

    Fraction add(Fraction F1, Fraction F2) //peform addition
    {
        int T = F1.top*F2.bottom+F1.bottom*F2.top;
        int B = F1.bottom*F2.bottom;
        return Fraction( T, B);
    }
    Fraction subtract(Fraction F1, Fraction F2) //perform subtraction
    {
        int T = F1.top*F2.bottom-F1.bottom*F2.top;
        int B = F1.bottom*F2.bottom;
        return Fraction( T, B);
    }
    Fraction multiple(Fraction F1, Fraction F2) //perform multiplication
    {
        int T = F1.top*F2.top;
        int B = F1.bottom*F2.bottom;
        return Fraction( T, B);
    }
   Fraction divide(Fraction F1, Fraction F2)  //perform division
   {
       int T = F1.top*F2.bottom;
       int B = F1.bottom*F2.top;
       return Fraction( T, B);
   }
    void compare(Fraction F1, Fraction F2) //perform comparasion
    {
        if (F1.top*F2.bottom<F2.top*F1.bottom)
            cout<<"The first fraction is smaller"<<endl;
        if (F1.top*F2.bottom==F2.top*F1.bottom)
            cout<<"The two fractions are equal"<<endl;
        if (F1.top*F2.bottom>F2.top*F1.bottom)
            cout<<"The first fraction is greater"<<endl;
    }
    void input() //Input function
    {
        cout<<"Please enter the numerator:"<<endl;
        cin>>top;
        cout<<"Please enter the denominator:"<<endl;
        cin>>bottom;
    }
    void output() //Output function
    {
        if (bottom == 1)
            cout<< top << endl;
        else
            cout << top << "/" << bottom <<endl;
    }
};


int main()
{
  Fraction result;
  Fraction a(1,2);
  Fraction b(2,3);
  int choice;
  cout<<"Please enter two fractions"<<endl;
  a.input();
  b.input();
  cout<<"*******************************"<<endl;
  cout<<"* Chose one number to continue*"<<endl;
  cout<<"* 1.Add                       *"<<endl;
  cout<<"* 2.Subtract                  *"<<endl;
  cout<<"* 3.Multiply                  *"<<endl;
  cout<<"* 4.Division                  *"<<endl;
  cout<<"*                    *"<<endl;
  cout<<"* other keys to exit          *"<<endl;
  cout<<"*******************************"<<endl;
  cin>>choice;
  switch(choice)
  {
      case '1':
          result.add(a,b);
          result.output();
          break;
      case '2':
          result.subtract(a,b);
          result.output();
          break;
      case '3':
          result.multiple(a,b);
          result.output();
          break;
      case '4':
          result.divide(a,b);
          result.output();
          break;
      case '5':
          (a,b);
          break;
      default:
          break;
  }
  return 0;
}



搜索更多相关主题的帖子: namespace default private include 
2013-03-23 12:57
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:10 
不错的说,思路,编程习惯都很好。有问题你debug没,能写出来这种程序,好好调试下应该能找出来的。楼主加油

同学习......同进步....你帮我......我帮你.....上善若水.....
2013-03-23 22:29
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
收藏
得分:10 
switch_case语句有问题,你把12345加了单引号

小菜小菜
2013-03-24 09:29
gwcome
Rank: 2
来 自:尼玛星球
等 级:论坛游民
帖 子:43
专家分:33
注 册:2013-1-22
收藏
得分:0 
楼主思路还有一点小问题自己debug看看就知道了

小菜小菜
2013-03-24 09:32
快速回复:使用class编译一个分数计算器的问题
数据加载中...
 
   



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

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