- 一个表示分数的数据类型,它能以2/3这样的格式输出或输入,能完成分数的加、减、乘、除等运算。
一个Fraction分数须满足以下两个条件:
①分母永远为正,分数的符号用分子表示;
②分子分母互质,即总表示为最简分数。
说明:
数据成员: fraction通过两个私有数据成员int 型num和den分别保存分子和分母;
成员函数:
(a)构造函数Fraction(int n=0,int d=1);
(b) Standardize函数进行标准化处理,例如:2/4 = 1/2 , 2/-3 = -2/3 ;
(c)GCD是求两个整数的最大公约数的函数,standardize在化简分数时要调用它;
(d)void Set(int,int); 为分数的分子、分母赋值;
(e)void Display(); 输出分数,如输出1/2 ;
(f)Fraction Add(Fraction x) 求当前对象和x的和;
(g)Fraction Sub(Fraction) 求当前对象和x的差;
(h)Fraction Mul(Fraction) 求当前对象和x的积;
(i)Fraction Div(Fraction) 求当前对象和x的商,注意x不能为0;
(j)bool BigOrEqual(Fraction) 比较当前对象和x的大小;
在main函数中,创建至少3个分数,测试加减乘除比较等功能。
#pragma once
#ifndef FRACTION_H
#define FRACTION_H
class Fraction {
public:
Fraction(int n = 0, int d = 1);
void Standardize();
int GCD(int a, int b);
void Set(int, int);
void Display();
Fraction Add(Fraction x);
Fraction Sub(Fraction x);
Fraction Mul(Fraction x);
Fraction Div(Fraction x);
bool BigorEqual(Fraction x);
private:
int num, den;
};
#endif
#include"fraction.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
Fraction::Fraction(int n, int d) {
num = n;
den = d;
}
int Fraction::GCD(int a, int b) {
if (b == 0) return a;
return GCD(b, a % b);
}
void Fraction::Standardize() {
int x = GCD(den, num);
x = abs(x);
num /= x;
den /= x;
}
void Fraction::Set(int a, int b) {
num = a;
den = b;
this->Standardize();
}
void Fraction::Display() {
cout << num << "/" << den << endl;
}
Fraction Fraction::Add(Fraction x) {
Fraction y;
y.den = den * x.den;
y.num = num * x.den + den * x.num;
y.Standardize();
return y;
}
Fraction Fraction::Sub(Fraction x) {
Fraction y;
y.den = den * x.den;
y.num = num * x.den - den * x.num;
y.Standardize();
return y;
}
Fraction Fraction::Mul(Fraction x) {
Fraction y;
y.den = den * x.den;
y.num = num * x.num;
y.Standardize();
return y;
}
Fraction Fraction::Div(Fraction x) {
if (x.num) {
Fraction y;
y.num = num * x.den;
y.den = den * x.num;
if (y.den < 0) {
y.num = -y.num;
y.den = -y.den;
}
return y;
}
cout << "The denominator cannot be zero" << endl;
exit(1);
}
bool Fraction::BigorEqual(Fraction x) {
Fraction y = this->Sub(x);
return y.num >= 0;
}
#include"fraction.h"
#include<iostream>
using namespace std;
int main() {
cout << "测试构造函数及输出分数函数" << endl;
Fraction a;
a.Display();
cout << "测试加法,请输入两个分数" << endl;
int n,d;
char ch;
cin >> n >> ch >> d;
a.Set(n, d);
cin >> n >> ch >> d;
Fraction b(n,d);
Fraction c = a.Add(b);
c.Display();
cout << "测试减法,请输入两个分数" << endl;
cin >> n >> ch >> d;
a.Set(n, d);
cin >> n >> ch >> d;
b.Set(n, d);
c = a.Sub(b);
c.Display();
cout << "测试乘法,请输入两个分数" << endl;
cin >> n >> ch >> d;
a.Set(n, d);
cin >> n >> ch >> d;
b.Set(n, d);
c = a.Mul(b);
c.Display();
cout << "测试除法,请输入两个分数" << endl;
cin >> n >> ch >> d;
a.Set(n, d);
cin >> n >> ch >> d;
b.Set(n, d);
c = a.Div(b);
c.Display();
return 0;
}
- 类的组合
设计人类(Person),包含姓名、生日、性别三种数据。其中生日是日期类(Date)的数据。
人类:
private:
姓名
生日 //是日期类的对象
性别
public:
有参构造函数Person(char* name, int year, int month, int day, char gender); //要有输出
析构函数; //要有输出
设置姓名函数void SetName(char* name);
设置生日函数void SetBirthday(int y, int m, int d);
获得姓名函数char* GetName();
获得生日函数Date GetBirthday();
输出信息函数void Show(); //输出人的所有信息
日期类:
private:
年
月
日
public:
有参构造函数 Date(int y, int m, int d); //要有输出
析构函数; //要有输出
设置日期函数SetDate(int y, int m, int d);
显示日期函数 void Show(); //输出日期值
主程序功能简介:
- 用户输入姓名、出生年、月、日、性别的信息
- 构造人类的对象A
- 显示该对象A
- 更改A的姓名
- 更改A的生日
- 重新显示A对象
#pragma once
#ifndef PERSON_H
#define PERSON_H
#include"date.h"
class Person {
public:
Person(char*, int, int, int, char);
~Person();
void SetName(char*);
void SetBirthday(int, int, int);
char* GetName();
Date GetBirthday();
void Show();
private:
char *nam;
Date bir;
char gen;
};
#endif
#pragma once
#ifndef DATE_H
#define DATE_H
class Date {
friend class Person;
public:
Date();
Date(int, int, int);
~Date();
void SetDate(int, int, int);
void Show();
private:
int y, m, d;
};
#endif
#include"person.h"
#include"date.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
void Person::Show() {
cout << "姓名:" << nam << endl;
cout << "出生日期:" << ends;
bir.Show();
cout << "性别" << gen << endl;
}
Person::Person(char* name, int a, int b, int c, char gender) {
cout << "已创建个人信息" << endl;
nam = (char*)malloc(sizeof(char) * 100);
nam = name;
bir.y = a;
bir.m = b;
bir.d = c;
gen = gender;
this->Show();
}
Person::~Person() {
cout << "删除个人信息" << endl;
//this->Show();
}
void Person::SetName(char* name)
{
nam = name;
}
void Person::SetBirthday(int a, int b, int c) {
bir.y = a;
bir.m = b;
bir.d = c;
}
char* Person::GetName() {
return this->nam;
}
Date Person::GetBirthday() {
return this->bir;
}
#include"date.h"
#include<iostream>
using namespace std;
void Date::Show() {
cout << y << "/" << m << "/" << d << endl;
}
Date::Date()
{
y = 0;
m = 0;
d = 0;
}
Date::Date(int a, int b, int c) {
cout << "已创建日期" << endl;
y = a;
m = b;
d = c;
//this->Show();
}
Date::~Date() {
cout << "删除日期" << endl;
//this->Show();
}
void Date::SetDate(int a, int b, int c) {
y = a;
m = b;
d = c;
this->Show();
}
#include"person.h"
#include"date.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
char* a = (char*)malloc(sizeof(char) * 50);
cout << "输入用户姓名" << endl;
cin >> a;
int b, c, d;
cout << "输入出生年月日" << endl;
cin >> b >> c >> d;
char x;
cout << "输入性别(m/f)" << endl;
cin >> x;
Person p1(a, b, c, d, x);
cout << "输入更改用户姓名" << endl;
cin >> a;
p1.SetName(a);
cout << "输入更改用户生日" << endl;
cin >> b >> c >> d;
p1.SetBirthday(b, c, d);
p1.Show();
return 0;
}