现在的位置: 首页 > 综合 > 正文

c++ primer 第十一章编程练习

2018年02月20日 ⁄ 综合 ⁄ 共 2512字 ⁄ 字号 评论关闭

1

/*2014 01 25 by arby*/

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "vect.h"
using namespace std;
using VECTOR::Vector;

int main()
{
	/*初始化*/
	srand(time(0));   //seed random-number generator
	Vector step;
	Vector result(0.0, 0.0);
	double director;
	ofstream fout;
	double target;
	double steps;
	int times = 0;
	
	/*操作*/
	fout.open("xiaoke.txt");
	fout << "Target Distance: ";
	cout << "Target Distance: ";
	cin>> target;
	fout << target << ", Step Size: ";
	cout  << ", Step Size: ";
	cin>>steps;
	fout << steps << endl;

	while(result.magval() < target)
	{
		director = rand()%360;
		step.set(steps, target, 'p');
		fout << times << ':' <<" " << result << endl;
		result = result + step;
		times++;
	}
	fout << "After " << (times-1) << " steps, the subject has the following location:" << endl;
	fout << result << endl;
	fout << "or" << endl;
	result.polar_mode();
	fout << result << endl;

	cin.get();
	cin.get();
	return 0;
}

4.

// mytime3.h -- Time class with friends
#ifndef MYTIME3_H_
#define MYTIME3_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);
	 Time operator*(double n) const;

    friend Time operator+(const Time &m, const Time & t);
    friend Time operator-(const Time &m, const Time & t);
    friend Time operator*(double m, const Time & t)
        { return t * m; }   // inline definition
    friend std::ostream & operator<<(std::ostream & os, const Time & t);

};
#endif

// mytime3.cpp  -- implementing Time methods
#include "mytime3.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;
}

Time operator+(const Time &m, const Time & t)
{
    Time sum;
    sum.minutes = m.minutes + t.minutes;
    sum.hours = m.hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}

Time operator-(const Time &m, const Time & t)
{
    Time diff;
    int tot1, tot2;
    tot1 = t.minutes + 60 * t.hours;
    tot2 = m.minutes + 60 * m.hours;
    diff.minutes = (tot2 - tot1) % 60;
    diff.hours = (tot2 - tot1) / 60;
    return diff;
}

Time Time::operator*(double mult) const
{
    Time result;
    long totalminutes = hours * mult * 60 + minutes * mult;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}

std::ostream & operator<<(std::ostream & os, const Time & t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os; 
}

/*2014 01 25 by arby*/

#include <iostream>
#include "mytime3.h"

int main()
{
    using std::cout;
	using std::cin;
    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;     // operator+()
    cout << "Aida + Tosca: " << temp << endl;
    temp = aida* 1.17;  // member operator*()
    cout << "Aida * 1.17: " << temp << endl;
    cout << "10 * Tosca: " << 10 * tosca << endl;

	cin.get();
	cin.get();
    return 0; 
}

抱歉!评论已关闭.