Advertisement
osipyonok

Untitled

May 31st, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. // lab3.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <map>
  9. #include <fstream>
  10. #include <sstream>
  11. #include <conio.h>
  12. #include <algorithm>
  13. #include <iomanip>
  14.  
  15. using namespace std;
  16.  
  17. #define EXP 13 //sizeof характеристика
  18. #define MAN 28 //sizeof мантиса
  19. #define BIOS DecToBin(pow(2,(double)(EXP-1))-1) //BIOS
  20. #define NotNPOS(a,b)  a == string::npos ? b : a
  21. #define max(a,b) a > b ? a : b
  22.  
  23. double StD10(string s){
  24.     double x;
  25.     istringstream (s) >> x;
  26. //  cout << "x: " << fixed << pow(10,x) << endl;
  27.     return pow(10,x);
  28. }
  29.  
  30. string StD10v2(string s){
  31.     int x = 0;
  32.     string ans = "";
  33.     istringstream (s) >> x;
  34.     if(x == 0) return "1";
  35.     if(x > 0){
  36.         ans = "1";
  37.         for(int i = 0 ; i < x ; ++i)
  38.             ans += "0";
  39.     }else{
  40.         ans = "1";
  41.         for(int i = -1 ; i > x ; --i)
  42.             ans = "0" + ans;
  43.         ans = "0." + ans;
  44.     }
  45.     return ans;
  46. }
  47. //102 1.02*10^2 1.010101011 * 2^
  48. double StD(string s){
  49.     double x;
  50.     istringstream (s) >> x;
  51.     return x;
  52. }
  53.  
  54. string DtS(double x){
  55.     ostringstream st;
  56.     st << x;
  57.     return st.str();
  58. }
  59.  
  60. string Reverse(string s){
  61.     for(int i = 0 ; i < EXP ; ++i)
  62.         s[i]= s[i]=='1'?'0':'1';
  63.     return s;
  64. }
  65.  
  66. string GetDefault(bool key, int base){
  67.     string ans = key?"1":"0";
  68.     while(ans.length()<base)
  69.         ans = "0" + ans;
  70.     return ans;
  71. }
  72.  
  73. string Add(string s1, string s2){
  74.     bool m=0;
  75.     string ans = GetDefault(0,EXP);
  76.     for(int i = EXP-1 ; i>=0 ; --i){
  77.         if(s1[i]=='0' && s2[i]=='0'){ ans[i] = m?'1':'0'; m = 0;}else
  78.         if(s1[i]!=s2[i]){ ans[i] = m?'0':'1'; m = m?1:0;}else
  79.         if(s1[i]=='1' && s2[i]=='1'){ ans[i] = m?'1':'0'; m = 1;}
  80.     }
  81.     return ans;
  82. }
  83.  
  84. string Complement(string s){
  85.     return(Add(Reverse(s),GetDefault(1,EXP)));
  86. }
  87.  
  88. string HunToBin(int n){
  89.     double d = n / pow(10,(double)DtS((double)n).length());
  90. //  cout << d << " = d " << DtS(floor(d*2))<< endl;
  91.     string ans = "";
  92. //  cout << ans << endl;
  93.     for( int i = 0 ; i < MAN ; ++i ){
  94.         ans = DtS(floor(d*2)) + ans;
  95.         d = d*2 - floor(d*2);
  96.     }
  97.     reverse(ans.begin(),ans.end());
  98.     return ans;
  99. }
  100.  
  101. string DecToBin(int n){
  102.     string ans = "";
  103.     if(n>=0){
  104.         while(n != 0){
  105.             ans = ((n % 2)?"1":"0") + ans;
  106.             n/=2;
  107.         }
  108.         while(ans.length()<EXP)
  109.             ans = "0" + ans;
  110.     }else
  111.         ans = Complement(DecToBin(abs(n)));
  112.     return ans;
  113. }
  114.  
  115. int Count1(string &a){
  116.     int ans = 0;
  117.     for( int i = 0 ; i < a.length() ; ++i )
  118.         ans += a[i] == '1';
  119.     return ans;
  120. }
  121.  
  122. void ToDouble(string &a, string &b){
  123.     a = DecToBin(abs((int)StD(a)));
  124.     b = HunToBin(abs((int)StD(b)));
  125. //  cout << b << "dsds ";
  126.     int exp = 0;
  127. //  cout << Count1(a) << " " << b << endl;
  128.     while(Count1(a)>1 || (Count1(a)==1&&a[a.length()-1]!='1')){
  129.         b = a[a.length()-1] + b;
  130.         a = a.substr(0,a.length()-1);
  131.         a = "0" + a;
  132.         ++exp;
  133.     }
  134.     while(Count1(a)<1){
  135.         a = a + b[0];
  136.         b = b.substr(1);
  137.         b += "0";
  138.         --exp;
  139.     }
  140.     while(b.length()<MAN)
  141.         b += "0";
  142.     b = b.substr(0,MAN);
  143.     a = Add(DecToBin(exp),BIOS);
  144. }//1.10001 k +  BIOS ,   101.100101  = 1.01100 * 2^2
  145.  
  146. bool CheckStandart(string n){
  147.     int i;
  148.     bool ck = 0;
  149.     if(n=="+infinity"){ cout << setw(17) << "+infinity       " ;
  150.         cout << "0 1"; for (i = 1; i < EXP; ++i){ cout << "1"; } cout << " 0 ";   for (i = 0; i < MAN; ++i){ cout << "0"; } cout << endl; return 0;
  151.     }else if(n=="-infinity"){ cout << setw(17) << "-infinity       " ;
  152.         cout << "1 1"; for (i = 1; i < EXP; ++i){ cout << "1"; } cout << " 0 ";   for (i = 0; i < MAN; ++i){ cout << "0"; } cout << endl; return 0;
  153.     }else if(n=="NaN"){ cout << setw(17) << "NaN             " ;
  154.         cout << "0 1"; for (i = 1; i < EXP; ++i){ cout << "1"; } cout << " 1 01"; for (i = 2; i < MAN; ++i){ cout << "0"; } cout << endl; return 0;
  155.     }else if(n=="MinAbs"){ cout << setw(17) << "MinAbs          " ;
  156.         cout << "0 0"; for (i = 1; i < EXP; ++i){ cout << "0"; } cout << " 1 ";   for (i = 0; i < MAN; ++i){ cout << "0"; } cout << endl; return 0;
  157.     }else if(n=="MaxPositive"){ cout << setw(17) << "MaxPositive     " ;
  158.         cout << "0 0"; for (i = 1; i < EXP; ++i){ cout << "1"; } cout << " 1 ";   for (i = 0; i < MAN; ++i){ cout << "1"; } cout << endl; return 0;
  159.     }else if(n=="MinNegative"){ cout << setw(17) << "MinNegative     " ;
  160.         cout << "1 0"; for (i = 1; i < EXP; ++i){ cout << "1"; } cout << " 1 ";   for (i = 0; i < MAN; ++i){ cout << "1"; } cout << endl; return 0;
  161.     }else if(n=="NotNormalisable"){ cout << setw(17) << "NotNormalisable " ;
  162.         cout << "0 0"; for (i = 1; i < EXP; ++i){ cout << "0"; } cout << " 0 ";   for (i = 0; i < MAN; ++i){ cout << "1"; } cout << endl; return 0;
  163.     }else if(n=="0"){ cout << setw(17) << "0               " ;
  164.         cout << "0 0"; for (i = 1; i < EXP; ++i){ cout << "0"; } cout << " 0 ";   for (i = 0; i < MAN; ++i){ cout << "0"; } cout << endl; return 0;
  165.     }
  166.     return 1;
  167. }
  168.  
  169. void InitProgram(){
  170.     CheckStandart("+infinity");
  171.     CheckStandart("-infinity");
  172.     CheckStandart("NaN");
  173.     CheckStandart("MinAbs");
  174.     CheckStandart("MaxPositive");
  175.     CheckStandart("MinNegative");
  176.     CheckStandart("NotNormalisable");
  177.  
  178. }
  179.  
  180. string MyMultiply(string a, string b){
  181.     if(a == "0" || a == "+0" || a == "-0") return "0";
  182.     bool ck = a[0] == '-' ? 1 : 0;
  183.     string ans = ck ? a.substr(1) : a;
  184.     if(b[0] == '1'){
  185.         int k = max(0,b.length() - 1);
  186.         if(ans.find('.')==string::npos && ans.find(',')==string::npos){
  187.             for(int i = 0 ; i < k ; ++i)
  188.                 ans += "0";
  189.         }else{         
  190.         size_t pos;
  191.         size_t pos1 = ans.find('.');
  192.         size_t pos2 = ans.find(',');
  193.         if(ans.find('.')!=string::npos || ans.find(',')!=string::npos){    
  194.             pos = NotNPOS(pos1,pos2);
  195.             for(int i = 0 ; i < k ; ++i){
  196.                 if(pos<ans.length()-1){
  197.                     char t;
  198.                     t = ans[pos];
  199.                     ans[pos] = ans[pos + 1];
  200.                     ans[pos + 1] = t;
  201.                     ++pos;
  202.                 }
  203.                 if(pos==ans.length()-1){
  204.                     ans = ans.substr(0,ans.length()-1);
  205.                     ++pos;
  206.                     continue;
  207.                 }
  208.                 if(pos>ans.length()-1){
  209.                     ++pos;
  210.                     ans += "0";
  211.                 }
  212.             }
  213.  
  214.         }
  215.         }
  216.  
  217.     }else if(b[0] == '0'){
  218.         int pos;
  219.         int pos1 = ans.find('.');
  220.         int pos2 = ans.find(',');
  221.         int k = max(0,b.length() - 2);
  222.         if(ans.find('.')==string::npos && ans.find(',')==string::npos){
  223.             ans += ".0";
  224.             pos1 = ans.find('.');
  225.             pos2 = ans.find(',');
  226.         }
  227.         pos = NotNPOS(pos1,pos2);
  228.         for(int i = 0 ; i < k ; ++i){
  229.             if(pos>0){
  230.                 char t;
  231.                 t = ans[pos];
  232.                 ans[pos] = ans[pos - 1];
  233.                 ans[pos - 1] = t;
  234.                 --pos;
  235.             }
  236.             if(ans[0]=='.' || ans[0]==','){
  237.                 ans = "0" + ans;
  238.                 --pos;
  239.                 continue;
  240.             }
  241.             if(pos<0){
  242.                 --pos;
  243.                 ans = "0.0"+ans.substr(2);
  244.             }
  245.         }
  246.        
  247.     }
  248.     if(ans.length()>1)
  249.         while(ans[0] == '0' && ans[1]!='.' && ans[1]!=',' )
  250.             ans = ans.substr(1);
  251.     while(ans[ans.size()-1] == '0' && !( ans.find('.')==string::npos && ans.find(',')==string::npos ) )
  252.         ans = ans.substr(0,ans.size()-1);
  253.     if(ans[ans.size()-1] == '.' || ans[ans.size()-1] == ',')
  254.         ans = ans.substr(0,ans.size()-1);
  255.     return ( ck ? "-" : "") + ans;
  256. }
  257.  
  258. int main()
  259. {
  260. //  cout << MyMultiply("-0.2","10") << endl;
  261. //  cout << StD10v2("-0") << endl;
  262.     InitProgram();
  263.     string n;
  264.     while(cin >> n){
  265.         if(StD(n)>2147483647.0)n = "+infinity";
  266.         else if(StD(n)<-2147483647.0)n = "-infinity";
  267.         if(CheckStandart(n)){//2*10^1
  268.         transform(n.begin(),n.end(),n.begin(),::tolower);
  269.         string a = "0" , b = "0";
  270.         bool sign , imp;
  271.         if(n.find('e')!=string::npos){
  272.             a = n.substr(0,n.find('e'));
  273.             b = n.substr(n.find('e')+1);
  274.             if(b.find('.')!=string::npos || b.find(',')!=string::npos){
  275.                 CheckStandart("NaN");
  276.                 continue;
  277.             }
  278.             //double tmp = StD10(b);
  279.             //cout << "tmp " << tmp << endl;
  280.             b = StD10v2(b);
  281.             n = MyMultiply(a,b);//DtS(StD(a)*StD(b));
  282.         }
  283.         a = "0" , b = "0";
  284.         if(n.find('.')!=string::npos){
  285.             a = n.substr(0,n.find('.'));
  286.             b = n.substr(n.find('.')+1);
  287.         }else if(n.find(',')!=string::npos){
  288.             a = n.substr(0,n.find(','));
  289.             b = n.substr(n.find(',')+1);
  290.         }else a = n;//424.5252
  291.         sign = a[0] == '-';//1.0101
  292.         imp = 1;
  293.         cout << "Your input: " << a << "." << b << endl;
  294.         ToDouble(a,b);
  295.         cout << "                 "<< sign << " " << a << " " << imp << " " << b << endl;
  296.         }
  297.     }
  298.     getch();
  299.     return 0;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement