【任务】对前一个小项目主程序的优化

//main.cpp
#include 
#include 
#include 
#include 
#include 
#include 
#include "hypotenuse.h"

extern float hypotenuse(float a, float b);

namespace hypo
{
    void calculate_hypotenuse();
    void display_combined_string();

    class HypoClass
    {
    public:
        std::string a = "a ";
        std::string b = "b ";
        std::string r = "c";
        void display_thanks();
    };
}

namespace 
{
    float get_input(const std::string& prompt) 
    {
        float value;
        while (true) 
        {
            std::cout << prompt;
            std::cin >> value;
            if (std::cin.fail()) 
            {
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cout << "Invalid input. Please enter a valid number." << std::endl;
            }
            else 
            {
                break;
            }
        }
        return value;
    }
}

void hypo::calculate_hypotenuse()
{
    std::array<float, 2> sides = { 0, 0 };
    float hypotenuse_length;

    sides[0] = get_input("Enter the length of the first side of the right triangle (a): ");
    sides[1] = get_input("Enter the length of the second side of the right triangle (b): ");

    hypotenuse_length = hypotenuse(sides[0], sides[1]);

    std::cout << "Thus,the length of the hypotenuse of the right triangle (c):  " << hypotenuse_length << "\nCompleted!\n" << std::endl;

    for (const auto& value : sides)
    {
        std::cout << value <<" ";
    }
    std::cout << hypotenuse_length << std::endl;
}

void hypo::display_combined_string()
{
    HypoClass hc;
    std::cout << hc.a + hc.b + hc.r << "\n" << std::endl;
}

void hypo::HypoClass::display_thanks()
{
    std::cout << "Thanks!\n" << std::endl;
}

int main(int argc, char* argv[])
{
    hypo::calculate_hypotenuse();
    hypo::display_combined_string();
    hypo::HypoClass hc;
    hc.display_thanks();
}

(以上根据GitHub Copilot的建议进行调整。)

你可能感兴趣的:(c++,开发语言)