Advertisement
osipyonok

Untitled

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