设计一个分数类,类名Fraction,要实现处理数学上分数功能? 定义一个分数类
- C++【类与对象】设计并实现分数类(1) Fraction 分数类的定义和显示
- c++中如何用类模板实现分数的输出以及加减乘除
- c语言程序中怎么打出分数
- JAVA简单加急:从键盘接收三门课分数,计算三门课的平均分和总成绩,编写成绩计算类实现功能
C++【类与对象】设计并实现分数类(1) Fraction 分数类的定义和显示
#include <iostream.h>
#include <cmath>
class Fraction {
private :
int rator;
int nator;
static int MaxFactor(int a, int b) { // 最大公因数
if(a == 0 || b == 0) return 1;
if(abs(a) > abs(b)) { int t = b; b = a; a = t; }
a = abs(a);
b = abs(b);
for(int i = a; i <= b; --i) {
if(a%i == 0 && b%i == 0)
return i;
}
return 1;
}
public :
Fraction(int rator = 0,int nator = 1) {
this->rator = rator; this->nator = nator;
if(this->nator < 0) {
this->rator *= -1;
this->nator *= -1;
}
}
friend Fraction operator+(const Fraction &fa, const Fraction &fb) {
Fraction ft;
ft.rator = fa.rator * fb.nator + fa.nator * fb.rator;
ft.nator = fa.nator * fb.nator;
int tmp = MaxFactor(ft.rator,ft.nator);
ft.rator /= tmp;
ft.nator /= tmp;
return ft;
}
friend Fraction operator-(const Fraction &fa, const Fraction &fb) {
Fraction ft;
ft.rator = fa.rator * fb.nator - fa.nator * fb.rator;
ft.nator = fa.nator * fb.nator;
int tmp = MaxFactor(ft.rator,ft.nator);
ft.rator /= tmp;
ft.nator /= tmp;
if(ft.nator < 0) {
ft.rator *= -1;
ft.nator *= -1;
}
return ft;
}
friend Fraction operator*(const Fraction &fa, const Fraction &fb) {
Fraction ft;
ft.rator = fa.rator * fb.rator;
ft.nator = fa.nator * fb.nator;
int tmp = MaxFactor(ft.rator,ft.nator);
ft.rator /= tmp;
ft.nator /= tmp;
return ft;
}
friend Fraction operator/(const Fraction &fa, const Fraction &fb) {
Fraction ft;
ft.rator = fa.rator * fb.nator;
ft.nator = fa.nator * fb.rator;
int tmp = MaxFactor(ft.rator,ft.nator);
ft.rator /= tmp;
ft.nator /= tmp;
if(ft.nator < 0) {
ft.rator *= -1;
ft.nator *= -1;
}
return ft;
}
Fraction &operator++() { // 加一个分数单位
++rator;
int tmp = MaxFactor(rator,nator);
rator /= tmp;
nator /= tmp;
return *this;
}
Fraction &operator--() { // 减一个分数单位
--rator;
int tmp = MaxFactor(rator,nator);
rator /= tmp;
nator /= tmp;
return *this;
}
void Show() const {
if(nator == 1 || rator == 0) cout << rator << endl;
else cout << rator << '/' << nator << endl;
}
};
int main() {
Fraction a(1),b(2,-3),c;
a.Show();
b.Show();
c = a + b;
c.Show();
++c;
c.Show();
--c;
c.Show();
--c;
c.Show();
return 0;
}
c++中如何用类模板实现分数的输出以及加减乘除
//类的头文件fclass.h
class fraction
{
private:
int nr; //分子
int dn; //分母
public:
fraction input();
fraction operator +(fraction &);
fraction operator -(fraction &);
fraction operator *(fraction &);
fraction operator /(fraction &);
void output(); //输出约分后的分数
void output1(); // 输出普通分数
};
//实现文件fclass.cpp
#include<iostream.h>
#include"fclass.h"
fraction fraction::operator+(fraction &a)
{
fraction g;
g.dn=dn*a.dn;
g.nr=nr*a.dn+a.nr*dn;
return g;
}
fraction fraction::operator-(fraction &a)
{
fraction g;
g.dn=dn*a.dn;
g.nr=nr*a.dn-a.nr*dn;
return g;
}
fraction fraction::operator*(fraction &a)
{
fraction g;
g.dn=dn*a.dn;
g.nr=nr*a.nr;
return g;
}
fraction fraction::operator/(fraction &a)
{
fraction g;
g.dn=dn*a.nr;
g.nr=nr*a.dn;
return g;
}
fraction fraction::input()
{
fraction a;
cin>>a.nr>>a.dn;
return a;
}
void fraction::output()
{
int i;
if(dn==0)
cout<<"error"<<endl;
else
if(nr%dn==0)
cout<<nr/dn<<endl;
else
{
for(i=2;i<=nr||i<=dn;)
{
if(nr%i==0&&dn%i==0)
{
nr=nr/i;
dn=dn/i;
i=2;
}
else
i++;
}
cout<<nr <<"/"<<dn<<endl;
}
}
void fraction::output1()
{
cout<<nr<<"/"<<dn;
}
//主程序
#include<iostream.h>
#include"fclass.h"
void menu()
{
cout<<"=================================================="<<endl;
cout<<"1-输入2个新的分数,可进行如下运算:"<<endl;
cout<<"2-加法"<<endl;
cout<<"3-减法"<<endl;
cout<<"4-乘法"<<endl;
cout<<"5-除法"<<endl;
cout<<"6-退出"<<endl;
cout<<"=================================================="<<endl;
}
void main()
{
void menu();
int m;
bool flag=true;
fraction s1,s2,s3;
while(flag)
{ menu();
cin>>m;
switch(m)
{
case 1:
cout<<"请按照如下方式输入2个分数: 分子/分母"<<endl;
s1=s1.input();
s2=s2.input();
cout<<"请输入你要进行的计算:"<<endl;
break;
case 2:
s3=s1+s2;
s1.output1();
cout<<" + ";
s2.output1();
cout<<"=";
s3.output();
break;
case 3:
s3=s1-s2;
s1.output1();
cout<<" - ";
s2.output1();
cout<<"=";
s3.output();
break;
case 4:
s3=s1*s2;
s1.output1();
cout<<" * ";
s2.output1();
cout<<"=";
s3.output();
break;
case 5:
s3=s1/s2;
s1.output1();
cout<<" / ";
s2.output1();
cout<<"=";
s3.output();
break;
case 6:
flag=false;
break;
default:
break;
}
}
}
c语言程序中怎么打出分数
printf("3/5");
如果你是想把0.6输出成3/5这样,那我就不知道了
JAVA简单加急:从键盘接收三门课分数,计算三门课的平均分和总成绩,编写成绩计算类实现功能
import java.util.Scanner;
public class ScoreCale {
public static void main(String[] args) {
ScoreCale sc = new ScoreCale();
System.out.println("总成绩是:" + sc.getTotalScores());
System.out.println("平均成绩是:" + sc.getAvgScores());
}
private double javaScore;
private double csharpeScore;
private double dbScore;
public ScoreCale() {
input();
}
public void input() {
Scanner s = new Scanner(System.in);
System.out.print("请输入Java的分数:");
javaScore = s.nextDouble();
System.out.print("请输入C#的分数:");
csharpeScore = s.nextDouble();
System.out.print("请输入db的分数:");
dbScore = s.nextDouble();
}
public double getTotalScores() {
return javaScore + csharpeScore + dbScore;
}
public double getAvgScores() {
return (javaScore + csharpeScore + dbScore) / 3;
}
}
//你就自己再建一个测试类,把main方法拷贝过去就可以了