Advertisement
PoulYakov

Homework4

Sep 26th, 2022
928
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.94 KB | None | 0 0
  1. ## 1
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. using namespace std;
  6. int main()
  7. {  
  8.     system("chcp 1251");
  9.     ofstream fileout("input.txt");
  10.     // Установить начальную точку генерирования последовательности
  11.     srand(31);
  12.     for (int i = 0; i < 10; i++) {
  13.         fileout << rand()<<"\n";
  14.     }
  15.  
  16.     int s=0;
  17.     fileout.close();
  18.     ifstream filein ("input.txt");
  19.     int number;
  20.     if (filein) {
  21.         for (int i = 0; i < 10; i++) {
  22.             filein >> number;
  23.             s += number;
  24.         }
  25.         cout << "Сумма чисел: " << s;
  26.     }
  27.     else {
  28.         cout << "Нету такого файла(";
  29.     }
  30.     filein.close();
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. ## 2
  38. #include <iostream>
  39. using namespace std;
  40.  
  41. int sign(float x) {
  42.     if (x > 0)
  43.         return 1;
  44.     else if (x == 0)
  45.         return 0;
  46.     else
  47.         return -1;
  48. }
  49.  
  50. int main()
  51. {
  52.     system("chcp 1251");
  53.     cout << "Задача «Знак числа»\n";
  54.     cout << "x = ";
  55.     float x;
  56.     cin >> x;
  57.     cout << "sign(x) = " << sign(x);
  58. }  
  59.  
  60. ## 3
  61. #define _USE_MATH_DEFINES
  62. #include <cmath>
  63. #include <iostream>
  64. using namespace std;
  65.  
  66. double area_rectangle() {
  67.     cout << "Площадь прямоугольника\n";
  68.     double a,b;
  69.     cout << "a = ";
  70.     cin >> a;
  71.     cout << "b = ";
  72.     cin >> b;
  73.     return a * b;
  74. }
  75. double area_triangle() {
  76.     cout << "Площадь треугольника\n";
  77.     double a, h;
  78.     cout << "a = ";
  79.     cin >> a;
  80.     cout << "h = ";
  81.     cin >> h;
  82.     return 0.5*a*h;
  83. }
  84. double area_circle() {
  85.     cout << "Площадь круга\n";
  86.     double r;
  87.     cout << "r = ";
  88.     cin >> r;
  89.     return M_PI*r*r;
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. int main()
  97. {
  98.     system("chcp 1251");
  99.     cout << "Задача «Геометрические фигуры»\n";
  100.     cout << "Укажите, площадь какой фигуры нужно вычислить\n\t1 - прямоугольник\n\t2 - треугольник\n\t3 - круг\n";
  101.     int n;
  102.     cin >> n;
  103.     switch (n)
  104.     {
  105.     case 1:
  106.         cout<<area_rectangle();
  107.         break;
  108.     case 2:
  109.         cout << area_triangle();
  110.         break;
  111.     case 3:
  112.         cout << area_circle();
  113.         break;
  114.     }
  115. }
  116.  
  117.  
  118.  
  119. ## 4
  120. #include <iostream>
  121.  
  122. using namespace std;
  123. #include <windows.h>
  124.  
  125. string symb_k_times(string symb, int k) {
  126.     string result = "";
  127.     for (int i = 0; i < k; i++)
  128.         result += symb;
  129.     return result;
  130. }
  131.  
  132. int main()
  133. {
  134.     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  135.  
  136.     system("chcp 1251");
  137.     cout << "Задача «Былая слава»\n";
  138.     SetConsoleTextAttribute(h, 0);
  139.     int fl = 1;
  140.     cout << symb_k_times(" ", 44)+"\n";
  141.     for (int i = 0; i < 6; i++) {
  142.         fl = (fl + 1) % 2;
  143.         SetConsoleTextAttribute(h, 63);
  144.         cout << " "+symb_k_times("* ", 8);
  145.  
  146.         SetConsoleTextAttribute(h, 200+fl*55);
  147.         cout<<symb_k_times("_", 27) + "\n";
  148.     }
  149.  
  150.     SetConsoleTextAttribute(h, 255);
  151.  
  152.     for (int i = 0; i < 7; i++) {
  153.         fl = (fl + 1) % 2;
  154.         SetConsoleTextAttribute(h, 200 + fl * 55);
  155.         cout <<symb_k_times("_", 44)+"\n";
  156.     }
  157.     SetConsoleTextAttribute(h, 0);
  158.     return 0;
  159.  
  160. }
  161.  
  162. ## 5
  163. #include <iostream>
  164. #include <math.h>
  165. using namespace std;
  166. int main() {
  167.     int height = 40;
  168.     int width = 120;
  169.     char A[100][200];
  170.     for (int i = 0; i < height; i++) {
  171.         for (int j = 0; j < width; j++) {
  172.             A[i][j]='O';
  173.         }
  174.     }
  175.     int y;
  176.     for (int i = -width/2+2; i < width/2-2; i++) {
  177.         y = sin((float) i/10)*10;
  178.         A[y+height/2][i+width/2] = ' ';
  179.     }
  180.  
  181.     for (int i = 0; i < width; i++)
  182.         A[height/2][i] = '-';
  183.  
  184.     for (int i = 0; i < height; i++)
  185.         A[i][width/2] = '|';
  186.  
  187.  
  188.  
  189.     A[height / 2][width - 1] = '>';
  190.     A[0][width/2] = '^';
  191.     A[height / 2][width / 2] = '+';
  192.  
  193.  
  194.     for (int i = 0; i < height; i++) {
  195.         for (int j = 0; j < width; j++) {
  196.             cout<<A[i][j];
  197.         }
  198.         cout << "\n";
  199.     }
  200.  
  201.  
  202.  
  203.     //a = sin(b)
  204.     return 0;
  205. }
  206.  
  207.  
  208. ## 6
  209. #include <iostream>
  210. #include <map>
  211. #include <string>
  212. #include <fstream>
  213. #include <vector>
  214.  
  215. using namespace std;
  216.  
  217. int main()
  218. {
  219.     system("chcp 1251");
  220.     map<string, int> m = {
  221.         {"I", 1}, {"II", 2},{"III", 3},{"IV", 4},{"V", 5},{"VI", 6},{"VII", 7},{"VIII", 8},{"IX", 9},
  222.         {"X", 10}, {"XX", 20}, {"XXX", 30}, {"XL", 40}, {"L", 50}, {"LX", 60}, {"LXX", 70}, {"LXXX", 80}, {"XC", 90},
  223.         { "C", 100 }, { "CC", 200 }, { "CCC", 300 }, { "CD", 400 }, { "D", 500 }, { "DC", 600 }, { "DCC", 700 }, { "DCCC", 800 }, { "CM", 900 },
  224.         { "M", 1000 } , { "MM", 2000 }, { "MMM", 3000 }
  225.     };
  226.     //['I', 'V', 'X', 'L', "C", 'D', 'M']
  227.     string inp;
  228.     cout << "roman_number = ";
  229.     cin >> inp;
  230.     int n = inp.length();
  231.    
  232.    
  233.     int i = 0;
  234.     string buff="";
  235.     bool was_error = false;
  236.  
  237.     vector<int> v;
  238.     while (i < inp.length()) {
  239.         if (m.count(buff + inp[i])) {
  240.             buff += inp[i];
  241.  
  242.         }
  243.         else if (!(inp[i] == 'I' || inp[i] == 'V' || inp[i] == 'X' || inp[i] == 'L' || inp[i] == 'C' || inp[i] == 'D' || inp[i] == 'M'))
  244.             was_error = true;
  245.         else {
  246.             v.push_back(m.find(buff)->second);
  247.             buff = inp[i];
  248.  
  249.         }
  250.         i++;
  251.     }
  252.     if (m.count(buff))
  253.         v.push_back(m.find(buff)->second);
  254.     int s = 0;
  255.     int prev_number = 10000;
  256.     for (int i : v) {
  257.         if (to_string(i).length() < to_string(prev_number).length()) {
  258.             s += i;
  259.             prev_number = i;
  260.         }
  261.         else {
  262.             was_error = true;
  263.         }
  264.     }
  265.     if (was_error)
  266.         cout << "Ошибка";
  267.     else
  268.         cout << s;
  269. }
  270.  
  271.  
  272. ## 7
  273. // hw4_task7.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  274. //
  275.  
  276. #include <iostream>
  277. using namespace std;
  278. int main()
  279. {
  280.     long m, b, c, s0=0, s, idx;
  281.     system("chcp 1251");
  282.     cout << "m = ";
  283.     cin >> m;
  284.     cout << "b = ";
  285.     cin >> b;
  286.     cout << "c = ";
  287.     cin >> c;
  288.     cout << "i = ";
  289.     cin >> idx;
  290.     for (int i = 1; i < idx; i++) {
  291.         s = (m * s0 + b) % c;
  292.         s0 = s;
  293.         cout << i << ") " << s << "\n";
  294.     }
  295.  
  296.  
  297. }
  298.  
  299. ## 8
  300.  
  301. #include <iostream>
  302. #include <fstream>
  303. using namespace std;
  304.  
  305. int main()
  306. {
  307.     system("chcp 1251");
  308.     ifstream fin("input.txt");
  309.     int A[10][10];
  310.     //cout << "Hello World!\n";
  311.     int n, m;
  312.     //cout << "Введите матрицу A"<<"\n";
  313.     //cout << "m = ";
  314.     fin >> m;
  315.     //cout << "n = ";
  316.     fin >> n;
  317.    
  318.  
  319.  
  320.     for (int i = 0; i < m; i++) {
  321.         for (int j = 0; j < n; j++) {
  322.             fin >> A[i][j];
  323.         }
  324.     }
  325.  
  326.     float B[10][10];
  327.     int w, t;
  328.     //cout << "Введите матрицу B" << "\n";
  329.     //cout << "w = ";
  330.     fin >> w;
  331.     //cout << "t = ";
  332.     fin >> t;
  333.  
  334.     for (int i = 0; i < w; i++) {
  335.         for (int j = 0; j < t; j++) {
  336.             fin >> B[i][j];
  337.         }
  338.     }
  339.  
  340.     float C[10][10];
  341.     for (int i = 0; i < m; i++) {
  342.         for (int j = 0; j < t; j++) {
  343.             C[i][j] = 0;
  344.             for (int q=0; q < n; q++) {
  345.                 C[i][j] += A[i][q] * B[q][j];
  346.             }
  347.         }
  348.     }
  349.     cout << "Матрица С\n";
  350.    
  351.     float s = 0;
  352.     float s_com = 0;
  353.  
  354.     int mini_s = 0, mini_s_com = 0;
  355.     int max_s = 0, max_s_com = 0;
  356.     for (int i = 0; i < m; i++) {
  357.         if (C[i][0] < C[mini_s][0])
  358.             mini_s = i;
  359.         if (C[i][0] > C[max_s][0])
  360.             max_s = i;
  361.         s += C[i][0];
  362.     }
  363.     cout << "1) " <<max_s+1<< " и " <<mini_s+1<<"\n";
  364.  
  365.  
  366.     for (int i = 0; i < m; i++) {
  367.         if (C[i][1] < C[mini_s_com][1])
  368.             mini_s_com = i;
  369.         if (C[i][1] > C[max_s_com][1])
  370.             max_s_com = i;
  371.         s_com += C[i][1];
  372.     }
  373.     cout << "1) " << max_s_com+1 << " и " << mini_s_com+1<<"\n";
  374.  
  375.     cout << "3) " << s<<"\n";
  376.     cout << "4) " << s_com<<"\n";
  377.     cout << "5) " << s_com+s<<"\n";
  378.  
  379.  
  380.  
  381. }
  382.  
  383.  
  384. ## 9
  385.  
  386.  
  387. #include <iostream>
  388. #include <string>
  389.  
  390.  
  391. using namespace std;
  392.  
  393.  
  394. string n_to_q(long n, long q) {
  395.     string result="";
  396.     if (n < q) {
  397.         if (n < 10 && n >= 0)
  398.             result += to_string(n);
  399.         else
  400.             result.push_back((char)(n + 55));
  401.     }
  402.  
  403.     else {
  404.         result = n_to_q(n / q, q);
  405.         if (n%q < 10 && n%q >= 0)
  406.             result += to_string(n%q);
  407.         else
  408.             result.push_back((char)(n%q + 55));
  409.     }
  410.     return result;
  411. }
  412.  
  413. int main()
  414. {
  415.  
  416.     long n, new_q, old_q;
  417.     string input;
  418.     system("chcp 1251");
  419.     cout << "Задача «Системы счисления»\n";
  420.     cout << "old_q = ";
  421.     cin >> old_q;
  422.     cout << "number = ";
  423.     cin >> input;
  424.  
  425.     n = 0;
  426.  
  427.     for (char i: input) {
  428.         n *= old_q;
  429.         if (isdigit(i))
  430.             n += ((int)(i - 48));
  431.         else
  432.             n += ((int)(toupper(i) - 55));
  433.     }
  434.     //cout << "n = "<<n<<"\n";
  435.     cout << "new_q = ";
  436.     cin >> new_q;
  437.     cout << n_to_q(n, new_q)<<"\n";
  438.  
  439. }
  440.  
  441.  
Advertisement
Comments
  • GrayMP
    1 year
    # text 0.12 KB | 0 0
    1. Кириллица - зло
    2. Не усложняй чтение (да и написание) себе и коллегам.
Add Comment
Please, Sign In to add comment
Advertisement