三角形的編譯程序
❶ 超簡單的c語言程序題 輸出一個正三角形
1、打開visual C++ 6.0軟體,准備好一個空白的c語言文件,首先引入預處理命令和主函數:
❷ C++ 編程,三角形類
#include "iostream.h"
#include "math.h"
class CPoint//定義點
{
private:
double x,y;
public:
CPoint(double a,double b){x=a;y=b;}//構造函數
CPoint(){x=0;y=0;}
void Set(double a,double b){x=a,y=b;}//設置點坐標
double GetX(){return x;}
double GetY(){return y;}
};
class CTreiangle//三角形類
{
private:
CPoint x,y,z;
double s1,s2,s3;
public:
void SetTriangle(CPoint P1,CPoint p2,CPoint P3);
bool Test();
double Area();
};
void CTreiangle::SetTriangle(CPoint P1,CPoint p2,CPoint p3)//設置三角形的三個點並計算三條邊的邊長
{
x=P1;
y=p2;
z=p3;
s1=sqrt((x.GetX()-y.GetX())*(x.GetX()-y.GetX())+(x.GetY()-y.GetY())*(x.GetY()-y.GetY()));
s2=sqrt((x.GetX()-z.GetX())*(x.GetX()-z.GetX())+(x.GetY()-z.GetY())*(x.GetY()-z.GetY()));
s3=sqrt((z.GetX()-y.GetX())*(z.GetX()-y.GetX())+(z.GetY()-y.GetY())*(z.GetY()-y.GetY()));
}
bool CTreiangle::Test()//檢測是不是三角形,原理三點不共線
{
if((y.GetX()-x.GetX())*z.GetY()==(y.GetY()-x.GetY())*z.GetX()+x.GetY()*y.GetY()-x.GetY()*x.GetY()-x.GetX()*y.GetY()+y.GetX()*x.GetY())
return false;
else
return true;
}
double CTreiangle::Area()//應用海倫公式計算面積並返回
{
double p,area;
p=(s1+s2+s3)/2.0;
area=sqrt(p*(p-s1)*(p-s2)*(p-s3));
return area;
}
int main()
{
CPoint temp[3];//定義三個點
double tempx,tempy;
CTreiangle treiangle;
while(1)
{
for(int i=0;i<3;i++)
{
cout<<"請輸入第"<<i+1<<"個點的X坐標"<<endl;
cin>>tempx;
cout<<"請輸入第"<<i+1<<"個點的Y坐標"<<endl;
cin>>tempy;
temp[i].Set(tempx,tempy);
}
treiangle.SetTriangle(temp[0],temp[1],temp[2]);
if(!treiangle.Test())
{
cout<<"輸入數據不合法不能夠形成三角形"<<endl;
cout<<"重新輸入數據"<<endl;
}
else
{
cout<<"三角形的面積為"<<treiangle.Area()<<endl;
cout<<"重新輸入數據"<<endl;
}
}
return 0;
}
VC6.0編譯運行通過
測試圖,以3,4,5為例
❸ C語言:編寫程序,輸入一個三角形的三條邊,若能構成一個三角形,則輸出相應提示信息並計算三角形面積。
#include<stdio.h>
#include<math.h>
#include<conio.h>
/*海倫公式/秦九韶三斜求積*/
/*已知三角形三邊長,返回三角形面積*/
floatheron(floata,floatb,floatc){
floatA,s;/*A:面積;s:半周長*/
s=(a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
returnA;
}
/*三角形三邊長判定*/
/*任意兩邊大於第三邊,可構成三角形,返回1,否則返回0*/
intedge(floata,floatb,floatc){
return(a+b>c&&a+c>b&&b+c>a);
}
intmain(void){
floata,b,c;/*三角形三邊長*/
printf("輸入三角形三邊長:");
scanf("%f%f%f",&a,&b,&c);
putchar(' ');
if(edge(a,b,c))/*任意兩邊和大於第三邊*/
printf("三角形面積:%.2f ",heron(a,b,c));
else
printf("三邊長不能構成三角形! ");
getch();/*屏幕暫留*/
return0;
}
❹ 如何用C語言編程序:輸入三個數,判斷是否構成三角形
需要准備的材料分別有:電腦、C語言編譯器。
1、首先,打開C語言編譯器,新建一個初始.cpp文件,例如:test.cpp。
❺ 怎麼用C++編寫各種三角形
通過循環控制變數,外層循環控制行,內層循環控制行中每一個位置(列)。外層循環比較簡單,循環控制變數取值從第一行到最後一行,內層循環要根據圖形的變化分別確定輸出空格和符號的循環次數。
比如一個等腰三角形的主要程序代碼:(定義變數的就不寫了)
for(i=1;i<=n-i;i++)
{for(j=1;j<=n-j;j++)cout<<`
`;for(j=1;j<=2*i-1;j++)cout<<`*`;for(j=1;j<=n-i;j++)cout<<`
`;cout<<endl;}
❻ 編程題:編寫程序輸入三角形的3條邊長,計算並輸出三角形的面積。
一、程序分析
三角形面積海倫公式:√[ p ( p - a ) ( p - b ) ( p - c ) ] 。其中 p = (a + b + c) / 2 。a、b、c分別是三角形的三邊長。
二、根據三角形面積計算公式用if語句編寫程序如下:
#include "stdio.h"
#include "math.h"
int main(void)
{
float a = 0, b = 0, c = 0, p = 0;
float area = 0;
printf("Please input three sides of triangle:");
scanf_s("%f %f %f", &a, &b, &c);
if((a + b) > c && (a + c) > b && (b + c) > a)
{
p = (a + b + c) / 2;
area = sqrt(p * (p - a) * (p - b) * (p - c));
}
else
printf("Triangle does not exist! ");
printf("The area of triangle is:%f ", area);
return 0;
(6)三角形的編譯程序擴展閱讀:
還可以使用switch語句計算三角形的面積,編寫程序如下
#include "stdio.h"
#include "math.h"
int main(void)
{
float a = 0, b = 0, c = 0;
float p = 0;
printf("Please input three sides of triangle:");
scanf_s("%f %f %f", &a, &b, &c);
switch (a + b > c && a + c > b && b + c > a)
{
case 0:printf("Triangle does not exist! "); break;
case 1:
p = (a + b + c)*0.5;
printf("The area of triangle is:%f ", sqrt(p * (p - a) * (p - b) * (p - c)));
break;
}
return 0;
}
❼ 用C++編寫一個三角形類Ctriangle
/*編寫一個三角形類Ctriangle,有a、b、c三條邊(double類型),提
供計算面積的函數GetArea()以及計算周長的函數GetPerimeter(),並提
供main函數進行測試
*/
#include<iostream>
#include<cmath>
using namespace std;
class Ctriangle
{
private:
double a,b,c; //私有欄位,保證程序數據的安全性
public:
double GetArea(); //面積獲取函數
double GetPerimeter();//周長獲取函數
void setA(double a); //設置a的值的函數
double getA(); //獲取a的函數
void setB(double b);//設置b的值的函數
double getB();//獲取b的函數
void setC(double c);//設置c的值的函數
double getC();//獲取c的函數
};
void Ctriangle::setA(double a)
{
this->a=a;
}
void Ctriangle::setB(double b)
{
this->b=b;
}
void Ctriangle::setC(double c)
{
this->c=c;
}
double Ctriangle::getA()
{
return this->a;
}
double Ctriangle::getB()
{
return this->b;
}
double Ctriangle::getC()
{
return this->c;
}
double Ctriangle::GetArea()
{
double p=(this->getA()+this->getB()+this->getC())/2;
double S=sqrt(p*(p-this->getA())*(p-this->getB())*(p-this->getC()));
return S;
}
double Ctriangle::GetPerimeter()
{
return this->getA()+this->getB()+this->getC();
}
void main()
{
Ctriangle test;
while(1)
{
cout<<"input a,b,c:"<<endl;
double a,b,c;
cin>>a>>b>>c;
if(a+b<c||a+c<b||b+c<a||a<=0||b<=0||c<=0) //輸入邊的合法性檢查,是否能組成三角形
{
cout<<"無法組成三角形"<<endl;
continue;
}
test.setA(a);
test.setB(b);
test.setC(c);
cout<<"面積="<<test.GetArea()<<endl;
cout<<"周長="<<test.GetPerimeter()<<endl;
}
}