C++ CodeWar KATA4:Human readable duration format

让我们先来读题:

Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

It is much easier to understand with an example:

* For seconds = 62, your function should return 
    "1 minute and 2 seconds"
* For seconds = 3662, your function should return
    "1 hour, 1 minute and 2 seconds"

        其实理解起来很简单,给定你一个秒数,让你根据现实转换成年,日,小时,分钟,秒的组合。其中要注意标点符号以及"and"的顺序及位置。

做题思路:

        首先将各个年月日的计算结果num放到一个vector中,再将各个计算结果对应的英语名称放到另一个vector<string>中。(注意顺序,应该是一一对应的)如果碰到num为0的情况,直接在遍历中continue即可,大于1的情况下,通过stringstream转换成string后再加's'即可。

        通过观察可以发现"and"仅会出现一次,并且出现在倒数第二个非零的num所对应的时间跨度以及最后一个非零的num所对应的时间跨度中间。于是我写了一个get_vector_notzero函数用于数出每个num后续对应的非零num个数。其余情况都是", "。

        下面请看代码。

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

using namespace std;
int get_vector_notzero(vector<int>vec,int pos)//计算当前位置的num后面还有多少个非零数
  { 
    int count = 0;
    for(auto i =pos+1;i<vec.size();i++)
      {
        if(vec[i]!=0) count +=1;
      }
    return count;
  }

string format_duration(int seconds) {
  // your code here
  stringstream ss;//用于int和string类型的转换
  vector<int> num;
  vector<string> str1;
  vector<string> punc;//放置标点符号的vector
  
  string now = "now";
  string year = "year";
  str1.push_back(year);
  string day = "day";
  str1.push_back(day);
  string hour = "hour";
  str1.push_back(hour);
  string minute ="minute";
  str1.push_back(minute);
  string second ="second";
  str1.push_back(second);
  

  punc.push_back(", ");
  punc.push_back(" and ");

  
  string res;//存放结果的string

  int Num_year,Num_day,Num_hour,Num_minute,Num_second;//计算所需的时间
  Num_year=seconds/31536000;
  num.push_back(Num_year);
  Num_day = (seconds-31536000*Num_year)/86400;
  num.push_back(Num_day);
  Num_hour = (seconds-31536000*Num_year-86400*Num_day)/3600;
  num.push_back(Num_hour);
  Num_minute = (seconds-31536000*Num_year-86400*Num_day-3600*Num_hour)/60;
  num.push_back(Num_minute);
  Num_second = seconds-31536000*Num_year-86400*Num_day-3600*Num_hour-60*Num_minute;
  num.push_back(Num_second);
  
    
  if(seconds==0) return now;
  else
    {
        for(auto i = 0;i<5;i++)
          {
            if(num[i]==0) continue;
            else
              {
                string temp_str;
                if(num[i]>1) str1[i]+='s';
                //res+=num[i];
                ss<<num[i];
                ss>>temp_str;
                res+=temp_str;
                res+=" ";
                ss.clear();
                res+=str1[i];
                
                int vector_notzero = get_vector_notzero(num,i);
                if(i==4) break;//如果是最后一个数,直接跳出循环
                else if(vector_notzero==0) break;//如果后面的数都为0,无需加标点,直接退出循环
                else if(vector_notzero==1) //如果后面还有一个数不为0,那么加and
                    {
                      res+=punc[1];//1是and 0是”,“
                      continue;
                    }
                else res+=punc[0];

              }
          }
    return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值