课后自主练习(排序)1019. 文件排序 easy《编程思维与实践》个人学习笔记

题目

在这里插入图片描述
在这里插入图片描述
输入的部分过长,放在最后

思路

难点主要在输入和输出。。。排序借助malloc new qsort或者sort都可以轻松解决,这个题目特别坑的是给了个“LIST /NAME”这些操作中间还有个空格。。。一下子没发现浪费了好多时间
本人自己写的cmp代码也比较繁琐,下面找了一下其他大佬的cmp代码比鄙人简洁的多,列在改进中

另外,个人理解sort的cmp中返回true是不进行调换
而qsort返回1(true)是调换

改进

在这里插入图片描述

代码

#include<iostream>
#include<algorithm>
using namespace std;
class file
{
public:
    int year;
    int mon;
    int day;
    int hour;
    int min;
    long long size;
    string name;
    
};

bool Ncmp(const file A ,const file B)
{
    return (A.name < B.name);
};
bool Scmp(const file A ,const file B)
{
    if(A.size != B.size)
    {return (A.size < B.size);}
    else 
        return A.name < B.name;
    
};
bool Tcmp(const file A ,const file B)
{
    if(A.year < B.year)
    {return true;}
    else if(A.year > B.year)
    {return false;}
    else
    {
        if(A.mon < B.mon)
        {return true;}
        else if(A.mon > B.mon)
        {return false;}
        else
        {
            if(A.day < B.day)
            {return true;}
            else if(A.day > B.day)
            {return false;}
            else
            {
                if(A.hour < B.hour)
                {return true;}
                else if(A.hour > B.hour)
                {return false;}
                else
                {
                    if(A.min < B.min)
                    {return true;}
                    else if(A.min > B.min)
                    {return false;}
                    else
                    {
                        return(A.name < B.name);
                    }
                }
            }
        }
    }

};


void func(int total)
{
    file* F = new file[total];
    for(int i = 0; i < total; i++)
    {
        char c;
        cin >> F[i].year >> c >> F[i].mon >> c >> F[i].day >> F[i].hour >> c >> F[i].min >> F[i].size >> F[i].name;
    }

    string list,opera;//输入的操作有空格。。。
    
    cin>>list >> opera;

    if(opera == "/NAME")
    {
        sort(F,F+total,Ncmp);
    }
    else if (opera == "/SIZE")//
    {
        sort(F,F+total,Scmp);
    }
    else if (opera == "/TIME")
    {
        sort(F,F+total,Tcmp);
    }
    
    for(int i = 0; i < total; i++)
    {
        printf("%04d-%02d-%02d %02d:%02d%17lld ",F[i].year,F[i].mon,F[i].day,F[i].hour,F[i].min,F[i].size);\
        cout << F[i].name << endl;
    }
    cout << endl;

}

int main()
{
    int total = 1;
    
    
    while(total)
    {   
        cin >> total;
        func(total);
    }
        
    return 0;
    
    
}

输入

5
2011-01-26 07:32 388 ScoreStat.c
2011-01-26 19:48 429 ScoreStat.cpp
2011-01-25 20:11 22016 成绩统计.doc
2011-01-26 19:48 307200 ScoreStat++.exe
2011-01-25 20:08 270336 ScoreStat.exe
LIST /NAME
100
2011-03-11 12:38 0 0.log
2011-03-13 16:34 0 1.txt
2008-06-21 05:55 3680 actsetup.log
2008-06-20 16:40 105 AMS2INST.LOG
2009-06-03 08:10 67 amy.ini
2009-04-01 21:14 66 Cert.INI
2005-08-14 05:00 82944 clock.avi
2008-06-20 14:35 200 cmsetacl.log
2010-02-15 17:21 7640 COM+.log
2010-10-13 11:57 598562 comsetup.log
2008-06-20 14:41 0 control.ini
2009-05-05 09:29 5760054 CoopenOldWallPaper.bmp
2004-08-22 17:04 69120 daemon.dll
2005-08-14 05:00 2 desktop.ini
2008-06-20 15:02 7254 DPINST.LOG
2008-06-20 14:37 133 DtcInstall.log
2007-06-13 21:21 977920 explorer.exe
2008-08-28 11:10 173 explorer.exe.config
2005-08-14 05:00 80 explorer.scf
2010-10-13 11:56 1722100 FaxSetup.log
2005-12-02 11:46 84 filespec7x
2010-02-15 10:15 0 frontpg.ini
2009-09-19 12:11 28 funshionplugin2.INI
2005-05-27 07:22 10752 hh.exe
2008-09-14 08:53 174791 hplj1010.his
2008-09-14 08:53 16734 hplj1010.ini
2008-06-21 05:17 4718 HPQLB.LOG
2011-01-06 08:22 1156 IE4ErrorLog.txt
2008-08-23 10:47 1187 ie7_main.log
2009-06-13 06:34 227494 ie8.log
2009-05-31 11:46 97996 ie8Uninst.log
2009-06-13 06:36 469132 ie8_main.log
2010-10-13 11:57 699472 iis6.log
2010-10-13 11:56 1393 imsins.BAK
2010-10-13 11:57 1393 imsins.log
2009-04-28 12:32 3686454 iSeeLogin.bmp
2009-05-01 21:37 247 iSeeScr.ini
2006-12-18 21:46 926208 iSeeScr.SCR
2010-07-04 10:04 108 iSeeScrView.ini
2003-02-28 16:35 6550 jautoexp.dat
2010-08-11 11:11 14536 KB2079403.log
2010-08-11 10:54 12277 KB2115168.log
2010-09-15 12:05 13816 KB2121546.log
2010-08-11 10:53 14142 KB2160329.log
2010-07-14 09:24 12307 KB2229593.log
2010-10-13 11:56 9405 KB2279986.log
2010-08-04 08:37 15642 KB2286198.log
2010-10-13 11:56 11546 KB2296011.log
2010-09-15 12:06 13163 KB2347290.log
2010-10-13 11:55 9314 KB2360937.log
2010-10-13 11:57 7893 KB2378111.log
2008-06-28 22:19 4664 KB885884.log
2008-06-21 04:43 10587 KB888111.log
2008-06-21 10:11 6354 KB900485.log
2008-06-21 09:11 9628 KB900725.log
2008-06-21 09:12 15952 KB901017.log
2008-06-21 09:12 9284 KB901190.log
2008-06-28 22:21 29878 KB902400.log
2008-06-21 09:11 9224 KB905414.log
2008-06-21 09:11 8737 KB905749.log
2008-06-21 09:12 9661 KB908519.log
2008-06-21 09:13 11978 KB908531.log
2008-06-21 09:12 9574 KB910437.log
2008-06-21 09:14 11227 KB911280.log
2008-06-25 21:42 11718 KB911562.log
2008-06-21 09:13 11904 KB911564.log
2008-06-21 09:12 10071 KB911927.log
2008-06-21 09:13 13500 KB913580.log
2008-06-21 09:14 13607 KB914388.log
2008-06-21 09:13 12478 KB914389.log
2008-06-21 09:13 11170 KB916595.log
2008-10-23 11:27 8334 KB917953.log
2010-02-23 12:01 20716 KB918005.log
2008-06-21 09:17 26131 KB918118.log
2008-06-21 09:14 11554 KB918439.log
2008-06-21 09:16 22589 KB920213.log
2008-06-21 09:14 12660 KB920670.log
2008-06-21 09:14 14402 KB920683.log
2008-06-21 09:14 13456 KB920685.log
2008-06-21 10:11 7966 KB920872.log
2008-06-21 09:15 16750 KB922582.log
2008-06-21 09:15 18878 KB922819.log
2008-06-21 09:16 16837 KB923191.log
2008-06-21 09:15 18684 KB923414.log
2009-04-16 14:54 11335 KB923561.log
2008-06-28 22:20 12345 KB923689.log
2008-06-21 09:16 23117 KB923980.log
2008-06-21 09:16 21088 KB924270.log
2008-06-21 09:15 16660 KB924496.log
2008-06-21 09:17 23957 KB924667.log
2010-02-23 12:03 18315 KB924941.log
2008-06-21 09:16 25353 KB925398.log
2008-06-21 09:17 34802 KB925454.log
2008-06-21 09:15 16070 KB925486.log
2009-09-01 09:55 14919 KB925720.log
2008-06-21 09:19 33141 KB925902.log
2008-06-21 09:17 23030 KB926255.log
2008-06-21 09:18 26224 KB926436.log
2008-06-21 09:18 29889 KB927779.log
2008-06-21 09:18 28576 KB927802.log
LIST /TIME
5
2011-01-26 07:32 388 ScoreStat.c
2011-01-26 19:48 429 ScoreStat.cpp
2011-01-25 20:11 22016 成绩统计.doc
2011-01-26 19:48 307200 ScoreStat++.exe
2011-01-25 20:08 270336 ScoreStat.exe
LIST /TIME
14
2011-03-13 16:32 0 1.txt
2011-01-25 19:17 542 run.bat
2011-03-13 16:29 259 stringSort01.in
2011-03-13 16:29 259 stringSort02.in
2011-03-13 16:29 259 stringSort03.in
2011-03-13 16:29 62 stringSort04.in
2011-03-13 16:30 62 stringSort05.in
2011-03-13 16:30 115 stringSort06.in
2011-01-25 19:36 340 stringSort07.in
2011-01-25 19:38 373 stringSort08.in
2011-01-25 19:39 378 stringSort09.in
2011-01-25 19:40 43 stringSort10.in
2011-01-26 09:15 1103 复件run.bat
2011-03-13 16:31 12 复件复件run.bat
LIST /NAME
5
2011-01-26 07:32 388 ScoreStat.c
2011-01-26 19:48 429 ScoreStat.cpp
2011-01-25 20:11 22016 成绩统计.doc
2011-01-26 19:48 307200 ScoreStat++.exe
2011-01-25 20:08 270336 ScoreStat.exe
LIST /SIZE
100
2011-03-11 12:38 0 0.log
2011-03-13 16:34 0 1.txt
2008-06-21 05:55 3680 actsetup.log
2008-06-20 16:40 105 AMS2INST.LOG
2009-06-03 08:10 67 amy.ini
2009-04-01 21:14 66 Cert.INI
2005-08-14 05:00 82944 clock.avi
2008-06-20 14:35 200 cmsetacl.log
2010-02-15 17:21 7640 COM+.log
2010-10-13 11:57 598562 comsetup.log
2008-06-20 14:41 0 control.ini
2009-05-05 09:29 5760054 CoopenOldWallPaper.bmp
2004-08-22 17:04 69120 daemon.dll
2005-08-14 05:00 2 desktop.ini
2008-06-20 15:02 7254 DPINST.LOG
2008-06-20 14:37 133 DtcInstall.log
2007-06-13 21:21 977920 explorer.exe
2008-08-28 11:10 173 explorer.exe.config
2005-08-14 05:00 80 explorer.scf
2010-10-13 11:56 1722100 FaxSetup.log
2005-12-02 11:46 84 filespec7x
2010-02-15 10:15 0 frontpg.ini
2009-09-19 12:11 28 funshionplugin2.INI
2005-05-27 07:22 10752 hh.exe
2008-09-14 08:53 174791 hplj1010.his
2008-09-14 08:53 16734 hplj1010.ini
2008-06-21 05:17 4718 HPQLB.LOG
2011-01-06 08:22 1156 IE4ErrorLog.txt
2008-08-23 10:47 1187 ie7_main.log
2009-06-13 06:34 227494 ie8.log
2009-05-31 11:46 97996 ie8Uninst.log
2009-06-13 06:36 469132 ie8_main.log
2010-10-13 11:57 699472 iis6.log
2010-10-13 11:56 1393 imsins.BAK
2010-10-13 11:57 1393 imsins.log
2009-04-28 12:32 3686454 iSeeLogin.bmp
2009-05-01 21:37 247 iSeeScr.ini
2006-12-18 21:46 926208 iSeeScr.SCR
2010-07-04 10:04 108 iSeeScrView.ini
2003-02-28 16:35 6550 jautoexp.dat
2010-08-11 11:11 14536 KB2079403.log
2010-08-11 10:54 12277 KB2115168.log
2010-09-15 12:05 13816 KB2121546.log
2010-08-11 10:53 14142 KB2160329.log
2010-07-14 09:24 12307 KB2229593.log
2010-10-13 11:56 9405 KB2279986.log
2010-08-04 08:37 15642 KB2286198.log
2010-10-13 11:56 11546 KB2296011.log
2010-09-15 12:06 13163 KB2347290.log
2010-10-13 11:55 9314 KB2360937.log
2010-10-13 11:57 7893 KB2378111.log
2008-06-28 22:19 4664 KB885884.log
2008-06-21 04:43 10587 KB888111.log
2008-06-21 10:11 6354 KB900485.log
2008-06-21 09:11 9628 KB900725.log
2008-06-21 09:12 15952 KB901017.log
2008-06-21 09:12 9284 KB901190.log
2008-06-28 22:21 29878 KB902400.log
2008-06-21 09:11 9224 KB905414.log
2008-06-21 09:11 8737 KB905749.log
2008-06-21 09:12 9661 KB908519.log
2008-06-21 09:13 11978 KB908531.log
2008-06-21 09:12 9574 KB910437.log
2008-06-21 09:14 11227 KB911280.log
2008-06-25 21:42 11718 KB911562.log
2008-06-21 09:13 11904 KB911564.log
2008-06-21 09:12 10071 KB911927.log
2008-06-21 09:13 13500 KB913580.log
2008-06-21 09:14 13607 KB914388.log
2008-06-21 09:13 12478 KB914389.log
2008-06-21 09:13 11170 KB916595.log
2008-10-23 11:27 8334 KB917953.log
2010-02-23 12:01 20716 KB918005.log
2008-06-21 09:17 26131 KB918118.log
2008-06-21 09:14 11554 KB918439.log
2008-06-21 09:16 22589 KB920213.log
2008-06-21 09:14 12660 KB920670.log
2008-06-21 09:14 14402 KB920683.log
2008-06-21 09:14 13456 KB920685.log
2008-06-21 10:11 7966 KB920872.log
2008-06-21 09:15 16750 KB922582.log
2008-06-21 09:15 18878 KB922819.log
2008-06-21 09:16 16837 KB923191.log
2008-06-21 09:15 18684 KB923414.log
2009-04-16 14:54 11335 KB923561.log
2008-06-28 22:20 12345 KB923689.log
2008-06-21 09:16 23117 KB923980.log
2008-06-21 09:16 21088 KB924270.log
2008-06-21 09:15 16660 KB924496.log
2008-06-21 09:17 23957 KB924667.log
2010-02-23 12:03 18315 KB924941.log
2008-06-21 09:16 25353 KB925398.log
2008-06-21 09:17 34802 KB925454.log
2008-06-21 09:15 16070 KB925486.log
2009-09-01 09:55 14919 KB925720.log
2008-06-21 09:19 33141 KB925902.log
2008-06-21 09:17 23030 KB926255.log
2008-06-21 09:18 26224 KB926436.log
2008-06-21 09:18 29889 KB927779.log
2008-06-21 09:18 28576 KB927802.log
LIST /NAME
1
2011-01-26 07:32 388 ScoreStat.c
LIST /TIME
14
2011-03-13 16:32 0 1.txt
2011-01-25 19:17 542 run.bat
2011-03-13 16:29 259 stringSort01.in
2011-03-13 16:29 259 stringSort02.in
2011-03-13 16:29 259 stringSort03.in
2011-03-13 16:29 62 stringSort04.in
2011-03-13 16:30 62 stringSort05.in
2011-03-13 16:30 115 stringSort06.in
2011-01-25 19:36 340 stringSort07.in
2011-01-25 19:38 373 stringSort08.in
2011-01-25 19:39 378 stringSort09.in
2011-01-25 19:40 43 stringSort10.in
2011-01-26 09:15 1103 复件run.bat
2011-03-13 16:31 12 复件复件run.bat
LIST /TIME
1
2011-01-26 07:32 388 ScoreStat.c
LIST /NAME
2
2011-01-26 19:48 429 ScoreStat.cpp
2011-01-26 19:48 307200 ScoreStat++.exe
LIST /TIME
14
2011-03-13 16:32 0 1.txt
2011-01-25 19:17 542 run.bat
2011-03-13 16:29 259 stringSort01.in
2011-03-13 16:29 259 stringSort02.in
2011-03-13 16:29 259 stringSort03.in
2011-03-13 16:29 62 stringSort04.in
2011-03-13 16:30 62 stringSort05.in
2011-03-13 16:30 115 stringSort06.in
2011-01-25 19:36 340 stringSort07.in
2011-01-25 19:38 373 stringSort08.in
2011-01-25 19:39 378 stringSort09.in
2011-01-25 19:40 43 stringSort10.in
2011-01-26 09:15 1103 复件run.bat
2011-03-13 16:31 12 复件复件run.bat
LIST /NAME
0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值