當前位置:編程學習大全網 - 編程軟體 - 編程計算圖形的面積。程序可計算圓形、長方形、正方形的面積,運行時先提示用戶選擇圖形的類型

編程計算圖形的面積。程序可計算圓形、長方形、正方形的面積,運行時先提示用戶選擇圖形的類型

#include<iostream>

#include<cmath> //因為用到了sqrt()函數,sqrt()為開根號

using namespace std;

int main()

{

cout<<"1計算圓面積"<<endl

<<"2計算矩形面積"<<endl

<<"3計算三角形面積"<<endl

<<"4退出"<<endl

<<"請選擇相應程序代碼:";

int x; //x對應編號

cin>>x;

double area;

while(x!=4) //當x!=4時循環,即當x=4時退出循環

{

if(x==1)

{

double r;

cout<<"半徑:";

cin>>r;

area=3.14*r*r;

cout<<"面積為 "<<area<<endl;

}

else if(x==2)

{

double a,b;

cout<<"長和寬:";

cin>>a>>b;

area=a*b;

cout<<"面積為 "<<area<<endl;

}

else if(x==3)

{

double a,b,c,d;

cout<<"三邊長:";

cin>>a>>b>>c;

d=0.5*(a+b+c);

area=sqrt(d*(d-a)*(d-b)*(d-c));

cout<<"面積為 "<<area<<endl;

}

else

cout<<"輸入有誤,請重新輸入!"<<endl;

cout<<"程序代碼:";

cin>>x;

}

return 0;

}

  • 上一篇:遊戲UI是什麽意思?
  • 下一篇:接口的作用,為什麽要用接口
  • copyright 2024編程學習大全網