Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ivanyuk_Zvit.docx
Скачиваний:
12
Добавлен:
12.05.2015
Размер:
2 Мб
Скачать
☆

Практична робота 9 Програми для роботи з власними функціями

Завдання 9.1

Написати функцію, яка повертає відсоток від отриманого в якості аргументу числа.

#include "stdafx.h"

#include <iostream>

#include <math.h>

#include <conio.h>

#include <stdlib.h>

#include <iomanip>

#include <stdio.h>

#include <time.h>

using namespace std;

void _tmain(int argc, _TCHAR* argv[])

{

float l;

float x;

cout << "x=";

cin >> x;

float Procent(float K);

l = Procent(x);

cout <<"1%="<< l<<endl;

system("PAUSE");

}

float Procent(float K)

{

return (K * 1)/100;

}

Завдання 9.2

Описати функцію SumRange(A,B) цілого типу, яка знаходить суму чисел від А до В.

#include "stdafx.h"

#include <iostream>

#include <math.h>

#include <conio.h>

#include <stdlib.h>

#include <iomanip>

#include <stdio.h>

#include <time.h>

using namespace std;

void _tmain(int argc, _TCHAR* argv[])

{

int A, B;

void SumRange(int x,int y);

cout << "A=";

cin >> A;

cout << "A=";

cin >> B;

SumRange(A, B);

system("PAUSE");

}

void SumRange(int x, int y)

{

int S=0;

for (int i = x; i <= y; i++)

{

S += i;

}

cout << "S=" << S << endl;

}

Практична робота 10 Програми для роботи зі структурами даних

Завдання 10.1

Використовуючи масив типу структура (з 6–10 елементів) скласти програму, за допомогою якої можна ввести у пам'ять комп’ютера дані з предметної області «Продовольчий магазин» і відібрати товари, вартість яких менша 10 грн. В предметній області мають бути присутні обчислювальні поля.

#include "stdafx.h"

#include <iostream>

#include <math.h>

#include <conio.h>

#include <stdlib.h>

#include <iomanip>

#include <stdio.h>

#include <time.h>

#include <windows.h>

using namespace std;

void _tmain(int argc, _TCHAR* argv[])

{

int N;

char tovar1[50];

struct Shop

{

char tovar[50];

float cena;

};

cout << "N=";

cin >> N;

Shop st[6];

system("cls");

for (int i = 0; i < N; i++)

{

cout << "Tovar # " << i + 1 << endl;

cout << "Nazvanie=";

cin >> tovar1;

strcpy(st[i].tovar, tovar1);

cout << "Cena=";

cin >> st[i].cena;

cout << "-----------------";

cout << endl;

}

system("cls");

for (int i = 0; i < N; i++)

{

if (st[i].cena < 10)

cout << st[i].tovar << endl;

}

cout << endl;

system("PAUSE");

}

Практична робота 11 Програми для роботи з файлами

Завдання 11.1

Модифікувати програму завдання 1 практичної роботи 6 таким чином, щоб значення зчитувались з текстового файлу, а результат обчислення записувався в файл. Реалізувати 2 варіанта роботи з файлами, за допомогою компонентів вкладки Dialogs (візуальний режим роботи програми) та за допомогою директив ifstream.h, ofstream.h (консольний режим роботи програми).

#include "stdafx.h"

#include <fstream>

#include <iostream>

#include <math.h>

#include <conio.h>

#include <stdlib.h>

#include <iomanip>

#include <stdio.h>

#include <time.h>

#include <windows.h>

using namespace std;

void _tmain(int argc, _TCHAR* argv[])

{

int a, b, c,S;

char st[50];

char st1[50];

char *a1;

char *b1;

char *c1;

fstream fl;

fl.open("D:\\zad11_1.txt",ios::in);

if (fl)

{

while (!fl.eof())

{

fl >> st1;

strcpy(st, st1);

}

a1 = strtok(st, ",");

b1 = strtok(NULL, ",");

c1 = strtok(NULL, ",");

}

else

cout << "error";

fl.close();

a = atoi(a1);

b = atoi(b1);

c = atoi(c1);

S = 2 * (a*b + b*c + a*c);

fl.open("D:\\zad11_1.txt", ios::app);

if (fl)

{

fl << "\n" << S;

}

else cout << "error";

fl.close();

system("PAUSE");

}

Завдання 11.2

//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

char * st;

char st1;

if (!OpenDialog1->Execute()) return;

TStringList *list = new TStringList;

list->LoadFromFile(OpenDialog1->FileName);

Label1->Caption = list->Strings[0];

TStringList *list1 = new TStringList;

list1->Delimiter = ',';

list1->DelimitedText = list->Strings[0];

Label2->Caption = list1->Strings[0];

Label3->Caption = list1->Strings[1];

Label4->Caption = list1->Strings[2];

int S = 2*(list1->Strings[0].ToInt() * list1->Strings[1].ToInt() + list1->Strings[1].ToInt()*list1->Strings[2].ToInt()

+ list1->Strings[0].ToInt()*list1->Strings[2].ToInt());

Label5->Caption = IntToStr(S);

TStringList *list3 = new TStringList;

list3->Add(IntToStr(S));

list3->SaveToFile("D:\\zad11_1.txt");

delete list;

delete list1;

delete list3;

}

//---------------------------------------------------------------------------

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]