三角形类二

// 第四周上机任务.cpp : 定义控制台应用程序的入口点。
//
/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:董慧
* 完成日期: 2013年3月23日
* 版本号: v1.0
* 输入描述:无
* 问题描述:设计求三角形周长和面积的类。
* 程序输出:三角形的周长和面积
*/

#include "stdafx.h"
#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
	//置三边的值,注意要能成三角形
	inline void setA(double x) ;
	inline void setB(double y) ;
	inline void setC(double z) ;
	inline double getA();//取三边的值
	inline double getB();
	inline double getC();
	bool isTriangle();
	double perimeter();//计算三角形的周长
	double area();//计算并返回三角形的面积
private:
	double a,b,c; //三边为私有成员数据
};
int main()
{	Triangle tri1;	//定义三角形类的一个实例(对象)
	double x,y,z;
	cout<<"请输入三角形的三边:";
	cin>>x>>y>>z;
	tri1.setA(x);tri1.setB(y);tri1.setC(z);	//为三边置初值
	if(tri1.isTriangle())
	{ 
		cout<<"三条边为:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;
		cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;
	}
	else
		cout<<"不能构成三角形"<<endl;
	system("pause");
	return 0;
}
	inline void Triangle::setA(double x)
	{
		a=x;
	}
	inline void Triangle::setB(double y)
	{
		b=y;
	}
	inline void Triangle::setC(double z) 
	{
		c=z;
	}
	inline double Triangle::getA()
	{
		return a;
	}
	inline double Triangle::getB()
	{
		return b;
	}
	inline double Triangle::getC()
	{
		return c;
	}
	bool Triangle::isTriangle()
	{
		if(a<0||b<0||c<0||a+b<c||a+c<b||b+c<a||a-b>c||a-c>b||b-c>a)
			return false;  
		else  
		    return true;  
	}  
	double  Triangle::perimeter()  
	{  
		return a+b+c;  
	}  
	double Triangle::area()  
	{  
		double p,s;  
		p=(a+b+c)/2;  
		s=sqrt(p*(p-a)*(p-b)*(p-c));  
		return s;  
	}  

 


 

总结:

错误:

inline void Triangle::setA(double x)
	{
		x=a;
	}
	inline void Triangle::setB(double y)
	{
		y=b;
	}
	inline void Triangle::setC(double z) 
	{
		z=c;
	}

输入xyz值单步执行后为

-		tri1	{a=-9.2559631349317831e+061 b=-9.2559631349317831e+061 c=-9.2559631349317831e+061 }	Triangle
		a	-9.2559631349317831e+061	double
		b	-9.2559631349317831e+061	double
		c	-9.2559631349317831e+061	double
		x	-9.2559631349317831e+061	double
		y	-9.2559631349317831e+061	double
		z	-9.2559631349317831e+061	double


哎,傻了吧唧的尴尬

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java实现三角形类的示例代码: ```java public class Triangle { public static void main(String[] args) { // 打印三角形 5行 for (int i = 1; i <= 5; i++) { // 一个空白的倒三角形,对应图中1 for (int j = 5; j >= i; j--) { System.out.print(" "); } // 一个直角三角形,对应图中2 for (int j = 1; j <= i; j++) { System.out.print("*"); } // 一个直角三角形,对应图中3 for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println(); } } } ``` 这个类实现了一个打印三角形的功能。它包含了三个for循环,第一个循环控制行数,第二个循环打印空白的倒三角形,第三个循环分别打印两个直角三角形。你可以根据需要修改行数和打印字符来实现不同类型的三角形。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java代码实现三角形](https://blog.csdn.net/qq_53192419/article/details/125984830)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java实现三角形](https://blog.csdn.net/weixin_61375386/article/details/124234472)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值