代码转换(c#到c ++)

时间:2014-02-04 04:58:00

标签: c# c++

如何将以下代码转换为c ++?

第一部分:

    public static void ReadPoints(string aFile, Point2D [] pArray)
    {
        try
        {
            using(StreamReader sr = new StreamReader(aFile))
            {
                sr.BaseStream.Seek(0,SeekOrigin.Begin);
                for(int i=0;i<pArray.Length;i++)
                {
                    string line = sr.ReadLine();
                    int index = line.IndexOf("\t");
                    pArray[i].X = double.Parse(line.Substring(0,index));
                    pArray[i].Y = double.Parse(line.Substring(index+1,line.Length-(index+1)));
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Warning: An exception has been thrown at ReadPoints()!");
            Console.WriteLine(e.ToString());
            return;
        }
        return;
    }

第二部分:

    public static int ReadInt(string prompt)
    {
        int anInt = 0;
        bool wrongInput;
        do
        {
            Console.WriteLine(prompt);
            string line = Console.ReadLine();
            try
            {
                anInt = Int32.Parse(line);
                wrongInput = false;
            }
            catch(FormatException e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Invalid input!!!");
                wrongInput = true;
            }
        }
        while(wrongInput);
        return anInt;

}

接下来的代码是Point2D类的定义。上面的代码用于读取点的坐标并从字符串中读取int。

class Point2D
{
public:
    double get_x();
    void set_x(double value);
    double get_y();
    void set_y(double value);

private:
    double x;
    double y;
};

double Point2D::get_x()
{
    return x;
}
void Point2D::set_x(double value)
{
    x = value;
}
double Point2D::get_y()
{
    return y;
}
void Point2D::set_y(double value)
{
    y = value;
}

1 个答案:

答案 0 :(得分:1)

对于你的第一部分,合理惯用的C ++看起来像这样:

struct Point2D { 
    double x, y; // pass-through get/set pair gained nothing over public data.

    friend std::istream &operator>>(std::istream &is, Point2D &p) { 
        return is >> p.x >> p.y;
    }    
};

是的,这有公共数据 - 而通常并不是一个好主意。然而,在这种情况下,它可能不是一个可怕的想法 - 并且有效地你的私有数据代码和纯粹的传递get / set对完成了什么。如果确实有一些不变量,那么类可以/应该强制执行,我就是将数据设为私有并强制执行它们,但是如果你不打算强制执行不变量,那么将数据设为私有并添加一个get / set对只需添加句法噪音,不是效用。要使用它来读取文件中的某些数据,您可以执行以下操作:

std::ifstream infile(aFile);
std::vector<Point2D> points{std::istream_iterator<Point2D>(infile),
                            std::istream_iterator<Point2D>()};

这实际上并不等同于ReadPoints - 它更像是ReadPoints 加上您目前调用ReadPoints创建的任何代码用文件中的点填充数组。也就是说,这会创建一个名为vector的{​​{1}},并使用points中名称从文件中读取的点填充它。

就阅读aFile而言,如果我真的需要确保(例如)用户只输入了数字,我可能会这样做:

int

目前,这不会强制bool is_int(std::string const &in) { // technically `-` isn't a digit, but we want to allow it. static const std::string digits{ "-0123456789" }; return in.find_first_not_of(digits) == std::string::npos; } int readint(std::string const &prompt) { std::string input; do { std::cout << prompt; std::getline(std::cin, input); } while (!is_int(input) && std::cout << "Bad input (non-digit entered)\n"); return stoi(input); } 位于数字的开头,因此它允许输入-。如果您需要确保不会出现这种情况(可能还有数字在范围内),您需要重新编写3-2以强制执行您想要的内容。