1

我的代码有一个问题,涉及如何使用表示时间的字符串数据成员,通过它所包含的冒号数进行处理,然后将其分隔并转换为整数。

目标:取一串时间,以##:##:##:## 的形式表示,用 1 到 3 个冒号分隔,例如 1:13:0:59,然后“切碎”之间的各个部分冒号。

前导日期和小时字段为零可以省略,因此 0:0:0:12、0:0:12 和 0:12 都是“12 秒”可接受的输入形式。“12”是不可接受的。但是,值可能会超过秒、分钟和小时的常规限制。例如, 25:3:90 和 1:1:4:30 。

当我输入 0:01 时,分钟会得到整个值。我不确定另一种分解字符串并从中创建单独整数的方法。

到目前为止,这是我的代码。

struct Times {
    int days;
    int hours;
    int minutes;
    int seconds;
    string time;

    void string_to_ints(string& s);
}

void string_to_ints(string& s) {
 // count the amount of colons
 for (int i = 0; i < time.length(); i++) {
     if (time[i] == ':')
     count++;
  }

 // initialize days hours minutes and seconds
if (count == 3) {

    day = time.substr(0, ':');
     d = day.size();
      hour = time.substr( d+1, ':');
       h = hour.size();
        min = time.substr( h+1, ':');
         m = min.size();
          sec = time.substr( m);
           ss= time.size();
 }
// initialize hours, minutes and seconds
if (count == 2) {
   hour = time.substr( 0, ':');
    h = hour.size();
     min = time.substr( h+1, ':');
      m = min.size();
       sec = time.substr( m);
        ss = time.size();
}

// initialize minutes and seconds
if (count == 1) {
   min = time.substr(0, ':');
     m = min.size();
      sec = time.substr( m );
       ss = time.size();
}

// convert the strings to integers
stringstream buffer(sec);
buffer >> seconds;

stringstream buffer2(min);
  buffer2>> minutes;

stringstream buffer3(hour);
  buffer3 >> hours;

stringstream buffer4(day);
  buffer4 >> days;

谢谢您的帮助。

4

2 回答 2

1

也许,像这样?

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main() {
    string time_str = "1:13:0:59";
    istringstream iss(time_str);
    string temp;
    int days = 0, hours = 0, minutes = 0, seconds = 0;

    size_t ndelims = count(time_str.begin(), time_str.end(), ':');
    int count = 0;


    if (ndelims == 3) {
        while(getline(iss, temp, ':')) {
            cout << atoi(temp.c_str()) << endl;

            if (count == 0) {
                days = atoi(temp.c_str());
            }
            else if(count == 1) {
                hours = atoi(temp.c_str());
            }
            else if(count == 2) {
                minutes = atoi(temp.c_str());
            }
            else if (count == 3) {
                seconds = atoi(temp.c_str());
            }
            count = count + 1;          
        }
        cout << days << " " << hours << " " << minutes << " " << seconds << endl;
    }
    else if (ndelims == 2) {
        while(getline(iss, temp, ':')) {
            cout << atoi(temp.c_str()) << endl;
            if(count == 0) {
                hours = atoi(temp.c_str());
            }
            else if(count == 1) {
                minutes = atoi(temp.c_str());
            }
            else if (count == 2) {
                seconds = atoi(temp.c_str());
            }
            count = count + 1;          
        }
        cout << days << " " << hours << " " << minutes << " " << seconds << endl;
    }
    else if(ndelims == 1) {
        while(getline(iss, temp, ':')) {
            cout << atoi(temp.c_str()) << endl;
            if(count == 0) {
                minutes = atoi(temp.c_str());
            }
            else if (count == 1) {
                seconds = atoi(temp.c_str());
            }
            count = count + 1;          
        }
        cout << days << " " << hours << " " << minutes << " " << seconds << endl;
    }

    return 0;
}

这通过计算(':')字符串中分隔符的数量,并将其分解为其组成标记(这将是找到的分隔符的数量 + 1)来工作。

然后,意识到最右边的标记总是秒,下一个最右边的标记是分钟,等等,我们可以像上面那样编写代码,它给出了你想要的解决方案

于 2013-10-23T22:59:50.600 回答
0

使用 strtok 将分隔符分隔为“:”的字符串。这适用于 char 数组。所以你将不得不使用一些哄骗。

http://www.cplusplus.com/reference/cstring/strtok/

http://www.cplusplus.com/reference/string/string/c_str/

于 2013-10-23T23:02:47.047 回答