Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab3.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <map>
- #include <fstream>
- #include <sstream>
- #include <conio.h>
- #include <algorithm>
- #include <iomanip>
- using namespace std;
- #define EXP 13 //sizeof характеристика
- #define MAN 28 //sizeof мантиса
- #define BIOS DecToBin(pow(2,(double)(EXP-1))-1) //BIOS
- #define NotNPOS(a,b) a == string::npos ? b : a
- #define max(a,b) a > b ? a : b
- double StD10(string s){
- double x;
- istringstream (s) >> x;
- // cout << "x: " << fixed << pow(10,x) << endl;
- return pow(10,x);
- }
- string StD10v2(string s){
- int x = 0;
- string ans = "";
- istringstream (s) >> x;
- if(x == 0) return "1";
- if(x > 0){
- ans = "1";
- for(int i = 0 ; i < x ; ++i)
- ans += "0";
- }else{
- ans = "1";
- for(int i = -1 ; i > x ; --i)
- ans = "0" + ans;
- ans = "0." + ans;
- }
- return ans;
- }
- //102 1.02*10^2 1.010101011 * 2^
- double StD(string s){
- double x;
- istringstream (s) >> x;
- return x;
- }
- string DtS(double x){
- ostringstream st;
- st << x;
- return st.str();
- }
- string Reverse(string s){
- for(int i = 0 ; i < EXP ; ++i)
- s[i]= s[i]=='1'?'0':'1';
- return s;
- }
- string GetDefault(bool key, int base){
- string ans = key?"1":"0";
- while(ans.length()<base)
- ans = "0" + ans;
- return ans;
- }
- string Add(string s1, string s2){
- bool m=0;
- string ans = GetDefault(0,EXP);
- for(int i = EXP-1 ; i>=0 ; --i){
- if(s1[i]=='0' && s2[i]=='0'){ ans[i] = m?'1':'0'; m = 0;}else
- if(s1[i]!=s2[i]){ ans[i] = m?'0':'1'; m = m?1:0;}else
- if(s1[i]=='1' && s2[i]=='1'){ ans[i] = m?'1':'0'; m = 1;}
- }
- return ans;
- }
- string Complement(string s){
- return(Add(Reverse(s),GetDefault(1,EXP)));
- }
- string HunToBin(int n){
- double d = n / pow(10,(double)DtS((double)n).length());
- // cout << d << " = d " << DtS(floor(d*2))<< endl;
- string ans = "";
- // cout << ans << endl;
- for( int i = 0 ; i < MAN ; ++i ){
- ans = DtS(floor(d*2)) + ans;
- d = d*2 - floor(d*2);
- }
- reverse(ans.begin(),ans.end());
- return ans;
- }
- string DecToBin(int n){
- string ans = "";
- if(n>=0){
- while(n != 0){
- ans = ((n % 2)?"1":"0") + ans;
- n/=2;
- }
- while(ans.length()<EXP)
- ans = "0" + ans;
- }else
- ans = Complement(DecToBin(abs(n)));
- return ans;
- }
- int Count1(string &a){
- int ans = 0;
- for( int i = 0 ; i < a.length() ; ++i )
- ans += a[i] == '1';
- return ans;
- }
- void ToDouble(string &a, string &b){
- a = DecToBin(abs((int)StD(a)));
- b = HunToBin(abs((int)StD(b)));
- // cout << b << "dsds ";
- int exp = 0;
- // cout << Count1(a) << " " << b << endl;
- while(Count1(a)>1 || (Count1(a)==1&&a[a.length()-1]!='1')){
- b = a[a.length()-1] + b;
- a = a.substr(0,a.length()-1);
- a = "0" + a;
- ++exp;
- }
- while(Count1(a)<1){
- a = a + b[0];
- b = b.substr(1);
- b += "0";
- --exp;
- }
- while(b.length()<MAN)
- b += "0";
- b = b.substr(0,MAN);
- a = Add(DecToBin(exp),BIOS);
- }//1.10001 k + BIOS , 101.100101 = 1.01100 * 2^2
- bool CheckStandart(string n){
- int i;
- bool ck = 0;
- if(n=="+infinity"){ cout << setw(17) << "+infinity " ;
- 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;
- }else if(n=="-infinity"){ cout << setw(17) << "-infinity " ;
- 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;
- }else if(n=="NaN"){ cout << setw(17) << "NaN " ;
- 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;
- }else if(n=="MinAbs"){ cout << setw(17) << "MinAbs " ;
- 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;
- }else if(n=="MaxPositive"){ cout << setw(17) << "MaxPositive " ;
- 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;
- }else if(n=="MinNegative"){ cout << setw(17) << "MinNegative " ;
- 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;
- }else if(n=="NotNormalisable"){ cout << setw(17) << "NotNormalisable " ;
- 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;
- }else if(n=="0"){ cout << setw(17) << "0 " ;
- 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;
- }
- return 1;
- }
- void InitProgram(){
- CheckStandart("+infinity");
- CheckStandart("-infinity");
- CheckStandart("NaN");
- CheckStandart("MinAbs");
- CheckStandart("MaxPositive");
- CheckStandart("MinNegative");
- CheckStandart("NotNormalisable");
- }
- string MyMultiply(string a, string b){
- if(a == "0" || a == "+0" || a == "-0") return "0";
- bool ck = a[0] == '-' ? 1 : 0;
- string ans = ck ? a.substr(1) : a;
- if(b[0] == '1'){
- int k = max(0,b.length() - 1);
- if(ans.find('.')==string::npos && ans.find(',')==string::npos){
- for(int i = 0 ; i < k ; ++i)
- ans += "0";
- }else{
- size_t pos;
- size_t pos1 = ans.find('.');
- size_t pos2 = ans.find(',');
- if(ans.find('.')!=string::npos || ans.find(',')!=string::npos){
- pos = NotNPOS(pos1,pos2);
- for(int i = 0 ; i < k ; ++i){
- if(pos<ans.length()-1){
- char t;
- t = ans[pos];
- ans[pos] = ans[pos + 1];
- ans[pos + 1] = t;
- ++pos;
- }
- if(pos==ans.length()-1){
- ans = ans.substr(0,ans.length()-1);
- ++pos;
- continue;
- }
- if(pos>ans.length()-1){
- ++pos;
- ans += "0";
- }
- }
- }
- }
- }else if(b[0] == '0'){
- int pos;
- int pos1 = ans.find('.');
- int pos2 = ans.find(',');
- int k = max(0,b.length() - 2);
- if(ans.find('.')==string::npos && ans.find(',')==string::npos){
- ans += ".0";
- pos1 = ans.find('.');
- pos2 = ans.find(',');
- }
- pos = NotNPOS(pos1,pos2);
- for(int i = 0 ; i < k ; ++i){
- if(pos>0){
- char t;
- t = ans[pos];
- ans[pos] = ans[pos - 1];
- ans[pos - 1] = t;
- --pos;
- }
- if(ans[0]=='.' || ans[0]==','){
- ans = "0" + ans;
- --pos;
- continue;
- }
- if(pos<0){
- --pos;
- ans = "0.0"+ans.substr(2);
- }
- }
- }
- if(ans.length()>1)
- while(ans[0] == '0' && ans[1]!='.' && ans[1]!=',' )
- ans = ans.substr(1);
- while(ans[ans.size()-1] == '0' && !( ans.find('.')==string::npos && ans.find(',')==string::npos ) )
- ans = ans.substr(0,ans.size()-1);
- if(ans[ans.size()-1] == '.' || ans[ans.size()-1] == ',')
- ans = ans.substr(0,ans.size()-1);
- return ( ck ? "-" : "") + ans;
- }
- int main()
- {
- // cout << MyMultiply("-0.2","10") << endl;
- // cout << StD10v2("-0") << endl;
- InitProgram();
- string n;
- while(cin >> n){
- if(StD(n)>2147483647.0)n = "+infinity";
- else if(StD(n)<-2147483647.0)n = "-infinity";
- if(CheckStandart(n)){//2*10^1
- transform(n.begin(),n.end(),n.begin(),::tolower);
- string a = "0" , b = "0";
- bool sign , imp;
- if(n.find('e')!=string::npos){
- a = n.substr(0,n.find('e'));
- b = n.substr(n.find('e')+1);
- if(b.find('.')!=string::npos || b.find(',')!=string::npos){
- CheckStandart("NaN");
- continue;
- }
- //double tmp = StD10(b);
- //cout << "tmp " << tmp << endl;
- b = StD10v2(b);
- n = MyMultiply(a,b);//DtS(StD(a)*StD(b));
- }
- a = "0" , b = "0";
- if(n.find('.')!=string::npos){
- a = n.substr(0,n.find('.'));
- b = n.substr(n.find('.')+1);
- }else if(n.find(',')!=string::npos){
- a = n.substr(0,n.find(','));
- b = n.substr(n.find(',')+1);
- }else a = n;//424.5252
- sign = a[0] == '-';//1.0101
- imp = 1;
- cout << "Your input: " << a << "." << b << endl;
- ToDouble(a,b);
- cout << " "<< sign << " " << a << " " << imp << " " << b << endl;
- }
- }
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement