-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers.c
88 lines (82 loc) · 2.6 KB
/
numbers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "numbers.h"
int main (int args, char *argv[])
{
int num;
switch (args)
{
case 1: /* No input and output files received */
printf("enter numbers between 0-99: ");
while (scanf("%d",&num) != EOF)
convert_numbers(num, outNotExist);
break;
case 2: /* Only input file received */
if (!(input = fopen(*++argv,"r")))
{
fprintf(stderr,"cannot open %s file\n",*argv );
exit(0);
}
while (fscanf(input,"%d",&num) != EOF)
convert_numbers(num, outNotExist);
fclose (input);
break;
case 3: /* Input and output file received */
if (!(input = fopen(*++argv,"r")))
{
fprintf(stderr,"cannot open %s file\n",*argv );
exit(0);
}
if (!(output = fopen(*++argv,"w")))
{
fprintf(stderr,"cannot create %s file\n",*argv );
exit(0);
}
while (fscanf(input,"%d",&num) != EOF)
convert_numbers(num, outExist);
fclose (input);
fclose (output);
break;
default: /* More than 2 arguments were accepted */
fprintf(stderr,"More than 2 arguments were accepted\n");
exit(0);
}
return 0;
}
/*
This method obtains an integer between 0 and 99 and converts each number into English words and prints them into output
@param num - integer between 0 and 99
@param out - out =1 if output file received, out =0 no if output file received
*/
void convert_numbers(int num, int out)
{
char *str1[] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char *str2[] = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
char *str3[] = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteeen", "seventeen", "eighteen", "nineteen"};
if ((num >= 0) && (num < 10)) /* between 0-9 */
{
if (out) /* have output file */
fprintf(output,"%s\n",*(str1 + num));
else /* dont have output file */
printf("%s\n",*(str1 + num));
}
else if ((num > 10) && (num < 20 )) /* between 11-19 */
{
if (out) /* have output file */
fprintf(output,"%s\n",*(str3 + (num % 10)-1));
else /* dont have output file */
printf("%s\n",*(str3 + (num % 10)-1));
}
else if ((num % 10 == 0 )) /* if the number its dozens */
{
if (out) /* have output file */
fprintf(output,"%s\n",*(str2 + (num / 10)-1));
else /* dont have output file */
printf("%s\n",*(str2 + (num / 10)-1));
}
else
{
if (out) /* have output file */
fprintf(output,"%s %s\n", *(str2 + (num/10) -1), *(str1 + (num % 10)));
else /* dont have output file */
printf("%s %s\n", *(str2 + (num/10) -1), *(str1 + (num % 10)));
}
}