You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

笛卡尔点向量存储与距离输出及主函数编写问题

Solution for Implementing the CartesianPoint Main Function

Let's break down how to build out your main function to meet all your requirements. First, I'll start with a quick example of what your existing CartesianPoint class might look like (to make the code self-contained), then walk through the main function step by step.

Example CartesianPoint Class (Assumed Structure)

Since you mentioned you already have this class, adjust this to match your actual implementation—this includes a method to calculate the distance between two points, which we'll use later:

#include <vector>
#include <iostream>
#include <cmath>
#include <limits>

class CartesianPoint {
private:
    double x;
    double y;
public:
    // Constructor
    CartesianPoint(double xVal = 0.0, double yVal = 0.0) : x(xVal), y(yVal) {}

    // Getters
    double getX() const { return x; }
    double getY() const { return y; }

    // Calculate distance to another point
    double distanceTo(const CartesianPoint& other) const {
        double dx = x - other.x;
        double dy = y - other.y;
        return sqrt(dx*dx + dy*dy);
    }

    // Overload << for easy printing
    friend std::ostream& operator<<(std::ostream& os, const CartesianPoint& point) {
        os << "(" << point.x << ", " << point.y << ")";
        return os;
    }
};

Main Function Implementation

Now let's build the main function with all your requirements:

int main() {
    std::vector<CartesianPoint> points;
    char choice;

    do {
        // Get valid x coordinate
        double x;
        std::cout << "Enter x value (-10 to 10): ";
        while (!(std::cin >> x) || x < -10 || x > 10) {
            std::cin.clear(); // Reset error flag
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
            std::cout << "Invalid input! Please enter a number between -10 and 10: ";
        }

        // Get valid y coordinate
        double y;
        std::cout << "Enter y value (-10 to 10): ";
        while (!(std::cin >> y) || y < -10 || y > 10) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input! Please enter a number between -10 and 10: ";
        }

        // Add the new point to the vector
        points.emplace_back(x, y);
        std::cout << "Added point " << points.back() << "\n";

        // Ask user for next action
        std::cout << "\nWhat would you like to do next?\n";
        std::cout << "A) Add another point\n";
        std::cout << "D) Calculate distance between two points\n";
        std::cout << "L) List all points\n";
        std::cout << "Q) Quit\n";
        std::cout << "Enter your choice: ";
        std::cin >> choice;

        // Handle each choice
        switch(toupper(choice)) {
            case 'D':
                if (points.size() < 2) {
                    std::cout << "Need at least two points to calculate distance!\n";
                } else {
                    int index1, index2;
                    std::cout << "Enter index of first point (0 to " << points.size()-1 << "): ";
                    std::cin >> index1;
                    std::cout << "Enter index of second point (0 to " << points.size()-1 << "): ";
                    std::cin >> index2;

                    // Validate indices
                    if (index1 >=0 && index1 < points.size() && index2 >=0 && index2 < points.size()) {
                        double dist = points[index1].distanceTo(points[index2]);
                        std::cout << "Distance between " << points[index1] << " and " << points[index2] << " is: " << dist << "\n";
                    } else {
                        std::cout << "Invalid indices!\n";
                    }
                }
                break;
            case 'L':
                std::cout << "Stored points:\n";
                for (size_t i = 0; i < points.size(); ++i) {
                    std::cout << i << ": " << points[i] << "\n";
                }
                break;
            case 'Q':
                std::cout << "Exiting program...\n";
                break;
            case 'A':
                // Continue loop to add another point
                break;
            default:
                std::cout << "Invalid choice! Please try again.\n";
        }
        std::cout << "\n";

    } while (toupper(choice) != 'Q');

    return 0;
}

Key Features Explained

  • Input Validation: The loops for x and y values ensure users can only enter numbers between -10 and 10, and handles non-numeric input gracefully.
  • Vector Storage: Uses std::vector<CartesianPoint> to dynamically store all user-created points.
  • Distance Calculation: Leverages the distanceTo method in the CartesianPoint class to compute the Euclidean distance between two selected points.
  • Interactive Menu: Gives users options to add more points, list all stored points, calculate distances, or quit the program.
  • User-Friendly Feedback: Provides clear messages for invalid inputs, insufficient points for distance calculation, etc.

Just adjust the CartesianPoint class parts to match your existing implementation (like if your getters or distance method have different names), and this main function should work perfectly for your needs.

内容的提问来源于stack exchange,提问作者Erik Jackson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:32:29