2014-10-30 110 views
-4

我有一個文本文件,內容是這樣的:從字符串中提取整數並轉換爲整數?

Department Max 
Bob 3 5 6 2 8 F1 
Frank 4 8 8 8 8 F2 
Jerry 7 9 9 0 9 F3 
William 0 0 12 13 14 F2 
Buck 3 4 10 8 8 4 6 F4 
Greg 1 2 1 4 2 F1 
Mike 3 4 4 8 4 F2 

而忽略了名字,我怎麼讀這些線和提取這些整數作爲獨立的整數,所以我可以把它們相加在一起?

到目前爲止,我有這樣的:

for (int i = 0; i < 10; i++) //Loop that checks the employee and their paygrade, 10 times for 10 lines of data 
    { 
     getline(inFile, s); //Gets next line in Salary.txt 
     string payGrade = s.substr(s.length() - 2, 2); //Checks the last two letters of the line to see paygrade 
     if (payGrade == "F1") 
     { 
      auto pos = s.find(' ', s.find('"')); 
      istringstream iss(s.substr(pos)); 


      F1employeeCounter++; 
     } 
     if (payGrade == "F2") 
     { 

      F2employeeCounter++; 
     } 
     if (payGrade == "F3") 
     { 

      F3employeeCounter++; 
     } 
     if (payGrade == "F4") 
     { 

      F4employeeCounter++; 
     } 
    } 

基本上我要檢查什麼類型的「薪級」每個員工。有四種不同類型的薪酬等級:F1,F2,F3,F4,每種薪酬等級的工作時間都有不同的支付方式。

+0

你可以用'atoi'將字符串轉換爲整數。您當然仍然需要拆分字符串。 – 2014-10-30 21:37:05

+0

http://stackoverflow.com/questions/7663709/convert-string-to-int-c + http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c可能有幫助 – matsjoyce 2014-10-30 21:37:21

+0

如果你有一個在線搜索很容易的問題,你可能需要先試着解決它。如果你之後仍然有問題,告訴我們你試過的東西 – PeterT 2014-10-30 21:45:31

回答

0
#include <iostream> 
#include <string> 
#include <sstream> 

int main() 
{ 
    std::istringstream iss("Bo 3 3 6 2 8\nFred 7 8 5 8 8\nJosh 7 9 4 0 1"); 

    while (iss.good()) //will exit this loop when there is no more text to read 
    { 
     std::string line; 

     //extract next line from iss 
     std::getline(iss, line); 

     //construct string stream from extracted line 
     std::stringstream ss(line); 

     std::string name; 
     int sum = 0; 
     int i = 0; 

     //extract name 
     ss >> name; 

     while(ss.good()) 
     { 
      //extract integer 
      ss >> i; 

      //add to the sum 
      sum += i; 
     } 

     //output name and sum 
     std::cout << name << ":" << sum << std::endl; 
    } 
} 

輸出:

Bo:22 
Fred:36 
Josh:21 
1

這裏遵循它如何,如果你事先該文件中的文本的結構總是會知道長得一樣在你的例子做一個例子。

傳統上,流在C++中用於從文本中解析數據(例如整數)。在這種情況下,圍繞std::istringstream纏繞的流迭代器std::istream_iterator用於解析整數標記。此外,標準算法std::accumulate用於對解析的整數進行求和。

std::ifstream input{"my_file"}; 

// For every line read: 
for (std::string line; std::getline(input, line);) { 
    // Find position of first space after first citation character ("). 
    auto pos = line.find(' ', line.find('"')); 

    // Use position to copy relevant numbers part into a string stream. 
    std::istringstream iss{line.substr(pos)}; 

    // Accumulate sum from parsed integers. 
    auto sum = std::accumulate(std::istream_iterator<int>{iss}, 
           std::istream_iterator<int>{}, 
           0); 

    /* Do something with sum... */ 
} 

Live example

+0

這沒有考慮到每行可變數量的整數。 – 2014-10-30 21:47:19

+0

@cmbasnett OP不會說姓名*不會有姓,或者每個4:th行都不會說'披薩時間'......這個例子清楚地顯示每行5個數字。 – Snps 2014-10-30 21:58:29

+0

自從我發表評論以來,答案發生了變化,此解決方案非常好。 – 2014-10-31 00:39:04