用c++的string 处理时间记录 18-2

string 的切分,转字符,getline 用法按行读入

未实现:转字符串 文件输出?

#include<fstream>
#include<iostream>
#include<string>
#include<algorithm>
/*
#include<stdlib.h>
#include<ctype.h>    //string.c_str
#include<cstring>   //为了用strcopy
*/
using namespace std;

struct stu
{
    string id;
    string itime;
    string otime;
    int sum;
};

bool cmp(stu a,stu b) //比较规则
{
    return a.sum>b.sum;
}


int main()
{
    ifstream infile;
    ofstream outfile;
    int index=0;
    int ot[10],it[10];
    stu buf[100];

    infile.open("test.in");
    if (!infile) return 0; //fail to open
    string strLine;
    int a;
    infile>>a;
    cout<<a<<endl;

    while(getline(infile,strLine)) //按行从外部读入
    {

        if(strLine.empty())
            continue;
        buf[index].id=strLine.substr(0,9);    //将string切分
        buf[index].itime=strLine.substr(10,5);
        buf[index].otime=strLine.substr(16,5);
        index++;
        cout<<strLine <<endl;
    }
    for(int i=0; i<a; i++)
    {
        if(buf[i].itime>=buf[i].otime)
        {
            it[0]=(buf[i].itime[0]-'0')*10+(buf[i].itime[1]-'0');//将string换成int
            it[1]=(buf[i].itime[3]-'0')*10+(buf[i].itime[4]-'0');
            ot[0]=(buf[i].otime[0]-'0')*10+(buf[i].otime[1]-'0');
            ot[1]=(buf[i].otime[3]-'0')*10+(buf[i].otime[4]-'0');
            buf[i].sum=-1;
            //continue;
        }
        else
        {
            it[0]=(buf[i].itime[0]-'0')*10+(buf[i].itime[1]-'0');
            it[1]=(buf[i].itime[3]-'0')*10+(buf[i].itime[4]-'0');
            ot[0]=(buf[i].otime[0]-'0')*10+(buf[i].otime[1]-'0');
            ot[1]=(buf[i].otime[3]-'0')*10+(buf[i].otime[4]-'0');
            buf[i].sum=(ot[0]-it[0])*60+ot[1]-it[1];  //计算时间
        }
        cout<<buf[i].itime<<" "<<it[0]<<" "<<it[1]<<endl;
        //cout<<endl;
        cout<<buf[i].otime<<" "<<ot[0]<<" "<<ot[1]<<endl;
        cout<<buf[i].sum<<endl;

    }//for
    sort(buf,buf+a,cmp);  //用自定义的cmp对buf排序
    cout<<buf[0].id<<" "<<buf[0].sum<<endl;  //输出最大值

    system("pause");
    return 0;




}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值