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;
}
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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++中拷贝构造函数的应用详解
- C++中引用(&)的用法与应用实例分析
- C++使用CriticalSection实现线程同步实例
- C++智能指针实例详解
- 解析C++ 浮点数的格式化输出