2017-05-09 22 views
-1

我試圖接收一串像這樣的點(2,4),(5,8),(12,7),(15.54,3.65)並將其分離爲X數組和y數組,請幫助我仍試圖這樣的初學者,這是災難性的將字符串分隔爲x和y的數組

string polygons; 
int i = 0; 
int length = polygons.length(); 
string x[10000]; 
int index = 0; 
int k = 0; 
getline(cin, polygons); 

for (i = 0; i < length; i++) 
{ 
    if (polygons[i] == '(') 
    { 
     k = polygons.substr(i + 1, 20).find_first_of(","); 

     x[index] = polygons.substr(i + 1, ((k + i) - (i + 1))); 
     index++; 
    } 
} 

int a = 0; 
string y[10000]; 
int index2 = 0; 
int c = 0; 

for (a = 0; a < length; a++) 
{ 
    if (polygons.substr(a) == ",") 
    { 
     c = polygons.substr(a + 1, 20).find_first_of(")"); 
    } 

    y[index2] = polygons.substr(a + 1, ((c + a) - (a + 1))); 
    index2++; 
} 
+2

建議整理括號並提供[mcve] – user4581301

+0

推薦:打破工作。 [使用'std :: string :: find'](http://en.cppreference.com/w/cpp/string/basic_string/find)來幫助找到'('和')'然後打印出來它們之間。一旦你有了這個工作,用['std :: stringstream'](http://en.cppreference.com/w/cpp/io/basic_stringstream)和'std :: getline'替換打印出來的代碼來分割成過去在括號之間的數字並打印數字。當這一切都正常工作,你有一個號碼對流,找到一個好方法來存儲對。 – user4581301

+0

謝謝你,我會試試這個 – Antonio

回答

0

要百分點是一個字符串數值,以點轉換,你可以利用.find_first_of()std::stod()。這是我的代碼,用於將點字符串轉換爲數字值。

#include <string> 
#include <iostream> 
#include <vector> 

using std::string; 
using std::vector; 
using std::cout; 
using std::endl; 

class Point 
{ 
public: 
    Point(double x= 0, double y= 0) : m_x(x), m_y(y) {} 
    inline double getX() const { return m_x; } 
    inline double getY() const { return m_y; } 
private: 
    double m_x, m_y; 
}; 

vector<Point> strPoints2NumPoints(const string& strPoints) 
{ 
    vector<Point> points; 

    for (int i(0); i < strPoints.size(); ++i){ 

     if (strPoints[i] == '('){ 
      // extract X & Y values as string 
      string temp = strPoints.substr(i+1, strPoints.find_first_of(")",i)-i-1); 
      // convert X as strig to double 
      double X = std::stod( temp.substr(0,temp.find(','))); 
      // convert Y as string to double 
      double Y = std::stod( temp.substr(temp.find(',')+1)); 
      points.push_back(Point(X,Y)); 
     } 
    } 

    return points; 
} 


int main(int argc, char* argv[]) 
{ 
    string strPoints("(2,4),(5,8),(12,7),(15.54,3.65)"); 
    vector<Point> numPoints; 

    cout << "Points as string: " << strPoints << endl; 

    numPoints = strPoints2NumPoints(strPoints); 

    cout << "Points as numbers: " << endl; 
    for(int i(0); i < numPoints.size(); ++i){ 
     cout << "Point:<" << numPoints[i].getX() << "," << numPoints[i].getY() << ">" << endl; 
    } 


    return 0; 
} 

上述代碼的輸出是

Points as string: (2,4),(5,8),(12,7),(15.54,3.65) 
Points as numbers: 
Point:<2,4> 
Point:<5,8> 
Point:<12,7> 
Point:<15.54,3.65> 
+0

謝謝你的時間這是優雅 – Antonio

+0

@Antonio,如果你認爲這個答案解決了你的問題,請點擊勾號接受它。歡呼聲 – CroCo

0

根據您的代碼和邏輯,我編輯的代碼和處理的錯誤:

string polygons; 
int i = 0; 
string x[10000]; 
int index = 0; 
int k = 0; 
getline(cin, polygons); 
int length = polygons.length(); 

for (i = 0; i < length; i++) 
{ 
    if (polygons[i] == '(') 
    { 
     // k = polygons.substr(i + 1, 20).find_first_of(","); 
     k = polygons.find_first_of(",", i); 

     // x[index] = polygons.substr(i + 1, ((k + i) - (i + 1))); 
     x[index] = polygons.substr(i + 1, k - i - 1); 
     index++; 
    } 
} 

int a = 0; 
string y[10000]; 
int index2 = 0; 
int c = 0; 

for (a = 0; a < length; a++) 
{ 
    /* 
    if (polygons.substr(a) == ",") 
    { 
     c = polygons.substr(a + 1, 20).find_first_of(")"); 
    } 

    y[index2] = polygons.substr(a + 1, ((c + a) - (a + 1))); 
    index2++; 
    */ 
    if (polygons[a] == ',' && polygons[a + 1] != '(') 
    { 
     k = polygons.find_first_of(")", a); 
     y[index2] = polygons.substr(a + 1, k - a - 1); 
     index2++; 
    } 
} 

這裏是我的代碼,只是改進和簡化您的代碼風格:

#include <iostream> 
#include <string> 
#include <vector> 

int main() 
{ 
    std::string Polygons; 
    getline(std::cin, Polygons); 

    std::vector<std::string> XCoords; 
    for (int i = 0; i < Polygons.length(); ++i) 
    { 
     if (Polygons[i] == '(') 
     { 
      auto CommaIndex = Polygons.find_first_of(",", i); 

      XCoords.push_back(Polygons.substr(i + 1, CommaIndex - i - 1)); 
     } 
    } 

    std::vector<std::string> YCoords; 
    for (int i = 0; i < Polygons.length(); ++i) 
    { 
     if (Polygons[i] == ',' && Polygons[i + 1] != '(') 
     { 
      auto Index = Polygons.find_first_of(")", i); 

      YCoords.push_back(Polygons.substr(i + 1, Index - i - 1)); 
     } 
    } 

    return 0; 
} 

希望它有幫助。

+0

謝謝你,感謝你的時間,這正是我所期待的 – Antonio

相關問題