2014-02-26 30 views
0

我需要更多的幫助。我已經設法將我的所有字符輸入從文本文件轉換爲數字。 示例: 從文件輸入:將單獨的數字轉換爲一個整數C++

$1,9,56#%34,9 
!4.23#$4,983 

輸出:

1956 
349 
423 
4983 

現在,我需要採取那些個別數字的1 9 5 6並使其讀爲一個整數。輸出看起來是一樣的,但他們實際上是整數。合理?我必須在我的外部循環中執行此操作。它也必須是EOF循環。所以,我知道我需要把第一個數字乘以10並添加下一個數字,然後乘以10,直到達到最後一個數字。我怎樣才能以一種高效率的非崩潰方式來編寫它? input.txt文件的輸入如上所述。 這是我迄今爲止... 任何幫助是極大的讚賞

/* 
*/ 

//Character Processing Algorithm 

#include <fstream> 
#include <iostream> 
#include <cctype>                               
using namespace std; 

char const nwln = '\n'; 

    int main() 
    { 
    ifstream data; 
    ofstream out; 
    char ch; 
    char lastch; 
    int sum; 

    data.open ("lincoln.txt"); //file for input 
    if (!data) 
     { 
     cout << "Error!!! Failure to Open lincoln.txt" << endl; 
     system ("pause"); 
     return 1; 
     } 
    out.open ("out.txt"); //file for output 
    if (!out) 
     { 
     cout << "Error!!! Failure to Open out.txt" << endl; 
     system ("pause"); 
     return 1; 
     } 

    data.get (ch); // priming read for end-of-file loop 

    while (data) 
      { 
      sum = 0; 
      while ((ch != nwln) && data) 
       { 
       if (isdigit(ch)) 
        out<<ch; 

       if (ch == '#') 
        out<<endl; 
       { 
       ; 
       } 

            lastch = ch; 
       data.get (ch); // update for inner loop 
       } // inner loop 
       if (lastch != '#') 
        out<<endl; 



       data.get (ch); // update for outer loop 

      } //outer loop 

    cout << "The End..." << endl; 
    data.close(); out.close(); 
    system ("pause"); 
    return 0; 
    } //main 
+0

符號'#'是數字的唯一分隔符? –

+0

是的,'#'是分隔數字的唯一符號。 – byzzyby

+0

然後我的帖子中的代碼示例就是你所需要的。:) –

回答

0

通過字符讀取輸入文件的字符。要檢查一個字符是否是數字,請使用std::isdigit。然後將該數字添加到字符串的後面。

如果你需要一個字符串轉換爲整數,使用std::stoi

+0

所有那些美妙的散文編碼... –

1

如果您需要簡單地輸出標準流STD所有號碼::法院(或其他流,例如文件),那麼你可以以下面的代碼爲例。我只將std :: cin輸入的文件輸入替換爲變量行。您可以使用文件輸入而不是標準流。另外的 代替

std::ostream_iterator<char>(std::cout),

使用

std::ostream_iterator<char>(out), 

和代替

std::cout << std::endl; 

使用

out << std::endl; 

AF致電std::copy_if

這裏是例子

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

int main() 
{ 
    std::string line; 

    while (std::getline(std::cin, line)) // instead of std::cin use data 
    { 
//  std::cout << line << std::endl; 

     std::string word; 
     std::istringstream is(line); 

     while (std::getline(is, word, '#')) 
     { 
//   std::cout << word << std::endl; 
      auto it = std::find_if(word.begin(), word.end(), 
            [](char c) { return (std::isdigit(c)); }); 
      if (it != word.end()) 
      { 
       std::copy_if(it, word.end(), 
           std::ostream_iterator<char>(std::cout), 
           [](char c) { return (std::isdigit(c)); }); 
       std::cout << std::endl; 
      } 
     } 
    } 
} 

測試輸入的數據是

$1,9,56#%34,9 
!4.23#$4,983 

輸出是

1956 
349 
423 
4983 

或者,你可以使用它之前定義的λ。

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

int main() 
{ 
    std::string line; 

    while (std::getline(std::cin, line)) // instead of std::cin use data 
    { 
//  std::cout << line << std::endl; 

     std::string word; 
     std::istringstream is(line); 

     while (std::getline(is, word, '#')) 
     { 
//   std::cout << word << std::endl; 

      auto lm_IsDigit = [](char c) { return (std::isdigit(c)); }; 

      auto it = std::find_if(word.begin(), word.end(), lm_IsDigit); 

      if (it != word.end()) 
      { 
       std::copy_if(it, word.end(), 
           std::ostream_iterator<char>(std::cout), 
           lm_IsDigit); 
       std::cout << std::endl; 
      } 
     } 
    } 
}