C++ C++;将内容移动到不起作用的函数中
我正在做一个学校的项目,在这个项目中,我应该把不同的“FIXME”部分放到主函数之外的函数中。我以为我什么都做了,然后什么都没做。我重新开始,我把范围缩小到导致问题的功能。我将在下面的代码中标记它。这就是说,我可以很容易地将它格式化为函数,并且我的函数语法是正确的。但是,如果我在函数中有东西,答案是完全不同的,而不是没有。以下是没有函数的代码:C++ C++;将内容移动到不起作用的函数中,c++,function,C++,Function,我正在做一个学校的项目,在这个项目中,我应该把不同的“FIXME”部分放到主函数之外的函数中。我以为我什么都做了,然后什么都没做。我重新开始,我把范围缩小到导致问题的功能。我将在下面的代码中标记它。这就是说,我可以很容易地将它格式化为函数,并且我的函数语法是正确的。但是,如果我在函数中有东西,答案是完全不同的,而不是没有。以下是没有函数的代码: #include <iostream> #include <iomanip> #include <cmath> #i
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)
// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y) {
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}
// convert degree value to radians
double DegToRad(double deg) {
return ((deg * pi) / 180.0);
}
// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}
void PrintIntro() { //This function is going to print the intro to the game!
cout << "Welcome to Upset Fowl!\n";
cout << "The objective is to hit the Mean Swine by launching an Upset Fowl.\n";
}
int main() {
double t = 1.0; // time (s)
double fowlY = 0.0; // object's height above ground (m)
double fowlAngle = 0.0; // angle of launch of fowl (rad)
double fowlVel = 0.0; // velocity of fowl (m/s)
double fowlX = 0.0; // object's horiz. dist. from start (m)
double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)
double swineX = 0.0; // distance to swine (m)
double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
bool didHitSwine = false; // did hit the swine?
srand(time(0));
swineX = 50; //(rand() % 201) + 50; I took out the randomness so I can keep track of answers easily.
PrintIntro();
cout << "\nThe Mean Swine is " << swineX << " meters away.\n";
cout << "Enter fowl launch angle (deg): ";
cin >> fowlAngle;
fowlAngle = ((fowlAngle * pi) / 180.0); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> fowlVel;
// FIXME Make into a function called LaunchFaowl
do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t = t + 1.0;
} while (fowlY > 0.0); // while above ground
PrintUpdate(t, fowlX, fowlY);
fowlLandingX = fowlX;
// FIXME Make into a function called DtrmnIfHit
beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
cout << "Hit'em!!!" << endl;
didHitSwine = true;
}
else {
cout << "Missed'em..." << endl;
didHitSwine = false;
}
return 0;
}
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)
// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y) {
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}
// convert degree value to radians
double DegToRad(double deg) {
return ((deg * pi) / 180.0);
}
// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}
void PrintIntro() { //This function is going to print the intro to the game!
cout << "Welcome to Upset Fowl!\n";
cout << "The objective is to hit the Mean Swine by launching an Upset Fowl.\n";
}
void GetUsrInpt(double piggy, double slope, double Velocity) { // FIXME Make into a function called GetUsrInpt
cout << "\nThe Mean Swine is " << piggy << " meters away.\n";
cout << "Enter fowl launch angle (deg): ";
cin >> slope;
slope = ((slope * pi) / 180.0); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> Velocity;
}
int main() {
double t = 1.0; // time (s)
double fowlY = 0.0; // object's height above ground (m)
double fowlAngle = 0.0; // angle of launch of fowl (rad)
double fowlVel = 0.0; // velocity of fowl (m/s)
double fowlX = 0.0; // object's horiz. dist. from start (m)
double fowlLandingX = 0.0; // fowl’s horiz. dist. from start (m)
double swineX = 0.0; // distance to swine (m)
double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
bool didHitSwine = false; // did hit the swine?
srand(time(0));
swineX = 50; //(rand() % 201) + 50;
PrintIntro();
GetUsrInpt(swineX, fowlAngle, fowlVel);
// FIXME Make into a function called LaunchFaowl
do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t = t + 1.0;
} while (fowlY > 0.0); // while above ground
PrintUpdate(t, fowlX, fowlY);
fowlLandingX = fowlX;
// FIXME Make into a function called DtrmnIfHit
beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
cout << "Hit'em!!!" << endl;
didHitSwine = true;
}
else {
cout << "Missed'em..." << endl;
didHitSwine = false;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数双pi=3.14159265;
常量双重力=9.8;//地球重力(m/s^2)
//给定时间、角度、速度和重力
//更新x和y值
空位轨迹(双t、双a、双v、,
双倍(x、y和y){
x=v*t*cos(a);
y=v*t*sin(a)-0.5*grav*t*t;
返回;
}
//将度值转换为弧度
双除雾器(双度){
返回((deg*pi)/180.0);
}
//打印时间、x和y值
无效打印更新(双t、双x、双y){
cout斜率必须通过参考传递:
void GetUsrInpt(double piggy, double& slope, double Velocity) { // FIXME Make into a function called GetUsrInpt
cout << "\nThe Mean Swine is " << piggy << " meters away.\n";
cout << "Enter fowl launch angle (deg): ";
cin >> slope;
slope = ((slope * pi) / 180.0); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> Velocity;
}
以相同的方式处理在函数中更改其值的其他变量,您将完成此操作。斜率必须通过引用传递:
void GetUsrInpt(double piggy, double& slope, double Velocity) { // FIXME Make into a function called GetUsrInpt
cout << "\nThe Mean Swine is " << piggy << " meters away.\n";
cout << "Enter fowl launch angle (deg): ";
cin >> slope;
slope = ((slope * pi) / 180.0); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> Velocity;
}
以相同的方式处理其他变量,您可以在函数中更改它们的值,这样您就可以完成它
您已经设置了函数GetUsrInpt,将双参数传递为>“按值传递”因此,只有swineX、>fowlAngle和fowlVel的值的副本被传递给函数。在>函数中更改它们不会更改它们在主函数中的值。如果要>更改它们并让主函数知道,则必须使用指向变量的指针通过>引用传递。
像这样
//按如下方式调用函数:
GetUsrInpt( &swineX, &fowlAngle, &fowlVel );
您已经设置了函数GetUsrInpt,将双参数传递为>“按值传递”因此,只有swineX、>fowlAngle和fowlVel的值的副本被传递给函数。在>函数中更改它们不会更改它们在主函数中的值。如果要>更改它们并让主函数知道,则必须使用指向变量的指针通过>引用传递。
像这样
//按如下方式调用函数:
GetUsrInpt( &swineX, &fowlAngle, &fowlVel );
请发布请发布非常感谢,这很有效。我想我现在明白了如何构建它们。再次感谢!非常感谢,这很有效。我想我现在明白了如何构建它们。再次感谢!