c++ Primer Plus(习题11.1~2)
/*书上的例题,不过换成文件输出*/
/*这个比方便了一点,不用那些标准io,和cout用法的用法一样*/
/*并且会覆盖以前的*/
/*连着做第二题,修改头文件,不修改接口*/
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include"vector.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream file; //输出到文件
file.open("酒鬼漫步.txt");
cout << "Enter target distance(q to quit):";
while (cin >> target)
{
cout << "Enter the length: ";
if (!(cin >> dstep))
break;
cout << "Target Distance: " << target << " , " << "Step Size: " << dstep<<endl;
file << "Target Distance: " << target << " , " << "Step Size: " << dstep << endl;
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
cout << steps << ": " << result << endl;
file<< steps << ": " << result << endl; //输出到文件
steps++;
}
cout << steps << ": " << result << endl; //外面补一个,我认为
file<<steps << ": " << result << endl;
cout << "After " << steps << " steps,the subject "
"has the following location:\n";
file<< "After " << steps << " steps,the subject "
"has the following location:\n";
cout << result << endl;
file << result << endl;
result.polar_mode();
cout << "Or\n" << result << endl;
file << "Or\n" << result << endl;
cout << "Average outward distance per step="
<< result.magval() / steps << endl;
file<<"Average outward distance per step="
<< result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout<<"Enter target distance(q to quit):";
}
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
file.close();
return 0;
}
头文件
/*vertor.h--矢量的类,with<<,made state.*/
#pragma once
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR //矢量的名称空间
{
class Vector
{
public:
enum Mode{RECT,POL}; //两种模式rect表示直角坐标,pol表示极坐标
private:
double x;
double y;
//double mag; //极坐标的长度
//double ang; //极坐标的角度
Mode mode; //模式的选择
//私有方法设置变量值
//void set_mag(); //设置矢量长度
//void set_ang(); //设置矢量角度
//void set_x();
//void set_y();
public:
Vector(); //默认构造函数
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form=RECT);
~Vector(); //析构函数
double xval()const { return x; }; //报告x坐标值
double yval()const { return y; }; //报告y坐标值
double magval()const ; //报告矢量长度
double angval()const ; //报告矢量角度
void polar_mode(); //设置模式为极坐标
void rect_mode(); //设置模式为直角坐标
//运算符重载
Vector operator+(const Vector &b)const; //重载加法
Vector operator-(const Vector &b)const; //重载j减法
Vector operator-()const; //-号
Vector operator*(double n)const;
//友元函数
friend Vector operator+(double n, const Vector &a); //重载加法
friend std::ostream&operator<<(std::ostream&os, const Vector &v); //重载<<运算符
};
}
#endif // !VECTOR_H
实现文件
/*vector类的方法*/
#include<cmath>
#include"vector.h" //已经包含iostream.h
using std::sqrt;
using std::sin;
using std::cos;
using std::atan; //返回半个圆的弧度
using std::atan2; //返回一个圆的弧度
using std::cout;
namespace VECTOR
{
//把弧度换算成角度
const double Rad_to_deg = 45.0 / atan(1.0);
//应该和57.2957795130823差不多
double Vector::magval()const
{
return sqrt(x*x+y*y); //直接定义这个函数了,第二题
}
double Vector::angval()const
{
if (x == 0.0&&y == 0.0)
return 0; //矢量角度
else
return atan2(y, x);
}
//公共方法
Vector::Vector() //默认构造函数
{
x = y = 0.0;
mode = RECT; //默认为直角坐标
}
Vector::Vector(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
//mag=n1;
x= n1*cos(n2); //改下这里就可以了,简单吧,还有一些小的地方
y = n1*sin(n2);
}
else
{
cout << "不正确的模式设置.";
cout << "采用默认模式!\n";
x = y= 0.0;
mode = RECT;
}
}
//用于重新设置设置vector类的模式
void Vector::reset(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
}
else if (form == POL)
{
x= n1*cos(n2);
y= n1*sin(n2);
}
else
{
cout << "不正确的模式设置.";
cout << "采用默认模式!\n";
x = y= 0.0;
mode = RECT;
}
}
Vector::~Vector() //析构函数,进行清理工作
{
}
//设置为极坐标模式
void Vector::polar_mode()
{
mode = POL;
}
void Vector::rect_mode()
{
mode =RECT;
}
//预算符重载定义
Vector Vector::operator+(const Vector&b)const //重载+号
{
return Vector(x + b.x, y + b.y);
}
Vector Vector::operator-(const Vector&b)const //重载-号
{
return Vector(x - b.x, y - b.y);
}
Vector Vector::operator-()const
{
return Vector(-x,-y); //重载-号无对象
}
Vector Vector::operator*(double n)const
{
return Vector(x*n, y*n); //重载*号
}
Vector operator*(double n, const Vector &a) //友元函数,重载*号
{
return a*n;
}
//可连续使用的重载运算符
std::ostream&operator<<(std::ostream&os, const Vector &v) //显示极坐标值,或者直角坐标值
{
if (v.mode == Vector::RECT) //直角坐标模式
os << "(x,y)=(" << v.x << "," << v.y << ")";
else if (v.mode == Vector::POL)
{
os << "(m,a)=(" << v.magval()<< "," << v.angval()*Rad_to_deg << ")"; //弧度变角度
}
else
os << "显示模式不正确!";
return os;
}
}
c++ Primer Plus(习题11.3)报告步数,修改使用文件就可以,头文件一样
/*第三题,只需要修改使用接口就行,方法是创建一个动态数组,*/
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include"vector.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
int count;
cout << "Enter target distance(q to quit):";
while (cin >> target)
{
cout << "Enter the length: ";
if (!(cin >> dstep))
break;
cout << "Enter the test times: ";
cin >> count;
int *p = new int[count]; //创建动态数组,来比较出最大最小,和求平均步数
int sum = 0; //记录总数
cout << "Target Distance: " << target << " , " << "Step Size: " << dstep << endl;
for (int i = 0; i < count; i++)
{
result.reset(0.0, 0.0);
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
}
p[i] = steps;
sum += steps;
cout << i + 1 << " test: " << p[i] << endl;
}
int min=p[0]; //初始化最大,最小步数
int max = p[0];
for (int i = 1; i < count; i++) //比较得出最大最小值。放在数组初始化那里不太好
{
if (p[i] > max)
max = p[i];
else if (p[i] < min)
min = p[i];
}
cout << "After " << count << " test,the max step is " << max << "\n"
<< "The min steps is " << min << endl
<< "and average steps is " << sum / count<<endl;
delete[] p; //与new配对
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance(q to quit):";
}
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
c++ Primer Plus(习题11.4)
/*使用mytime接口,看重载函数是否成功*/
#include<iostream>
#include"mytime.h"
int main()
{
using std::cout;
using std::endl;
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida and Tosca:\n";
cout << aida << ";" << tosca << endl;
temp = aida + tosca;
cout << "Aida + Tosca: " << temp << endl;
temp = aida*1.17;
cout << "Aida * 1.17: " << temp << endl;
cout << "10.0 * Tosca: " << 10.0*tosca << endl;
return 0;
}
头文件
/*习题11.4,重写time类,需要用友元函数实现*/
#pragma once
#ifndef MYTIME_H
#define MYTIME_H
#include<iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time(); //默认构造函数
Time(int h, int m=0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
//这里是要改成友元函数重载的运算符
friend Time operator+(const Time &t,const Time&t2);
friend Time operator-(const Time &t, const Time&t2);
friend Time operator*(const Time &t, double m);
friend Time operator*(double m, const Time &t)
{ return t*m; }
friend std::ostream&operator<<(std::ostream&os, const Time &t);
};
#endif // !MYTIME_H
实现文件
/*my time 头文件实现*/
#include"mytime.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours = h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
//这里是要改的友元重载运算符函数,记得前面不加friend了
Time operator+(const Time &t,const Time &t2)
{
Time plus;
long totalminutes = t.hours * 60 + t.minutes+t2.hours*60+t2.minutes;
plus.hours = totalminutes / 60;
plus.minutes = totalminutes % 60;
return plus;
}
Time operator-(const Time &t, const Time&t2)
{
Time sub;
long totalminutes = t.hours * 60 + t.minutes-t2.hours * 60 + t2.minutes;
sub.hours = totalminutes / 60;
sub.minutes = totalminutes % 60;
return sub;
}
Time operator*(const Time &t, double m)
{
Time mul;
long totalminutes = t.hours*m * 60 + t.minutes*m;
mul.hours = totalminutes / 60;
mul.minutes = totalminutes % 60;
return mul;
}
std::ostream&operator<<(std::ostream&os, const Time &t)
{
os << t.hours << " hours, " << t.minutes << " minutes.";
return os;
}
c++ Primer Plus(习题11.5||11.6)
/*有状态成员的stonewt类,试下各个方法*/
#include<iostream>
#include"stonewt.h"
int main()
{
using namespace std;
Stonewt a(9, 2.8);
Stonewt b(2, 1.4);
double c = 1.0;
cout << "a+b \n";
cout << a + b;
cout << "a-b \n";
cout << a -b;
cout << "a*c \n";
cout << a*c;
cout << "c*a \n";
cout << c*a;
cout << "Source a: " << a;
a.pou_mode();
cout << a;
a.flo_mode();
cout << a;
a.sto_mode();
cout << a;
return 0;
}
头文件
/*又要编写一个类,书上的11.5和6头文件,一起做*/
#pragma once
#ifndef STONEWT_H
#define STONEWT_H
#include<iostream>
class Stonewt
{
private:
enum Mode{STO,POU,FLO}; //sto 英石模式,POU总磅数,FLO浮点磅格式
Mode mode;
int stone; //英石,又是个外国单位
double pds_left; //磅数的结余
double pounds; // 总磅数
static int Lbs_per_sto; //静态类成员
public:
Stonewt(); //默认构造函数
Stonewt(int n1, double n2, Mode from = FLO);
Stonewt(double lbs); //接受pounds的构造函数,也可做转换函数
~Stonewt();
void sto_mode();
void pou_mode();
void flo_mode(); //设置三种模式
Stonewt operator+(const Stonewt&n1);
Stonewt operator-(const Stonewt&n1);
Stonewt operator*(const double n)const;
bool operator>(const Stonewt &s); //连着第第六题的重载全部关系运算符
bool operator<(const Stonewt &s);
bool operator==(const Stonewt &s);
bool operator!=(const Stonewt &s);
bool operator>=(const Stonewt &s);
bool operator<=(const Stonewt &s);
friend Stonewt operator*(double n, const Stonewt &s); //重载乘法
friend std::ostream& operator<<(std::ostream &os, const Stonewt &s);
};
#endif // !STONEWT_H
接口实现
/*提供了stonewt的实现*/
#include"stonewt.h"
int Stonewt::Lbs_per_sto = 14; //1英石等于14磅
Stonewt::Stonewt()
{
stone = pds_left = pounds = 0;
mode = POU;
}
Stonewt::Stonewt(int n1, double n2, Mode from)
{
stone = n1;
pds_left = n2;
pounds = stone*Lbs_per_sto + n2;
mode = from;
}
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_sto;
pds_left = int(lbs) % Lbs_per_sto + lbs - int(lbs);
pounds = lbs;
mode = POU;
}
//三种模式,用来显示数值
Stonewt::~Stonewt()
{
}
void Stonewt::sto_mode()
{
mode = STO;
}
void Stonewt::pou_mode()
{
mode = POU;
}
void Stonewt::flo_mode()
{
mode = FLO;
}
Stonewt Stonewt::operator+(const Stonewt&n1)
{
Stonewt temp;
temp.pounds = pounds + n1.pounds;
temp.stone = temp.pounds / Lbs_per_sto;
temp.pds_left=int(temp.pounds)%Lbs_per_sto+ temp.pounds - int(temp.pounds);
return temp;
}
Stonewt Stonewt::operator-(const Stonewt&n1)
{
Stonewt temp;
temp.pounds = pounds - n1.pounds;
temp.stone = temp.pounds / Lbs_per_sto;
temp.pds_left = int(temp.pounds) % Lbs_per_sto + temp.pounds - int(temp.pounds);
return temp;
}
Stonewt Stonewt::operator*(const double n)const
{
Stonewt temp;
temp.pounds=pounds*n;
temp.pds_left= int(temp.pounds) %Lbs_per_sto + temp.pounds - int(temp.pounds);
temp.stone = temp.pounds / Lbs_per_sto;
return temp;
}
Stonewt operator*(double n1, const Stonewt &s)
{
return s*n1;
}
//重载关系运算符
bool Stonewt::operator>(const Stonewt &s)
{
return pounds>s.pounds ? true : false; //大于
}
bool Stonewt::operator<(const Stonewt &s)
{
return pounds <s.pounds ? true : false; //小于
}
bool Stonewt::operator==(const Stonewt &s)
{
return pounds==s.pounds ? true : false; //等于
}
bool Stonewt::operator!=(const Stonewt &s)
{
return pounds != s.pounds ? true : false; //不等于
}
bool Stonewt::operator>=(const Stonewt &s)
{
return pounds >= s.pounds ? true : false; //大于等于
}
bool Stonewt::operator<=(const Stonewt &s)
{
return pounds <= s.pounds ? true : false; //小于等于
}
std::ostream& operator<<(std::ostream &os, const Stonewt &s)
{
if (s.mode == Stonewt::STO)
{
double temp; //把磅数转化为英石,这就是英石模式
temp = s.stone + s.pds_left / Stonewt::Lbs_per_sto;
os << "Stone mode: " <<temp<< " stone" << std::endl;
}
else if (s.mode == Stonewt::POU) //整数磅模式
os << "Integer pounds mode: " <<s.pounds<<" pounds."<< std::endl;
else if (s.mode == Stonewt::FLO) //浮点磅模式
os << "Float pounds mode: "<< s.stone << " stone,"<<s.pds_left<<" pounds"<< std::endl;
return os;
}
c++ Primer Plus(习题11.6)
//重载6个关系运算符,前面头文件已经包含5题的和⑥题的
//本来头文件重载了一个赋值运算符,结果发现,用户输入的值根本无法读入数组
//构造函数(接受一个参数的)和赋值运算符啥都没有做
//所以,去掉其一,便可以正常玩耍
#include"stonewt.h"
int main()
{
using std::cout;
using std::cin;
using std::endl;
Stonewt test[6] = {156,312,110};
Stonewt up=14*11; //要大于11英石的,配的是接受pound值的构造函数
double temp;
int counts = 0;
cout<<"Please enter three numbles which are put into the last three items:\n";
for (int i = 3; i < 6; i++)
{
cout<< i - 2 << ": ";
cin>>temp;
test[i] = temp;
}
cout << "**********Ok**********" << endl
<< "Hare the array elements: \n";
for (int i = 0; i < 6; i++)
cout << test[i];
Stonewt max =0;
Stonewt min =test[0];
for (int i = 0; i < 6; i++)
{
if (test[i]>=up)
counts++;
if (test[i] > max)
max = test[i];
if (test[i] < min)
min= test[i];
}
cout<< "Max: " << max
<< "Min: " << min
<< "past 11 stone(154pounds) items have " <<counts <<endl;
return 0;
}
c++ Primer Plus(习题11.7)自己的复数
#include<iostream>
#include"mycomplex.h"
using namespace std;
int main()
{
Complex a(3.0, 4.0);
Complex c;
cout << "Input a implex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c;
cout << "complex conjugate(~c) is " << ~c;
cout << "a is " << a;
cout << "a+c is " << a + c;
cout << "a-c is " << a - c;
cout << "a*c is " << a*c;
cout << "2*c is" << 2 * c;
cout << "c*2 is" << c * 2;
cout << "Input a implex number (q to quit):\n";
}
cout << "Bye!\n";
return 0;
}
//设计自己的complex类
#pragma once
#ifndef MYCOMPLEX_H
#define MYCOMPLEX_H
#include<iostream>
class Complex
{
private:
double x;
double y;
public:
Complex();
Complex(double s, double f);
~Complex();
Complex operator+(const Complex &f)const;
Complex operator-(const Complex &f)const;
Complex operator*(const Complex &f)const;
Complex operator*(const double s)const;
Complex operator~();
friend Complex operator*(const double s, const Complex&f) { return f*s; };
friend std::ostream&operator<<(std::ostream&oa, const Complex &f);
friend std::istream&operator>>(std::istream&ia,Complex &f);
};
#endif // !MYCOMPLEX_H
//mycomplex实现
#include"mycomplex.h"
Complex::Complex()
{
x = y = 0.0;
}
Complex::Complex(double s, double f)
{
x = s;
y = f;
}
Complex::~Complex()
{
}
Complex Complex::operator+(const Complex &f)const
{
Complex temp;
temp.x = x + f.x; // a*c = (A+C, (D + B)i)
temp.y = y + f.y;
return temp;
}
Complex Complex::operator-(const Complex &f)const
{
Complex temp;
temp.x = x - f.x;
temp.y = y - f.y;
return temp;
}
Complex Complex::operator*(const Complex &f)const
{
Complex temp;
temp.x = x*f.x - y*f.y; //a*c=(A*C-B*D,(A*D+B*C)i)
temp.y = x*f.y + y*f.x;
return temp;
}
Complex Complex::operator*(const double s)const
{
Complex temp;
temp.x = x*s;
temp.y = y*s;
return temp;
}
Complex Complex::operator~()
{
Complex temp;
temp.x = x;
temp.y = -y;
return temp;
}
std::ostream&operator<<(std::ostream&oa, const Complex &f)
{
oa<<"( " << f.x << ", " << f.y << "i)\n";
return oa; //书上是os,试着换下名称也行
}
std::istream&operator>>(std::istream&is,Complex &f)
{
std::cout << "Input complex real part: ";
if(!(is >>f.x))
return is;
std::cout << "imaaginary part: ";
is >> f.y;
return is;
}
陆续更新中,持续学习中~~~~~~~~~