Porównywanie dużych liczb

0

Witam. Czy jest ktoś, kto orientuje się co trzeba poprawić w tym programie?

#include <iostream>
#include <string>
using namespace std;
int main()
{
string a, b,c;
while(cin>>a>>b>>c)
{
if(b=="==")
if(a.length()==c.length())
{int i=0;
 while(a[i]==c[i] and i<a.length()) i++;
 if (i==a.length()) cout<<1<<endl;
   else cout<<0<<endl;
}
 else cout<<0<<endl;


if(b=="<=")
if(a.length()>c.length())
cout<<0<<endl;
else
if(a.length()<c.length())
cout<<1<<endl;
{int i=0;
 while(a[i]<=c[i] and i<a.length()) i++;
 if (i<=a.length()) cout<<1<<endl;
   else cout<<0<<endl;
}
if(b==">=")
if(a.length()<c.length())
 cout<<0<<endl;
else
  if(a.length()>c.length())
cout<<1<<endl;
 else
{
 int i=0;
while(a[i]>=c[i] and i<a.length()) 
i++;
if (i>=a.length())    
cout<<1<<endl;
else cout<<0<<endl;
            }
if(b=="!=")
if(a.length()!=c.length())
cout<<1<<endl;
else
{
int i=0;
 while(a[i]==c[i] and i<a.length()) i++;
 if (i==a.length()) cout<<0<<endl;
   else cout<<1<<endl;
}
}
    return 0;
}
6

Z całą pewnością formatowanie...

0
  1. Program nie działa sprawdź: 0007 == 7 lub 0001 >= 100
  2. Napisz funkcję która porównuje (poprawnie) dwie liczby int cmp(const string &a,const string &b) i zwraca: 0 - a=b; dodatnia liczba - a>b; ujemna liczba - a<b
  3. Zrób mapę z lambdami map<string,bool (*fun)(int)> M { { "==", [](int cmp)->int { return cmp==0; } }, { "<=", [](int cmp)->int { return cmp<=0; } }, ... };
  4. Odpalasz: cout<<M[b](cmp(a,c))<<endl;
0

Mialem napisac o formatowaniu, ale skoro mnie ktos juz ubiegl...

Dodaj unit testy.
W visual studio 2013 masz je wbudowane, nie trzeba nic sciagac. Ew boost.test w trybie naglowkowym. Z tym ze bedziesz musial raczej zrobic osobny modul...

1 użytkowników online, w tym zalogowanych: 0, gości: 1