第六周实验报告(5)
来源:互联网 发布:网络监控管理平台 编辑:程序博客网 时间:2024/06/06 02:41
* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 三角形
* 作 者: 李瑞
* 完成日期: 2012 年3 月 27日
* 版 本 号: v1.0
* 对任务及求解方法的描述部分
* 输入描述:……
* 问题描述:……
* 程序输出:……
* 程序头部的注释结束
头文件:
#include<iostream>#include<Cmath>using namespace std;class CPoint{private:double x; // 横坐标double y; // 纵坐标public:void input(); //以x,y 形式输入坐标点float Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)};class CTriangle{public:CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //给出三点的构造函数void setTriangle(CPoint &X,CPoint &Y,CPoint &Z); //输入三角形点的坐标float perimeter(void); //计算三角形的周长float area(void); //计算并返回三角形的面积bool isRightTriangle(); //是否为直角三角形bool isIsoscelesTriangle(); //是否为等腰三角形private:CPoint A,B,C; //三顶点};
main:
#include"CTriangle.h"#include<iostream>using namespace std;void main(){CPoint c1, c2, c3;c1.input();c2.input();c3.input(); CTriangle c(c1, c1, c1);c.setTriangle(c1, c2, c3);cout << "三角形的周长是:" << c.perimeter() << endl;cout << "三角形的面积是:" << c.area() << endl;cout << (c.isRightTriangle()?"是":"不是") << "直角三角形" <<endl; cout << (c.isIsoscelesTriangle()?"是":"不是") << "等腰三角形" <<endl; system("pause");}
资源文件:
#include"CTriangle.h"#include<iostream>using namespace std;float CPoint::Distance(CPoint p) const{return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));}void CPoint::input() //以x,y 形式输入坐标点{char comma;cout << "请输入点坐标,格式: x,y" << endl;while(1){cin >> x >> comma >> y ;if(comma != ','){cout << "格式不正确,请重新输入:" << endl;}else{break;}}}void CTriangle::setTriangle(CPoint &X, CPoint &Y, CPoint &Z){float s1 = X.Distance(Y);float s2 = Y.Distance(Z);float s3 = X.Distance(Z);if(s1 + s2 > s3 && s2 + s3 > s1 && s1 + s3 > s2){A = X;B = Y;C = Z;}else{cout << "不能构成三角形,退出!" << endl;exit(1);}}float CTriangle::perimeter(void){return (A.Distance(B) + B.Distance(C) + A.Distance(C));}float CTriangle::area(void){float a = A.Distance(B);float b = B.Distance(C);float c = A.Distance(C);float p = (a + b + c) / 2; return sqrt( p * (p - a) * (p - b) * (p - c) );}bool CTriangle::isRightTriangle(){float a = A.Distance(B);float b = B.Distance(C);float c = A.Distance(C);if(a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a){return true;}else{return false;}}bool CTriangle::isIsoscelesTriangle(){float a = A.Distance(B);float b = B.Distance(C);float c = A.Distance(C);if(a == b || b == c || a == c){return true;}else{return false;}}
优点:
分文件夹存放代码,可以使程序井然有序,便于管理,特别是在将来的大项目的时候,这一点非常重要。
- 第六周实验报告(5)
- 第六周实验报告(任务5)
- 第六周实验报告(5)
- 第六周实验报告5
- 第六周实验报告5
- 第六周实验报告5
- 第六周实验报告5
- 第六周实验报告
- 第六周实验报告任务5
- 第六周实验报告任务5
- 第六周实验报告(一)
- 第六周实验报告(1)
- 第六周实验报告(1)
- 第六周实验报告(2)
- 第六周实验报告(2)
- 第六周实验报告(一)
- 第六周实验报告(3)
- 第六周实验报告(任务1)
- jquety
- 2012C++程序设计实验报告【6.3】
- U-boot分析与移植(3)----U-boot stage2分析 .
- Mathtype安装与批修改公式的字体大小
- 定制你的Bash环境
- 第六周实验报告(5)
- 第六周任务(5)
- C语言运算符优先级列表
- Android开发之SurfaceView
- JAVA技术入门
- xss相关的少见内容
- U-boot分析与移植(4)----U-boot 添加命令
- jquery 鼠标移上去显示大图片
- Gitolite架设git服务器