有一些日期,日期格式为“MM/DD/YYYY”编程将其按日期大小排列,下面我们就来聊聊关于表格怎么一键排序?接下来我们就一起去了解一下吧!
表格怎么一键排序?有一些日期,日期格式为“MM/DD/YYYY”编程将其按日期大小排列,下面我们就来聊聊关于表格怎么一键排序?接下来我们就一起去了解一下吧!
表格怎么一键排序
ICPC--1211: 日期排序题目描述有一些日期,日期格式为“MM/DD/YYYY”。编程将其按日期大小排列。
样例输入15/12/199910/21/200310/22/200302/12/200411/30/200512/31/2005
15/12/199910/21/200310/22/200302/12/200411/30/200512/31/2005
#include <iostream>using namespace std;struct Date{char d[20];};int if_ear_is_earlier(Date,Date);int year(Date);int month(Date);int day(Date); int wei(char);int main(){const int arsize=6;//数组的大小可自定义Date date[arsize];//输入日期for(int i=0;i<arsize;i){cin>>date[i].d;}//比较大小,用选择排序法for(int i=0;i<arsize-1;i){// Date ear=date[i];for(int j=i;j<arsize;j)if(if_ear_is_earlier(date[i],date[j]))//调用函数,判断是否date[i]代表的日期更早;else{//如果不,则进行数值对换,使date[i]的日期更早Date t=date[i];date[i]=date[j];date[j]=t;}}//cout<<endl;for(int i=0;i<arsize;i){cout<<date[i].d<<endl;}return 0;}int if_ear_is_earlier(Date ear,Date date1){//判断ear是否比date1早,是则返回1,不是则返回0//首先比较年份int year_ear=year(ear);//调用函数,使返回ear所表示的年的值int year_date1=year(date1);//进行判断if(year_ear<year_date1)return 1;elseif(year_ear>year_date1)return 0;else{int month_ear=month(ear);//调用函数,比较月份int month_date1=month(date1);if(month_ear<month_date1)return 1;elseif(month_ear>month_date1)return 0;else{int day_ear=day(ear);//调用函数,比较日int day_date1=day(date1);if(day_ear<day_date1)return 1;elsereturn 0;}} }int year(Date date)//将年份的字符转换为数字{return wei(date.d[6])*1000 wei(date.d[7])*100 wei(date.d[8])*10 wei(date.d[9]); //调用wei函数 }int month(Date date)//将月份的字符转换为数字{return wei(date.d[0])*10 wei(date.d[1]);} int day(Date date)//将日期的字符转换为数字{return wei(date.d[3])*10 wei(date.d[4]);}int wei(char c)//将字符转换为数字{int n=c-'0';return n;}