| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 732 人关注过本帖
标题:新手问一个问题。c++子类的使用。出现问题
只看楼主 加入收藏
董世
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2012-11-26
结帖率:33.33%
收藏
 问题点数:0 回复次数:2 
新手问一个问题。c++子类的使用。出现问题
这是假分数分数换成带分数。

要求就是要有一个ifraction子类,然后继承Fraction里的东西,还要有一个convertF来把假分数换算成带分数。我这个程序没有运行,我知道我在设计ifraction和convert那里有问题,我的引用应该是有问题,可能不能访问,这里是两个分数加法,然后显示假分数,带分数。
程序代码:
#include<iostream>
using namespace std;

 
class Fraction   // define a class called Fraction
{
public:
void settop(int tt);                // convert the variable 
void setbottom(int bb);
void fractionprint(int t,int b);     //define main operator function add sub mul and div  
Fraction F_add(Fraction & A);
    void simple();                      // output and 化简(do not know how to write in English) function 
void print();
protected:
int b;
int t;
};
class iFraction: protected Fraction   // 定义ifraction//
{
friend void convertF();                   //友元类的convertf//
public:
void printmixed();                          //这个是输出的带分数的//
private:
int m;
int mt;
int mb;
}
void convertF()                               //我觉得这里肯定有问题//
{
m=t/b;
mt=t%b;
mb=b;
}
void iFraction::printmixed();          //这个是输出带分数的程序,好像也有问题//
{
if(b==1||t==0)
cout<<t<<endl;
else
convert()
cout<<m<<mt<<mb<<endl;
}
Fraction Fraction::F_add(Fraction &A)
{
if(A.b==b)
{
t=A.t+t;
}
else
{
t=t*A.b+A.t*b;
b=A.b*b;
}
simple();
cout<< "The sum of it is:";
print();
printmixed();
return A;
}
void Fraction::simple()
{
int i;
for(i=2;t>=i;i++)
{
if(b%i==0&&t%i==0)
{
b=b/i;
t=t/i;
i--;
}
}
}
void Fraction::print()
{
if(b==1||t==0)
cout<<t<<endl;
else
cout<<t<<"/"<<b<<endl;
}
int main()
{
int t1,t2,b1,b2,c;// declare new varibles 
char ch;         
Fraction A,B,s;   //declare pointer
cout<<" Enter numerator and denominator of 1st fraction:";   // input the number
cin>>t1>>b1;
cout<<" Enter numerator and denominator of 2nd fraction:";
cin>>t2>>b2;
s.fractionprint(t1,b1);
s.fractionprint(t2,b2);
cout<<endl;
A.settop(t1);
B.settop(t2);
A.setbottom(b1);
B.setbottom(b2);
    A.F_add(B);
}


[ 本帖最后由 董世 于 2013-4-29 16:40 编辑 ]
搜索更多相关主题的帖子: 假分数 带分数 convert 
2013-04-29 16:39
董世
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2012-11-26
收藏
得分:0 
求大神帮忙啊
2013-04-30 09:36
董世
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2012-11-26
收藏
得分:0 
这个可以编译出来,但是我不明白出来的是乱码
2013-05-03 20:32
快速回复:新手问一个问题。c++子类的使用。出现问题
数据加载中...
 
   



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

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