怎麼在c++中編寫程式輸入半徑計算圓的周長和麵積
- IT科技
- 關注:1.04W次
/ test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #define PI 3.1415962 using namespace std; class circle { private: int r; //半徑 public: circle() //無引數建構函式以10為半徑 { r=1
C語言的應用很廣泛。今天舉一個很簡單的例子來學習C++的基本格式和基本知識。從鍵盤輸入半徑,輸出周長和麵積。以VC++為例。
材料/工具
安裝成功的VC++軟體普通電腦
#include"iostream" #define PI 3.1415962 using namespace std; class circle { private: int r; //半徑 public: circle() //無引數建構函式以10為半徑 { r=10; } circle(double x) //建構函式 { r=x; } double length() { return 2*PI*r; } do
方法
開啟VC++,調節好文字大小,新建c++環境。
修改如下: #include using namespace std; const float PI=3.14; class circle { public: circle() { r=0; } void input(); float area(); float length(); private: float r; }; void circle::input() { cin>>r; } float circle::length() {re
編寫程式:
# include<stdio.h>
方法: #include #define PI 3.14 int main() { float r,c,area; printf("請輸入圓的半徑:"); scanf("%f",&r); c=2 * PI * r; area=PI * r * r; printf("該圓的周長是%.2f,面積是%.2fa",c,area); } 擴充套件資料一、圓的面積公式: 圓的面積計算
void main()
#include using namespace std; int main(){ int r; coutr; cout
{
float r,c,s;
#define PI 3.14#include using namespace std;int main(){ double r,p,area; coutr; p=PI*r*2; cout
printf("請輸入圓的半徑: ");
#include using namespace std;int main(){#define PI 3.14159265 double r, c, s; cin>>r; c=2*PI*r; s=PI*r*r; cout
scanf("%f",&r);/*用於輸入一個浮點資料存放於地址&r*/
#include using namespace std; const double PI=3.14159; void main() { double r; //coutr; cout
c=2*3.1415926*r;/*計算周長,賦值給C*/
#include using namespace std; int main() { float r; coutr; cout
s=3.1415926*r*r; /*用於計算面積*/
#include #include using namespace std;int main(){ double r = -1; coutr; if(r >= 0) { cout
printf("周長c=%8.4f;n面積s=%8.4fn",c,s); /*周長,面積都是8個字元寬,4位小數*/
class circle { double rd;//半徑成員 public: circle() { rd=0.0; }//建構函式 cirle(double b) { rd=b; }//重構建構函式 double zc() { return 2*pi*rd; } double mj() { return pi*rd*rd; } }
}
編譯,連線,執行修改錯誤:
【題意分析】本題是最基礎的C++類使用習題,主要就是練習類的定義,私有成員,外部成員,成員變數,成員函式的使用等。下面這段程式碼每行都有註釋,除了實現題中的功能,還有一些基礎的示例對理解C++的類很有幫助,具體程式碼如下: 【程式程式碼】#include //
檢驗結果是否正確:
#include void main() {float r,s; #define p=3.14; printf("請輸入圓的半徑n"); scanf("%f",r); s=p*r*r; printf("圓的面積S=%6.2fn",s); } 介紹: C語言是一門通用計算機程式語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、
輸入半徑3.8,周長=2*3.8*3.1415926=28.8761;面積=3.1415926*3.8*3.8=45.3646,檢驗正確。
#include using namespace std;#define PI 3.141592653579893238;int main() { double r, A; cout > r; cout > A; double area = 2 * PI * r * r; double l = 2*PI*r * A/360.0; cout
擴充套件閱讀,以下內容您可能還感興趣。
C++問題:計算任意圓的周長和麵積
#復include<iostream>
using namespace std;
int main()
{
float r;
cout<<"請輸入半徑:";
cin>>r;
cout<<"該圓的周長是:"<<2*3.14*r<<endl;
cout<<"該圓的面制積是::<<r*r*3.14<<endl;
return 0;
}
計算 公式一樓兄弟,實現很簡知單 ,要多程式設計,道才可以進步
求C++程式設計大神: 輸入一個圓半徑r,當r>=0時,計算並輸出圓的面積和周長;否則,輸出提示資訊。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double r = -1;
cout<<"請輸入百半徑r:";
cin>>r;
if(r >= 0)
{
cout<<"圓的周長為度:"<<2*M_PI*r<<endl;
cout<<"圓的面積知為:"<<M_PI*r*r<<endl;
}
else
cout<<"圓的半徑不能為負道!"<<endl;
return 0;
}追問不好意思 我要C語言的追答#include <stdio.h>
#include <math.h>
int main()
{
double r = -1;
printf("請輸入半徑r:");
scanf("%lf", &r);
if(r >= 0)
{
printf("圓的周長為:%lfn", 2*M_PI*r);
printf("圓的面積為:%lfn", M_PI*r*r);
}
else
printf("圓的半徑不能為負!n");
return 0;
}
C++程式設計:定義一個圓類要求屬性為半徑,操作為計算圓的周長和麵積。
class circle
{
double rd;//半徑成度員
public:
circle()
{
rd=0.0;
}//構造版函式權
cirle(double b)
{
rd=b;
}//重構建構函式
double zc()
{
return 2*pi*rd;
}
double mj()
{
return pi*rd*rd;
}
}本回答被提問者採納
C++程式設計序題:(一)定義一個圓類XY,計算圓的面積和周長(). 要求:1.該類有一個私用的資料成員R,表示半徑;
【題意分析】
本題是最基礎的C++類使用習題,主要就是練習類的定義,私有成員,外部成員,成員變數,成員函式e68a847a6431333363376366的使用等。下面這段程式碼每行都有註釋,除了實現題中的功能,還有一些基礎的示例對理解C++的類很有幫助,具體程式碼如下:
【程式程式碼】
#include <iostream> //控制檯操作標頭檔案
class XY //定義一個類
{private: //私有成員關鍵字
double R; //圓的半徑
public: //公開成員關鍵字
XY(){R=0;} //無參建構函式
XY(double N){R=N;} //有參建構函式
int Set(double N){R=N;} //設定半徑的成員函式
double Get(){return R;} //獲得半徑的成員函式
double Grith() //求周長的成員函式
{return R*2*3.1415926;}//周長等於2∏R
double Area() //求面積的成員函式
{return R*R*3.1415926;}//面積等於∏R的平方
}; //類定義結束
int main() //主函式
{double t; //雙精度變數
XY O; //XY類物件
printf("請輸入半徑:"); //提示輸入半徑
scanf("%lf",&t); //從鍵盤接受半徑大小
O.Set(t); //以t的值設定物件O的半徑
t=O.Grith(); //通過成員函式Grith計算周長
pri0ntf("該圓周長為:%lfn",t); //輸出圓的周長
t=O.Area(); //通過成員函式Area計算面積
printf("該圓的面積為:%lfn",t);//輸出圓的周長
printf("n"); //換行
system("PAUSE"); //螢幕暫停,以便看到顯示結果
return 0;} //結束程式
【執行結果】
以上程式在DEV C++中執行通過,執行結果截圖如下:
C語言:程式設計輸入圓的半徑,計算圓的面積並輸出,輸出時保留2位小數。
#include<stdio.h>
void main()
{float r,s;
#define p=3.14;
printf("請輸入抄圓的半徑n");
scanf("%f",r);
s=p*r*r;
printf("圓的面積S=%6.2fn",s);
}
介紹:
C語言是一門通用計算機編襲程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處百理低階儲存器、產生少量的機器碼以及不需要任何執行環境支援便能執行的編度程語言。
參考資料
C語言:程式設計輸入圓的半徑,計算圓的面積並輸出,輸出時保留2位小數。.[引用時間2018-1-9]
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/25oqp6.html