[{"data":1,"prerenderedAt":231},["ShallowReactive",2],{"content:\u002F2022\u002Fc-next-sec":3,"surround:\u002F2022\u002Fc-next-sec":220},{"id":4,"title":5,"body":6,"categories":194,"date":196,"description":197,"draft":198,"extension":199,"image":200,"meta":201,"navigation":203,"path":204,"permalink":200,"published":200,"readingTime":205,"recommend":200,"references":200,"seo":210,"sitemap":211,"stem":212,"tags":213,"type":217,"updated":218,"__hash__":219},"content\u002Fposts\u002F2022\u002Fc-next-sec.md","C语言字符串时间获取下一秒的思考",{"type":7,"value":8,"toc":174},"minimark",[9,13,22,26,36,39,42,48,51,57,60,64,70,73,79,82,88,91,97,100,106,109,112,120,136,156,171],[10,11,12],"h2",{"id":12},"题目",[14,15,16,17,21],"p",{},"输入一个24小时制时间 ",[18,19,20],"code",{"code":20},"HH:MM:SS","，输出它的下一秒。",[23,24,25],"h3",{"id":25},"时间结构体定义",[27,28,34],"pre",{"className":29,"code":31,"language":32,"meta":33},[30],"language-c","typedef struct\n{\n    int h;\n    int m;\n    int s;\n} time;\n","c","",[18,35,31],{"__ignoreMap":33},[10,37,38],{"id":38},"函数",[23,40,41],{"id":41},"输入时间到变量",[27,43,46],{"className":44,"code":45,"language":32,"meta":33},[30],"void getT(time *t)\n{\n    scanf(\"%d:%d:%d\", &t->h, &t->m, &t->s);\n}\n",[18,47,45],{"__ignoreMap":33},[23,49,50],{"id":50},"输出时间",[27,52,55],{"className":53,"code":54,"language":32,"meta":33},[30],"void printT(time t)\n{\n    printf(\"%02d:%02d:%02d\", t.h, t.m, t.s);\n}\n",[18,56,54],{"__ignoreMap":33},[23,58,59],{"id":59},"计算下一秒",[61,62,63],"h4",{"id":63},"写法一",[27,65,68],{"className":66,"code":67,"language":32,"meta":33},[30],"time nextSec(time t)\n{\n    if (t.s \u003C 59)\n        t.s++;\n    else if (t.m \u003C 59)\n        t.m++, t.s = 0;\n    else if (t.h \u003C 23)\n        t.h++, t.m = t.s = 0;\n    else\n        t.h = t.m = t.s = 0;\n    return t;\n}\n",[18,69,67],{"__ignoreMap":33},[61,71,72],{"id":72},"写法二",[27,74,77],{"className":75,"code":76,"language":32,"meta":33},[30],"time nextSec2(time t)\n{\n    if (t.s \u003C 59)\n        t.s++;\n    else\n    {\n        t.s = 0;\n        if (t.m \u003C 59)\n            t.m++;\n        else\n        {\n            t.m = 0;\n            if (t.h \u003C 23)\n                t.h++;\n            else\n                t.h = 0;\n        }\n    }\n    return t;\n}\n",[18,78,76],{"__ignoreMap":33},[61,80,81],{"id":81},"写法三",[27,83,86],{"className":84,"code":85,"language":32,"meta":33},[30],"time nextSec3(time t)\n{\n    if (t.s == 59)\n        if (t.m == 59)\n            if (t.h == 23)\n                t.h = t.m = t.s = 0;\n            else\n                t.h++, t.m = t.s = 0;\n        else\n            t.m++, t.s = 0;\n    else\n        t.s++;\n    return t;\n}\n",[18,87,85],{"__ignoreMap":33},[61,89,90],{"id":90},"写法四",[27,92,95],{"className":93,"code":94,"language":32,"meta":33},[30],"time nextSec4(time t)\n{\n    if (t.s == 59)\n    {\n        t.s = 0;\n        if (t.m == 59)\n        {\n            t.m = 0;\n            if (t.h == 23)\n                t.h = 0;\n            else\n                t.h++;\n        }\n        else\n            t.m++;\n    }\n    else\n        t.s++;\n    return t;\n}\n",[18,96,94],{"__ignoreMap":33},[23,98,99],{"id":99},"主函数",[27,101,104],{"className":102,"code":103,"language":32,"meta":33},[30],"int main()\n{\n    time t;\n    getT(&t);\n    printT(nextSec(t));\n}\n",[18,105,103],{"__ignoreMap":33},[10,107,108],{"id":108},"思考",[14,110,111],{},"从功能和实现的角度来看，所有四种写法都可以正确地将输入的时间t增加1秒，并返回增加1秒后的时间结果。因此，从这个角度来说，四种写法都是正确的。但是，从可读性和可维护性的角度来看，有一些差异。",[14,113,114,115,119],{},"第一种写法使用了多个 ",[18,116,118],{"className":117,"code":118,"language":32},[30],"else if"," 语句，每一个分支都需要检查当前秒数t.s、当前分钟数t.m和当前小时数t.h是否满足条件并进行相应的修改。这使得代码变得冗长且难以阅读。如果后续需要修改时间增加的规则，就需要修改多个分支，容易出现错误。",[14,121,122,123,127,128,131,132,135],{},"第二种写法使用了嵌套的 ",[18,124,126],{"className":125,"code":126,"language":32},[30],"if"," 语句来实现相同的功能。这样做可以消除 ",[18,129,118],{"className":130,"code":118,"language":32},[30]," 的嵌套，从而使得代码看起来更加清爽。但是，这种写法也有相应的问题：嵌套的if会使代码的缩进层数增加，从而增加了阅读复杂性；此外，嵌套的 ",[18,133,126],{"className":134,"code":126,"language":32},[30]," 语句也可能降低代码的可读性。",[14,137,138,139,142,143,146,147,150,151,155],{},"第三种写法使用了嵌套的 ",[18,140,126],{"className":141,"code":126,"language":32},[30]," 语句和多个嵌套的 ",[18,144,126],{"className":145,"code":126,"language":32},[30]," 语句的组合来实现时间增加功能。此外，使用多个嵌套的 ",[18,148,126],{"className":149,"code":126,"language":32},[30]," 语句可以减少代码的缩进层数并且规避使用 ",[18,152,154],{"className":153,"code":154,"language":32},[30],"else"," 语句的问题。尽管这种写法有更多的if嵌套，但其清晰度和可读性优于前两种写法。",[14,157,158,159,162,163,166,167,170],{},"第四种写法也使用了嵌套的 ",[18,160,126],{"className":161,"code":126,"language":32},[30]," 语句，但是这里只使用了一个 ",[18,164,154],{"className":165,"code":154,"language":32},[30]," 语句来代替多个 ",[18,168,118],{"className":169,"code":118,"language":32},[30]," 语句，从而减少代码的数量和难度。此外，减少缩进引起的嵌套和深度也有助于提高代码的可读性和可维护性。因此，这种写法比前三种写法更好。",[14,172,173],{},"总体而言，第四种写法最为简洁、易读、易懂，并且最便于维护和修改。",{"title":33,"searchDepth":175,"depth":175,"links":176},4,[177,182,193],{"id":12,"depth":178,"text":12,"children":179},2,[180],{"id":25,"depth":181,"text":25},3,{"id":38,"depth":178,"text":38,"children":183},[184,185,186,192],{"id":41,"depth":181,"text":41},{"id":50,"depth":181,"text":50},{"id":59,"depth":181,"text":59,"children":187},[188,189,190,191],{"id":63,"depth":175,"text":63},{"id":72,"depth":175,"text":72},{"id":81,"depth":175,"text":81},{"id":90,"depth":175,"text":90},{"id":99,"depth":181,"text":99},{"id":108,"depth":178,"text":108},[195],"开发","2022-12-18 19:53:37","“回字有四样写法”，在 C 语言中，我为「获取下一秒」找了四种写法。",false,"md",null,{"slots":202},{},true,"\u002F2022\u002Fc-next-sec",{"text":206,"minutes":207,"time":208,"words":209},"4 min read",3.75,225000,750,{"title":5,"description":197},{"loc":204},"posts\u002F2022\u002Fc-next-sec",[214,215,216],"C语言","时间处理","编程思考","tech","2023-03-18 22:09:08","Mf-7-Yghapbjgd_I06rTD7oOtWm8vD8PpgBzVBPZaW8",[221,226],{"title":222,"path":223,"stem":224,"date":225,"type":217,"children":-1},"西邮Linux兴趣小组2020纳新面试题题解","\u002F2022\u002Flinux-interview-2020","posts\u002F2022\u002Flinux-interview-2020","2022-11-20 13:57:19",{"title":227,"path":228,"stem":229,"date":230,"type":217,"children":-1},"简单实现C语言字符串缓冲区","\u002F2023\u002Fc-strbuf","posts\u002F2023\u002Fc-strbuf","2023-01-01 23:58:33",1782091376955]