再讀C++ Primer 又熟悉了一下C++基本文法 ,感覺C++ Primer的确是一本好書。是以寫了個分數的運算類
#pragma once
// #ifndef myProcess
// #define myProcess
#include " FixedFractions.h "
class Process
... {
public:
Process(void);
~Process(void);
double ProAdd(FixedFractions f1,FixedFractions f2);
double ProMultiply(FixedFractions f1,FixedFractions f2);
} ;
// #endif
#pragma once
// #ifndef myFixedFractions
// #define myFixedFractions
class FixedFractions
... {
public:
FixedFractions(void);
~FixedFractions(void);
FixedFractions(int i,int I1,int I2);
public:
int I;//整數部分
int i1;//分母
int i2;//分子
} ;
#include " StdAfx.h "
#include " .process.h "
double Add( const FixedFractions & f1, const FixedFractions & f2)
... {
double temp=0;
double temp1=0;
double temp2=0;
temp1=f1.I*f1.i1+f1.i2;
temp2=f2.I*f2.i1+f2.i2;
if(f1.i1==f2.i1)
...{
temp=(temp1+temp2)/f1.i1;
return temp;
}
else
...{
int a=f1.i1*f2.i1;
temp=(temp1*f2.i1+temp2*f1.i1)/a;
return temp;
}
}
double Multiply( const FixedFractions & f1, const FixedFractions & f2)
... {
double temp=0;
double temp1=0;
double temp2=0;
temp1=f1.I*f1.i1+f1.i2;
temp2=f2.I*f2.i1+f2.i2;
temp=(temp1*temp2)/(f1.i1*f2.i1);
return temp;
}
Process::Process( void )
... {
}
Process:: ~ Process( void )
... {
}
double Process::ProAdd(FixedFractions f1,FixedFractions f2)
... {
double d=Add(f1,f2);
// Add(f1,f2);
// return 0;
return d;
}
double Process::ProMultiply(FixedFractions f3,FixedFractions f4)
... {
double d=Multiply(f3,f4);
// Multiply(f1,f2);
// return 0;
return d;
}
#include " StdAfx.h "
#include " . ixedfractions.h "
FixedFractions::FixedFractions( void )
... {
I=0;
i1=0;
i2=0;
}
FixedFractions:: ~ FixedFractions( void )
... {
}
FixedFractions::FixedFractions( int i, int I1, int I2)
... {
I=i;
i1=I1;
i2=I2;
}
// class080114.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
// #include "FixedFractions.h"
#include " Process.h "
#include " iostream "
using namespace std;
int _tmain( int argc, _TCHAR * argv[])
... {
FixedFractions fi1(1,2,1);
FixedFractions fi2(1,2,1);
Process* pr=new Process();
cout<<pr->ProAdd(fi1,fi2)<<endl;
cout<<pr->ProMultiply(fi1,fi2)<<endl;
return 0;
}