Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ГОСЫ / practical_part.odt
Скачиваний:
25
Добавлен:
05.06.2015
Размер:
30.78 Кб
Скачать

Table of Contents

1, 23, 43 2

2, 16 6

3 9

4, 12, 14, 21, 35 11

5 12

6 13

7, 8, 32, 41, 45 14

9, 25, 26 15

10, 27 17

11, 17 19

13, 34 20

15, 22 24

18, 19 27

20, 31 28

24 29

28, 44 32

29, 30, 42 33

33, 38 34

36, 37 35

39 37

40 38

33, 38 Assembler!!! 39

1, 23, 43

#include <iostream>

#include <string>

#include <stdexcept>

using namespace std;

const int mon_len[] = {31,28,31,30,31,30,31,31,30,31,30,31};

struct Date {

private:

unsigned short year;

unsigned char month, day;

public:

Date(unsigned short y = 2000, unsigned char m = 1, unsigned char d = 1) {

year = y;

month = m;

day = d;

}

int get_year() const { return year; }

int get_month() const { return month; }

int get_day() const { return day; }

void set_year(unsigned short y) {

if (y > 0 ) {

year = y;

} else {

throw range_error("Error: Value of year is wrong. It must be > 0");

}

}

void set_month(unsigned char m) {

if (m >= 1 && m <= 12) {

month = m;

} else {

throw range_error("Error: Value of month is wrong. It must be between 1 and 12 included");

}

}

void set_day(unsigned char d) {

if (d >= 1 && d <= mon_len[get_month()-1]) {

day = d;

} else {

throw range_error("Error: Value of day is wrong. It must be between 1 and count days of the month included");

}

}

Date &operator++() { // for prefix // Date &operator++(int) { --for postfix

++day;

if (day > mon_len[get_month() - 1]) {

day = 1;

++month;

if (month > 12) {

month = 1;

++year;

}

}

return *this;

}

Date &operator--() { // for prefix // Date &operator--(int) { --for postfix

--day;

if ( day == 0 ) {

--month;

if (month == 0) {

month = 12;

--year;

}

day = mon_len[month - 1];

}

return *this;

}

};

ostream &operator << (ostream &out, const Date &date) {

out <<

date.get_year() << "." <<

int(date.get_month()) << "." <<

int(date.get_day());

return out;

}

void input_date(Date &date) {

int val;

while (true) {

cout << "\nInput a new date:" << endl;

try {

cout << "Input year: ( > 0 ): ";

cin >> val;

date.set_year(val);

cout << "Input month: (from 1 till 12): ";

cin >> val;

date.set_month(val);

cout << "Input day: (from 1 till the count of days in the month): ";

cin >> val;

date.set_day(val);

break;

} catch (range_error& e ) {

cout << e.what() << endl;

}

cout << "\nTry again." << endl;

}

}

int main() {

Date date;

cout << "List of commands:" << endl;

cout << "\t output" << endl;

cout << "\t input" << endl;

cout << "\t inc" << endl;

cout << "\t decr" << endl;

cout << "\t quite" << endl;

cout << endl;

bool quite = false;

string cmd = "";

while (! quite) {

cout << "Input command: ";

cin >> cmd;

if (cmd == "output") {

cout << date << endl;

} else if ( cmd == "input") {

input_date(date);

} else if ( cmd == "inc") {

++date;

} else if ( cmd == "decr") {

--date;

} else if ( cmd == "quite" || cmd == "q") {

quite = true;

cout << "exit" << endl;

} else {

cout << "Unknown command!" << endl;

}

}

return 0;

}

2, 16

#include <iostream>

#include <string>

#include <map>

#include <iomanip>

using namespace std;

class Directory {

typedef map<string, string> strmap;

strmap direct;

public:

Directory() {}

void insert(string phone, string name) {

direct.insert(map<string, string>::value_type(phone, name));

}

void output() {

cout << "Contents of the directory:" << endl;

cout << setw(25) << " phone " << " | " << setw(15) << " name " << endl;

strmap::const_iterator iter;

for (iter = direct.begin(); iter != direct.end(); iter++) {

cout << setw(25) << (*iter).first << " | " << setw(15) << (*iter).second << endl;

}

}

void findby_phone(const string phone) {

strmap::const_iterator pos = direct.find(phone);

if (pos == direct.end())

cout << "Can't find any records" << endl;

else

cout << "Find: \n\t" << (*pos).second << endl;

}

void findby_name(const string name) {

bool result = false;

cout << "Find: " << endl;

strmap::const_iterator iter;

for (iter = direct.begin(); iter != direct.end(); iter++)

if ( (*iter).second == name ) {

cout << "\t" << (*iter).first << endl;

result = true;

}

if (! result)

cout << "Can't find any records" << endl;

}

};

Int main() {

Directory direct;

//default

direct.insert("89262624847", "Alex");

direct.insert("89035621111", "Alex");

direct.insert("+11231231212", "Tylor");

direct.insert("+7 (905) 567-58-34", "Vika");

cout << "List of commands:" << endl;

cout << "\t output" << endl;

cout << "\t insert" << endl;

cout << "\t findby_name" << endl;

cout << "\t findby_phone" << endl;

cout << "\t quite" << endl;

cout << endl;

bool quite = false;

string cmd = "";

while (! quite) {

cout << "Input command: ";

cin >> cmd;

if (cmd == "output") {

direct.output();

} else if ( cmd == "insert") {

string name; cout << "\n Input name: "; cin >> name;

string phone; cout << " Input phone: "; cin >> phone;

direct.insert(phone, name);

} else if ( cmd == "findby_name") {

string name; cout << " Input name: "; cin >> name;

direct.findby_name(name);

} else if ( cmd == "findby_phone") {

string phone; cout << " Input phone: "; cin >> phone;

direct.findby_phone(phone);

} else if ( cmd == "quite") {

quite = true;

cout << "exit" << endl;

} else {

cout << "Unknown command!" << endl;

}

}

return 0;

}

3

#include <iostream>

#include <string>

using namespace std;

class Dict {

struct Element {

int key;

string val;

Element(int k=0, string v="") {

key = k;

val = v;

}

};

Element* array;

int size;

public:

Dict() {

array = 0;

size = 0;

}

string& operator[] (const int key) {

// try to find value with such key

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

if (array[i].key == key)

return array[i].val;

// create new value if can't find

// realloc (extend array)

Element* temp = new Element[size+1];

copy(array, array + size, temp);

delete[] array;

array = temp;

++size;

array[size-1].key = key;

array[size-1].val = "";

return array[size-1].val;

}

~Dict() {

delete[] array;

array = 0;

}

};

Int main() {

Dict dict;

cout << "List of commands:" << endl;

cout << "\t insert" << endl;

cout << "\t output" << endl;

cout << "\t quite" << endl;

cout << endl;

bool quite = false;

string cmd = "";

while (! quite) {

cout << "Input command: ";

cin >> cmd;

if (cmd == "insert" || cmd == "in") {

int key; cout << " Input key: "; cin >> key;

string val; cout << " Input value: "; cin >> val;

dict[key] = val;

} else if ( cmd == "output" || cmd == "out") {

int key; cout << " Input key: "; cin >> key;

cout << "Value by key(" << key << ") = " << dict[key] << endl;

} else if ( cmd == "quite" || cmd == "q" || cmd == "exit") {

quite = true;

cout << "exit" << endl;

} else {

cout << "Unknown command!" << endl;

}

}

return 0;

}

4, 12, 14, 21, 35

#include <iostream>

using namespace std;

Int main() {

int n = 0;

cout << "n = "; cin >> n;

int S = 0;

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

S += i;

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

return 0;

}

5

#include <iostream>

using namespace std;

#define NUM 8

Соседние файлы в папке ГОСЫ