Advertisement
StoneHaos

misha1

Oct 6th, 2020 (edited)
1,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <utility>
  4. #include <string>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. struct Mystr {
  10.     double kof;
  11.     int stepen;
  12. };
  13.  
  14. list<Mystr>::iterator offset(list<Mystr>::iterator iter, int n) {
  15.     for (int i = 0; i < n; ++ i)
  16.         iter++;
  17.     return iter;
  18. }
  19.  
  20. void Diff(list<Mystr> p, list<Mystr> &q) {
  21.     q = p;
  22.     for (auto i = q.begin(); i != q.end(); ++ i) {
  23.         i->kof *= i->stepen;
  24.         i->stepen --;
  25.     }
  26.     int cnt = 0;
  27.     for (auto i = q.begin(); i != q.end();) {
  28.         if (i->stepen == -1) {
  29.             q.erase(i);
  30.             i = offset(q.begin(), cnt);
  31.         }
  32.         else
  33.             ++ i; ++ cnt;
  34.     }
  35. }
  36.  
  37. void Print(list<Mystr> p) {
  38.     bool flag = true;
  39.     for (auto i = p.begin(); i != p.end(); ++ i) {
  40.         if (i->kof == 0)
  41.             continue;
  42.         if (!flag && i->kof >= 0)
  43.             cout << "+";
  44.  
  45.         if (i->stepen == 0)
  46.             cout << i->kof;
  47.         else
  48.             cout << i->kof << "*" << "x" << "^" << i->stepen;
  49.         flag = false;
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     srand(time(NULL));
  56.     list <Mystr> Mylist, Mylist_;
  57.     list <Mystr>::iterator it;
  58.     int n, k;
  59.     double a;
  60.     cin >> n;
  61.     Mystr p;
  62.  
  63.     for (int i = 0; i < n; i++) {
  64.         p.kof = rand() % 40 - 20;
  65.         p.stepen = rand() % 10;
  66.         Mylist.push_back(p);
  67.     }
  68.  
  69.     Print(Mylist);
  70.     cout << "\n";
  71.     Diff(Mylist, Mylist_);
  72.     Print(Mylist_);
  73.     cout << "\n";
  74.  
  75.     return 0;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement