Добавил:
baraevaliza
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабы и курсачи 2022-2023 / 1 сем / Baraeva_Elizaveta_cw
.pdf
11
2. ПРИМЕРЫ
Таблица 1 – Результаты тестирования
№ п/п
Входные данные
Выходные данные
Комментарии
1
My sister runs a barber, shop in
Boston. my sister runs a barber,
sHop in Boston.
My sister runs a
barber, shop in
Boston.
Удаление
повторяющихся
предложений
2
Hi. I am Jack.i am jack. I am
JACK. HI.
Hi. I am Jack.
Удаление
повторяющихся
предложений
3
Today is d14m12y1854, wow, or
d11m11y2050. Today is
d28m11y2024.Tomorow is
d12m11y2050.
14:12:1854
28:11:2024
11:11:2050
12:11:2050
Поиск дат,
вывод их по
возрастанию
4
Today is d01m01y2000. I was
born d25m03y1987. Tomorrow is
02,01,2000.
25:03:1987
01:01:2000
Поиск дат,
вывод их по
возрастанию
5
Today is d26m11y2020.It snowed
all day today. The snow is very
beautiful. The snow stopped.
Удаление всех
предложений с
нечетным
количеством
слов
6
I like to go to the beach. My
favorite beach is called Emerson
Beach. It is very long, with soft
sand and palm trees. It is very
beautiful. I like to make
sandcastles and watch the
sailboats go by. Sometimes there
are dolphins and whales in the
water.
It is very long, with
soft sand and palm
trees. It is very
beautiful.
Удаление всех
предложений с
нечетным
количеством
слов
7
Today is d01m01y2000. I was
born d25m03y1987. Tomorrow is
02,01,2000.
todaY iS
d01m01y2000. I
waS borN
d25m03y1987.
tomorroW iS
02,01,2000.
Преобразование
всех слов в
которых нет
цифр
8
Today is d26m11y2020.It snowed
all day today. The snow is very
beautiful. The snow stopped.
todaY iS
d26m11y2020. iT
snoweD alL daY
todaY. thE snoW iS
verY beautifuL. thE
snoW stoppeD.
Преобразование
всех слов в
которых нет
цифр

12
9
i like to go to the beach. my
favorite beach is called Emerson
Beach. It is very long, with soft
sand and palm trees.it is very
beautiful. i like to make
sandcastles and watch the
sailboats go bY. sometimes there
are dolphins and whales in the
water.
i like to go to the
beach. it is very
beautiful. sometimes
there are dolphins
and whales in the
water.
Вывод всех
предложений, в
которых нет
заглавных букв
10
Hi. I am Jack.i ,am jack. I am
JACK. HIi.
i ,am jack.
Вывод всех
предложений, в
которых нет
заглавных букв

13
ЗАКЛЮЧЕНИЕ
В ходе выполнения курсовой работы, была разработана программа,
которая корректно работает с указателями и динамической памятью.

14
СПИСОК ИСПОЛЬЗОВАННЫХ ИСТОЧНИКОВ
1. Язык программирования Си: монография / Б.Керниган, Д.Ритчи; Пер. с
англ. под ред. В.С.Штаркмана. - 3-е изд., испр. - СПб. : Невский диалект, 2001. 351 с.

15
ПРИЛОЖЕНИЕ А
ИСХОДНЫЙ КОД ПРОГРАММЫ
Название файла: main.c
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define SENTENCE_SIZE 50
#define TEXT_SIZE 25
int input_sentence(char** str){
int str_size = SENTENCE_SIZE;
int i = 0;
char c;
*str = malloc(str_size * sizeof(char));
while ((c = getchar()) != '.'){
if (c == '\n'){
free(*str);
return -1;
}
if (i == 0 && c == ' '){
continue;
}
(*str)[i] = c;
if (i == str_size - 1){
str_size += SENTENCE_SIZE;
(*str) = realloc((*str), str_size * sizeof(char));
}
i++;
}
if (str_size - i <= 1){
str_size += 2;
(*str) = realloc((*str), str_size * sizeof(char));
}
(*str)[i] = '.';
(*str)[i + 1] = '\0';
return 1;
}
int input_text(char*** text){
int text_size = TEXT_SIZE;
int N = 0;
*text = malloc(text_size * sizeof(char*));
while (input_sentence(*text + N) != -1){
if (N == text_size - 1){
text_size += TEXT_SIZE;
(*text) = realloc((*text), text_size * sizeof(char*));
}
N++;
}
return N;
}
void print_text(char **text, int text_size){

16
for(int i = 0; i < text_size; i++){
printf("%s ", text[i]);
}
puts("");
}
void free_text(char **text, int text_size){
for(int i = 0; i < text_size; i++){
free(text[i]);
}
free(text);
}
void delete_null_sentences(char*** text, int* text_size){
for (int i = 0; i < *text_size; i++){
if ((*text)[i] == NULL){
memmove((*text + i), (*text + i + 1), ((*text_size - i) *
sizeof(char*)));
i--;
(*text_size)--;
}
}
}
void delete_same_sentences(char*** text, int* text_size){
char* str1;
char* str2;
for(int i = 0; i < *text_size - 1; i++){
for(int j = i + 1; j < *text_size; j++){
str1 = (*text)[i];
str2 = (*text)[j];
if(str1 != NULL && str2 != NULL){
if(strcasecmp(str1, str2) == 0){
free((*text)[j]);
(*text)[j] = NULL;
}
}
}
}
delete_null_sentences(text, text_size);
}
void delete_sentences_with_odd_words(char*** text, int* text_size){
int flag = 0, k = 0;
for(int i = 0; i < *text_size; i++){
for(int j = 0; j < strlen((*text)[i]); j++){
if(isalnum((*text)[i][j]) != 0 && flag == 0){
k++;
flag++;
}
if(isalnum((*text)[i][j]) == 0){
flag = 0;
}
}
if(k % 2){
free((*text)[i]);
(*text)[i] = NULL;

17
}
k = 0;
}
delete_null_sentences(text, text_size);
}
void print_sentences_without_capital_letters(char **text, int text_size){
int k = 0;
for(int i = 0; i < text_size; i++){
for(int j = 0; j < strlen(text[i]); j++){
if(isupper(text[i][j])){
k++;
break;
}
}
if(k == 0){
printf("%s ", text[i]);
}
k = 0;
}
puts("");
}
struct Date{
int day;
int month;
int year;
};
int comp(const void * first, const void * second){
struct Date *first_date = (struct Date*) first;
struct Date *second_date = (struct Date*) second;
int result = first_date->year - second_date->year;
if (result)
return result;
result = first_date->month - second_date->month;
if (result)
return result;
result = first_date->day - second_date->day;
return result;
}
void output_of_dates(char **text, int text_size){
int day, month, year, k = 0, size = TEXT_SIZE;
struct Date *dates;
dates = malloc(size * sizeof(struct Date));
for(int i = 0; i < text_size; i++){
for(int j = 0; j < strlen(text[i]); j++){
if ((sscanf(&text[i][j], "d%02dm%02dy%04d", &day, &month,
&year)) == 3){
if(k == size){
size += TEXT_SIZE;
dates = realloc(dates, size * sizeof(struct Date));
}
dates[k].day = day;
dates[k].month = month;
dates[k].year = year;

18
k++;
j += 11;
}
}
}
qsort(dates, k, sizeof(struct Date), comp);
for (int i = 0; i < k; i++)
printf("%02d:%02d:%02d\n", dates[i].day, dates[i].month,
dates[i].year);
free(dates);
}
void convert_words_without_numbers(char ***text, int text_size){
int k = 0, start = 0;
for(int i = 0; i < text_size; i++){
for(int j = 0; j < strlen((*text)[i]); j++){
if(isalpha((*text)[i][j])){
if(k == 0){
start = j;
}
k++;
}
if((*text)[i][j] == ' ' || (*text)[i][j] == ',' ||
(*text)[i][j] == '.'){
if(j - start == k){
for(int l = start; l < j - 1; l++){
(*text)[i][l] = tolower((*text)[i][l]);
}
(*text)[i][j-1] = toupper((*text)[i][j-1]);
}
k = 0;
start = 0;
}
}
}
}
void hint(){
puts("Выберите действие:");
puts("0 -> Выход из программы");
puts("1 -> Вывести все даты по возрастанию");
puts("2 -> Удалить все предложения, в которых количество слов
нечетно");
puts("3 -> Преобразовать слова, в которых нет цифр");
puts("4 -> Вывести предложения, в которых нет заглавных букв");
puts("5 -> Печать текста");
}
void select_action(char ***text, int *text_size){
hint();
int a;
scanf("%d", &a);
switch(a){
case 0:{
break;
}
case 1:{

19
output_of_dates(*text, *text_size);
break;
}
case 2:{
delete_sentences_with_odd_words(text, text_size);
print_text(*text, *text_size);
break;
}
case 3:{
convert_words_without_numbers(text, *text_size);
print_text(*text, *text_size);
break;
}
case 4:{
print_sentences_without_capital_letters(*text, *text_size);
break;
}
case 5:{
print_text(*text, *text_size);
break;
}
default:{
puts("Неправильный ввод!");
}
}
}
int main(){
char **text = NULL;
int text_size;
puts("Введите текст:");
text_size = input_text(&text);
delete_same_sentences(&text, &text_size);
select_action(&text, &text_size);
free_text(text, text_size);
return 0;
}
Соседние файлы в папке 1 сем
