Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 26 additions & 58 deletions Semester_1/Lab_5/Lab_5.c
Original file line number Diff line number Diff line change
@@ -1,76 +1,44 @@
// ������ ������. �������� ����� ���������� �� ����������
// ('.' - ���, ',' - ���, '?' - ���, ':' - ���)

// If cyrillic symbols are broken:
// ctrl + shift + P -> Change file rncoding -> Reopen with encoding -> Windows 1251

#define USE_RUS 0 // 1 - yes
// Ввести строку. Заменить знаки препинания на сокращения
// ('.' - тчк, ',' - зпт, '?' - впр, ':' - дтч)

#include <stdio.h>
#include <stdlib.h>
#if USE_RUS == 1
#include <locale.h>
#endif
#include "reducts.c"

#define length(x) (sizeof(x) / sizeof((x)[0]))
int len = 0;

char *readstr()
{
int len = 0;
char *readstr() {
len = 0;
int capacity = 1; // ������� ������������ ������
char *s = (char *)malloc(sizeof(char)); // ������������ ������ ������
int capacity = 1;
char *s = (char *)malloc(sizeof(char));
char c = getchar();
while (c != '\n')
{
while (c != '\n') {
s[(len)++] = c;
if (len >= capacity)
{ // ���� �������� ������ ������ ������� ����������, �� �������� ��� ������
capacity *= 2; // ����������� ������� � ��� ����
s = (char *)realloc(s, capacity * sizeof(char)); // ������ ����� ������ � ����������� ��������
if (len >= capacity) {
capacity *= 2;
s = (char *)realloc(s, capacity * sizeof(char));
}
c = getchar();
}
s[len] = '\0';
return s; // ���������� ���������
return s;
}

void print_char(char ch)
{
switch (ch)
{
case '.':
printf(USE_RUS == 1 ? "���" : "DOT");
break;
case ',':
printf(USE_RUS == 1 ? "���" : "COMMA");
break;
case '!':
printf(USE_RUS == 1 ? "����" : "EXCLM");
break;
case '?':
printf(USE_RUS == 1 ? "����" : "QSTN");
break;
case ':':
printf(USE_RUS == 1 ? "����" : "COLON");
break;
default:
printf("%c", ch);
break;
void main() {
printf("Input string:\n");
char *str = readstr();

printf("\nString after replace:\n");
for (int i = 0; i < len; i++) {
int flag = 1;
for (int c = 0; c < W_COUNT; c++) {
if (str[i] == punct[c]) {
printf("%s", words[c]);
flag = 0;
}
}
if (flag) printf("%c", str[i]);
}
}

void main()
{
#if USE_RUS == 1
setlocale(LC_ALL, "Russian");
system("chcp 1251");
#endif

printf(USE_RUS == 1 ? "������� ������:\n" : "Input string:\n");
char *str = readstr(); // ��������� ������������ ������
printf(USE_RUS == 1 ? "\n������ ����� ������:\n" : "\nString after replace:\n");
for (int i = 0; i < len; i++)
print_char(str[i]);
free(str);
}
6 changes: 2 additions & 4 deletions Semester_1/Lab_5/Lab_5_dop.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Ввести строку. Заменить знаки препинания на сокращения
// ('.' - тчк, ',' - зпт, '?' - впр, ':' - дтч)

// доп: обратная задача, заменить сокращения на символы
// * учитывать любой регистр

#include <stdio.h>
#include <stdlib.h>

#define W_COUNT 5
const char words[W_COUNT][6] = {"DOT", "COMMA", "EXCLM", "QSTN", "COLON"};
const char punct[W_COUNT] = {'.', ',', '!', '?', ':'};
#include "reducts.c"

char lower(int c) {
return c > 64 && c < 91 ? c + 32 : c;
Expand Down
15 changes: 15 additions & 0 deletions Semester_1/Lab_5/reducts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#define W_COUNT 24

const char words[W_COUNT][7] = {
"XCLM", "QTES", "HASH", "DLLR", "PRCNT", "AMPSND",
"LBRCK", "RBRCK", "ASTRK", "PLUS", "MINUS", "SLSH",
"DOT", "COMMA", "COLON", "SCLN", "ATSMB", "QSTN",
"LARR", "EQUL", "RARR", "TILDE", "PIPE", "UNDSC"
};

const char punct[W_COUNT] = {
'!', '"', '#', '$', '%', '&',
'(', ')', '*', '+', '-', '/',
'.', ',', ':', ';', '@', '?',
'<', '=', '>', '~', '|', '_'
};