Advertisement
mamamaria

стринги

Mar 10th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. stringstream str;
  6. double q = 0.2;
  7. double p = 0.9;
  8. int n = 12;
  9. int counterLB = 0;
  10. int counterRB = 0;
  11.  
  12. void generatorLB() {
  13.  
  14.     if (rand() < p * RAND_MAX) {
  15.         str << "(";
  16.         counterLB++;
  17.     }
  18. }
  19.  
  20. void generatorRB() {
  21.  
  22.     if (rand() < q * RAND_MAX) {
  23.         str << ")";
  24.         counterRB++;
  25.     }
  26. }
  27.  
  28. void generatorOp() {
  29.     int op = rand() % 4;
  30.     switch (op) {
  31.     case 0:
  32.         str << "+";
  33.         break;
  34.     case 1:
  35.         str << "-";
  36.         break;
  37.     case 2:
  38.         str << "*";
  39.         break;
  40.     case 3:
  41.         str << "/";
  42.         break;
  43.     }
  44. }
  45.  
  46. void generate() {
  47.    
  48.     int cnt = 0;
  49.     str << "x"; str << (cnt + 1);
  50.     cnt++;
  51.  
  52.     while (cnt != n)
  53.     {  
  54.        
  55.         generatorOp();
  56.         generatorLB();
  57.         str << "x"; str << (cnt + 1);
  58.         cnt++;
  59.         if (counterLB != 0)
  60.             generatorRB();
  61.        
  62.     }
  63.     while (counterLB>counterRB)  generatorRB();
  64.  
  65. }
  66.  
  67. int main()
  68. {
  69.     setlocale(LC_ALL, "Russian");
  70.     generate();
  71.     cout << str.str();
  72.  
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement