到目前为止,这是我的 C++ 代码:
该程序要求用户输入他们的组名。如果名称是 c4,则程序会询问 x 和 y 坐标、形状中圆的半径以及沿线的每个点之间的间距。然后程序将计算形状的点和角度。形状应该是一个圆,三条线从圆的边缘开始,分别为 22.5、45 和 67.5 度。
/ computinggroup.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream> //necessary for cout and endl
#include <cmath> //necessary for mathematic calculations
#include <string> //necessary for declaration of letters
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//group project
//Group c4
//date: 22/03/2013
{
string c; //declaration of letter
int d = 0; //inisialise group name elements
int i, b;
double pi = 3.141592654; //define pi for trig calculations
double r, a = 0;
double x, x1 = 0;
double y, y1 = 0;
double xa, xb, ya, yb = 0;
double increment, spacing, numofpoints;
double AngleB;
double AngleV;
double AngleTheta1;
double AngleTheta2;
/****************************************************************************
* SECTION 1 - Group name is entered by user, the programme continues if it is
* the correct name, if not a message displays and the programme stops
*****************************************************************************/
cout << "Enter letter from group name: ";
cin >> c;
cout << "Enter number from group name: ";
cin >> d;
if (c == "C", d == 4)
{
cout << "Group " << c << d;
cout << endl;
}
else
{
cout << "Incorrect group name entered, the programme cannot continue. Press any key to exit";
cin >> d;
return 0;
}
/****************************************************************************
* SECTION 2 - User enters values for centre point of the circle
* and radius, the programme creates x and y coordinate points for 360 points
* around a circle
*****************************************************************************/
cout << "Enter x coordinate for the centre of the circle: ";
cin >> x1;
cout << "Enter y coordinate for the centre of the circle: ";
cin >> y1;
cout << "Enter radius of the circle: ";
cin >> r;
cout << "Enter spacing required between each point along the lines: ";
cin >> spacing;
numofpoints = (2 * pi * r)/spacing;
increment = 360/numofpoints;
for (i = 0; i < 360 ; i+=increment) //loop the calculation for values of x for i = 0 to 360 in intervals of 'increment'
{
x = x1 + (r * cos((i * pi)/180));
cout << "(" << x << ",";
y = y1 + (r * sin((i * pi)/180));
cout << y << ")";
cout << endl;
AngleB = acos(x/pow((pow(x, 2))+(pow(y, 2)), 0.5))*(180/pi);
cout<<"\nAngle 'B' = "<<AngleB<<" Degrees"<<endl;
AngleV = acos(((pow(x, 2))+(pow(y, 2))+(10000)-(4225))/(200*(pow(((pow(x, 2))+(pow(y, 2))), 0.5))))* (180/pi);
cout<<"Angle 'V' = "<<AngleV<<" Degrees"<<endl;
AngleTheta1 = AngleB - AngleV;
cout<<"Theta 1 = "<<AngleTheta1<<" Degrees"<<endl;
AngleTheta2 =(asin(pow(((sin(AngleV*pi/180))*(pow(((pow(x, 2))+(pow(y, 2))), 0.5))/65), 0.5)))*(180/pi);
cout<<"Theta 2 = "<<AngleTheta2<<" Degrees"<<endl;
}
/****************************************************************************
* SECTION 3 - start and end coordinates for each of the 3 lines
* The naming is as follows: example, 'xa'; 'x' means the x coordinate,
* 'a' means start point as oppossed to 'b' for end point
*****************************************************************************/
cout << endl;
cout << "Coordinates of three lines: ";
cout << endl;
//the first line is at 22.5 deg, trigonometry is used to create the function
{
double a = 22.5;
xa = x1 + (r * cos((a * pi)/180));
ya = y1 + (r * sin((a * pi)/180));
AngleB = acos(xa/pow((pow(xa, 2))+(pow(ya, 2)), 0.5))*(180/pi);
cout<<"\nAngle 'B' = "<<AngleB<<" Degrees"<<endl;
AngleV = acos(((pow(xa, 2))+(pow(ya, 2))+(10000)-(4225))/(200*(pow(((pow(xa, 2))+(pow(ya, 2))), 0.5))))* (180/pi);
cout<<"Angle 'V' = "<<AngleV<<" Degrees"<<endl;
AngleTheta1 = AngleB - AngleV;
cout<<"Theta 1 = "<<AngleTheta1<<" Degrees"<<endl;
AngleTheta2 =(xa*sin(pow(((sin(AngleV*pi/180))*(pow(((pow(xa, 2))+(pow(ya, 2))), 0.5))/65), 0.5)))*(180/pi);
cout<<"Theta 2 = "<<AngleTheta2<<" Degrees"<<endl;
xb = x1 + (2 * r + (r * cos((a * pi)/180)));
yb = y1 + (2 * r + (r * sin((a * pi)/180)));
AngleB = acos(xb/pow((pow(xb, 2))+(pow(yb, 2)), 0.5))*(180/pi);
cout<<"\nAngle 'B' = "<<AngleB<<" Degrees"<<endl;
AngleV = acos(((pow(xb, 2))+(pow(yb, 2))+(10000)-(4225))/(200*(pow(((pow(xb, 2))+(pow(yb, 2))), 0.5))))* (180/pi);
cout<<"Angle 'V' = "<<AngleV<<" Degrees"<<endl;
AngleTheta1 = AngleB - AngleV;
cout<<"Theta 1 = "<<AngleTheta1<<" Degrees"<<endl;
AngleTheta2 =(xb*sin(pow(((sin(AngleV*pi/180))*(pow(((pow(xb, 2))+(pow(yb, 2))), 0.5))/65), 0.5)))*(180/pi);
cout<<"Theta 2 = "<<AngleTheta2<<" Degrees"<<endl;
cout << "Line at " << a << " degrees = (" << xa << ", " << ya << ") to (" << xb << ", " << yb << ")";
cout << endl;
}
//the second line is at 45 deg
{
double a = 45;
xa = x1 + (r * cos((a * pi)/180));
ya = y1 + (r * sin((a * pi)/180));
AngleB = acos(xa/pow((pow(xa, 2))+(pow(ya, 2)), 0.5))*(180/pi);
cout<<"\nAngle 'B' = "<<AngleB<<" Degrees"<<endl;
AngleV = acos(((pow(xa, 2))+(pow(ya, 2))+(10000)-(4225))/(200*(pow(((pow(xa, 2))+(pow(ya, 2))), 0.5))))* (180/pi);
cout<<"Angle 'V' = "<<AngleV<<" Degrees"<<endl;
AngleTheta1 = AngleB - AngleV;
cout<<"Theta 1 = "<<AngleTheta1<<" Degrees"<<endl;
AngleTheta2 =(xa*sin(pow(((sin(AngleV*pi/180))*(pow(((pow(xa, 2))+(pow(ya, 2))), 0.5))/65), 0.5)))*(180/pi);
cout<<"Theta 2 = "<<AngleTheta2<<" Degrees"<<endl;
xb = x1 + (2 * r + (r * cos((a * pi)/180)));
yb = y1 + (2 * r + (r * sin((a * pi)/180)));
AngleB = acos(xb/pow((pow(xb, 2))+(pow(yb, 2)), 0.5))*(180/pi);
cout<<"\nAngle 'B' = "<<AngleB<<" Degrees"<<endl;
AngleV = acos(((pow(xb, 2))+(pow(yb, 2))+(10000)-(4225))/(200*(pow(((pow(xb, 2))+(pow(yb, 2))), 0.5))))* (180/pi);
cout<<"Angle 'V' = "<<AngleV<<" Degrees"<<endl;
AngleTheta1 = AngleB - AngleV;
cout<<"Theta 1 = "<<AngleTheta1<<" Degrees"<<endl;
AngleTheta2 =(xb*sin(pow(((sin(AngleV*pi/180))*(pow(((pow(xb, 2))+(pow(yb, 2))), 0.5))/65), 0.5)))*(180/pi);
cout<<"Theta 2 = "<<AngleTheta2<<" Degrees"<<endl;
cout << "Line at " << a << " degrees = (" << xa << ", " << ya << ") to (" << xb << ", " << yb << ")";
cout << endl;
}
//the third line is at 67.5 deg
{
double a = 67.5;
xa = x1 + (r * cos((a * pi)/180));
ya = y1 + (r * sin((a * pi)/180));
AngleB = acos(xa/pow((pow(xa, 2))+(pow(ya, 2)), 0.5))*(180/pi);
cout<<"\nAngle 'B' = "<<AngleB<<" Degrees"<<endl;
AngleV = acos(((pow(xa, 2))+(pow(ya, 2))+(10000)-(4225))/(200*(pow(((pow(xa, 2))+(pow(ya, 2))), 0.5))))* (180/pi);
cout<<"Angle 'V' = "<<AngleV<<" Degrees"<<endl;
AngleTheta1 = AngleB - AngleV;
cout<<"Theta 1 = "<<AngleTheta1<<" Degrees"<<endl;
AngleTheta2 =(xa*sin(pow(((sin(AngleV*pi/180))*(pow(((pow(xa, 2))+(pow(ya, 2))), 0.5))/65), 0.5)))*(180/pi);
cout<<"Theta 2 = "<<AngleTheta2<<" Degrees"<<endl;
xb = x1 + (2 * r + (r * cos((a * pi)/180)));
yb = y1 + (2 * r + (r * sin((a * pi)/180)));
AngleB = acos(xb/pow((pow(xb, 2))+(pow(yb, 2)), 0.5))*(180/pi);
cout<<"\nAngle 'B' = "<<AngleB<<" Degrees"<<endl;
AngleV = acos(((pow(xb, 2))+(pow(yb, 2))+(10000)-(4225))/(200*(pow(((pow(xb, 2))+(pow(yb, 2))), 0.5))))* (180/pi);
cout<<"Angle 'V' = "<<AngleV<<" Degrees"<<endl;
AngleTheta1 = AngleB - AngleV;
cout<<"Theta 1 = "<<AngleTheta1<<" Degrees"<<endl;
AngleTheta2 =(xb*sin(pow(((sin(AngleV*pi/180))*(pow(((pow(xb, 2))+(pow(yb, 2))), 0.5))/65), 0.5)))*(180/pi);
cout<<"Theta 2 = "<<AngleTheta2<<" Degrees"<<endl;
cout << "Line at " << a << " degrees = (" << xa << ", " << ya << ") to (" << xb << ", " << yb << ")";
cout << endl;
}
cout << "Press any key to exit ... "; //the user has the option to exit
cin >> b;
return 0;
}
return 0;
}