Advertisement
khaiwen1111

mw lab 3

Jan 6th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. void display_header(void);
  9. void display_result(double atime, double ampere, double voltage);
  10. void display_max_result(double m_vol, double m_cur, double m_time);
  11. bool check_number(string str)
  12. {
  13.     for (int i = 0; i < str.length(); i++)
  14.     {
  15.         if (isdigit(str[i]) == false)
  16.         {
  17.             return false;
  18.         }
  19.         else
  20.             return true;
  21.     }
  22. }
  23.  
  24. int main()
  25. {
  26.     int j, counter;
  27.     string resistance, imax, frequency;
  28.     double r, v, i_max, f, t, max_current, max_time, max_voltage = 0, i, time, trig, deg, atime;
  29.     const double pi = 3.14159265359;
  30.     char again;
  31.     do
  32.     {  
  33.         cout << "Please ONLY enter Number"<<endl;
  34.         do
  35.         {
  36.             cout << "Enter your resistance value,R: ";
  37.             cin >> resistance;
  38.         } while (check_number(resistance)!=1);
  39.         r = stod(resistance);
  40.         do
  41.         {
  42.             cout << "Enter your Imax value,Im  (in Ampere): ";
  43.             cin >> imax;
  44.         } while (check_number(imax) != 1);
  45.         i_max = stod(imax);
  46.        
  47.         do
  48.         {
  49.             cout << "Enter your frequency,f (in frequency): ";
  50.             cin >> frequency;
  51.         } while (check_number(imax) != 1);
  52.         f = stod(frequency);
  53.        
  54.  
  55.         display_header();
  56.  
  57.  
  58.         for (t = 0; t <= 10; t++)
  59.         {
  60.             time = 100 * (t / f);
  61.             atime = time * 0.001;
  62.             trig = 2 * pi * f * (atime);
  63.             i = i_max * sin(trig);
  64.             v = i * r;
  65.  
  66.             display_result(time, i, v);
  67.  
  68.             if (v > max_voltage)
  69.             {
  70.                 max_voltage = v;
  71.                 max_time = time;
  72.                 max_current = i;
  73.             }
  74.         }
  75.  
  76.         display_max_result(max_voltage, max_voltage, max_time);
  77.         cout.flush();
  78.         cin.ignore();
  79.         cout << endl << "Do you wish to continue ? (Y/N)";
  80.         cin >> again;
  81.     } while (again == 'Y' || again == 'y');
  82.  
  83.     return 0;
  84.  
  85. }
  86. void display_header(void)
  87. {
  88.     cout << showpoint << right;
  89.     cout << endl << setw(12) << "Time(ms)" << setw(15) << "Current(A)" << setw(15) << "Voltage(V)" << endl;
  90. }
  91. void display_result(double atime, double ampere, double voltage)
  92. {
  93.     cout << fixed;
  94.     cout << setw(12) << setprecision(2) << atime << setw(15) << setprecision(4) << ampere << setw(15) << setprecision(4) << voltage << endl;
  95. }
  96. void display_max_result(double m_vol, double m_cur, double m_time)
  97. {
  98.     cout << endl << "Vmax is: " << m_vol << "V occurs with current " << m_cur << " at time " << m_time;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement