Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////
- // Files
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int fromSTRtoINT(string str) { // преобразование string --> int
- int answ = 0, len = str.length();
- for (int i = 0; i < len; i++) {
- answ += (str[i] - '0') * pow(10, (len - 1) - i);
- }
- return answ;
- }
- string exA(string str) {
- string name, priceStr, fromAgeStr, toAgeStr;
- int price, fromAge, toAge, point = 0;
- while (str[point] != ' ') { // определение имени
- name += str[point];
- point++;
- }
- point++;
- while (str[point] != ' ') { // определение цены
- priceStr += str[point];
- point++;
- }
- price = fromSTRtoINT(priceStr);
- point++;
- while (str[point] != ' ') { // определение минимального возраста
- fromAgeStr += str[point];
- point++;
- }
- fromAge = fromSTRtoINT(fromAgeStr);
- point++;
- while ((str[point] != ' ') and (point < str.length())) { // определение максимального возраста
- toAgeStr += str[point];
- point++;
- }
- toAge = fromSTRtoINT(toAgeStr);
- if ((price <= 400) and ((fromAge <= 8) and (toAge >= 8))) {
- return name + "; ";
- }
- else return "";
- }
- int exB(string str) {
- string priceStr;
- int point = 0;
- while (str[point] != ' ') { // определение имени
- point++;
- }
- point++;
- while (str[point] != ' ') { // определение цены
- priceStr += str[point];
- point++;
- }
- return fromSTRtoINT(priceStr);
- }
- string exC(string str) {
- string name, priceStr, fromAgeStr, toAgeStr;
- int price, fromAge, toAge, point = 0;
- while (str[point] != ' ') { // определение имени
- name += str[point];
- point++;
- }
- point++;
- while (str[point] != ' ') { // определение цены
- priceStr += str[point];
- point++;
- }
- point++;
- while (str[point] != ' ') { // определение минимального возраста
- fromAgeStr += str[point];
- point++;
- }
- fromAge = fromSTRtoINT(fromAgeStr);
- point++;
- while ((str[point] != ' ') and (point < str.length())) { // определение максимального возраста
- toAgeStr += str[point];
- point++;
- }
- toAge = fromSTRtoINT(toAgeStr);
- if ((fromAge <= 4) and (toAge >= 10)) {
- return name + " " + priceStr + "; ";
- }
- else return "";
- }
- int main()
- {
- setlocale(0, "");
- int n, price, fromAge, toAge;
- string name;
- cout << "Введите кол-во игрушек: ";
- cin >> n;
- cout << endl;
- ofstream file("file.txt"); // создание файла с инфой про игрушки
- if (file.is_open()) {
- cout << "Формат ввода: <имя>_<цена>_<возраст от>_<возраст до>" << endl << endl;
- for (int i = 0; i < n; i++) {
- cout << "Введите сведения об игрушке через пробел: ";
- cin >> name >> price >> fromAge >> toAge;
- file << name << " " << price << " " << fromAge << " " << toAge << endl;
- }
- file.close();
- cout << endl;
- }
- else {
- cout << "Не удалось открыть файл!";
- exit(1);
- }
- string line;
- ifstream toy("file.txt");
- int* arrB = new int[n], max = 0;
- string* arrA = new string[n];
- string* arrC = new string[n];
- if (toy.is_open()) {
- for (int i = 0; i < n; i++) {
- getline(toy, line); // считывает по 1 строке из файла
- arrA[i] = exA(line); // a)
- arrB[i] = exB(line); // b)
- arrC[i] = exC(line); // c)
- }
- cout << "а) названия игрушек, цена которых не превышает 400 р., и которые подходят детям 8 лет: ";
- for (int j = 0; j < n; j++) {
- if (arrB[j] > max) {
- max = arrB[j];
- }
- cout << arrA[j] << " ";
- }
- cout << endl;
- cout << "б) Цена самой дорогой игрушки: " << max << endl;
- cout << "c) Названия и цены игрушек, которые подходят одновременно детям 4-х и 10-и лет: ";
- for (int i = 0; i < n; i++) {
- cout << arrC[i];
- }
- cout << endl;
- }
- else {
- cout << "Не удалось открыть файл!";
- exit(1);
- }
- return 0;
- }
- ////////////////////////////////////////////////
- // Ryads
- #include <iostream>
- using namespace std;
- int main()
- {
- setlocale(0, "");
- int n, a;
- double y = 0, sumSin = 0;
- cout << "n = ";
- cin >> n;
- while (n > 0) {
- a = n;
- while (a > 0) { // считает знаменатель
- sumSin += sin(a);
- a -= 1;
- }
- y += n / sumSin; // считает всю дробь
- n -= 1;
- }
- cout << endl;
- cout << "y = " << y << endl;
- return 0;
- }
- ///////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement