簡體   English   中英

無法弄清楚如何生成用戶輸入的坐標(x,y)

[英]Can't figure out how to generate coordinates(x,y) entered by the user

無法弄清楚如何生成用戶輸入的坐標(x,y)並找到最接近用戶輸入的生成點。

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>//for generating a random number
using namespace std;

int main()
{
    int x,k,y,quad,x1,y1;// the variables that we are going to use
    cout << "Enter the value of x : "<<endl;
    cin >>x;
    cout << "Enter the value of y : "<<endl;
    cin >>y;
    cout <<"Enter the amount of numbers you want to be generated : "<<endl;
    cin >>k;//how many coordinates to be generated
    srand(time(0));//to make sure that the numbers are random
        for(int i = 0; i < k; i++){

        int x = rand() % k;//generate x
        int y = rand() % k;//generate y
        x1 = x;
        y1 = y;

        cout << "(" << x << "," << y <<")"<< "\n\n";
    }
    if (((x1*x1)+(y1*y1))<(x*x)+(y*y)){
        cout <<"("<<x1<<","<<y1<<") is the nearest point to ("<<x<<","<<y<<")"<<endl;
    }

    if (x<0 && y<0){//these are to show in which quadrant are the coordinates
        quad = 3;
    }
    else if(x<0 &&y>0){
        quad = 2;
    }
    else if(x>0 && y>0){
        quad = 1;
    }
    else if(x>0 && y<0){
        quad = 4;
    }
    else if(x<0 && y==0){
        quad = 5;
    }
    else if(y<0 && x==0){
        quad = 6;
    }
    else if(x>0 && y==0){
        quad = 7;
    }
    else if(y>0 && x==0){
        quad = 8;
    }
    switch(quad){
        case 1: cout <<"("<<x<<","<<y<<")"<<"are in the 1st quadrant"<<endl;break;
        case 2: cout <<"("<<x<<","<<y<<")"<<"are in the 2nd quadrant"<<endl;break;
        case 3: cout <<"("<<x<<","<<y<<")"<<"are in the 3rd quadrant"<<endl;break;
        case 4: cout <<"("<<x<<","<<y<<")"<<"are in the 4th quadrant"<<endl;break;
        case 5: cout <<"("<<x<<","<<y<<")"<<"are between the 2nd and 3rd"<<endl;break;
        case 6: cout <<"("<<x<<","<<y<<")"<<"are between the 3rd and 4th"<<endl;break;
        case 7: cout <<"("<<x<<","<<y<<")"<<"are between the 1st and 4th"<<endl;break;
        case 8: cout <<"("<<x<<","<<y<<")"<<"are between the 1st and 2nd"<<endl;break;
        default : cout <<"("<<x<<","<<y<<")"<<"are in the (0,0)"<<endl;break;


    }
}

我知道解決方案可能很簡單,但我就是想不通。 我是大學新生,問這樣的問題我覺得很弱智,但這是我最后的希望。 任何幫助將不勝感激。^^


請檢查此代碼
我沒有使用象限,因為你想找到最接近給定點的點。

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>
using namespace std;

float dist(int x1, int y1, int x2, int y2) { 
    return sqrt(pow((x1-x2),2) + pow((y1-y2),2));   
}

int main()
{
    int x,y,k;
    cout << "Enter the value of x : "<<endl;
    cin >>x;
    cout << "Enter the value of y : "<<endl;
    cin >>y;
    cout <<"Enter the amount of numbers you want to be generated : "<<endl;
    cin >>k;//how many coordinates to be generated
    //Define arrays for points, 
    int *X = new int[k];
    int *Y = new int[k];
    srand(time(0));//to make sure that the numbers are random
    for(int i = 0; i < k; i++){

        X[i] = rand() % k;
        Y[i] = rand() % k;

        cout << "(" << X[i] << "," << Y[i] <<")"<< "\n\n";
   }
   float minDist = dist(x,y,X[0],Y[0]);
   int number = 0;

   for( int i = 1; i < k; i ++ ) {
        if( dist(x,y,X[i],Y[i]) < minDist ) {
        minDist = dist(x,y,X[i],Y[i]);
        number = i;
    }
}

cout << "Nearest point (" << X[number] << "," << Y[number] << ")";
cout << "Distance is " << minDist;

return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM