forked from Alialmanea/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roman_Numeral_Converter_With_İnput&Output_file.txt.c
150 lines (126 loc) · 3.8 KB
/
Roman_Numeral_Converter_With_İnput&Output_file.txt.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 100
typedef struct char10 { char c[10]; } String;
char* Roman_Numeral_Converter(int num){
String *Roman;
Roman = malloc (sizeof (String));
strcpy(Roman->c, "");
while(num != 0)
{
if (num >= 1000) // 1000 - m
{
strncat(Roman->c,"M",1);
num -= 1000;
}
else if (num >= 900) // 900 - cm
{
strncat(Roman->c,"CM", 2);
num -= 900;
}
else if (num >= 500) // 500 - d
{
strncat(Roman->c,"D", 1);
num -= 500;
}
else if (num >= 400) // 400 - cd
{
strncat(Roman->c,"CD", 2);
num -= 400;
}
else if (num >= 100) // 100 - c
{
strncat(Roman->c,"C", 2);
num -= 100;
}
else if (num >= 90) // 90 - xc
{
strncat(Roman->c,"XC", 2);
num -= 90;
}
else if (num >= 50) // 50 - l
{
strncat(Roman->c,"I", 1);
num -= 50;
}
else if (num >= 40) // 40 - xl
{
strncat(Roman->c,"XI", 2);
num -= 40;
}
else if (num >= 10) // 10 - x
{
strncat(Roman->c,"X", 1);
num -= 10;
}
else if (num >= 9) // 9 - ix
{
strncat(Roman->c,"IX", 2);
num -= 9;
}
else if (num >= 5) // 5 - v
{
strncat(Roman->c,"V", 1);
num -= 5;
}
else if (num >= 4) // 4 - iv
{
strncat(Roman->c,"IV",2);
num -= 4;
}
else if (num >= 1) // 1 - i
{
strncat(Roman->c,"I", 1);
num -= 1;
}
}
return Roman;
}
int main(void)
{
int i=0,count=0;
FILE *InputFile;
char pathofinput[Max],pathofoutput[Max];
printf("============Roman Numeral Converter==========\n");
printf("Enterd a valid number should be from 1 to 3,999\n\n");
printf("Enter The path of input file");
printf("\n For Example /Users/dabbaghie/Desktop/input.txt");
printf("\nHere ==> ");
scanf("%s",pathofinput);
printf("============Roman Numeral Converter==========\n");
printf("Enterd a valid number should be from 1 to 3,999\n\n");
printf("Enter The path of output file");
printf("\n For Example /Users/dabbaghie/Desktop/output.txt");
printf("\nHere ==> ");
scanf("%s",pathofoutput);
strncat(pathofoutput,"/output.txt",11);
InputFile =fopen(pathofinput,"r");
char *Roman;
int numberArray[Max];
if (InputFile == NULL){ //Condition statement for the path and file
printf("Error Reading File\n");
exit (0);//Exit form the App
}
for (int i = 0; i < Max; i++){ // get The numbers by using ((,)) which Separated numbers
fscanf(InputFile, "%d,", &numberArray[i] );
}
fclose(InputFile);
while (numberArray[i]!=0){ //Count The Numbers
count++;
i++;
}
for (int i = 0; i < count; i++){ //
Roman=Roman_Numeral_Converter(numberArray[i]);
printf("%d.%d ==>%s\n",i+1,numberArray[i],Roman);
}
InputFile =fopen(pathofoutput,"w"); //Writing the result on file.txt
for (int i = 0; i < count; i++){
fprintf(InputFile,"%s,",
Roman_Numeral_Converter(numberArray[i])); //For Example :XV,IV,II
}
fprintf(InputFile,"\n\n\n The Count of Numbers is: %d.",count);
fprintf(InputFile,"\n\n\n Ali.Almanea</>");
fclose(InputFile);
return 0;
}