Вывод:В ходе работы я научился работать со строками и текстовыми файлами.

Факультет информационных технологий и робототехники

 

Кафедра программного обеспечения вычислительной техники

и автоматизированных систем

 

Отчет по лабораторной работе № 7

 

по дисциплине: ”Основы алгоритмизации и программирования”

 

на тему: ”Строки и файлы”

Вариант 15

 

Выполнил: студенты группы 10702214 Туан Ань

 

Приняла: ст.пр. Пармон С.И

 

Минск 2014

 


Лабораторная работа №7. Строки и файлы.

Цель работы:Научиться работать со строками и текстовыми файлами.

Задание 1

Напишите программу, которая определяет, явялется ли введенная с клавиатуры строка восьмеричным числом.

Код программы:

#include <iostream>

 

using namespace std;

 

int main()

{

setlocale(LC_ALL, "RUS");

char s[10] = "";

bool b = 0;

scanf("%s", s);

for (int i = 0; i <= sizeof(s) - 1; i++)

if (s[i] > 55 || s[i] < 48 && s[i] != 0)

b = 1;

if (!b)

cout << "Является" << endl;

else

cout << "Не является" << endl;

system("pause");

}

Скриншоты результатов:

Задание 2

Код программы:

#include <iostream>

 

using namespace std;

 

char c[] = "Create hyperlinks by dragging and dropping. Add graphics and multimedia elements in seconds. Make screenshots of your application with the integrated screenshot tool and enhance them quickly with the screenshot editing program. Compile your project to any supported output format with a couple of clicks. Work in collaboration with a team of authors all editing the same project at the same time. Produce help and documentation faster and more efficiently and have fun in the process!";

void fun1();

void fun2();

void fun3();

 

int main()

{

setlocale(LC_ALL, "RUS");

fun1();

fun2();

fun3();

for (int i = 0; i <= sizeof(c) - 1; i++)

cout << c[i];

system("pause");

}

 

void fun1()

{

int n = 0;

for (int i = 0; i <= sizeof(c) - 1; i++)

if (c[i] >= 97 && c[i] <= 122)

n++;

cout << "Количество строчных букв: " << n << endl;

}

 

void fun2()

{

int n = 1;

int a;

cout << "Слово, содержащее букву 'v': ";

for (int i = 0; i <= sizeof(c) - 1; i++)

{

if (c[i] == 32)

{

a = i+1;

n++;

}

if (c[i] == 118)

{

do

{

cout << c[a];

a++;

}

while (c[a] >= 97 && c[a] <= 122);

break;

}

}

cout << endl << "Его порядковый номер: " << n << endl;

}

 

void fun3()

{

int a = 0;

int b;

for (int i = 0; i <= sizeof(c) - 1; i++)

{

if ((c[i] == 32 && c[i + 1] == 115) || c[i] == 83)

{

a = i;

do

{

a++;

} while (c[a] >= 97 && c[a] <= 122 || c[a] == 32 && c[a + 1] == 115 || c[a] == 83);

b = i;

for (b; b <= sizeof(c) - 1; b++)

c[b] = c[a++];

 

}

}

}

Скриншоты результатов:

Задание 3

Написать программу, которая считывает из текстового файла и записывает в новый файл только строки, содержащие двоичные числа

Код программы:

 

#include <iostream>

#include <cstdio>

 

using namespace std;

 

void zap(int);

 

FILE *in;

FILE *out;

char ch;

int n = 1, str = 0;

bool b = 0;

 

int main()

{

setlocale(LC_ALL, "RUS");

if ((in = fopen("File.txt", "r")) != NULL)

{

if ((out = fopen("File2.txt", "w")) != NULL)

{

ch = getc(in);

while (ch != EOF)

{

if (ch == '0' || ch == '1')

b = 1;

if (ch == '\n' && b)

{

zap(n - str);

str = n + 1;

b = 0;

}

else

if (ch == '\n')

str = n + 1;

n++;

ch = getc(in);

}

}

}

fclose(in);

fclose(out);

system("pause");

}

 

void zap(int kol)

{

char mm;

fseek(in, -kol - 2, 1);

for (int i = 1; i <= kol + 1; i++)

{

mm = getc(in);

putc(mm, out);

}

}

 

Скриншоты результатов:

 

Вывод:В ходе работы я научился работать со строками и текстовыми файлами.