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



该程序应该将从命令行输入的数字排序为-a表示小到大排序,或-d表示大到小排序。我之前编译了这个程序,运行了它,输出很好。我后来在我的笔记本电脑上测试了它,然后排序都无法正常工作。

我输入了 ./sort -a 5 14 10 18 20 2 100 6 7 1 结果:-1950355064 2 5 6 10 14 18 20 100

我输入:./sort -d 5 14 10 18 20 2 100 6 7 1

结果: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: %sn", 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);
}

主要有两个问题。

  1. stringToInt[i] = atoi(argv[i]);:字符串的索引应该从0开始.
  2. 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: %sn", 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);
}

我相信我设法通过更改来使其工作

large_to_small(stringToInt, N); ---> large_to_small(stringToInt, argc);
small_to_large(stringToInt, N); ---> small_to_large(stringToInt, argc);

最新更新