Advertisement
Guest User

Complex# Converter

a guest
Dec 6th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.07 KB | None | 0 0
  1. clc;
  2. clear;
  3.  
  4. mtlb_fprintf(" ");
  5.  
  6. mtlb_fprintf("*-------------------*");
  7. mtlb_fprintf("*                   *");
  8. mtlb_fprintf("* COMPLEX CONVERTER *");
  9. mtlb_fprintf("*                   *");
  10. mtlb_fprintf("*-------------------*");
  11.  
  12. mtlb_fprintf(" ");
  13.  
  14. flag = 1;
  15.  
  16. while flag == 1 then
  17.    
  18.     clear;
  19.  
  20.      mtlb_fprintf("Rectangular to Polar -> 1");    
  21.      mtlb_fprintf("Polar to Rectangular -> 2");
  22.      mtlb_fprintf("-----");
  23.      _option = input("> Which operation? (1|2) -> ", "s");
  24.    
  25.     mtlb_fprintf(" ");
  26.    
  27.     if _option=='1' then
  28.         // data for conversion to polar
  29.         _real = input("> Real part = ");
  30.         _imag = input("> Imaginary part = ");
  31.        
  32.         _mod = ((_real)^2 + (_imag)^2)^(1/2)
  33.         _angle = atand(_imag/_real)
  34.        
  35.         mtlb_fprintf(" ");
  36.        
  37.         mtlb_fprintf("- Polar Form: %.2f < %.2f° \n",_mod, _angle);
  38.        
  39.     elseif _option=='2' then  
  40.         // data for conversion to rectangular
  41.         _mod = input("> Module = ");
  42.         _angle = input("> Angle = ");
  43.        
  44.         _real = (_mod) * (cos(_angle*0.0174)) // cos operates with rad angle - multiplying for 3.14/180 = 0.0174 (converts degree to rad)
  45.         _imag = (_mod) * (sin(_angle*0.0174)) // sin operates with rad angle - multiplying for 3.14/180 = 0.0174 (converts degree to rad)
  46.      
  47.         mtlb_fprintf(" ");
  48.      
  49.         if _imag==0 | _imag>0 then
  50.             mtlb_fprintf("- Rectangular Form: %.2f + %.2fj\n", _real, _imag);
  51.         else
  52.             mtlb_fprintf("- Rectangular Form: %.2f - %.2fj\n", _real, _imag*(-1)); // adjusting the imaginary negative value to fit into the hard coded (-) signal
  53.             // abs function doesn't work well with the float precision
  54.         end
  55.        
  56.     end
  57.        
  58.     mtlb_fprintf(" ");    
  59.    
  60.     mtlb_fprintf("-------------");
  61.     answer = input(">> New operation? (y|n) -> ", "s")
  62.     if answer=="Y" | answer=="y" then
  63.         flag=1;
  64.         mtlb_fprintf(" ");
  65.     elseif answer=="N" | answer=="n" then
  66.         flag=0;
  67.         exit;
  68.     end
  69.    
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement