第七周实验报告(一)

01.#include<iostream>  
02.#include<Cmath>  
03.using namespace std;  
04.  
05.class CPoint  
06.{  
07.private:  
08.    double x;      // 横坐标  
09.    double y;      // 纵坐标  
10.public:  
11.    void input();        //以x,y 形式输入坐标点  
12.    float Distance(CPoint p) const;       // 两点之间的距离(一点是当前点,另一点为参数p)  
13.};  
14.  
15.class CTriangle  
16.{  
17.public:  
18.    CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //给出三点的构造函数  
19.    void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);   //输入三角形点的坐标  
20.    float perimeter(void);              //计算三角形的周长  
21.    float area(void);                   //计算并返回三角形的面积  
22.    bool isRightTriangle();             //是否为直角三角形  
23.    bool isIsoscelesTriangle();         //是否为等腰三角形  
24.private:  
25.    CPoint A,B,C; //三顶点  
26.};  
 

main:
 


[cpp] view plaincopyprint?
01.#include"CTriangle.h"  
02.#include<iostream>  
03.using namespace std;  
04.  
05.void main()  
06.{  
07.    CPoint c1, c2, c3;  
08.  
09.    c1.input();  
10.  
11.    c2.input();  
12.  
13.    c3.input();  
14.  
15.    CTriangle c(c1, c1, c1);  
16.  
17.    c.setTriangle(c1, c2, c3);  
18.  
19.    cout << "三角形的周长是:" << c.perimeter() << endl;  
20.  
21.    cout << "三角形的面积是:" << c.area() << endl;  
22.  
23.    cout << (c.isRightTriangle()?"是":"不是") << "直角三角形" <<endl;    
24.  
25.    cout << (c.isIsoscelesTriangle()?"是":"不是") << "等腰三角形" <<endl;   
26.  
27.    system("pause");  
28.}  
 

资源文件:
 


[cpp] view plaincopyprint?
01.#include"CTriangle.h"  
02.#include<iostream>  
03.using namespace std;  
04.  
05.float CPoint::Distance(CPoint p) const  
06.{  
07.    return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));  
08.}  
09.  
10.void CPoint::input()              //以x,y 形式输入坐标点  
11.{  
12.    char comma;  
13.    cout << "请输入点坐标,格式: x,y" << endl;  
14.      
15.    while(1)  
16.    {  
17.        cin >> x >> comma >> y ;  
18.        if(comma != ',')  
19.        {  
20.            cout << "格式不正确,请重新输入:" << endl;  
21.        }  
22.        else  
23.        {  
24.            break;  
25.        }  
26.    }  
27.}  
28.  
29.void CTriangle::setTriangle(CPoint &X, CPoint &Y, CPoint &Z)  
30.{  
31.    float s1 = X.Distance(Y);  
32.  
33.    float s2 = Y.Distance(Z);  
34.  
35.    float s3 = X.Distance(Z);  
36.  
37.    if(s1 + s2 > s3 && s2 + s3 > s1 && s1 + s3 > s2)  
38.    {  
39.        A = X;  
40.  
41.        B = Y;  
42.  
43.        C = Z;  
44.    }  
45.  
46.    else  
47.    {  
48.        cout << "不能构成三角形,退出!" << endl;  
49.        exit(1);  
50.    }  
51.  
52.}  
53.  
54.float CTriangle::perimeter(void)  
55.{  
56.    return (A.Distance(B) + B.Distance(C) + A.Distance(C));  
57.}  
58.  
59.float CTriangle::area(void)  
60.{  
61.    float a = A.Distance(B);  
62.  
63.    float b = B.Distance(C);  
64.  
65.    float c = A.Distance(C);  
66.  
67.    float p = (a + b + c) / 2;    
68.  
69.    return sqrt( p * (p - a) * (p - b) * (p - c) );  
70.}  
71.  
72.bool CTriangle::isRightTriangle()  
73.{  
74.    float a = A.Distance(B);  
75.  
76.    float b = B.Distance(C);  
77.  
78.    float c = A.Distance(C);  
79.  
80.    if(a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a)  
81.    {  
82.        return true;  
83.    }  
84.  
85.    else  
86.    {  
87.        return false;  
88.    }  
89.}  
90.  
91.bool CTriangle::isIsoscelesTriangle()  
92.{  
93.    float a = A.Distance(B);  
94.  
95.    float b = B.Distance(C);  
96.  
97.    float c = A.Distance(C);  
98.  
99.    if(a == b || b == c || a == c)  
100.    {  
101.        return true;  
102.    }  
103.  
104.    else  
105.    {  
106.        return false;  
107.    }  
108.}  


解答

01.#include<iostream>  
02.  
03.using namespace std;  
04.  
05.class Time  
06.{  
07.public:  
08.    Time(int=0,int=0,int=0);  
09.    void show_time( ); //根据is_24和from0,输出适合形式-20:23:5/8:23:5 pm/08:23:05 pm  
10.    void add_seconds(int); //增加n秒钟  
11.    void add_minutes(int); //增加n分钟   
12.    void add_hours(int); //增加n小时   
13.    static void change24();  //改变静态成员is_24,在和时制之间转换  
14.    static void changefrom0();   //改变静态成员from0,切换是否前导0  
15.private:  
16.    static bool is_24; //为true时,24小时制,如20:23:5;为flase,12小时制,显示为8:23:5 pm   
17.    static bool from0; //为true时,前导0,8:23:5显示为08:23:05  
18.    int hour;  
19.    int minute;  
20.    int sec;  
21.};  
22.//下面写出静态成员的初始化及各成员函数的定义  
23.  
24.bool Time::is_24 = true;  
25.bool Time::from0 = false;  
26.  
27.Time::Time( int h, int m, int s ) : hour(h), minute(m), sec(s){};  
28.  
29.void Time::show_time()  
30.{  
31.    //输出时  
32.      
33.    int h = ( is_24 ) ? hour : hour % 12;   
34.      
35.    if ( h < 10 && from0 )   
36.          
37.        cout << "0";    
38.      
39.    cout << h << ":";  
40.      
41.    //输出分  
42.    if ( minute < 10 && from0 )  
43.          
44.        cout << "0";  
45.      
46.    cout << minute << ":";  
47.      
48.    //输出秒  
49.    if ( sec < 10 && from0 )  
50.          
51.        cout << "O";  
52.      
53.    cout << sec ;  
54.      
55.    //输出 am / pm  
56.    if ( h > 12 )  
57.          
58.        cout << "pm" << endl;  
59.      
60.    else  
61.          
62.        cout << "am" << endl;  
63.      
64.}  
65.  
66.void Time::add_seconds(int n)  
67.{  
68.    sec += n;  
69.      
70.    if (sec > 59 )  
71.    {  
72.        add_minutes( sec / 60 );  
73.          
74.        sec = sec % 60;  
75.    }  
76.}  
77.  
78.void Time::add_minutes( int n )  
79.{  
80.    minute += n;  
81.      
82.    if ( minute > 59 )  
83.    {  
84.        add_hours ( minute / 60 );  
85.          
86.        minute = minute % 60;  
87.    }  
88.}  
89.  
90.void Time::add_hours( int n )  
91.{  
92.    hour += n;  
93.      
94.    if ( hour > 24 )  
95.    {  
96.        hour = hour % 24;  
97.    }  
98.}  
99.  
100.void Time::change24()  
101.{  
102.    is_24 = !is_24;  
103.}  
104.  
105.void Time::changefrom0()  
106.{  
107.    from0 = !from0;  
108.}  
109.  
110.int main( )     
111.{  
112.    Time t1(23,14,25),t2(8,45,6);   
113.    cout<<"24时制, 不前导:"<<endl;  
114.    cout<<"    t1是:";  
115.    t1.show_time();  
116.    cout<<"    t2是:";  
117.    t2.show_time();  
118.    t1.add_hours(10);  
119.    t2.add_hours(10);  
120.    Time::changefrom0(); //注意此处调用静态成员  
121.    cout<<"10小时后, 切换是否前导:"<<endl;  
122.    cout<<    "t1是:";  
123.    t1.show_time();  
124.    cout<<    "t2是:";  
125.    t2.show_time();  
126.    t1.change24();  
127.    cout<<"换一种制式:"<<endl;  
128.    cout<<"    t1是:";  
129.    t1.show_time();  
130.    cout<<"    t2是:";  
131.    t2.show_time();  
132.    system("pause");  
133.    return 0;  
134.}  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值