[英]Sort command-line arguments
C 語言新手,在完成這項工作任務時遇到了問題。 我使用 C89 編譯器。
編寫一個程序,對它的 10 個數字的命令行參數進行排序,假設這些數字是整數。 第一個命令行參數指示排序是按降序 (-d) 還是升序 (-a),如果用戶輸入無效選項,程序應顯示錯誤消息。
程序運行示例:
./sort –a 5 2 92 424 53 42 8 12 23 41
2 5 8 12 23 41 42 53 92 424
./sort –d 5 2 92 424 53 42 8 12 23 41
424 92 53 42 41 23 12 8 5 2
到目前為止我所擁有的是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//function prototype
void swap(int *arr, int i, int j);
//main function
int main(int argc, char *argv[])
{
//declare the required variables
int array[10];
int maxIndex = 0, minIndex =0;
int i = 0, j = 0, n = 2;
//check whether the number of arguments are less than 2 or not
if (argc < 2)
{
//print the error message
printf("Data is insuffient! Please insert proper input at command line. \n");
}
else
{
// read the integers from the command line
for (i = 0; i < 10; i++)
array[i] = atoi(argv[2 + i]);
}
printf("Selection sort on integer arrays \n\n");
//print the elements that are read from the command line
printf("Elements before sorting are: \n");
//print the sorted elements
for(i =0;i<10;i++)
printf("%d ", array[i]);
printf("\n");
//check whether the first argument is -a, if
//-a sort the elements in the array in asscending order
if (strcmp(argv[1], "-a") == 0)
{
//logic to sort the elements in asscending order using selection sort
for (i = 0; i < 10; ++i)
{
minIndex = i;
for (int j = i + 1; j < 10; ++j)
{
if (array[j] < array[minIndex])
minIndex = j;
}
swap(array, minIndex, i);
}
}
//check whether the first argument is -d, if
//-d sort the elements in the array in descending order
if (strcmp(argv[1], "-d") == 0)
{
//logic to sort the elements in descending order using selection sort
for (i = 0; i < 10; ++i)
{
maxIndex = i;
for (j = i + 1; j < 10; ++j)
{
if (array[j] > array[maxIndex])
maxIndex = j;
}
swap(array, maxIndex, i);
}
}
//print the elements
printf("\nElements after sorting are: \n");
//print the sorted elements
for(i =0;i<10;i++)
printf("%d ", array[i]);
printf("\n\n");
return 0;
}
//definition of swap function
void swap(int *arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
直接的問題是你如何閱讀你的論點。
if (argc < 2)
{
//print the error message
printf("Data is insuffient! Please insert proper input at command line. \n");
}
如果沒有給出參數,這將打印消息,但隨后愉快地繼續程序和未初始化的數組。 它需要退出。
if (argc < 2)
{
fprintf(stderr, "Please enter some numbers to be sorted.\n");
exit(1);
}
這也意味着其余的代碼不必包含在一個巨大的else
子句中。 這被稱為“提前退出”或“提前返回”,它使代碼更加簡單。
接下來是你如何閱讀參數。
// read the integers from the command line
for (i = 0; i < 10; i++)
array[i] = atoi(argv[2 + i]);
該循環假設有 10 個參數。 如果少,它會讀到胡言亂語。 如果有更多,它不會讀取它們。 相反,使用argc
。
/* Skip the name of the program and the first option */
int argv_offset = 2;
int num_numbers = argc - argv_offset;
for( i = argv_offset; i < argc; i++ ) {
array[i-argv_offset] = atoi(argv[i]);
}
在這種情況下,我選擇將i
與argv
和argc
因為還有更多需要協調。 我將我的偏移量放入一個變量中以解釋為什么它在那里並避免改變一個而另一個。
假設有 10 個數字的這個問題是代碼其余部分的問題。 這就是我設置num_numbers
來跟蹤它的原因。 所有硬編碼的 10 都可以用它替換。
現在 10 不再是硬編碼的,您必須處理以下問題:如果參數比您分配的內存多怎么辦? 你可以拒絕他們太久。 或者您可以增加數組的大小。 我讓您閱讀有關 C 中的realloc
和動態內存分配的信息。
兩個風格筆記。 首先,雖然您可以在沒有大括號的情況下編寫循環和 ifs,但始終使用大括號。 為什么? 因為最終你會寫這個。
for( blah blah blah )
do this thing
and do this other thing
你會盯着它看幾個小時,想知道為什么它不起作用。
接下來, //
不是有效的 C89。 你必須使用/* ... */
。 大多數 C 編譯器都會允許它,但是如果您將-std=c89
添加到您的編譯器中,您將收到警告。
cc -std=c89 -Wall -g test.c -o test
test.c:5:1: warning: // comments are not allowed in this language [-Wcomment]
//function prototype
^
沒關系,C99 允許//
現在大多數編譯器都支持 C99 的重要部分。
最后,C 會很高興地讓你離開一個數組,直到它崩潰。 使用內存檢查器,例如valgrind 。 它會讓你看到那些導致奇怪行為的隱藏內存問題。 這就是我如何如此快速地發現您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.