首页 > 技术文章 > 全国高校绿色计算大赛 预赛第一阶段(C++)第4关:计算日期

277223178dudu 2018-10-29 11:39 原文

挑战任务

我们吃的食物都有保质期,现在食品监督管理局想要制作一个能准确计算食品过期日期的小程序,需要请你来进行设计。

例如:A食品在2018年1月1日生产,保质期是20天,则它的过期日期在2018年1月21日。

编程要求

补全函数string getDate(string releaseDate,int day)其中releaseDate表示食品出厂日期day表示保质期,请根据传入的数据计算食品的过期日期,格式为yyyy-mm-dd4位年份2位月份2位日期。比如:2015-02-19

请严格按照格式书写,不能出现其它文字或符号,并将最终结果做为函数的返回值返回。

测试说明

样例1:

输入:
2016-01-0120

输出:

2016-01-21

#ifndef _TEST
#define _TEST
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
std::vector<std::string> split(std::string str,std::string pattern)
{
  std::string::size_type pos;
  std::vector<std::string> result;
  str+=pattern;
  int size=str.size();
 
  for(int i=0; i<size; i++)
  {
    pos=str.find(pattern,i);
    if(pos<size)
    {
      std::string s=str.substr(i,pos-i);
      result.push_back(s);
      i=pos+pattern.size()-1;
    }
  }
  return result;
}
class Task{
	public:
		string getDate(string releaseDate,int days){
  
            int year = atoi(split(releaseDate,"-")[0].c_str());
            int month = atoi(split(releaseDate,"-")[1].c_str());
            int day = atoi(split(releaseDate,"-")[2].c_str());
          
        int j = 0;
         for(j=1;j<=days;j++)
        {
            day=day+1;
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            {
                if(day==32)
                {
                    month=month+1;
                    day=1;
                }
                if(month==13)
                {
                    year=year+1;
                    month=1;
                }
            }
            if(month==2)
            {
                if(((year%4==0&&year%100!=0)||year%400==0))
                {
                  if(day==30)
                  {
                      day=1;
                      month=month+1;
                  }
                }
                else
                {
                    if(day==29)
                    {
                        day=1;
                        month=month+1;
                    }
                }
            }
            if(month==4||month==6||month==9||month==11)
            {
                if(day==31)
                {
                    month=month+1;
                    day=1;
                }
                if(month==13)
                {
                    year=year+1;
                    month=1;
                }
            }
        }

            
       char tmp[15] = "";
        sprintf(tmp,"%d-%02d-%02d",year,month,day);
        string re = tmp;
        return re;          			
	}
};
#endif

  

#ifndef _TEST
#define _TEST
#include <iostream>
#include <vector>
using namespace std;

class Task{
	public:
		vector<char> inversion(string str){
            vector <char> vec;
            int index = str.length();
            while(index--){
            	vec.push_back(str[index]);
            	}	
            return vec;
			}
};
#endif

  

#include <iostream>
#include <vector>
#include <stdio.h>
#include "Task.hpp"

using namespace std;


int main(){
	string str;
	getline(cin,str);

	Task tt;
    vector <char> result;
	result = tt.inversion(str);
    for(int i = 0; i<str.length();i++){
    	cout << result[i];   
    }
    cout << endl;
}

 

运行结果

 

 

推荐阅读