Int main() {
unsigned short size = 3;
cout << "Input size of array = "; cin >> size;
unsigned short* array = new unsigned short[size];
cout << "Input array:" << endl;
for(unsigned short i = 0; i < size; i++) {
cout << " array[" << i << "] = "; cin >> array[i];
}
cout << "\nInputed array:" << endl;
outputArray(array, size);
unsigned short m = 5; cout << "Input m = "; cin >> m;
unsigned long sum = 0;
for(unsigned short i = 0; i < size; i++)
if ( array[i] < m )
sum += array[i];
cout << "\nSum of elements < " << m << " = " << sum << endl;
return 0;
}
/*
* word type: 2 bytes, 0..65535
* unsigned short in C++ is the same as word in Pascal
*/
13, 34
#include <iostream>
#include <cstring>
#include <ctime>
#include <exception>
#include <string>
#define DISTINCT 10
using namespace std;
struct stringException : public exception {
const char * what () const throw () {
return "String Exception";
}
};
struct cantfind_substr : public stringException {
const char * what () const throw () {
return "Can't find substr in current string";
}
};
class String {
char* str;
int len;
int maxlen_inheap;
char* creationTime;
void fillin_creationTime() {
time_t now = time(0); // current date/time based on current system
creationTime = ctime(&now); // convert now to string form
}
public:
String(const char* string = "") {
str = 0; len = 0;
fillin_creationTime();
set_str(string);
}
// copy constructor
String(const String &string) {
str = 0; len = 0;
fillin_creationTime();
set_str(string.get_str());
len = string.get_len();
}
char* get_str() const { return str; }
int get_len() const { return len-1; }
char* get_creationTime() const { return creationTime; }
void set_str(const char* string) {
int newlen = strlen(string) + 1; // with null char
// Change size to more size in any case and to less one in case of big distinction
if (newlen > maxlen_inheap || newlen < (maxlen_inheap - DISTINCT)) {
delete[] str; str = 0;
str = new char[newlen];
maxlen_inheap = newlen;
}
len = newlen;
copy(string, string+newlen, str);
fillin_creationTime();
}
String& operator= (const String &string) {
// check for self-assignment by comparing the address of the
// implicit object and the parameter
if (this == &string)
return *this;
set_str(string.get_str());
return *this;
}
String& operator= (const char* string) {
set_str(string);
return *this;
}
int find_substr(const char* substr) {
for (int i = 0; i < len-1; i++)
if (str[i] == substr[0]) {
int j;
for (j = 0; substr[j] != '\0'; j++)
if (str[i+j] != substr[j])
break;
if (substr[j] == '\0')
return i+1;
}
throw cantfind_substr();
}
~String() {
if (str) {
delete[] str; str = 0;
}
}
};
ostream &operator << (ostream &out, const String &str) {
out << str.get_str();
return out;
}
istream& operator>> (istream &in, String &str) {
string inputted;
getline(in, inputted); getline(in, inputted);
str.set_str(inputted.c_str());
return in;
}
int main(int argc, char const *argv[]){
String str("hello");
cout << "List of commands:" << endl;
cout << "\t input" << endl;
cout << "\t output" << endl;
cout << "\t outlen" << endl;
cout << "\t find" << endl;
cout << "\t creation_time" << endl;
cout << "\t quite" << endl;
cout << endl;
bool quite = false;
string cmd = "";
while (! quite) {
cout << "Input command: ";
cin >> cmd;
if (cmd == "input" || cmd == "in") {
cout << " Input string: "; cin >> str;
} else if ( cmd == "output" || cmd == "out") {
cout << " " << str << endl;
} else if ( cmd == "outlen") {
cout << " Lenght of the string = " << str.get_len() << endl;
} else if ( cmd == "find") {
string substr;
cout << " Input substring which you want to find in the inputted string: ";
getline(cin, substr); getline(cin, substr);
try {
cout << " Index of substr in the string = " << str.find_substr(substr.c_str()) << endl;
}
catch(cantfind_substr e) {
cout << " " << e.what() << endl;
}
} else if ( cmd == "creation_time" || cmd == "ct") {
cout << " Creation time of the string = " << str.get_creationTime();
} else if ( cmd == "quite" || cmd == "q") {
quite = true;
cout << "exit" << endl;
} else {
cout << " Unknown command!" << endl;
}
}
return 0;
}
15, 22
#include <iostream>
#include <iomanip>
using namespace std;
#define SIZE 10 //100
#define NAME_WIDTH 20
static unsigned int n = 0;
class Student {
public:
string name;
unsigned int num;
string depart;
string num_speciality;
string birthday;
string address;
string phone;
Student(string na = "Default Name",
unsigned int nu = ++n,
string dep = "Department",
string nu_s = "230105/c",
string bi = "01/02/2003",
string ad = "Moscow",
string ph = "+7 (123) 456-78-90") {
name = na;
num = nu;
depart = dep;
num_speciality = nu_s;
birthday = bi;
address = ad;
phone = ph;
}
};
bool operator< (Student &stu1, Student &stu2) {
return (stu1.name < stu2.name);
}
bool operator> (Student &stu1, Student &stu2) {
return (stu1.name > stu2.name);
}
ostream& operator<< (ostream &out, const Student &stu) {
out << setiosflags(ios::left);
out << " " << setw(NAME_WIDTH) << stu.name
<< " " << stu.num
<< " " << stu.depart
<< " " << stu.num_speciality
<< " " << stu.birthday
<< " " << stu.address
<< " " << stu.phone;
return out;
}
template <typename T>
void output(T* array) {
for(int i=0; i<SIZE; i++)
cout << array[i] << endl;
}
/*
Name: qsort (quick sort) or Hoare sort
Cases:
O(n*log(n)) --best
O(n*log(n)) --average
O(n^2) --worst
*/
template <typename T>
void sort(T* array, int l=0, int r=SIZE-1) {
int i=l;
int j=r;
// Выбираем опорный элемент
T* pivot = &array[static_cast<int>((i+j)/2)];
// Сортируем относительно опроного элемента
do {
while (array[i] < *pivot)
i++;
while (array[j] > *pivot)
j--;
if (i<=j) {
T temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
} while (i<j);
// Обрабатываем полученные две части
if (l<j)
sort(array, l, j);
if (i<r)
sort(array, i, r);
}