C++ 在字符串中分解时间的条件

C++ Condition to break apart time in string

本文关键字:时间 条件 分解 字符串 C++      更新时间:2023-10-16

我的代码有一个问题,涉及如何获取表示时间的字符串数据成员,由它包含的冒号数处理,然后分隔并转换为整数。

目标: 取一串时间,以 ##:##:##:## 的形式表示,用 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;

感谢您的帮助。

也许,像这样?

#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)来工作。

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

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

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

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