2013-03-24 17 views
0

這是我到目前爲止的C++代碼:如何使用csv文件將用於創建形狀的數據從C++程序轉移到excel中?

程序要求用戶輸入他們的組名。如果名稱是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; 
} 
+0

我不知道C++,但不將'math'庫包含'pi'功能,這樣你就不會需要定義一個變量。除此之外,我不清楚你的問題是什麼。 – 2013-03-24 14:44:18

+0

歡迎來到StackOverflow,Rory。你當然會在你的程序中投入很多工作。然而,不清楚的是你的具體問題是什麼(正如Doug Glancy指出的那樣)。嘗試通過磨合讓你回到編碼階段的關鍵障礙來改善問題。 – chuff 2013-03-28 01:16:20

回答

0

我根本不瞭解C++,所以我不能告訴你如何讓它實際寫入文件。

如果你不想明確打開一個文件,你可以使用stdout?

因此,您可以從命令行運行程序並將結果傳輸到文件。

如:mymathapp.exe > result.csv

CSV格式是不難理解的。

每行代表一行,列數據只是該行上的分隔列表。 (你可以使用逗號,或製表符等)。字符串值通常是"Quoted",但它通常不是一個要求(excel,其他應用程序通常相當寬容)。

如何輸出一行到stdout的示例。

fprintf(stdout, "%d, %d, %d, %d\n", val1, val2, val3, val4);

相關問題