Professional Documents
Culture Documents
звіт 7 - АП
звіт 7 - АП
Звіт
Про виконання лабораторної роботи №7
Підпрограми (функції) в мові програмування С
Варіант 13
Виконала:
студент групи КН-11з
Вакарова В.В.
Прийняв:
ассистент, к.т.н
Оборська О.В.
Львів 2021
Мета роботи: ознайомитися із особливостями застосування функцій у
мові С.
Лабораторне завдання:
Завдання 1
Блок-схема:
OutputhNum:
CalculateSum:
InputNum:
Main:
Код:
Результат:
Завдання 2
13. Вивести всі слова, що починаються з літер, які вказані користувачем (слова
не дублювати, вивести кожне окреме слово та кількість його входжень).
Блок-схема:
GetText:
InputLetters:
PrintArray:
CheckDuplicates:
Search:
Main:
Код:
#include <stdio.h>
#include <string.h>
struct Words {
char Letters[15];
int Count;
};
void GetText(FILE* T, char* P) {
int k = 0;
while (feof(T) == 0) {
fscanf(T, "%c", &P[k]);
k++;
}
P[k - 1] = '\0';
}
void InputLetters(char* P) {
printf("Enter letters to search: ");
gets(P);
}
void PrintArray(int n, struct Words Words[20]) {
int check;
printf("\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < 15; j++) {
check = j;
if (Words[i].Letters[j] != '0') printf("%c", Words[i].Letters[j]);
else break;
}
if (check != 0) printf(" - %d time(s)\n", Words[i].Count);
}
if (Words[0].Count == 0) printf("No words were found!");
}
void CheckDuplicates(int n, struct Words Words[20]) {
int k = 0;
for (int i = 0; i < n; i++) {
Words[i].Count = 1;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
while (Words[i].Letters[k] != '0' || Words[j].Letters[k] != '0') {
if (tolower(Words[i].Letters[k]) == tolower(Words[j].Letters[k])) {
k++;
}
else {
k = 0;
break;
}
}
if (k != 0) {
Words[j].Letters[0] = '0';
Words[i].Count++;
Words[j].Count = 0;
}
k = 0;
}
}
PrintArray(n, Words);
}
void Search(char* P, char* L, struct Words Words[20]) {
int i = 0, j = 0, k = 0, t = 0, n = 0;
Words[0].Count = 0;
while (P[i] != '\0' && i < 200) {
while (L[j] != '\0' && i < 200) {
if ((P[i] == L[j] || P[i] == L[j] - 32) && (P[i - 1] == ' ' || j != 0 || i == 0))
{
j++;
}
else {
j = 0;
}
i++;
}
if (j != 0 && i < 200) {
k = i - j;
while (P[k] != ' ' && P[k] != '.' && P[k] != ',' && P[k] != '!' && P[k] != '\0')
{
Words[n].Letters[t] = P[k];
t++;
k++;
}
Words[n].Letters[t] = '0';
t = 0;
i = k;
n++;
}
j = 0;
}
CheckDuplicates(n, Words);
}
int main() {
FILE* T = fopen("Text.txt", "r");
struct Words Words[20];
char Text[200], Letters[10];
char* P = Text;
GetText(T, P);
char* L = Letters;
InputLetters(L);
Search(P, L, Words);
}
Результат:
Контрольні запитання: