我该如何打印表格? [英] How do I print the table ?

查看:85
本文介绍了我该如何打印表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的射弹方程创建一个程序,绘制并显示射弹的路径。该程序必须具有以下内容:

1)程序的主要功能将调用所有支持函数,但主函数不执行任何计算,它只调用其他函数。

)一种功能,要求用户输入介于10和100米/秒之间的速度和15到75度之间的角度。此功能必须只获得初始速度和角度。

1)从用户请求输入速度和角度的函数

2)单独的函数来计算x,y放入数组的时间值。

3)显示值和最大值表的单独函数

绘制输出的单独函数
3)一个单独的函数,它将计算X,Y和时间值并将它们存储在数组中。 X和Y计算的每个计算都必须存储在数组位置。

4)一个单独的函数,用于绘制输出图(类似于给出的示例)。

5)一个函数,它在数组中显示存储在数组中的每个计算表中的数据,如:

X距离Vx Vert距离Vy Time

:::::

:::::

:::::

: ::::



6)最后一个函数应在表格后打印出最大高度,最大高度时间,总飞行时间和最大范围。





公式

水平距离,x = Vxt

水平速度, Vx = Vx0

垂直距离,y = Vyo * t-.5gt ^ 2

垂直速度,Vy = Vyo -gt



其中,Vx是沿轴的速度

Vxo是沿x轴的初始速度

Vy是沿y轴的速度

Vxo是沿y轴的初始速度

g是重力

t是时间



飞行时间,t = 2VoSin(角度)/ g

高度,H = Vo ^ 2sin ^ 2(角度)/ 2g

范围,r = Vo ^ 2Sin(2angle)/ g



我尝试过:



Using the equations for a projectile below create a program that graphs and displays the path of a projectile. The program must have the following:
1)The main function of the program will call all the supporting functions but the main function performs no calculations, it just calls other functions.
2) A function that request the user type in the intial velocity between 10 and 100 m/sec and an angle between 15 to 75 degrees. This function must only get the intial velocity and angle.
1) A function that request the input velocity and angle from user
2) Separate function to calculate the x, y and time values that are placed into an array.
3) A separate function to display the table of values and maximum values
A separate function that graphs the output
3) A separate function that will calculate the X , Y and time values and store those in an array. Each calculation for both the X and Y calculations must be stored in an array location.
4) A separate function that graphs the output (similar to the example given).
5) A function that displays the data from the array in a table of each of the calculations that was stored in the array , something like:
X distance Vx Vert distance Vy Time
: : : : :
: : : : :
: : : : :
: : : : :

6) The last function should print out after the tables what would be maximum height, Time at max height, total time of flight and max range.


Formulas
Horizontal distance, x=Vxt
Horizontal velocity, Vx = Vx0
Vertical distance, y=Vyo*t-.5gt^2
vertical velocity, Vy= Vyo -gt

where,Vx is the velocity along axis
Vxo is the initial velocity along x axis
Vy is the velocity along y axis
Vxo is the initial velocity along y axis
g is gravity
t is time

Time of flight, t= 2VoSin(angle)/g
Height,H= Vo^2sin^2(angle)/2g
range, r=Vo^2Sin(2angle)/g

What I have tried:

#include <iostream>
#include <cmath>
#include <iomanip>
#define pi 3.14159265

using namespace std;


//functions
float cannon();
float calcx();
float calcy();
float calch();
float calcr();
float flightt();


float graph();
float time();
float array1[];
float array2[];
float array3[];

void display();

//inputs-----
float vi;//intial velocity
float angle;
float x;
float vx;
double w,o;
double TotalFlight;

double z;
double stime;


float height;
float range;


int main() {
	int stop;
	cannon();
	display();
	
	cin >> stop;
	return 0;	
}

float cannon() {

	cout << "Enter inital velocity between 10 and 100 m/sec" << endl;
	cin >> vi;
	cout << "Enter an angle between 15 to 75 degrees" << endl;
	cin >> angle;

	return angle;
}
float calcx() {//HORIZONTAL DISTANCE
		x = vi * z;
return x;

}
float calcy() { //HORIZONTAL VELOCSIT

	vx = (vi*z) - ((.5)*9.8*powf(z, 2));

	return vx;
}
float calch() {//maximum height
	
	height = ((powf(vi, 2))*(powf(sin(angle*(2 * pi / 360)), 2))) / (2 * 9.8);
	
	return height;
}
float calcr() {//horizontal range
	
	range = (powf(vi, 2))* sin((2 * angle) * (2 * pi / 360)) / 9.8;
	
	return range;
}
float flightt() {//flight time
		TotalFlight = (2 * vi)*(sin(angle*(2 * pi / 360))) / 9.8;
	
		o = TotalFlight;
	return TotalFlight;
}



void display() {

	cout << setw(12) << "x-distance" << setw(14) << "X-Velocity" << setw(24) << "Height" << setw(24) << "(Horizontal distance)" << setw(24) << "Time of flight" << endl;
	for (w = 0; w < TotalFlight; w=w+ 1) {
		cout << w << endl;
	}
	for (w = 0; w < range; w = w + .01) {
		cout << w << endl;
	}
	for (w = 0; w < height; w = w + .01) {
		cout << w << endl;
	}


	for (z = 0; z <= TotalFlight; z= z + .1) {
		cout << setw(12) << calcx() << setw(14) << calcy() << setw(24) << calch() << setw(24) << calcr() << setw(24) << flightt() << setw(30) << endl;
	}	
}

推荐答案

必须格式化数据的输出。这是最好的字符串格式化示例。仔细阅读示例并尝试一下。



函数可以输入参数如下:

The output you must format the data. Here is the best example for string formatting. Read the example carefully and try it.

Functions can have input parameters like:
float calcx(float x, float vi)



因为您使用的是C ++,所以应该考虑为数据使用类或结构,尽可能避免使用全局变量。代码更清晰。喜欢


Because you are in C++ you should consider using classes or structs for the data, avoid globals as far as possible. It is clearer code. Like

class Cannon
{
  float speed;
  float angle;
  void askForData();
};

你的老师会喜欢它; - )

Your teacher will like it ;-)


我同意KarstenK。我会为此做一个类,你的函数应该是它的方法。您将获得所有公式并且已声明数据,因此您只需在数据上应用公式。一些事情 - 基本的三角函数与双精度函数一起工作,所以我会对所有数据使用该类型。我没有看到时间使用的度量单位所以我会使用十分之几秒。如果这导致过多的数据,那么整秒(1.0)或更长时间缩小它。我不喜欢单字母变量名,除了循环计数器,当然不是全局变量。我还想详细说出方法名称。



这是一个类的开头:
I agree with KarstenK. I would make a class for this and your functions should be its methods. You are given all of the formulas and you have the data declared so you just have to apply the formulas on the data. A few things - the basic trigonometric functions work with doubles so I would use that type for all data. I didn't see a unit of measure to use for time so I would use tenths of seconds. If that results in too much data then shrink it whole seconds (1.0) or more. I don't like single letter variable names except for loop counters and certainly not for global variables. I also like to spell out methods names a bit more.

Here is a start at a class:
class Cannon
{
public:  // methods
    Cannon();    // constructor - initialize the members
    ~Cannon();   // destructor

    // the basic formulas

    double CalcHorizontalDistance( double t )
    {
        return m_VelocityHorz * t;
    }

    double CalcVerticalDistance( double t )
    {
        return ( m_VelocityVert * t ) - ( 0.5 * m_Gravity * t * t );
    }

    double CalcTimeOfFlight()
    {
        return 2.0 * m_InitVelocity * sin( m_InitAngle ) / m_Gravity;
    }

    void    GetInputs();
    void    GenerateTable();
    void    Display();

public:  // members
    double     m_InitVelocity;
    double     m_InitAngle;     // input as degrees, stored as radians
    double     m_FlightTime;
    double     m_Gravity;       // really should be a constant
    double     m_VelocityHorz;
    double     m_VelocityVert;

    double *   m_DistanceHorz;
    double *   m_DistanceVert;
    double *   m_Times;
};

Cannon::Cannon()  // constructor
{
    m_DistanceHorz = nullptr;
    m_DistanceVert = nullptr;
    m_Times = nullptr;
    m_Gravity = 9.8;
}

Cannon::~Cannon()  // destructor
{
    delete [] m_DistanceHorz;
    delete [] m_DistanceVert;
    delete [] m_Times;
}

// to allocate an array of doubles do this:
// m_Times = new double[timeEntries];

现在,您必须从输入中获取输入,并使用公式确定飞行时间。给定飞行时间,然后您可以为表分配数组。由于时间单位是十分之几秒,因此将时间缩放10,这将告诉您分配数组的大小。然后编写一个循环,以0.1秒的增量逐步计算时间,直到您到达航班结束。每次计算水平和垂直距离,你就有了你的桌子。我认为你已经足够完成这个计划了。我不想为你写一切,所以我会把剩下的留给你。如果你遇到困难,那就提出更多问题,但关键是要尝试。祝你好运。

Now, you have to get the inputs and from the inputs you determine the flight time with the formula. Given a flight time then you can allocate the arrays for the table. Since the unit of time is tenths of seconds, scale the time by 10 and that will tell you how big to allocate the arrays. Then write a loop that steps through time in increments of 0.1 seconds until you have reached the end of the flight. Calculate horizontal and vertical distances at each time and you have your table. I think you have enough to finish up this program. I don't want to write everything for you so I will leave the rest up to you. If you get stuck then ask more questions but the key thing is to try things. Best of luck.


这篇关于我该如何打印表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆