期待创建一个距离转换类

时间:2017-03-16 18:34:43

标签: c++

我正在尝试创建一个可在英里,米,英尺,英寸和码之间转换的类。据说我的返回语句没有存储任何值,我被告知我不需要这么多函数,而且我的打印功能是空的,因为我无法弄清楚要放入什么内容所以我在主要版本中将其长篇大论。我知道,过多的问题,我正在寻找指导。我一直在寻找大量的文档而我只是没有得到它 - 也许有人可以用外行术语解释。

#include <string>
#include <iostream>
using namespace std;


class DistanceConverter { //Class name
    public:
        DistanceConverter() { //Default constructor
            miles_ = 0;
        }
        DistanceConverter(double Miles) { //Overload constructor
            miles_ = Miles;
        }
        //Miles mutators and accessors
        void SetMilesToMeters(double Miles) {
            miles_ = Miles * 1609.34;
        }
        double GetMilesFromMeters() {
            return miles_;
        }

        void SetMilesToInches(double Miles) {
            miles_ = Miles * 63360;
        }
        double GetMilesFromInches() {
            return miles_;
        }


        void SetMilesToFeet(double Miles) {
            miles_ = Miles * 5280;
        }
        double GetMilesFromFeet() {
            return miles_;
        }

        void SetMilesToYards(double Miles) {
            miles_ = Miles * 1760;
        }
        double GetMilesFromYards() {
            return miles_;
        }

        //Yards mutators and accessors
        void SetYardsToMiles(double Miles) {
            miles_ = Miles / 1760;
        }
        double GetYardsFromMiles() {
            return miles_;
        }

        void SetYardsToFeet(double Miles) {
            miles_ = Miles * 3;
        }
        double GetYardsFromFeet() {
            return miles_;
        }

        void SetYardsToInches(double Miles) {
            miles_ = Miles * 36;
        }
        double GetYardsFromInches() {
            return miles_;
        }

        void SetYardsToMeters(double Miles) {
            miles_ = Miles * 0.9144;
        }
        double GetYardsFromMeters() {
            return miles_;
        }

        //Feet mutators and accessors
        void SetFeetToMiles(double Miles) {
            miles_ = Miles / 5280;
        }
        double GetFeetFromMiles() {
            return miles_;
        }

        void SetFeetToYards(double Miles) {
            miles_ = Miles / 3;
        }
        double GetFeetFromYards() {
            return miles_;
        }

        void SetFeetToInches(double Miles) {
            miles_ = Miles * 12;
        }
        double GetFeetFromInches() {
            return miles_;
        }

        void SetFeetToMeters(double Miles) {
            miles_ = Miles * 0.3048;
        }
        double GetFeetFromMeters() {
            return miles_;
        }

        //Inches mutators and accessors
        void SetInchesToMiles(double Miles) {
            miles_ = Miles / 63360;
        }
        double GetInchesFromMiles() {
            return miles_;
        }

        void SetInchesToYards(double Miles) {
            miles_ = Miles / 36;
        }
        double GetInchesFromYards() {
            return miles_;
        }

        void SetInchesToMeters(double Miles) {
            miles_ = Miles * 0.0254;
        }
        double GetInchesFromMeters() {
            return miles_;
        }

        void SetInchesToFeet(double Miles) {
            miles_ = Miles / 12;
        }
        double GetInchesFromFeet() {
            return miles_;
        }

        //Meters mutators and accessors
        void SetMetersToMiles(double Miles) {
            miles_ = Miles / 1609.34;
        }
        double GetMetersFromMiles() {
            return miles_;
        }

        void SetMetersToYards(double Miles) {
            miles_ = Miles / 0.9144;
        }
        double GetMetersFromYards() {
            return miles_;
        }

        void SetMetersToFeet(double Miles) {
            miles_ = Miles / 0.3048;
        }
        double GetMetersFromFeet() {
            return miles_;
        }

        void SetMetersToInches(double Miles) {
            miles_ = Miles / 0.0254;
        }
        double GetMetersFromInches() {
            return miles_;
        }

        //Print function
        void PrintDistances() {}

    private:
        double miles_; 
};


int main() {
    //Mile objects
    DistanceConverter MileToMeter;
    DistanceConverter MileToInch;
    DistanceConverter MileToFeet;
    DistanceConverter MileToYard;
    //Yard objects
    DistanceConverter YardToMile;
    DistanceConverter YardToFeet;
    DistanceConverter YardToInch;
    DistanceConverter YardToMeter;
    //Feet Objects
    DistanceConverter FeetToMile;
    DistanceConverter FeetToYard;
    DistanceConverter FeetToInch;
    DistanceConverter FeetToMeter;
    //Inch objects
    DistanceConverter InchToMile;
    DistanceConverter InchToYard;
    DistanceConverter InchToMeter;
    DistanceConverter InchToFeet;
    //Meter Objects
    DistanceConverter MeterToMile;
    DistanceConverter MeterToYard;
    DistanceConverter MeterToFeet;
    DistanceConverter MeterToInch;

    //Miles to X conversions
    MileToMeter.SetMilesToMeters(1);
    cout<<"Miles to meters: " << MileToMeter.GetMilesFromMeters() << endl;
    MileToMeter.PrintDistances();

    MileToInch.SetMilesToInches(1);
    cout<<"Miles to inches: " << MileToInch.GetMilesFromInches() << endl;
    MileToInch.PrintDistances();

    MileToFeet.SetMilesToFeet(1);
    cout<<"Miles to feet: " << MileToFeet.GetMilesFromFeet() << endl;
    MileToFeet.PrintDistances();

    MileToYard.SetMilesToYards(1);
    cout<<"Miles to yards: " << MileToYard.GetMilesFromYards() << endl;
    MileToYard.PrintDistances();

    //Yards to X conversions
    YardToMile.SetYardsToMiles(10.5);
    cout<<"Yards to miles: " << YardToMile.GetYardsFromMiles() << endl;
    YardToMile.PrintDistances();

    YardToFeet.SetYardsToFeet(20);
    cout<<"Yards to feet: " << YardToFeet.GetYardsFromFeet() << endl;
    YardToMile.PrintDistances();

    YardToInch.SetYardsToInches(5.3);
    cout<<"Yards to inches: " << YardToInch.GetYardsFromInches() << endl;
    YardToMile.PrintDistances();

    YardToMeter.SetYardsToMeters(11);
    cout<<"Yards to meters: " << YardToMeter.GetYardsFromMeters() << endl;
    YardToMile.PrintDistances();

    //Feet to X conversions
    FeetToMile.SetFeetToMiles(16);
    cout<<"Feet to miles: " << FeetToMile.GetFeetFromMiles() << endl;
    YardToFeet.PrintDistances();

    FeetToYard.SetFeetToYards(16);
    cout<<"Feet to yards: " << FeetToYard.GetFeetFromYards() << endl;
    FeetToYard.PrintDistances();

    FeetToInch.SetFeetToInches(2);
    cout<<"Feet to inches: " << FeetToInch.GetFeetFromInches() << endl;
    FeetToInch.PrintDistances();

    FeetToMeter.SetFeetToMeters(11);
    cout<<"Feet to meters: " << FeetToMeter.GetFeetFromMeters() << endl;
    FeetToMeter.PrintDistances();

    //Inches to X conversions
    InchToMile.SetInchesToMiles(100);
    cout<<"Inches to miles: " << InchToMile.GetInchesFromMiles() << endl;
    InchToMile.PrintDistances();

    InchToYard.SetInchesToYards(15);
    cout<<"Inches to yards: " << InchToYard.GetInchesFromYards() << endl;
    InchToYard.PrintDistances();

    InchToMeter.SetInchesToMeters(55);
    cout<<"Inches to meters: " << InchToMeter.GetInchesFromMeters() << endl;
    InchToMeter.PrintDistances();

    InchToFeet.SetInchesToFeet(12);
    cout<<"Inches to feet: " << InchToFeet.GetInchesFromFeet() << endl;
    InchToFeet.PrintDistances();

    //Meter to X conversions
    MeterToMile.SetMetersToMiles(1);
    cout<<"Meters to miles: " << MeterToMile.GetMetersFromMiles() << endl;
    MeterToMile.PrintDistances();

    MeterToYard.SetMetersToYards(100);
    cout<<"Meters to yards: " << MeterToYard.GetMetersFromYards() << endl;
    MeterToYard.PrintDistances();

    MeterToFeet.SetMetersToFeet(100);
    cout<<"Meters to feet: " << MeterToFeet.GetMetersFromFeet() << endl;
    MeterToFeet.PrintDistances();

    MeterToInch.SetMetersToInches(100);
    cout<<"Meters to inches: " << MeterToInch.GetMetersFromInches() << endl;
    MeterToInch.PrintDistances();

}

3 个答案:

答案 0 :(得分:1)

我建议为每个单元使用classstruct

在你的设计中,没有什么可以阻止我使用英尺数来调用里程数。

通过使用类作为单元类型,您可以编写转换方法(运算符),以便编译器将根据变量类型(在本例中为单元)调用适当的方法。

例如:

struct Feet
{
   unsigned int value;
};
class Miles;
class Meters;
Feet Convert_From(const Meters& m);
Meters Convert_From(const Miles& mi);

编译器将根据变量类型选择适当的转换函数。

编辑1:构造函数
您还可以使用构造函数进行转换:

struct Inches
{
  unsigned int value;
};
struct Feet
{
  unsigned int value;
  Feet(unsigned int f)
    : value(f)
  { ; }
  Feet(const Inches& in)
  {
    value = in/12;
  }
};

//...
Inches in;
in.value = 24;
Feet f(in);
cout << f.value << endl;

答案 1 :(得分:1)

以下是如何使用更简单,更易于使用的模式实现此目的的想法。

请注意,它只会执行Miles <-> Feet <-> Inches次转化,其余的(米,码等)会留给您练习。

最值得注意的是,它使用static方法,因此您不需要初始化新对象只是为了执行简单的转换。它也只有一个public方法,从中执行所有转换。

这样可以提高灵活性,并且对用户来说也更容易。

#include <iostream>

enum class DistanceType
{
    Unknown = 0,
    Inches,
    Feet,
    Miles
};

class DistanceConverter
{
public:

    static double Convert(double const value, DistanceType const from, DistanceType const to)
    {
        switch(from)
        {
        case DistanceType::Miles:
            return ConvertMiles(value, to);

        case DistanceType::Feet:
            return ConvertFeet(value, to);

        case DistanceType::Inches:
            return ConvertInches(value, to);

        default:
            return value;
        }
    }

protected:

private:

    static double ConvertMiles(double const value, DistanceType const to)
    {
        switch(to)
        {
        case DistanceType::Feet:
            return (value * MilesToFeet());

        case DistanceType::Inches:
            return (value * MilesToFeet()) * FeetToInches();

        default:
            return value;
        }
    }

    static double ConvertFeet(double const value, DistanceType const to)
    {
        switch(to)
        {
        case DistanceType::Miles:
            return (value / MilesToFeet());

        case DistanceType::Inches:
            return (value * FeetToInches());

        default:
            return value;
        }
    }

    static double ConvertInches(double const value, DistanceType const to)
    {
        switch(to)
        {
        case DistanceType::Miles:
            return (value / FeetToInches()) / MilesToFeet();

        case DistanceType::Feet:
            return (value / FeetToInches());

        default:
            return value;
        }
    }

    static constexpr double MilesToFeet() { return 5280.0; };
    static constexpr double FeetToInches() { return 12.0; };
};

int main()
{
    const double miles        = 5.0;
    const double milesToFeet  = DistanceConverter::Convert(miles,        DistanceType::Miles,  DistanceType::Feet);
    const double feetToInches = DistanceConverter::Convert(milesToFeet,  DistanceType::Feet,   DistanceType::Inches);
    const double inchesToFeet = DistanceConverter::Convert(feetToInches, DistanceType::Inches, DistanceType::Feet);
    const double feetToMiles  = DistanceConverter::Convert(inchesToFeet, DistanceType::Feet,   DistanceType::Miles);

    std::cout << miles << " miles is: " << std::endl;
    std::cout << "\t" << milesToFeet << " feet" << std::endl;
    std::cout << "\t" << feetToInches << " inches" << std::endl;
    std::cout << std::endl;

    std::cout << feetToInches << " inches is: " << std::endl;
    std::cout << "\t" << inchesToFeet << " feet" << std::endl;
    std::cout << "\t" << feetToMiles << " miles" << std::endl;
    std::cout << std::endl;

    return 0;
}

哪个输出

5 miles is:
    26400 feet
    316800 inches

316800 inches is:
    26400 feet
    5 miles

请注意,我使用第一次转换的结果(英里到英寸)来验证第二次转换的结果(英寸到英里)。

答案 2 :(得分:0)

要将功能从减少到2 * n,请为您的班级使用一个主要维度,并为此维度提供辅助转换:

class Distance
{
private:
    double meters_;
    Distance(double meters) : meters_(meters) {}
public:
    static Distance FromMeters(double meters) { return { meters }; }
    static Distance FromMiles(double miles) { return { 1609.34 * miles }; }
    static Distance FromInches(double inches) { return { 0.0254 * inches }; }
    static Distance FromFeet(double feet) { return { 0.3048 * feet }; }
    static Distance FromYards(double yards) { return { 0.9144 * yards} ; }

    double ToMeters() const { return meters_; }
    double ToMiles() const { return meters_ / 1609.34; }
    double ToInches() const { return meters_ / 0.0254; }
    double ToFeet() const { return meters_ / 0.3048; }
    double ToYards() const { return meters_ / 0.9144; }

};

然后以那种方式使用

std::cout << "Miles to inches: " << Distance::FromMiles(1).ToInches() << std::endl;