您的位置:首页 > 编程语言 > C语言/C++

chapter11test6

2015-06-07 20:53 351 查看
根据题目要求,够早了一个类数组,程序思路很简单,就是重载各种运算符比较费时,我对那6个关系运算符的理解是加减乘,大于,小于,等于;我这里没有用加减乘,其用法和前一个粒子相同,程序贴在下面。

stone.h

#ifndef STONE_H_

#define STONE_H_

class Stone

{

private:
enum { unit = 14 };
int stone;
double fpound;
double pound;

public:
Stone(double p);
Stone(int st, double fp);
Stone();
~Stone();
void set(Stone &s, double n);
Stone operator+(Stone &s);
Stone operator-(Stone &s);
Stone operator*(double n);
bool operator>(Stone &s);
bool operator<(Stone &s);
bool operator=(Stone &s);
friend std::ostream & operator<<(std::ostream &os, const Stone &s);

};

#endif

stone.cpp

#include<iostream>

#include"stone.h"

Stone::Stone(double p)

{
stone = int(p) / unit;
fpound = int(p) % unit + p - int(p);
pound = p;

}

Stone::Stone(int st, double fp)

{
stone = st;
fpound = fp;
pound = st*unit + fp;

}

Stone::Stone()

{
stone = fpound = pound = 0;

}

Stone::~Stone()

{}

void Stone::set(Stone &s, double n)

{
Stone T(n);
s = T;

}

Stone Stone::operator+(Stone &s)

{
stone += s.stone;
fpound = int(fpound + s.fpound) % unit + (fpound + s.fpound) - int(fpound + s.fpound);
pound = pound + s.pound + int(fpound + s.fpound) / unit;
return *this;

}

Stone Stone::operator-(Stone &s)

{
if (pound < s.pound)
std::cout << "First weight is too little.\n";
else
pound = pound - s.pound;
return *this;

}

Stone Stone::operator*(double n)

{
pound = pound*n;
stone = int(pound) / 14;;
fpound = int(pound) % 14 + pound - int(pound);
return *this;

}

bool Stone::operator>(Stone &s)

{
if (pound >s.pound)
return true;
else
return false;

}

bool Stone::operator<(Stone &s)

{
if (pound < s.pound)
return true;
else
return false;

}

bool Stone::operator=(Stone &s)

{
if (pound = s.pound)
return true;
else
return false;

}

std::ostream & operator<<(std::ostream &os, const Stone &s)

{
os << "Pound= " << s.pound << std::endl;
return os;

}

user.cpp

#include<iostream>

#include"stone.h"

int main()

{
using namespace std;
Stone stard(11, 0);
int static num = 0;
Stone arr[6] = { 123.4, 234.5, 345.6 };
cout << "Enter the last three pound value :";
for (int i = 3; i < 6; i++)
{
double n;
cout << i+1 << "# :";
cin >> n; cin.get();
arr[i].set(arr[i],n);
}
Stone min = arr[0]; Stone max = arr[0];
for (int i = 0; i < 6; i++)
{
if (min>arr[i])
min = arr[i];
else if (max < arr[i])
max = arr[i];
else ;
if (arr[i]>stard)
if (arr[i]=stard)
num++;
else;
else;
}
cout << "Here are my trying :" << endl;
cout << "Max " << max << " ;Min " << min<<endl;
cout << "Values no little than 11 stones has " << num << endl;
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++