C ++项目帮助生成变量。 [英] C++ project help Generating variables.

查看:41
本文介绍了C ++项目帮助生成变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我必须制作一个程序来计算x

和2d空间中y点之间的距离。

代码基本上是这样的

1.用户说他们有多少积分(最多10个)


2.用户输入积分


3.使用sqrt ((x2-x1)^ 2 +(y2-y1)^ 2))它计算2点之间的距离




4.显示第一点和最后一点之间的长度。

我的问题是如何接受数据。我不知道如何改变

输入数量或如何声明变量。比如说用户

想要6分我怎么让程序知道只要问用户6点
点。然后我如何为每个

点做同样的计算。

i尝试使用while循环并且到目前为止是我的代码。

#include< iostream> ;;

#include< cmath.h>


使用命名空间std;


双倍长度(双倍xa,双倍xb,双倍ya,双倍yb)

{

双倍长度= 0;


length = sqrt(((xb-xa)*(xb-xa))+((yb-ya)*(yb-ya)));


返回(长度);


}


int main()


int points = 0 ;

int ans = 0;

双倍长度(双倍,双倍,双倍,双倍)

double xa = 0;

double xb = 0;

double ya = 0;

double yb = 0;


cout< <您要输入多少分(最多10个)?\ n \ n" ;;

cin>>分数;

while(积分1)

{

cout<<"请输入x值\ n" ;;

cin> > xa;

cout<<"请输入ay value \ n" ;;

cin>> ya;

cout<<"请输入x值\ n" ;;

cin>> xb;

cout<<"请输入ay value \ n" ;;

cin>> yb;


ans = ans + length(xa,xb,ya,yb)


积分=积分-1;

}


返回(0);


我正在使用VC ++

i知道代码有点蹩脚但是嘿这对正确有什么帮助

:)


提前感谢任何可以解决这个问题的天才。

Basicly i have to make a program that calculates the distance between x
and y points in 2d space.
the code basicly goes like this
1. User says how many points they have (max of 10)

2. User enters points

3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
between 2 points

4. It displays the length between the first and last point.
My problem is how do i accept the data. im not sure how to vary the
number of inputs or how to declare the variables. like say the user
wants 6 points how do i let the program know only to ask the user for 6
points. and then how do i do the same calculation for each of those
points.
i tried using a while loop and heres my code so far.

#include <iostream>;
#include <cmath.h>

using namespace std;

double length(double xa,double xb,double ya,double yb)
{
double length=0;

length=sqrt(((xb-xa)*(xb-xa))+((yb-ya)*(yb-ya)));

return (length);

}

int main()

int points=0;
int ans=0;
double length(double,double,double,double)
double xa=0;
double xb=0;
double ya=0;
double yb=0;

cout <<"How many points would you like to input (Max 10)?\n\n";
cin >>points;
while (points 1)
{
cout <<"Please enter an x value\n";
cin >>xa;
cout <<"Please enter a y value\n";
cin >>ya;
cout <<"Please enter an x value\n";
cin >>xb;
cout <<"Please enter a y value\n";
cin >>yb;

ans=ans+length(xa,xb,ya,yb)

points=points-1;
}

return (0);

Im using VC++
i know the codes a little crappy but hey thats what help is for right
:)

Thanks in advance to any genius who can sort this mess out.

推荐答案

11月27日上午8:07,纳特金" < jamesf ... @ dsl.pipex.comwrote:
On Nov 27, 8:07 am, "Nutkin" <jamesf...@dsl.pipex.comwrote:

基本上我必须制作一个程序来计算x之间的距离
和y点在2d空间。

代码基本上是这样的


1.用户说他们有多少分(最多10个)


2.用户输入积分


3.使用sqrt((x2-x1)^ 2 +(y2-y1)^ 2))计算距离

两点之间


4.显示第一点和最后一点之间的长度。


我的问题是我如何接受数据。我不知道如何改变

输入数量或如何声明变量。比如说用户

想要6分我怎么让程序知道只要问用户6点
点。然后我如何对每个

点进行相同的计算。
Basicly i have to make a program that calculates the distance between x
and y points in 2d space.
the code basicly goes like this

1. User says how many points they have (max of 10)

2. User enters points

3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
between 2 points

4. It displays the length between the first and last point.

My problem is how do i accept the data. im not sure how to vary the
number of inputs or how to declare the variables. like say the user
wants 6 points how do i let the program know only to ask the user for 6
points. and then how do i do the same calculation for each of those
points.



我想要使用容器级来存储积分,这很像:/ b $ b这样的东西:


#include< iostream>

#include< vector>

#include< map //获取std :: pair


int main()

{

std :: vector< std :: pair< double,double points; //存储积分

int nrPoints;


std :: cout<< 点数:;

std :: cin> nrPoints;


for(int i = 0; i< nrPoints; + + i)// for循环很好,但也可以使用



{

double x,y;

std :: cout<< 输入x:" ;;

std :: cin> x;

std :: cout<< 输入y:;

std :: cin> y;


points.push_back(std :: make_pair(x,y)) ; //将点添加到

集合

}


//计算距离


返回0;

}


然后你可以像这样访问积分:


分[2] .first //第三点的x值(从0开始)

points [2] .second //第二点的y值


为了得到距离我会做这样的事情:


for(int i = 0; i< nrPoints - 1; ++ i)/ / notice nrPoints -1

{

ans + = length(points [i] .first,points [i + 1] .first,points [i] .second,

积分[i + 1]。秒)

}


您还可以使用两个向量,一个用于x-和一个用于y值。


-

Erik Wikst?m

I thin you want to use a container-class to store the points in,
something like this:

#include <iostream>
#include <vector>
#include <map// to get std::pair

int main()
{
std::vector<std::pair<double, double points; // stores the points
int nrPoints;

std::cout << "Number of points: ";
std::cin >nrPoints;

for (int i = 0; i < nrPoints; ++i) // For-loops are nice, but can use
while too
{
double x, y;
std::cout << "Enter x: ";
std::cin >x;
std::cout << "Enter y: ";
std::cin >y;

points.push_back(std::make_pair(x,y)); // add the point to the
collection
}

// Calculate distance

return 0;
}

Then you can access the points just like this:

points[2].first // The x-value of the third point (starts from 0)
points[2].second // The y-value of the second point

To get the distance I''d do something like this:

for (int i = 0; i < nrPoints - 1; ++i) // Notice nrPoints -1
{
ans += length(points[i].first, points[i+1].first, points[i].second,
points[i+1].second)
}

You could also use two vectors, one for x- and one for y-values.

--
Erik Wikst?m


er****@student.chalmers.se < er **** @ student.chalmers.sewrote:
er****@student.chalmers.se <er****@student.chalmers.sewrote:

>我很瘦,你想用一个容器类来存储点,
这样的东西:

#include< iostream>
#include< vector>
#include< map //获取std :: pair
>I thin you want to use a container-class to store the points in,
something like this:

#include <iostream>
#include <vector>
#include <map// to get std::pair



另一种可能性是使用std :: complex< doublefor(x,y)对。


Steve

Another possibility is using std::complex<doublefor the (x,y) pair.

Steve



er **** @ student.chalmers.se 写道:

11月27日上午8:07,Nutkin < jamesf ... @ dsl.pipex.comwrote:
On Nov 27, 8:07 am, "Nutkin" <jamesf...@dsl.pipex.comwrote:

基本上我必须制作一个程序来计算x之间的距离
和y点在2d空间。

代码基本上是这样的


1.用户说他们有多少分(最多10个)


2.用户输入积分


3.使用sqrt((x2-x1)^ 2 +(y2-y1)^ 2))计算距离

两点之间


4.显示第一点和最后一点之间的长度。


我的问题是我如何接受数据。我不知道如何改变

输入数量或如何声明变量。比如说用户

想要6分我怎么让程序知道只要问用户6点
点。然后我如何对每个

点进行相同的计算。
Basicly i have to make a program that calculates the distance between x
and y points in 2d space.
the code basicly goes like this

1. User says how many points they have (max of 10)

2. User enters points

3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
between 2 points

4. It displays the length between the first and last point.

My problem is how do i accept the data. im not sure how to vary the
number of inputs or how to declare the variables. like say the user
wants 6 points how do i let the program know only to ask the user for 6
points. and then how do i do the same calculation for each of those
points.



我很瘦,你想使用容器级来存储积分,

这样的东西:


#include< iostream>

#include< vector>

#include< map //获取std :: pair


int main()

{

std :: vector< std :: pair< double,double points; //存储积分

int nrPoints;


std :: cout<< 点数:;

std :: cin> nrPoints;


for(int i = 0; i< nrPoints; + + i)// for循环很好,但也可以使用



{

double x,y;

std :: cout<< 输入x:" ;;

std :: cin> x;

std :: cout<< 输入y:;

std :: cin> y;


points.push_back(std :: make_pair(x,y)) ; //将点添加到

集合

}


//计算距离


返回0;

}


然后你可以像这样访问积分:


分[2] .first //第三点的x值(从0开始)

points [2] .second //第二点的y值


为了得到距离我会做这样的事情:


for(int i = 0; i< nrPoints - 1; ++ i)/ / notice nrPoints -1

{

ans + = length(points [i] .first,points [i + 1] .first,points [i] .second,

积分[i + 1]。秒)

}


您还可以使用两个向量,一个用于x-和一个用于y值。


-

Erik Wikst?m


I thin you want to use a container-class to store the points in,
something like this:

#include <iostream>
#include <vector>
#include <map// to get std::pair

int main()
{
std::vector<std::pair<double, double points; // stores the points
int nrPoints;

std::cout << "Number of points: ";
std::cin >nrPoints;

for (int i = 0; i < nrPoints; ++i) // For-loops are nice, but can use
while too
{
double x, y;
std::cout << "Enter x: ";
std::cin >x;
std::cout << "Enter y: ";
std::cin >y;

points.push_back(std::make_pair(x,y)); // add the point to the
collection
}

// Calculate distance

return 0;
}

Then you can access the points just like this:

points[2].first // The x-value of the third point (starts from 0)
points[2].second // The y-value of the second point

To get the distance I''d do something like this:

for (int i = 0; i < nrPoints - 1; ++i) // Notice nrPoints -1
{
ans += length(points[i].first, points[i+1].first, points[i].second,
points[i+1].second)
}

You could also use two vectors, one for x- and one for y-values.

--
Erik Wikst?m




我觉得这有点过头但是我可以看到你要来的地方

来自。所以我会更多地研究它,看看我是否可以使用它来获得

。但是我们已经被教过了矢量,所以我不确定它是否有效。
是有效的。非常感谢你给我一些选择,我现在可以用现金来讨价还价。


如果有人有更基本的想法会很棒..



I think thats a bit over my head but i can see where you are coming
from. So i shall study it a bit more and see if i can get away with
using it. But we havnt been taught vectors yet so im not sure if it
will be valid. Thanks so much for giving me some options im gonna have
a play about with ti now.

If anyone has a more basic idea would be awesome..


这篇关于C ++项目帮助生成变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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