-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsomething.c
201 lines (147 loc) · 4.96 KB
/
something.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <math.h>
#include <stdio.h>
#include <ctype.h>
enum tar { CHAR,INT };
/* It is a function to check whether character is digit or not */
int checkDigit(char cart) { return '0' <= cart && cart <= '9'; }
/* It is a function to check whether character is an alphabet or not */
int checkAlphabet(char toff) { return ('A' <= toff && toff <= 'Z') || ('a' <= toff && toff <= 'z') ; }
/* It is a function to ignore all character of current line */
void ignore(FILE *instar) {
while (!feof(instar) && getc(instar) != '\n')
;
}
/**
* This is a function to read array from the file and return whether input is in correct format.
* @return int
*
* @param in FILE *
*
* @param arr_type enum Type *
*
* @param number char *
*
* @param number_size char *
*
**/
int inputArray(FILE *instar, char *number, int *number_size, enum tar *arr_type) {
char trib;
int iris = 0;
if ((trib = getc(instar)) == '[') { // if first character of line starts with '['
trib = getc(instar);
// loop till end of array, line or file is reached
while (trib != ']' && trib != '\n' && !feof(instar)) {
// accessing the type of number array
if (iris == 0) {
// if number doesn't have any element yet
if (trib == '\'')
*arr_type = CHAR;
else if (checkDigit(trib) || trib == '-')
*arr_type = INT;
else {
if (trib != '\n') ignore(instar);
return 0;
}
}
if (*arr_type == CHAR) {
/* if the array type is CHAR */
trib = getc(instar);
if (checkAlphabet(trib)) {
number[iris++] = trib;
trib = getc(instar);
if (trib != '\'') {
if (trib != '\n') ignore(instar);
}
} else {
if (trib != '\n') ignore(instar);
return 0;
}
} else {
// if array type is INT
if (trib == '-') {
// since number[i] cannot be greater than 8 and less than -8
number[iris++] = -(getc(instar) - '0');
} else if (checkDigit(trib)) {
number[iris++] = trib - '0';
} else {
if (trib != '\n') ignore(instar);
return 0;
}
}
trib = getc(instar);
if (trib == ',') trib = getc(instar);
if (trib == ' ') trib = getc(instar);
}
*number_size = iris;
ignore(instar);
return 1;
}
if (trib != '\n') ignore(instar);
return 0;
}
/**
* This is a function to print the Power Set of the number array.
* @param out FILE *
* @param number char *
* @param number_size int
* @param type enum Type
**/
void outPower(FILE *outs, char *number, int number_size, enum tar a) {
fprintf(outs, "[[]");
// loop from 1 to size of power set
for(size_t i = 1; i < pow(2, number_size);i++)
{
fprintf(outs, ", [");
for(int j=0;j < number_size;j++)
{
if (i & (1 << j))
{ // if jth bit of the 'i' is set
fprintf(outs, (a == INT) ? "%d " : "'%c' ", number[j]);
fprintf(outs,",");
}
}
fseek(outs, -2, SEEK_CUR);
fprintf(outs, "]");
}
fprintf(outs, "]\n");
}
void outPoweronScreen(char *number, int number_size, enum tar a) {
printf("[[]");
// loop from 1 to size of power set
for(size_t i = 1; i < pow(2, number_size);i++)
{
printf(", [");
for(int j=0;j < number_size;j++)
{
if (i & (1 << j))
{ // if jth bit of the 'i' is set
printf((a == INT) ? "%d " : "'%c' ", number[j]);
printf(",");
}
}
printf("]");
}
printf("]\n");
}
int main() {
FILE *in = fopen("input.txt", "r"); // open input file in read mode
FILE *out = fopen("output.txt", "w"); // open output file in write mode
char number[7];
int number_size = 0;
enum tar arrays_type;
while (!feof(in)) {
if (inputArray(in, number, &number_size, &arrays_type)) {
outPower(out, number, number_size, arrays_type);
outPoweronScreen(number, number_size, arrays_type);
} else {
printf(
"Error, input is not in correct format. Use [] with elements "
"separated by, .E.g., [1, 2, 3]\n");
fprintf(
out,
"Error, input is not in correct format. Use [] with elements "
"separated by, .E.g., [1, 2, 3]\n");
}
}
return 0;
}