Advertisement
Guest User

Untitled

a guest
Dec 27th, 2013
18
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. // task1
  9.  
  10. void findRoot(double val, double &res1, double &res2, double &res3) {
  11.     if (val < 0) {
  12.         res1 = res2 = res3 = -1;
  13.         return;
  14.     }  
  15.     res1 = pow(val, 1.0/2);
  16.     res2 = pow(val, 1.0/3);
  17.     res3 = pow(val, 1.0/4);
  18. }
  19.  
  20. void task1()
  21. {
  22.     double val;
  23.     double res1, res2, res3;
  24.     cout << "Input val:" << endl;
  25.     cin >> val;
  26.    
  27.     findRoot(val, res1, res2, res3);
  28.     cout << val << " " << res1 << " " << res2 << " " << res3 << endl;  
  29. }
  30.  
  31. // task2
  32.  
  33. int power(int n, int k) {
  34.   if (k == 0)
  35.     return 1;
  36.   else if (k % 2)
  37.     return n * power(n, k - 1);
  38.   else
  39.     return power(n * n, k / 2);
  40. }
  41.  
  42. void task2() {
  43.   int n, k;
  44.  
  45.   cout << "Input n: "; cin >> n;
  46.   cout << "Input k: "; cin >> k;
  47.  
  48.   if (k < 0)
  49.     cout << "k < 0";
  50.   else
  51.     cout << "n^k = " << power(n, k);
  52.  
  53.   cout << endl;
  54. }
  55.  
  56. // task4
  57.  
  58. double eval(const string &str, const vector<double> &args) {
  59.   double acc = 0, x;
  60.   int arg = 0;
  61.   enum { ADD, SUB, MUL, DIV, NOP, ERR } op = NOP;
  62.  
  63.   for (int i = 0; i < str.size(); i++) {
  64.     switch (str[i]) {
  65.     case 'i': case 'd':
  66.       if (arg >= args.size()) {
  67.         cout << "Need more args";
  68.         exit(1);
  69.       }
  70.        
  71.       x = args[arg++];
  72.      
  73.       switch (op) {
  74.       case ADD: acc += x; break;
  75.       case SUB: acc -= x; break;
  76.       case MUL: acc *= x; break;
  77.       case DIV: acc /= x; break;
  78.       case NOP: acc =  x; break;
  79.       case ERR: cout << "Syntax error";
  80.                 exit(1); }
  81.      
  82.       op = ERR; break;
  83.      
  84.     case '+': op = ADD; break;
  85.     case '-': op = SUB; break;
  86.     case '/': op = DIV; break;
  87.     case '*': op = MUL; break;
  88.     default : op = ERR; }
  89.   }
  90.  
  91.   return acc;
  92. }
  93.  
  94. void task4() {
  95.   string expr, line;
  96.   vector<double> args;
  97.  
  98.   cout << "Input expr: "; getline(cin, expr);
  99.   cout << "Input args: ";
  100.   while (getline(cin, line), !line.empty())
  101.     args.push_back( atof(line.c_str()) );
  102.  
  103.   cout << "Result: " << eval(expr, args) << endl;
  104. }
  105.  
  106. // task5
  107.  
  108. template <typename T>
  109. const T& max_(const T &a, const T &b) {
  110.   return a > b ? a : b;
  111. }
  112.  
  113. template <typename T>
  114. const T& max_(const T &a, const T &b, const T &c) {
  115.   return max(max(a, b), c);
  116. }
  117.  
  118. void task5() {
  119.   int a, b, c;
  120.  
  121.   cout << "Input a: "; cin >> a;
  122.   cout << "Input b: "; cin >> b;
  123.   cout << "Input c: "; cin >> c;
  124.  
  125.   cout << "Max: " << max_(a, b, c);
  126. }
  127.  
  128. int main() {
  129.     int i;
  130.  
  131.     do {
  132.       cout << "Input task number (1-5):" << endl;
  133.       cin >> i;
  134.       switch(i) {
  135.       case 1:
  136.         task1(); break;
  137.       case 2:
  138.         task2(); break;
  139.       case 3:
  140.         //task3(); break;
  141.       case 4:
  142.         task4(); break;
  143.       case 5:
  144.         task5(); break;
  145.       case 0:
  146.         cout << "Program is done" << endl; break;
  147.       default:
  148.         cout << "Invalid option" << endl; }
  149.     } while(i != 0);
  150.    
  151.     return 0;
  152. }
Advertisement
RAW Paste Data Copied
Advertisement