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;
}