問題一、如果我編譯顯示 [Error] ld returned 1 exit status,但我並沒有還在執行的視窗,也把防毒軟體停用了,該怎麼處裡阿?
問題二、初學oop,真的很不會寫QQ我只會一般架構化的程式碼,可以教我到底要怎麼填入class內阿,因為main檔不可以動,所以一定要寫成oopˊˋ真的非常感謝~
題目:求一圓或兩圓的面積和周長
(第一個程式碼是oop,第二個程式碼是一般的寫法,且第二個應該是對的)
oop寫的程式碼
#include<math.h>
#define PI 3.1415926
#include
#include
using namespace std;
//最大的問題是我不知道如何分別存進c1,c2
class Circle{
public:
Circle();
float cal_area();
float cal_circumference();
float set_circle(float x, float y, float r);
float set_circle(float x, float y, float r, Circle);
void print();
private:
float center_x;
float center_y;
float radius;
};
Circle::Circle(){
center_x=0.0;
center_y=0.0;
radius=0.0;
};
//這個Circle要怎麼做?這個是constructor嗎?
float Circle::cal_area(){
return PIradiusradius;
}
float Circle::cal_circumference(){
return PIradius2;
}
float Circle::set_circle(float x,float y,float r){
center_x=x;
center_y=y,
radius=r;
cal_area();
cal_circumference();
}
//directly set attributes of circle and print the information of this circle
float set_circle(float x, float y, float r, Circle);
//該如何將c1或者c1c2分別存進去阿
//1. if the new circle c1:(x, y, radius) has no contact with c2 or only one contact point,
//directly set the attributes
//2.if the new circle c1 has two contact points with c2,
//always shrinkthe radius of new circle (c1) to become only one contact point
//3.if c1 (x, y) is exactly in the circumference of c2,
//make new circle be the same with c2
void Circle::print();
//cout結果
//main檔不可以動
int main(){
int type = 0;
float x = 0, y = 0, r = 0;
cout << "Enter type: ";
cin >> type;
//type1表示一個一個圓
//type2表示有兩個圓
if (type == 1){
Circle c1;
cout << "Enter X, Y, Radius: ";
cin >> x >> y >> r;
c1.set_circle(x, y, r);
c1.print();
}
if (type == 2){
float x2 = 0, y2 = 0, r2 = 0;
cout << "Enter X, Y, Radius, X2, Y2, Radius2: ";
cin >> x >> y >> r >> x2 >> y2 >> r2;
Circle c1;
Circle c2;
c2.set_circle(x2, y2, r2);
c1.set_circle(x, y, r, c2);
c1.print();
c2.print();
}
return 0;
}
以下有我用架構化寫的程式碼+測資(應該是對的)
#include
#include<math.h>
#define PI 3.1415926
using namespace std;
int main(){
int type = 0;
float x=0 , y=0 , r=0;
cout << "Enter type: ";
cin >> type;
if (type == 1){
cout<<"Enter X, Y, Radius: ";
cin >> x >> y >> r;
float area;
float cir;
area=PIrr;
cir=PI2r;
cout<<x<<""<<y<<""<<r<<""<<area<<""<<cir;
}
if (type == 2){
float x2 = 0, y2 = 0, r2 = 0;
cout << "Enter X, Y, Radius, X2, Y2, Radius2: ";
cin >> x >> y >> r >> x2 >> y2 >> r2;
float area;
float cir;
float area2;
float cir2;
float z;
z=(x-x2)*(x-x2)+(y-y2)*(y-y2);
if(z<0)
z=abs(z);
z=pow(z,0.5);//z=兩個圓心點的距離
//如果兩圓相離
if(z>=r+r2){
area=PI*r*r;
cir=PI*2*r;
cout<<x<<"_"<<y<<"_"<<r<<"_"<<area<<"_"<<cir<<endl;
area2=PI*r2*r2;
cir2=PI*2*r2;
cout<<x2<<"_"<<y2<<"_"<<r2<<"_"<<area2<<"_"<<cir2<<endl;
}
//如果一圓在另外一圓內
else if(z<abs(r-r2)){
//如果第二個圓比較大
if(r2>=r){
area2=PI*r2*r2;
cir2=PI*2*r2;
cout<<x2<<"_"<<y2<<"_"<<r2<<"_"<<area2<<"_"<<cir2<<endl;
cout<<x2<<"_"<<y2<<"_"<<r2<<"_"<<area2<<"_"<<cir2<<endl;
}
//如果第一個圓比較大
else{
area=PI*r*r;
cir=PI*2*r;
cout<<x<<"_"<<y<<"_"<<r<<"_"<<area<<"_"<<cir<<endl;
cout<<x<<"_"<<y<<"_"<<r<<"_"<<area<<"_"<<cir<<endl;
}
}
//如果兩圓相切
else if(z>abs(r-r2)&&z<r+r2){
int w;
if(r2>r){
w=z-r2;
}
else{
w=z-r;
}
area=PI*w*w;
cir=PI*2*w;
cout<<x<<"_"<<y<<"_"<<w<<"_"<<area<<"_"<<cir<<endl;
area2=PI*r2*r2;
cir2=PI*2*r2;
cout<<x2<<"_"<<y2<<"_"<<r2<<"_"<<area2<<"_"<<cir2<<endl;
}
}
return 0;
}
//測資
//Enter type: 1
//Enter X, Y, Radius: 3 3 3
//3_3_3_28.2743_18.8496
//Enter type: 2
//Enter X, Y, Radius, X2, Y2, Radius2: 0 0 3 5 0 4
//0_0_1_3.14159_6.28319
//5_0_4_50.2655_25.1327
非常感謝大家!!
拜託下次用(發問介面上方左邊算來第五個按鈕)把程式碼包起來
實在太傷眼了
#include <math.h>
#include <iostream>
#define PI 3.1415926
using namespace std;
class Circle{
public:
Circle();
float cal_area();
float cal_circumference();
float set_circle(float x, float y, float r);
float set_circle(float x, float y, float r, Circle cr);
void print();
float center_x;
float center_y;
float radius;
};
Circle::Circle() {
center_x=0.0;
center_y=0.0;
radius=0.0;
};
float Circle::cal_area(){
return PI * radius * radius;
}
float Circle::cal_circumference(){
return PI * radius * 2;
}
float Circle::set_circle(float x,float y,float r){
center_x=x;
center_y=y,
radius=r;
}
//directly set attributes of circle and print the information of this circle
float Circle::set_circle(float x, float y, float r, Circle cr) {
float z, x2, y2, r2;
x2 = cr.center_x;
y2 = cr.center_y;
r2 = cr.radius;
z=(x-x2)*(x-x2)+(y-y2)*(y-y2);
if(z<0)
z=abs(z);
z=pow(z,0.5);//z=兩個圓心點的距離
//1. if the new circle c1:(x, y, radius) has no contact with c2 or only one contact point,
//directly set the attributes
if(z>=r+r2){
center_x=x;
center_y=y,
radius=r;
} else {
//2.if the new circle c1 has two contact points with c2,
//always shrinkthe radius of new circle (c1) to become only one contact point
center_x=x;
center_y=y;
radius=z-r2;
}
//3.if c1 (x, y) is exactly in the circumference of c2,
//make new circle be the same with c2
if (z==r2) {
center_x=x2;
center_y=y2,
radius=r2;
}
}
//cout結果
void Circle::print() {
cout << center_x <<"_"<<center_y<<"_"<<radius<<"_"<<cal_area()<<"_"<<cal_circumference() << endl;
}
//main檔不可以動
int main(){
int type = 0;
float x = 0, y = 0, r = 0;
cout << "Enter type: ";
cin >> type;
//type1表示一個一個圓
//type2表示有兩個圓
if (type == 1){
Circle c1;
cout << "Enter X, Y, Radius: ";
cin >> x >> y >> r;
c1.set_circle(x, y, r);
c1.print();
}
if (type == 2){
float x2 = 0, y2 = 0, r2 = 0;
cout << "Enter X, Y, Radius, X2, Y2, Radius2: ";
cin >> x >> y >> r >> x2 >> y2 >> r2;
Circle c1;
Circle c2;
c2.set_circle(x2, y2, r2);
c1.set_circle(x, y, r, c2);
c1.print();
c2.print();
}
return 0;
}