C程序,字符串到int数组命令行排序

C Program, string to int array command line sort

该程序应该对从命令行输入的数字进行排序,-a 用于从小到大排序,或 -d 用于从大到小排序。我早些时候编译了这个程序,运行 它并且输出很好。稍后我在我的笔记本电脑上对其进行了测试,然后排序都无法正常工作。

I entered ./sort -a 5 14 10 18 20 2 100 6 7 1
The Result: -1950355064 2 5 6 10 14 18 20 100

I entered: ./sort -d 5 14 10 18 20 2 100 6 7 1
The Result: 761262572 32767 18 14 10 6 20 5 100 2

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

#define N 10

// function declarations
void small_to_large(int a[], int n);
void large_to_small(int a[], int n);

int main(int argc, char *argv[])
{
    int i;
    char letter_d[3] = "-d";            // find "-d" in function
    char letter_a[3] = "-a";            // find "-a" in function
    int arg_d;              
    int arg_a;              
    char *find_letter = argv[1];        
    int stringToInt[N+1];               

    for( i = 0; i < argc; i++){     
        printf("%s ", argv[i]);
    }
    printf("\n");

    arg_d = strcmp(find_letter, letter_d);  
    arg_a = strcmp(find_letter, letter_a);  

    if (arg_d == 0) {               
        printf("descend!\n");
        for(i = 2; i<argc; i++) {
            stringToInt[i] = atoi(argv[i]); 
        }
        large_to_small(stringToInt, N);     
        for(i = 0; i < N; i++) {        
            printf(" %d", stringToInt[i]); 
        }
    }
    else if (arg_a == 0) {          
        printf("ascend!\n");
        for(i = 2; i < argc; i++) {
            stringToInt[i] = atoi(argv[i]); 
        }
        small_to_large(stringToInt, N);     
        for(i = 0; i < N; i++) {        
            printf(" %d", stringToInt[i]);
        }
    }
        else {
            printf("Invalid command: %s\n", find_letter);   
        }
    printf("\n");
    return 0;
}

// small_to_large function
void small_to_large(int a[], int n) 
{   
    int i, largest = 0, temp;

    if (n == 1)    
        return;

    for (i = 1; i < n; i++)     
        if (a[i] > a[largest])       
            largest = i;

    if (largest < n - 1) {     
        temp = a[n-1];     
        a[n-1] = a[largest];     
        a[largest] = temp;   
    }
    small_to_large(a, n - 1); 
}

// large_to_small function
void large_to_small(int a[], int n)
{
    int i, largest = 0, temp;

    if(n == 1)
        return;

    for (i = n; i >= 2; i--) 
        if(a[i] < a[largest])
            largest = i;
    if (largest < n - 1) {
        temp = a[n-1];
        a[n-1] = a[largest];
        a[largest] = temp;
    }
    large_to_small(a, n-1);
}

我相信我通过更改

设法让它工作
large_to_small(stringToInt, N); ---> large_to_small(stringToInt, argc);
small_to_large(stringToInt, N); ---> small_to_large(stringToInt, argc);

主要有两个问题

  1. stringToInt[i] = atoi(argv[i]); : stringToInt 的索引应该 从 0.
  2. 开始
  3. large_to_small 是错误的。排序算法可能相同。所以你可以使用相同的代码。

修复示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <stdbool.h>

// function declarations
void small_to_large(int a[], int n);
void large_to_small(int a[], int n);

#define OPT_D "-d"
#define OPT_A "-a"

int main(int argc, char *argv[]) {
    int i;
    for(i = 0; i < argc; i++){   
        printf("%s ", argv[i]);
    }
    printf("\n");

    int n = argc - 2;//-2 : program_name and option
    if(n <= 0){
        printf("Usage : %s option(-a or -d) numbers...\n", *argv);
        return EXIT_FAILURE;
    }

    int stringToInt[n];
    for(i = 0; i < n; ++i)
        stringToInt[i] = atoi(argv[i+2]);

    char *opt = argv[1];
    bool opt_d, opt_a;
    opt_d = !strcmp(opt, OPT_D);
    opt_a = !strcmp(opt, OPT_A);

    if(opt_d){
        printf("descend!\n");
        large_to_small(stringToInt, n);
    } else if(opt_a) {
        printf("ascend!\n");
        small_to_large(stringToInt, n);
    } else {
        printf("Invalid option: %s\n", opt);
        return EXIT_FAILURE;
    }

    for(i = 0; i < n; i++) {
        printf("%d ", stringToInt[i]);
    }
    printf("\n");
    return 0;
}

void selection_sort(int a[], int n, bool test(int a, int b)){
    int i, select = 0, temp;

    if (n == 1) 
        return;

    for (i = 1; i < n; i++)
        if(test(a[i], a[select]))
            select = i;

    if (select !=  n-1) {
        temp = a[n-1];   
        a[n-1] = a[select];
        a[select] = temp;
    }
    selection_sort(a, n - 1, test);
}

static inline bool greater(int a, int b){
    return a > b;
}
static inline bool smaller(int a, int b){
    return a < b;
}

// small_to_large function
void small_to_large(int a[], int n) {
    selection_sort(a, n, greater);
}

// large_to_small function
void large_to_small(int a[], int n){
    selection_sort(a, n, smaller);
}