C ++编程,无法编程查找速度和距离

时间:2016-02-29 20:03:59

标签: c++ physics

嘿我是一个初学者参加C ++课程的介绍,这是我的第一个任务。我几天前发布了这个,但我仍然有些迷失。我必须使用公式:d = v0 * t + 1/2 * g t ^ 2,并且v = v0 + g t。其中v0保持恒定在0并且g也保持恒定在9.807 m / s ^ 2。我一直收到一个错误,这是"预期不合格的id" {"令牌"在第一个{并且似乎无法解决它们,并且我确定这些代码不正确,所以你能帮我解决这个问题吗?

#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

const float GRAVITY = 9.807, INITIALVELOCITY = 0;
int seconds;


{
cout << "Please enter the time in seconds." << endl;
cin >> seconds;
}  //end function Time

int main(int argc, char *argv[])
{
float distance, velocity, seconds;
void getSeconds (void);

cout.setf (ios::fixed,ios::floatfield);

cin >> seconds;
while (seconds > 0) {

distance = INITIALVELOCITY * seconds + (0.5 * GRAVITY * pow(seconds, 2));
velocity = INITIALVELOCITY + (GRAVITY * seconds);

cout.precision (0);
cout << endl << "WHEN THE TIME IS" << seconds << "SECONDS THE DISTANCE"
"TRAVELED IS" << distance << "METERS THE VELOCITY IS" << velocity <<
"METERS PER SECOND.";
cout. precision(1);
cout<< seconds << distance << velocity << endl << endl;

}

system ("PAUSE");
return EXIT_SUCCESS;
} //end main

3 个答案:

答案 0 :(得分:4)

fadecolors <- function(colors, steps=4) {
    rr <- col2rgb(colors)
    unlist(Map(function(a) {
        rgb( seq(255, a[1],length.out=steps+1)[-1],
        seq(255,a[2],length.out=steps+1)[-1],
        seq(255,a[3],length.out=steps+1)[-1],
           maxColorValue=255)
    }, as.data.frame(rr)))
}

colors <- c("cornflowerblue","cornsilk4","red","orange")
barplot(data2, legend= rownames(data2), beside= TRUE,las=2,cex.axis=0.7,cex.names=0.7,ylim=c(0,3000), col=fadecolors(colors))

不是一件事。它是出现在任何函数之外的代码块。这在c ++中是不可能的,因为每个代码块都需要一个函数来输入它。起始进入点由

构成
   {
      cout << "Please enter the time in seconds." << endl;
      cin >> seconds;
   }  //end function Time

功能块。

你已经有了

int main() {
}

那里。

您是否真的在询问如何将该语句分解为函数?然后答案可能是

cin >> seconds;

并在 int getTime() { cout << "Please enter the time in seconds." << endl; cin >> seconds; return seconds; } //end function Time

main()

并非我真的建议您像这样组织代码。

答案 1 :(得分:0)

{
cout << "Please enter the time in seconds." << endl;
cin >> seconds;
}  //end function Time

我猜这应该是一个功能?如果是这样,请给它起个名字......

答案 2 :(得分:0)

现在它可以工作......不要从书籍中复制,书籍总是留下来定义或指定...总是尝试自己编写代码。如果你自己编写代码,那么Structure of a program再次成为现实;)

#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

const float GRAVITY = 9.807, INITIALVELOCITY = 0;
int seconds;

int time()
 {
   int seconds = 0;
   cout << "Please enter the time in seconds." << endl;
   cin >> seconds;
   return seconds;
 }  //end function Time

int main(int argc, char *argv[])
  {
   float distance, velocity, seconds;
   seconds = time();

   while (seconds > 0) {

    distance = INITIALVELOCITY * seconds + (0.5 * GRAVITY * pow(seconds, 2));
    velocity = INITIALVELOCITY + (GRAVITY * seconds);

    cout.precision (0);
    cout << endl << "WHEN THE TIME IS" << seconds << "SECONDS THE DISTANCE"
"TRAVELED IS" << distance << "METERS THE VELOCITY IS" << velocity <<
"METERS PER SECOND.";
    cout. precision(1);
    cout<< seconds << distance << velocity << endl << endl;
    seconds--;
}

 system ("PAUSE");
 return EXIT_SUCCESS;
} //end main