Porównywanie dużych liczb

0

Dla ośmiu przypadków testowych kod nie działa. Nie mogę ich odnaleźć. Mógłby ktoś mi pomóc?

#include <iostream>

using namespace std;
void rowne(string l1,string l2)
{
    int a=l1.size(),b=l2.size();
    bool tak= true;
    if(a!=b)
        tak=false;
    else
    {
        for (int i=0; i<a; i++)
            if((int)l1[i]!=(int)l2[i])
            {
                tak=false;
                break;
            }
    }
    if (tak == true)
        cout<< "TAK";
    else cout<<"NIE";
}
void rozne(string l1,string l2)
{
     int a=l1.size(),b=l2.size();
    bool tak= true;
    if(a!=b)
        tak=false;
    else
    {
        for (int i=0; i<a; i++)
            if((int)l1[i]!=(int)l2[i])
            {
                tak=false;
                break;
            }
    }
    if (tak == false)
        cout<< "TAK";
    else cout<<"NIE";
}
void mniejsze(string l1,string l2)
{
    int a=l1.size(),b=l2.size(),w=0;
    bool tak= true;
    if(a!=b)
    {
        if (a>b)
            tak=false;

    }
    else
    {
        for (int i=0; i<a; i++)

            {if((int)l1[i]>(int)l2[i])
            {
                tak=false;
            }
            else if ((int)l1[i]==(int)l2[i])
                w++;}
            if (w==a)
                tak= false;

     }

    if (tak == true)
        cout<< "TAK";
    else cout<<"NIE";
}
void wieksze(string l1,string l2)
{
    int a=l1.size(),b=l2.size(),w=0;
    bool tak= true;
    if(a!=b)
    {
        if (a<b)
            tak=false;

    }
    else
    {
        for (int i=0; i<a; i++)
            {if((int)l1[i]<(int)l2[i])
            {
                tak=false;

            }
             else if ((int)l1[i]==(int)l2[i])
                w++;}
            if (w==a)
                tak= false;
    }

    if (tak == true)
        cout<< "TAK";
    else cout<<"NIE";
}
void wieksze_r(string l1,string l2)
{
    int a=l1.size(),b=l2.size();
    bool tak= true;
    if(a!=b)
    {
        if (a<b)
            tak=false;

    }
    else
    {
        for (int i=0; i<a; i++)
            if((int)l1[i]<(int)l2[i])
            {
                tak=false;
                break;
            }
    }
    if (tak == true)
        cout<< "TAK";
    else cout<<"NIE";
}

void mniejsze_r(string l1,string l2)
{
     int a=l1.size(),b=l2.size();
    bool tak= true;
    if(a!=b)
    {
        if (a>b)
            tak=false;

    }
    else
    {
        for (int i=0; i<a; i++)

            if((int)l1[i]>(int)l2[i])
            {
                tak=false;
            }
    }
    if (tak == true)
        cout<< "TAK";
    else cout<<"NIE";
}
int main()
{
    string l1,l2,znak;
    cin>> l1>>znak>>l2;
    if (znak=="==")
        rowne(l1,l2);
    if (znak=="!=")
        rozne(l1,l2);
    if (znak=="<")
        mniejsze(l1,l2);
    if (znak==">")
        wieksze(l1,l2);
    if (znak==">=")
        wieksze_r(l1,l2);
    if (znak=="<=")
        mniejsze_r(l1,l2);

    return 0;
}

 
2

A cos w tym guscie nie moze byc?

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

enum class Ordering {
  Lt,
  Eq,
  Gt
};

class OrderingH {
public:
  static Ordering fromInt(int i) {
    if(i <  0) return Ordering::Lt;
    if(i == 0) return Ordering::Eq;
    if(i >  0) return Ordering::Gt;
  }
};

template<typename T>
class Ord {
public:
  bool operator==(const T t2) const { return (compare(t2) == Ordering::Eq); }
  bool operator!=(const T t2) const { return (compare(t2) != Ordering::Eq); }
  bool operator< (const T t2) const { return (compare(t2) == Ordering::Lt); }
  bool operator<=(const T t2) const { return (compare(t2) != Ordering::Gt); }
  bool operator> (const T t2) const { return (compare(t2) == Ordering::Gt); }
  bool operator>=(const T t2) const { return (compare(t2) != Ordering::Lt); }

protected:
  virtual Ordering compare(const T t2) const = 0;
};

class BigNum : public Ord<BigNum> {
public:
  static BigNum Create(string strNum) {
    if(all_of(strNum.cbegin(), strNum.cend(), [](const char c) { return isdigit(c); })) {
      strNum.erase(0, strNum.find_first_not_of('0'));
      return BigNum(strNum);
    }
    else {
      return BigNum("");
    }
  }

private:
  BigNum(string str): repr(str) {}

  Ordering compare(const BigNum num2) const override {
    for(auto it1 = repr.cbegin(), it2 = num2.repr.cbegin();
             it1 != repr.cend() || it2 != num2.repr.cend();
             ++it1, ++it2) {
      Ordering ord = OrderingH::fromInt(*it1 - *it2);
      if(ord != Ordering::Eq)
        return ord;
    }
    return OrderingH::fromInt(repr.size() - num2.repr.size());
  }

  const string repr;
};

int main() {
  BigNum b1 = BigNum::Create("1234124321312312312312312312312312312312312312312312312312");
  BigNum b2 = BigNum::Create("000234124321312312312312312312312312312312312312312312312312");
  BigNum b3 = BigNum::Create("1234124321312312312312312312312312312312312312312312312312");

  cout << (b1 <  b2) << "\n";
  cout << (b1 >  b2) << "\n";
  cout << (b1 == b2) << "\n";
  cout << (b1 != b2) << "\n";
  cout << (b3 == b1) << "\n";
  return 0;
}
3

Czemu ludzie lubią sobie utrudniać :/

#include <iostream>
#include <string>

int cmp(const string &a,const string &b)
  {
   int ra=a.size(),rb=b.size();
   return ra!=rb?((ra>rb)-(ra<rb)):strcmp(a.c_str(),b.c_str());
  }

int main()
  {
   static char *ans[]={"NIE","TAK"};
   string a,op,b;
   cin>>a>>op>>b;
   if(op=="==") cout<<ans[cmp(a,b)==0]<<endl;
   else if(op=="!=") cout<<ans[cmp(a,b)!=0]<<endl;
   else if(op=="<") cout<<ans[cmp(a,b)<0]<<endl;
   else if(op==">") cout<<ans[cmp(a,b)>0]<<endl;
   else if(op=="<=") cout<<ans[cmp(a,b)<=0]<<endl;
   else if(op==">=") cout<<ans[cmp(a,b)>=0]<<endl;
   return 0;
  }

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