我正在做一个学校项目,在该项目中,我应该学习各个“ FIXME”部分,并将其提供给主要功能之外的功能。我以为我一切都正常,然后什么也没有。我重新开始,然后将范围缩小到导致问题的原因。我将在下面的代码中对其进行标记。就是说,我可以轻松地将其格式化为函数,并且如果我在函数中包含内容,则我的函数语法是正确的答案。这是没有功能的代码:
#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;
}
现在,我要问的是这些程序在哪里误入歧途?我找不到它们的任何理由,并且我搜索了所有变量,并且认为它们不应该成为问题。我该如何解决?
注意:我不希望人们为我处理所有的FIXME部分!我只想知道哪里出了问题,以便我照顾其余的人。
您已经将函数GetUsrInpt设置为将双精度参数传递为>“按值传递”,因此只有swineX,> fowlAngle和fowlVel值的副本才传递给该函数。在>功能中更改它们不会更改主功能中的值。如果要更改它们并使主要功能知道它,则必须使用指向变量的指针> reference传递。像这样
void GetUsrInpt(double* ptrPiggy, double* pSlope, double* pVelocity)
{
//...
cin >> *pPiggy;
cin >> *pSlope;
cin >> *pVelocity;
//...
}
//这样调用函数:
GetUsrInpt( &swineX, &fowlAngle, &fowlVel );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句