Skip to content

Commit d8478cc

Browse files
Saveliy Grigoryevsava-cska
authored andcommitted
Support input/output with files
1 parent 78aed3f commit d8478cc

File tree

33 files changed

+1139
-223
lines changed

33 files changed

+1139
-223
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <stdio.h>
2+
#include <fcntl.h>
3+
#include "file.h"
4+
5+
int file_fgetc(FILE *fA, FILE *fB, FILE *fC) {
6+
unsigned char x = fgetc(fA);
7+
if (x >= '0' && x <= '9') {
8+
unsigned char a = fgetc(fB);
9+
if (a >= 'a' && a <= 'z') {
10+
return 1;
11+
} else {
12+
return 2;
13+
}
14+
} else {
15+
unsigned char a = fgetc(fC);
16+
unsigned char b = fgetc(fA);
17+
if (a >= 'a' && a <= 'z') {
18+
if (b >= '0' && b <= '9') {
19+
return 3;
20+
} else {
21+
return 4;
22+
}
23+
} else {
24+
return 5;
25+
}
26+
}
27+
}
28+
29+
int file_fgets(FILE *fA) {
30+
char a[8];
31+
fgets(a, 6, fA);
32+
if (a[0] == 'u' && a[1] == 't' && a[2] == 'b' && a[3] == 'o' && a[4] == 't') {
33+
return 1;
34+
}
35+
return 0;
36+
}
37+
38+
char file_fputc(int x, int y, FILE *fA) {
39+
if (x < y) {
40+
fputc('<', fA);
41+
return '<';
42+
} else if (x > y) {
43+
fputc('>', fA);
44+
return '>';
45+
} else {
46+
fputc('=', fA);
47+
return '=';
48+
}
49+
}
50+
51+
char file_fputs(char c, FILE *fA) {
52+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
53+
char a[] = "Vowel";
54+
fputs("Vowel", fA);
55+
return 'V';
56+
} else {
57+
char a[] = "Consonant";
58+
fputs("Consonant", fA);
59+
return 'C';
60+
}
61+
}
62+
63+
int sum_two_from_file(FILE *f) {
64+
int x, y;
65+
fread(&x, sizeof(int), 1, f);
66+
fread(&y, sizeof(int), 1, f);
67+
if (x + y > 0) {
68+
return 1;
69+
} else if (x + y < 0) {
70+
return -1;
71+
} else {
72+
return 0;
73+
}
74+
}
75+
76+
int file_fread(FILE *fA, FILE *fB) {
77+
int arrA[4];
78+
int arrB[4];
79+
fread(arrA, sizeof(int), 4, fA);
80+
fread(arrB, sizeof(int), 4, fB);
81+
82+
int sumA = 0, sumB = 0;
83+
for (int i = 0; i < 4; i++) {
84+
sumA += arrA[i];
85+
sumB += arrB[i];
86+
}
87+
if (sumA == sumB) {
88+
return 0;
89+
} else if (sumA > sumB) {
90+
return 1;
91+
} else {
92+
return -1;
93+
}
94+
}
95+
96+
char file_fwrite(FILE *fA, int x) {
97+
if (x > 0) {
98+
char a[] = "Positive";
99+
fwrite(a, sizeof(char), 8, fA);
100+
return 'P';
101+
} else if (x < 0) {
102+
char a[] = "Negative";
103+
fwrite(a, sizeof(char), 8, fA);
104+
return 'N';
105+
} else {
106+
char a[] = "Zero";
107+
fwrite(a, sizeof(char), 4, fA);
108+
return 'Z';
109+
}
110+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef FILE_H
2+
#define FILE_H
3+
4+
int file_fgetc(FILE *fA, FILE *fB, FILE *fC);
5+
6+
int file_fgets(FILE *fA);
7+
8+
char file_fputc(int x, int y, FILE *fA);
9+
10+
char file_fputs(char c, FILE *fA);
11+
12+
int sum_two_from_file(FILE *f);
13+
14+
int file_fread(FILE *fA, FILE *fB);
15+
16+
char file_fwrite(FILE *fA, int x);
17+
18+
#endif
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#include <stdio.h>
2+
#include "input_output.h"
3+
4+
int simple_getc() {
5+
unsigned char a = getc(stdin);
6+
if (a == '0') {
7+
return 0;
8+
} else if (a == '1') {
9+
return 1;
10+
} else if (a == '2') {
11+
return 2;
12+
} else if (a == '3') {
13+
return 3;
14+
} else if (a == '4') {
15+
return 4;
16+
} else if (a == '5') {
17+
return 5;
18+
} else if (a == '6') {
19+
return 6;
20+
} else if (a == '7') {
21+
return 7;
22+
} else if (a == '8') {
23+
return 8;
24+
} else if (a == '9') {
25+
return 9;
26+
}
27+
return -1;
28+
}
29+
30+
int simple_fgetc(int x) {
31+
if (x >= 0 && x <= 9) {
32+
unsigned char a = fgetc(stdin);
33+
if (a >= 'a' && a <= 'z') {
34+
return 1;
35+
} else {
36+
return 2;
37+
}
38+
} else {
39+
unsigned char a = fgetc(stdin);
40+
unsigned char b = fgetc(stdin);
41+
if (a >= 'a' && a <= 'z') {
42+
if (b >= '0' && b <= '9') {
43+
return 3;
44+
} else {
45+
return 4;
46+
}
47+
} else {
48+
return 5;
49+
}
50+
}
51+
}
52+
53+
int simple_fread() {
54+
int arrA[4];
55+
int arrB[4];
56+
fread(arrA, sizeof(int), 4, stdin);
57+
fread(arrB, sizeof(int), 4, stdin);
58+
59+
int sumA = 0, sumB = 0;
60+
for (int i = 0; i < 4; i++) {
61+
sumA += arrA[i];
62+
sumB += arrB[i];
63+
}
64+
if (sumA == sumB) {
65+
return 0;
66+
} else if (sumA > sumB) {
67+
return 1;
68+
} else {
69+
return -1;
70+
}
71+
}
72+
73+
int simple_fgets() {
74+
char a[8];
75+
fgets(a, 6, stdin);
76+
if (a[0] == 'u' && a[1] == 't' && a[2] == 'b' && a[3] == 'o' && a[4] == 't') {
77+
return 1;
78+
}
79+
return 0;
80+
}
81+
82+
int simple_getchar() {
83+
unsigned char a = getchar();
84+
unsigned char b = getchar();
85+
86+
if (a + b < 0) {
87+
return -1;
88+
}
89+
90+
if (a + b < 100) {
91+
return 0;
92+
} else if (a + b < 200) {
93+
return 1;
94+
} else {
95+
return 2;
96+
}
97+
}
98+
99+
int simple_gets() {
100+
char a[8];
101+
gets(a);
102+
if (a[0] == 'u' && a[1] == 't' && a[2] == 'b' && a[3] == 'o' && a[4] == 't') {
103+
return 1;
104+
}
105+
return 0;
106+
}
107+
108+
char simple_putc(int x, int y) {
109+
if (x + y > 0) {
110+
putc('1', stdout);
111+
return '1';
112+
} else if (x + y < 0) {
113+
putc('2', stdout);
114+
return '2';
115+
} else {
116+
putc('0', stdout);
117+
return '0';
118+
}
119+
}
120+
121+
char simple_fputc(int x, int y) {
122+
if (x < y) {
123+
fputc('<', stdout);
124+
return '<';
125+
} else if (x > y) {
126+
fputc('>', stdout);
127+
return '>';
128+
} else {
129+
fputc('=', stdout);
130+
return '=';
131+
}
132+
}
133+
134+
char simple_fwrite(int x) {
135+
if (x > 0) {
136+
char a[] = "Positive";
137+
fwrite(a, sizeof(char), 8, stdout);
138+
return 'P';
139+
} else if (x < 0) {
140+
char a[] = "Negative";
141+
fwrite(a, sizeof(char), 8, stdout);
142+
return 'N';
143+
} else {
144+
char a[] = "Zero";
145+
fwrite(a, sizeof(char), 4, stdout);
146+
return 'Z';
147+
}
148+
}
149+
150+
char simple_fputs(char c) {
151+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
152+
char a[] = "Vowel";
153+
fputs("Vowel", stdout);
154+
return 'V';
155+
} else {
156+
char a[] = "Consonant";
157+
fputs("Consonant", stdout);
158+
return 'C';
159+
}
160+
}
161+
162+
char simple_putchar(int x, int y) {
163+
if (3 * x > 2 * y) {
164+
putchar('>');
165+
return '>';
166+
} else if (3 * x < 2 * y) {
167+
putchar('<');
168+
return '<';
169+
} else {
170+
putchar('=');
171+
return '=';
172+
}
173+
}
174+
175+
char simple_puts(char c) {
176+
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
177+
char a[] = "Vowel";
178+
puts(a);
179+
return 'V';
180+
} else {
181+
char a[] = "Consonant";
182+
puts(a);
183+
return 'C';
184+
}
185+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef INPUT_OUTPUT_H
2+
#define INPUT_OUTPUT_H
3+
4+
int simple_getc();
5+
6+
int simple_fgetc(int x);
7+
8+
int simple_fread();
9+
10+
int simple_fgets();
11+
12+
int simple_getchar();
13+
14+
int simple_gets();
15+
16+
char simple_putc(int x, int y);
17+
18+
char simple_fputc(int x, int y);
19+
20+
char simple_fwrite(int x);
21+
22+
char simple_fputs(char c);
23+
24+
char simple_putchar(int x, int y);
25+
26+
char simple_puts(char c);
27+
28+
#endif

0 commit comments

Comments
 (0)