在Linux終端編譯運行C程序


以下演示代碼純屬自己找事兒干弄着玩滴😀
作用是把一串數字翻譯成英文的。
特點有:
(1) 既允許以直接給main函數傳入一波參數的形式輸入若干數字,也允許在程序開始執行后,陸陸續續地輸入若干數字。
(2) 不限制輸入數字的個數。
(3) 結束輸入的方式可以是換行后輸入Ctrl + D來結束輸入或者置以任意非數字符號來結束數字序列


假設該源代碼文件名為number_dictionary.c
使用gcc編譯:
gcc -o nd ./number_dictionary.c
運行:
./nd -1 -2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 end

./nd
-1 -2 0 1 2 3 4 5
6 7 8 9 10 11 12 13 end
未支持負數時的運行結果:

增加對負數的支持以后的其他輸入測試:

源代碼:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <ctype.h>
 4 #include <math.h>
 5 
 6 #define INT_ARRAY_GROW_STEP 4
 7 
 8 typedef struct{
 9     int *numbers; // 容量能自增長的數組
10     int count; // 數組中當前實際存放的元素個數
11     int max_count; // 數組中當前最多能存放的元素個數,即當前的數組容量
12 } INT_ARRAY;
13 
14 /*
15 ** 判斷一個字符串是否是以(數字)或(負號 數字)開頭
16 */
17 int is_number_string(char *string){
18     return isdigit(string[0])?1:
19         (string[0] == '-' && isdigit(string[1]))?2:0;
20     // 增加了對負數的支持,但這里不支持在負號和絕對值之間有若干空格
21 }
22 
23 /*
24 ** 擴展數組容量
25 */
26 void int_array_grow(INT_ARRAY *int_array, int cnt_add){
27     int_array->numbers = (int *)realloc(int_array->numbers, (int_array->count + cnt_add) * sizeof(int));
28     int_array->max_count += cnt_add;
29 }
30 
31 /*
32 ** 把數字翻譯成其絕對值對應的英文單詞
33 */
34 char *translate_interger(char **numbers_dictionary, int dictionary_capacity, int number){
35     int index = (abs(number) < dictionary_capacity)?abs(number):(dictionary_capacity - 1);
36     return numbers_dictionary[index];
37 }
38 
39 /*
40 ** 向數組中追加新元素
41 */
42 void append_number(INT_ARRAY *int_array, int number){
43     if(int_array->count == int_array->max_count){
44         int_array_grow(int_array, INT_ARRAY_GROW_STEP);
45     }
46     (int_array->numbers)[(int_array->count)++] = number;
47 }
48 
49 /*
50 ** 把數組中的全部數字依次翻譯成其絕對值對應的英文單詞
51 */
52 void translate_integerArray(char **dictionary, int dictionary_capacity, INT_ARRAY *int_array){
53     int i = 0;
54     while(i < int_array->count){
55         int number = (int_array->numbers)[i];
56         printf("%4d : %s%s\n", number, (number < 0)?"minus ":"", translate_interger(dictionary, dictionary_capacity, number)); // 增加了對負數的支持 57         ++i;
58     }
59 }
60 
61 int main(int argc, char **argv){
62     // 這就是個小字典哈,你可以在"eleven", "OUT-OF-RANGE"之間繼續添加詞條
63     char *numbers_dictionary[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "OUT-OF-RANGE"};
64     // 計算小字典已有詞條總數
65     int dictionary_capacity = sizeof(numbers_dictionary)/sizeof(char *);
66 
67     // 創建並初始化自定義可擴容數組類型的變量
68     int *numbers = (int *)malloc(INT_ARRAY_GROW_STEP*sizeof(int));
69     INT_ARRAY int_array = {numbers, 0, INT_ARRAY_GROW_STEP};
70 
71     /*
72     ** 把用戶輸入的數字轉儲到自定義可擴容數組類型的變量中
73     */
74     if(argc == 1){ // 處理在開始執行程序后陸陸續續地輸入數字的情形
75         printf("Input a series of intergers between zero and ten:\n");
76         int n;
77         while(scanf("%d", &n) == 1){
78             append_number(&int_array, n);
79         }
80     }else{ // 處理以給main函數傳參的形式輸入若干數字的情形
81         int i;
82         for(i = 1; i < argc && is_number_string(argv[i]); ++i){
83             append_number(&int_array, atoi(argv[i]));
84         }
85     }
86 
87     // 把數組中的全部數字依次翻譯成其絕對值對應的英文單詞
88     translate_integerArray(numbers_dictionary, dictionary_capacity, &int_array);
89 
90     return 0;
91 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM