zoukankan      html  css  js  c++  java
  • 第五周作业

    这个作业属于那个课程 C语言程序设计II
    这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass4-2018/homework/2826
    我在这个课程的目标是 学会使用字符串编程
    这个作业在那个具体方面帮助我实现目标 运用字符串以及数组,用数组来储存单词
    参考文献 C Primer Plus

    7-1 英文单词排序 (25 分)

    本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。

    输入格式:

    输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。

    输出格式:

    输出为排序后的结果,每个单词后面都额外输出一个空格。

    输入样例:

    blue
    red
    yellow
    green
    purple
    #

    输出样例:

    red blue green yellow purple 

    代码

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char input[21][11] = { '' };
        char a[11] = { '' };
        int i = 0;
        while (1) {
            scanf("%s", input[i]);
            if (input[i][0] == '#')
                break;
                i++;
        }
        input[i][0] = '';
        int len = i;
        int j = 0;
        for (i = 0; i < len; i++)
        {
            for(j = 0; j < len-i; j++)
            {
                if (strlen(input[j - 1]) > strlen(input[j]))
                {
                    strcpy(a, input[j - 1]);
                    strcpy(input[j - 1], input[j]);
                    strcpy(input[j], a);
                }
            }
        }
        for(i = 0; i < len; i++)
            printf("%s ", input[i]);
        return 0;
    
    }

    博客园要求代码

    #include<stdio.h>
    #include<stdlib.h  >
    #include<string.h>
    int main()
    {
        
        FILE * fp;
        char input[21][11] = { '' };
        char a[11] = { '' };
        int i = 0;
        if((fp=fopen("C:\Users\hhw\Desktop\huhongwei.text","a+"))==NULL){
            printf("File open error!
    ");
            exit (0);
        }
        while (1) {
            fscanf(fp,"%s", input[i]);
            if (input[i][0] == '#')
                break;
                i++;
        }
        input[i][0] = '';
        int len = i;
        int j = 0;
        for (i = 0; i < len; i++)
        {
            for(j = 0; j < len-i; j++)
            {
                if (strlen(input[j - 1]) > strlen(input[j]))
                {
                    strcpy(a, input[j - 1]);
                    strcpy(input[j - 1], input[j]);
                    strcpy(input[j], a);
                }
            }
        }
        for(i = 0; i < len; i++)
            fprintf(fp,"%s ", input[i]);
        if(fclose(fp)){
            printf("Can not close the file!
    ");
            exit (0);
        }
        return 0;
    
    }

    运行截图

    1.预习的主要内容

    指针和字符串,指针和数组,使用指针形参

    2.完成情况截图

    3.预习中存在的疑惑

    (1),在数组和指针同时使用时,代码的读懂性不高,感觉很混乱

    (2).指针具有兼容性,但指针的指针和数组的数组为何是双重间接?

    学习感悟

    结对编程有好有坏,虽然可以相互讨论,但是由于这次不是自愿结对编程是随机的。结对编程的伙伴积极度不高

  • 相关阅读:
    为 HTTP/2 头压缩专门设计的 HPACK
    HTTP2 帧基础知识以及Header、CONTINUATION、DATA帧相关资料:
    扩充巴科斯-瑙尔范式 ABNF简介
    我Win下常用工具清单
    gRPC版本的 Google APIs
    gRPC 的route_guide例子
    proto3 笔记1
    编译gRPC Go版本使用的 ProtoBuffer 文件
    新浪校园招聘2013.10.30浙大玉泉4教301笔试的前端妹纸,像雾像雨又像风
    Android下Notification,样式style,主题theme的功能实现
  • 原文地址:https://www.cnblogs.com/hhwcg/p/10607081.html
Copyright © 2011-2022 走看看