本文介绍了如何计算总距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何实现这个
d = squareroot((x1 - x2)square +(y1 - y2)square)
这里是代码
我想这个公式使用inputx和inputy。
------ -------------------------------------------------- --------------------
how to implement this
d = squareroot (( x1 - x2)square + (y1 - y2) square)
here are the codes
I am suppose to use inputx and inputy for this formula.
----------------------------------------------------------------------------
#include "MissionPlan.h"
using namespace std;
void MissionPlan::printMainmenu() //Function to print out Main Menu
{
"---------------------------------------------------"<<endl<<
"Welcome to Mission Plan program! \n"<<endl<<
"1) Input statistic data"<<endl<<
"2) Compute civ index value (for all records)"<<endl<<
"3) Print top 5 exploration destinations"<<endl<<
"4) Print total distance"<<endl<<
"5) Exit"<<endl<<endl<<
"Plese enter your choice : ";
}
void MissionPlan::bubbleSort(PointTwoD array[],const int SIZE)//Bubble sort function for array
{
int i,j;
for(i=0;i<size;i++)>
{
for(j=0;j<i;j++)>
{
if(array[i].getcivIndex()>array[j].getcivIndex())
{
PointTwoD tempPTD;
tempPTD.setcivIndex(array[i].getcivIndex());
tempPTD.setxy(array[i].getx(),array[i].gety());
array[i].setcivIndex(array[j].getcivIndex());
array[j].setcivIndex(tempPTD.getcivIndex());
array[i].setxy(array[j].getx(),array[j].gety());
array[j].setxy(tempPTD.getx(),tempPTD.gety());
}
}
}
}
int MissionPlan::main() // switching choices for different output
{
int choice, inputx, inputy, inputPlanets, inputMoons, entry = 0;
string inputsunType;
float inputParticulateDensity, inputPlasmaDensity;
const int SIZE = 20;
LocationData newLocationData[SIZE];
PointTwoD newPointTwoD[SIZE];
printMainmenu();
cin >> choice;
while(choice != 999)
{
switch(choice)
{
case 1: // entry 1
{
while(entry<size)>
{
cout<<"[Input statistical data]"<<endl;
cout<<"Please enter x-ordinate : ";
cin>>inputx;
newPointTwoD[entry].setx(inputx);
cout<<"Please enter y-ordinate : ";
cin>>inputy;
newPointTwoD[entry].sety(inputy);
cout<<"Please enter sunType (Type) : ";
cin>>inputsunType;
newLocationData[entry].setsunType(inputsunType);
cout<<"Please enter no of Earth-Like Planets : ";
cin>>inputPlanets;
newLocationData[entry].setnoOfEarthLikePlanets(inputPlanets);
cout<<"Please enter no of Earth-Like Moons : ";
cin>>inputMoons;
newLocationData[entry].setnoOfEarthLikeMoons(inputMoons);
cout<<"Please enter the average particulate density : ";
cin>>inputParticulateDensity;
newLocationData[entry].setaveParticulateDensity(inputParticulateDensity);
cout<<"Please enter the average plasma density : ";
cin>>inputPlasmaDensity;
newLocationData[entry].setavePlasmaDensity(inputPlasmaDensity);
cout<<endl<<"Record successfully stored. Going back to main menu"<<endl;
newPointTwoD[entry].setlocationData(newLocationData[entry]);
++entry;
printMainmenu();
cin>>choice;
break;
};
}break;
case 2 : //entry 2
{
int x = 0;
float computeCivIndex[SIZE];
for(int x = 0;x<size;)>
{
computeCivIndex[x] = newLocationData[x].computeCivIndex(newLocationData[x].getsunType(),newLocationData[x].getnoOfEarthLikePlanets(), newLocationData[x].getnoOfEarthLikeMoons(), newLocationData[x].getaveParticulateDensity(), newLocationData[x].getavePlasmaDensity());
newPointTwoD[x].setcivIndex(computeCivIndex[x]);
++x;
}
cout<<endl<<"Computation completed! ( "<<entry<<" records were updated )"<<endl<<endl<<endl<<endl<<endl;
printMainmenu();
cin>>choice;
}break;
case 3 : //entry 3
{
cout<<endl<<"Total no. of records available = "<<entry<<endl<<
"Printing top 5 exploration destination"<<endl;
bubbleSort(newPointTwoD,SIZE);
for(int x = 0;x<5;++x)
{
newPointTwoD[x].toString();
}
cout<<"Done!"<<endl<<endl<<endl<<endl<<endl;
printMainmenu();
cin>>choice;
}break;
case 4:
推荐答案
double d = sqrt ((( x1 - x2)*( x1 - x2)) + ((y1 - y2)*(y1 - y2)));
您必须包含< math.h>
,因为函数 sqrt
被使用。
提示:始终在您的陈述中正确使用括号。大多数情况下,不正确使用括号会导致未定义的行为。
You must include <math.h>
, because the function sqrt
is used.
Tip: Always use brackets properly in your statements. Most often, not using brackets properly causes undefined behavior.
这篇关于如何计算总距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!