在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 }

 

转载于:https://www.cnblogs.com/yawenunion/p/8850615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值