zoukankan      html  css  js  c++  java
  • Linux Linux程序练习三

    /*
    index1 = 45
    index2 = 36
    index3 = 231
    index4 = 43
    index5 = 100
    index6 = 123
    index7 = 51
     *
     *
     通过读取读取c.txt文件内容中等号右值,并将右值最大值,最小值和平均值打印到屏幕。
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    //删除'
    '
    void removeenter(char * str)
    {
        if (str == NULL)
        {
            printf("传入参数不可以为空!
    ");
            return;
        }
        char * temp = str;
        while (*temp)
        {
            if (*temp == '
    ')
            {
                *temp = '';
            }
            temp++;
        }
    }
    
    //获取数字
    char * getnum(char * str)
    {
        if (str == NULL)
        {
            printf("传入参数不可以为空!
    ");
            return NULL;
        }
        str=strchr(str,'=');
        while (*str)
        {
            if (*str>'0'&&*str<'9')
            {
                break;
            }
            str++;
        }
        return str;
    }
    
    //read the file
    void readfile(char *path, int *arr, int *len)
    {
        if (path == NULL || arr == NULL || len == NULL)
        {
            printf("传入参数不可以为空!
    ");
            return;
        }
        //define the file stream
        FILE * pfr = NULL;
        //open the file in read mode
        pfr = fopen(path, "r");
        //judge
        if (pfr == NULL)
        {
            printf("read the file failed ! error msg:%s
    ", strerror(errno));
            return;
        }
        //read the file
        char buf[50] = { 0 };
        int index = 0;
        while (fgets(buf, sizeof(buf), pfr))
        {
            //fgets()函数读取字符串时,会读取'
    '
            removeenter(buf);
            arr[index++] = atoi(getnum(buf));
            memset(buf, 0, sizeof(buf));
        }
        *len = index;
        //close the file
        if (pfr)
        {
            fclose(pfr);
            pfr = NULL;
        }
    }
    
    void SelectionSort(int *arr, int len, int *max, int *min, double *avg)
    {
        if (arr == NULL || max == NULL || min == NULL || avg == NULL)
        {
            printf("传入参数不可以为空!
    ");
            return;
        }
        int i = 0, j = 0, k = 0, temp = 0;
        for (; i < len; i++)
        {
            k = i;
            for (j = i + 1; j < len; j++)
            {
                if (arr[k] > arr[j])
                {
                    k = j;
                }
            }
            if (k != i)
            {
                temp = arr[i];
                arr[i] = arr[k];
                arr[k] = temp;
            }
        }
        *max = arr[len - 1];
        *min = arr[0];
        temp = 0;
        printf("排序之后的
    ");
        for (i = 0; i < len; i++)
        {
            printf("%d
    ", arr[i]);
            temp += arr[i];
        }
        printf("排序在此结束
    ");
        *avg = temp / len;
    }
    
    int main(int arg, char * args[])
    {
        if (arg < 2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        int arr[50] = { 0 };
        int len = 0;
        int maxnum = 0;
        int minnum = 0;
        double avgnum = 0;
        readfile(args[1], arr, &len);
        SelectionSort(arr, len, &maxnum, &minnum, &avgnum);
        printf("最大值:%d
    ", maxnum);
        printf("最小值:%d
    ", minnum);
        printf("平均值:%lf
    ", avgnum);
        return 0;
    }
  • 相关阅读:
    使用JS对中文字符串进行utf8的Base64编码
    subprocess理解
    25组新鲜出炉的有用图标集
    jQuery UI 1.8.9 发布
    正则匹配拼音
    jQuery Mobile 教程 (1)
    10款精选的用于构建良好易用性网站的jQuery插件
    Html 5 video/audio 格式转换 ogg
    10个有用的jquery 图片插件
    asp.net MVC 权限设计(续)
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5801734.html
Copyright © 2011-2022 走看看