简体   繁体   中英

Use function that prompts he user to input x-y coordinates (float types) of some points in a Cartesian plane

I really like to do programming. Since English is my second language and I am learning this programming in my home alone!. I just wanna complete this code I need help!!

  1. List item

  2. The program should then output a message indicating whether the point entered is the origin, is located on the x- (or y-) axis, or appears in a particular quadrant. The following shows the plane of xy coordinates and the four quadrants.

  3. The program should continue to prompt for the values of xy coordinates until user enter both x and y equal to zero

  4. I want to use "char getPostion(float, float)" function to explore the use of if-else statements to identify the quadrant where the point is situated. You can define some character constants liked what you did in Lab 3 using '1', '2', '3', '4', 'x', 'y', 'o' to denote 1st quadrant, 2nd quadrant, 3rd quadrant, 4th quadrant, x-axis, y-axis, and origin

  5. I want to use one more function "void printposition (float, float, char)" A function to print out the relevant information upon knowing where the point is situated in the plane. You can explore the use of switch-case statement to print out the relevant message (using the above character values to be the “case” values)

First, when I compile the code, I want to display like this.

  • List item

Enter x and y: 7.3, 8.23 => (7.30, 8.23) is above X-axis ==> It is at first quadrant

Enter x and y: -9, 2.3 => (-9, 2.3) is above X-axis ==> It is at second quadrant

Enter x and y: -7, 0 => (-7.00, 0) is at x-axis

Enter x and y: 0, 5 => (0, 5.00) is at Y-axis

Enter x and y: 0, 0 => (0, 0) is at origin

This is my code to solve this problem. And I really want to solve this problem. and I am still trying to do it! This is not correct! I really need help

const char Fstquadrant = '1';
const char Sstquadrant = '2';
const char Tstquadrant = '3';
const char fstquadrant = '4';
const char xaxis = 'x';
const char yaxis = 'y';
const char origin = 'o';

int char getPosition(float, float);
void printPositoin (float, float, char);



int main()
{
    float x, y;

}

int char getPostion(float x, float y)
{


    if(x ==0 && y ==0)
        return const  "Fstquadrant";

    else if(x==0)
        return const  "yaxis";

    else if(y ==0)
        return const  "xaxis";

    else if(x>0 && y<0)
        return const   "fstquadrant";


    else if(x<0 && y <0)
        return const   "Tstquadrant";

    else if(x<0 && y>0)
        return const  "Sstquadrant";

    else
        return const "Fstquadrant";



}

void printPosition (float x, float y, char)
{

    cout << "Enter x and y: ";
    cin >> x >> y;

    int category;

    switch(category)
    {
        case Fstquadrant: 
        case Sstquadrant: 
        case Tstquadrant: 
        case fstquadrant: 
        case xaxis: 
        case yaxis: 
        case origin: 
    }

}

I highly recommend using a struct or class to group your coordinates. Usually, this is called a Point class:

struct Point
{
  double x;
  double y;
};

Note: always prefer double to float unless you have a requirement to use the shorter float type.

You can then add some I/O functions:

struct Point
{
    double x;
    double y;
    Point() : x(0.0), y(0.0) { }
    input_from_console();
    friend std::ostream& operator<<(std::ostream& output, const Point& p);
};

void Point::input_from_console()
{
    std::cout << "Enter X ordinate: ";
    std::cin >> x;
    std::cout << "Enter Y ordinate: ";
    std::cin >> y;
}

std::ostream& operator<<(std::ostream& output, const Point& p)
{
    output << "<" << p.x << ", " << p.y << ">";
    return output;
}

This allows you to do the following in main() :

int main()
{
  Point p; // Constructor initializes to <0.0, 0.0>
  p.input_from_console();
  std::cout << "Your coordinates are: " << p << "\n";
  return 0;
}

I strongly suspect that this is a homework question and not a "self-learning" question, but here is part of the answer

Your input should be placed in a while loop in the main fn.

Additionally, first function should be returning the constant character, and not constant char “varname”.

For your second function, you also need to give the parameter a name that you will use in the switch case.

I'm not sure about the logic of your first function to determine the quad placement, but what I would do is to use nested if-elses to make it easy to cover all possible scenarios.

For example, if x > 0, else if x == 0, else.

Within each if, I would then compare the y values.

Here's a sample based on your code to get you started.

#include <iostream>
#include <iomanip>

using namespace std;
int char getPosition(float, float);
void printPositoin (float, float, char);
const char Fstquadrant = ‘1’;
const char Sstquadrant = ‘2’;
const char Tstquadrant = ‘3’;
const char fstquadrant = ‘4’;
const char xaxis = ‘x’;
const char yaxis = ‘y’;
const char origin = ‘o’;

int main()
{
    float x, y;
    //declare a trigger here of your preference
    while(//trigger condition is met)
    {
        //place your cin and cout here
        //If your cin both equate to zero,
        //run the functions and break the trigger condition
        // else, run the function.
    }

}

int char getPostion(float x, float y)
{


   if(x ==0 && y ==0)
        return Fstquadrant;

   else if(x==0)
        return yaxis;

   else if(y ==0)
        return xaxis;

   else if(x>0 && y<0)
        return fstquadrant;


   else if(x<0 && y <0)
        return Tstquadrant;

   else if(x<0 && y>0)
        return Sstquadrant;

   else
        return Fstquadrant;




}

void printPosition (float x, float y, char category)
{

   switch(category)
    {
        case Fstquadrant: //cout your result for each case and do a break
        case Sstquadrant:
        case Tstquadrant:
        case fstquadrant:
        case xaxis:
        case yaxis:
        case origin:
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM