OAIP/lab23/main.c

173 lines
3.4 KiB
C
Raw Normal View History

2024-11-22 14:42:10 +04:00
#include <stdio.h>
2024-11-22 15:59:17 +04:00
#include <ctype.h>
#include <string.h>
#include <locale.h>
2024-11-22 14:42:10 +04:00
#define BUF_LEN 128
void task1() {
2024-11-22 15:59:17 +04:00
puts("===== task1 =====");
char infilepath[] = "files/text1.txt";
FILE* fin = fopen(infilepath, "r");
if (fin == NULL) {
printf("Cannot open file %s\n", infilepath);
return;
}
char outfilepath[] = "files/out1.txt";
FILE* fout = fopen(outfilepath, "w");
2024-11-22 14:42:10 +04:00
if (fin == NULL) {
2024-11-22 15:59:17 +04:00
printf("Cannot open file %s\n", outfilepath);
2024-11-22 14:42:10 +04:00
return;
}
char buf[BUF_LEN];
while (fgets(buf, BUF_LEN, fin) != NULL) {
2024-11-22 15:59:17 +04:00
for (int i = 0; buf[i] != '\0'; i++) {
if (buf[i] == '\t') {
buf[i] = '%';
}
}
fprintf(fout, "%s", buf);
printf(">>%s<<", buf);
}
fclose(fin);
fclose(fout);
}
2024-11-22 14:42:10 +04:00
2024-11-22 15:59:17 +04:00
void task2() {
puts("===== task2 =====");
char infilepath[] = "files/text2.txt";
FILE* fin = fopen(infilepath, "r");
if (fin == NULL) {
printf("Cannot open file %s\n", infilepath);
return;
}
char outfilepath[] = "files/out2.txt";
FILE* fout = fopen(outfilepath, "w");
if (fin == NULL) {
printf("Cannot open file %s\n", outfilepath);
return;
2024-11-22 14:42:10 +04:00
}
2024-11-22 15:59:17 +04:00
char buf[BUF_LEN];
while (fgets(buf, BUF_LEN, fin) != NULL) {
for (int i = 0; buf[i] != '\0'; i++) {
buf[i] = toupper(buf[i]);
}
fprintf(fout, "%s", buf);
printf(">>%s<<", buf);
}
fclose(fin);
fclose(fout);
2024-11-22 14:42:10 +04:00
}
2024-11-22 15:59:17 +04:00
void task3() {
puts("===== task3 =====");
char infilepath[] = "files/text3.txt";
FILE* fin = fopen(infilepath, "r");
if (fin == NULL) {
printf("Cannot open file %s\n", infilepath);
return;
}
2024-11-22 14:42:10 +04:00
2024-11-22 15:59:17 +04:00
char outfilepath[] = "files/out3.txt";
FILE* fout = fopen(outfilepath, "w");
if (fin == NULL) {
printf("Cannot open file %s\n", outfilepath);
return;
}
char buf[BUF_LEN];
while (fgets(buf, BUF_LEN, fin) != NULL) {
for (int i = 0; buf[i] != '\0'; i++) {
if (isalpha(buf[i])) {
fputc('#', fout);
break;
}
}
fprintf(fout, "%s", buf);
printf(">>%s<<", buf);
}
fclose(fin);
fclose(fout);
}
void task4() {
puts("===== task4 =====");
char infilepath[] = "files/text4.txt";
FILE* fin = fopen(infilepath, "r");
if (fin == NULL) {
printf("Cannot open file %s\n", infilepath);
return;
}
char outfilepath[] = "files/out4.txt";
FILE* fout = fopen(outfilepath, "w");
if (fin == NULL) {
printf("Cannot open file %s\n", outfilepath);
return;
}
char buf[BUF_LEN];
while (fgets(buf, BUF_LEN, fin) != NULL) {
for (int i = 0; buf[i] != '\0'; i++) {
if (ispunct(buf[i])) buf[i] = '_';
}
fprintf(fout, "%s", buf);
printf(">>%s<<", buf);
}
fclose(fin);
fclose(fout);
}
void task5() {
puts("===== task5 =====");
char infilepath[] = "files/text5.txt";
FILE* fin = fopen(infilepath, "r");
if (fin == NULL) {
printf("Cannot open file %s\n", infilepath);
return;
}
char outfilepath[] = "files/out5.txt";
FILE* fout = fopen(outfilepath, "w");
if (fin == NULL) {
printf("Cannot open file %s\n", outfilepath);
return;
}
char buf[BUF_LEN];
while (fgets(buf, BUF_LEN, fin) != NULL) {
int count = 0;
for (int i = 0; buf[i] != '\0'; i++) {
if (buf[i] == ';') {
count += 1;
}
}
fprintf(fout, "%d:%s", count, buf);
printf(">>%d:%s<<", count, buf);
}
fclose(fin);
fclose(fout);
}
int main() {
2024-11-22 14:42:10 +04:00
2024-11-22 15:59:17 +04:00
setlocale(LC_CTYPE, "");
2024-11-22 14:42:10 +04:00
2024-11-22 15:59:17 +04:00
task1();
task2();
task3();
task4();
task5();
2024-11-22 14:42:10 +04:00
return 0;
}