2011-02-23 132 views
1

我需要輸入3用逗號將單個字符串分解爲多個字符串C++?

全稱1分離全名:約翰史密斯,弗林
全名2:沃爾特·肯尼迪,羅伯特
全名3:薩姆,低音,克林頓

然後輸出像這樣

名1:約翰
名2:沃爾特
名3:山姆

中間名1:史密斯
中間名2:肯尼迪
中間名3:低音

姓1:弗林
姓2:羅伯茨
姓3:克林頓

我如何做這些? 到目前爲止,這是我的代碼

#include<iostream> 
#include<cstdio> 
#include<cstring> 
using namespace std; 

int main() { 
    char first[3][100]; 
    char middle[3][100]; 
    char last[3][100]; 
    char full[3][100]; 
    int i; 

    cout << "Enter 3 Full Names <first, middle and last names separated by comma>:" << endl; 
    for (i=0; i<3; i++) { 
     cout << "Full Name " << i+1 << ":" ; 
     gets (full[i]);   
    } 

    cout << "The first names are: " << endl; 
    for (i=0; i<3; i++) { 
     strcpy (first[i], full[i]); 
     if (strcmp (first[i], ", ")) { 
      cout << "First Name "<< i+1 << ":" ; 
      strcpy (first[i], full[i]); 
      cout << (first[i]); 
      cout << endl; 
     } 
    } 
    cout << "The middle names are: " << endl; 
    for (i=0; i<3; i++) { 
     cout << "Middle Name "<< i+1 << ":" ; 
     cout << (middle[i]); 
     cout << endl; 
    } 
    cout << "The last names are: " << endl; 
    for (i=0; i<3; i++) { 
     cout << "Last Name "<< i+1 << ":" ; 
     cout << (last[i]); 
     cout << endl; 
    } 
    system("pause"); 
    return 0; 
} 
+6

哪裏標記[作業]? – p4553d

+1

添加了作業標籤。 – Tom

+0

我們不允許使用矢量 剛的iostream,爲c_string和cstdio! 有人可以張貼一個字符串,然後將它分成3個多串的例子嗎? 我只是學習C++所以請簡化它 謝謝。 – 2011-02-23 14:27:08

回答

1

我覺得這裏你想做的是string類劃分方法,該方法應該是這樣的:

void SplitName(const string& fullName, const string& delims, vector<string>& names) 
{ 
    size_t i = 0; 
    size_t j = 0; 
    while (i < fullName.size()) 
    { 
     i = fullName.find_first_not_of(delims, i); 
     j = fullName.find_first_of(delims, i); 
     if (i < fullName.size()) 
     { 
      names.push_back(fullName.substr(i, j - i)); 
     } 
     i = j; 
    } 
} 

可以定義「:」作爲分隔符,那麼名字[1]是名字,名字[2]是中間名,名字[3]是姓。

+0

我們只allwed使用的:#include 的#include 的#include Silver

1

我可能會做這樣的事情:

編輯:經過一番思考,我決定重複真的打擾我太多,所以我已經消除它。我不確定它在技術上是否允許(std::string不是POD),但它似乎起作用,並且使我感覺更好,更具可擴展性。

#include <iostream> 
#include <sstream> 
#include <algorithm> 
#include <string> 
#include <vector> 
#include <iterator> 

struct name { 
    std::string first, middle, last; 
}; 

std::istream &operator>>(std::istream &is, name &n) { 
    char ch; 
    is.ignore((unsigned)-1, ':'); 
    is.ignore(1); 

    std::getline(is, n.first, ','); 
    std::getline(is, n.middle, ','); 
    std::getline(is, n.last); 
    return is; 
} 

struct item { 
    size_t offset; 
    char *caption; 
}; 

void show(name const &n, item const &i) { 
    // as predicted, eliminating the duplication did lead to one gnarly line of code: 
    std::string &name = *(std::string *)((char *)&n+i.offset); 
    std::cout << i.caption << name << "\n"; 
} 

int main() {  
    std::vector<name> names; 

    std::string raw_data("Full Name 1: John, Smith, Flynn\nFull Name 2: Walter, Kennedy, Roberts\nFull Name 3: Sam, Bass, Clinton"); 

    std::istringstream infile(raw_data); 

    std::copy(std::istream_iterator<name>(infile), 
       std::istream_iterator<name>(), 
       std::back_inserter(names)); 

    item items[] = { 
     {offsetof(name, first), "First Name: "}, 
     {offsetof(name, middle), "Middle Name: "}, 
     {offsetof(name, last), "Last name: "} 
    }; 

    for (int i=0; i<3; i++) { 
     for (int j=0; j<3; j++) 
      show(names[j], items[i]); 
     std::cout << "\n"; 
    } 
    return 0; 
} 
+0

我們只allwed使用的:#include 的#include 的#include Silver

+0

但你爲什麼不使用指針到成員代替?您使用偏移的方式看起來像是嘗試手動重新實現指向成員的指針。 – AnT

+0

@安:五年前,我可能會有一個答案,但在這個遲到的日子,我真的不記得了。 –

0
void DumpName(char* pBuffer, char cDelimiter, int iCounter); 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    char full[3][100]; 

    cout << "Enter 3 Full Names <first, middle and last names separated by comma>:" << endl; 
    for (int i=0; i<3; i++) { 
     cout << "Full Name " << i+1 << ":" ; 
     gets (full[i]);   
    } 

    for(int i=0; i<3; i++) 
    { 
     cout << "First Name "<< i+1 << ":" ; 
     DumpName(full[i], ',', 0); 
    } 

    for(int i=0; i<3; i++) 
    { 
     cout << "Middle Name "<< i+1 << ":" ; 
     DumpName(full[i], ',', 1); 
    } 

    for(int i=0; i<3; i++) 
    { 
     cout << "Last Name "<< i+1 << ":" ; 
     DumpName(full[i], ',', 2); 
    } 

    return 0; 
} 

void DumpName(char* pBuffer, char cDelimiter, int iCounter) 
{ 
    int iFound = 0; 

    char* pStart = pBuffer; 
    char* pCurrent = pBuffer; 
    while(*pCurrent != '\0') 
    { 
     if(*pCurrent == cDelimiter) 
     { 
      if(iFound == iCounter) 
      { 
       *pCurrent = '\0'; 
       cout << pStart << endl; 
       *pCurrent = cDelimiter; 
       return; 
      } 

      pStart = pCurrent; 
      pStart ++; 
      iFound ++; 
     } 

     pCurrent ++; 
    } 

    if((iCounter - iFound) == 0) 
     cout << pStart << endl; 
} 
相關問題