phanso.h
#pragma once
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class phanso
{
private:
int tu,mau;
public:
phanso(void);
~phanso(void);
void settu(int);
void setmau(int);
int gettu();
int getmau();
int rutgon();
void nhapps();
void xuatps();
};
phanso.cpp
#include "phanso.h"
phanso::phanso(void)
{
}
phanso::~phanso(void)
{
}
void phanso::nhapps()
{
char s[100];
int i,j,k,kt,dem;
do{
kt=0;dem=0;k=0;
cout<<"\nnhap phan so dang a/b: ";
fflush(stdin);
gets(s);
for(i=0;i<strlen(s);i++)
if((s[i]>='0' && s[i]<='9') || s[i]=='/')
{
dem++;
if(s[i]=='/') k++;
}
if(dem!=strlen(s) || k!=1)
{
cout<<"\nloi dinh dang! nhap lai!!";
kt=1;
}
}while(kt);
char a[100];
i=0;k=0;
while(s[i]!='/')
a[k++]=s[i++];
a[k]='\0';
tu=atoi(a);
i++;k=0;
while(i<strlen(s)) a[k++]=s[i++];
a[k]='\0';
mau=atoi(a);
}
void phanso::xuatps()
{
cout<<tu<<"/"<<mau;
}
void phanso::settu(int x)
{
tu=x;
}
void phanso::setmau(int x)
{
mau=x;
}
int phanso::gettu()
{
return tu;
}
int phanso::getmau()
{
return mau;
}
int phanso::rutgon()
{
int a=abs(tu),b=abs(mau);
while(a!=b)
if(a>b) a-=b;
else b-=a;
if(a==1) return 0;
tu/=a;
mau/=a;
return 1;
}
mangps.h
#pragma once
#include"phanso.h"
class mangps
{
private:
phanso a[100];
int n;
public:
mangps(void);
~mangps(void);
void nhapmangps();
void xuatmangps();
void tong();
};
mangps.cpp
#include "mangps.h"
mangps::mangps(void)
{
}
mangps::~mangps(void)
{
}
void mangps::nhapmangps()
{
int kt,i=0,dem=0,k;
cout<<"\n *nhap phan so thu "<<i+1<<":";
a[i].nhapps();
i++;
nhap:;
cout<<"\n *nhap phan so thu "<<i+1<<":";
a[i].nhapps();
loop:;
cout<<"\n-----menu-----------";
cout<<"\n| 1. nhap tiep. |";
cout<<"\n| 0. xong. |";
cout<<"\n--------------------";
cout<<"\nlua chon cua ban: ";
cin>>k;
if(k!=0 && k!=1)
{
cout<<"\nkhong hop le!!!";
goto loop;
}
if(k==1)
{
i++;
goto nhap;
}
n=i+1;
}
void mangps::xuatmangps()
{
for(int i=0;i<n;i++)
{
if(i>=1) cout<<" + ";
a[i].xuatps();
}
}
void mangps::tong()
{
int i;
phanso x;
x.settu(a[0].gettu());
x.setmau(a[0].getmau());
for(i=1;i<n;i++)
{
x.settu(x.gettu()*a[i].getmau()+ x.getmau()*a[i].gettu());
x.setmau(x.getmau()*a[i].getmau());
}
xuatmangps();cout<<" = ";x.xuatps();
if(i=x.rutgon())
{
cout<<" = ";x.xuatps();
}
}
source.cpp
#include"mangps.h"
void main()
{
mangps x;
x.nhapmangps();
cout<<"\ntong cac phan so: \n";
x.tong();
system("pause");
}
Không có nhận xét nào:
Đăng nhận xét