This is what I have been working:
Giving 2 positive integer (at most 100 dight).
Compare that 2 integer .
Note: That 2 integer can contain leading zeros.(that means you have to remove zeros first)
Because 100 digits is too big so I use string
But in my program below,it just return '=' I debugged it and know the for loop doesn't working
Can anyone help? Thanks in avdance.
My code:
#include <iostream>
#include <string>
using namespace std; // I know this is bad but it's just a small program
char compareBigInterger(string str1,string str2)
{
while (str1[0] != 0) str1.erase(0, 1);
while (str2[0] != 0) str2.erase(0, 1);
char answerHold {' '};
if (str1.size() > str2.size())
{
answerHold = '>';
}
else if (str1.size() < str2.size())
{
answerHold = '<';
}
else
{
for (int i = 0; i < str1.size(); i++)
{
if (int(str1[i] - 48) < int(str2[i] - 48)) // 48 = '1' - 1
{
answerHold = '<';
break;
}
else if (int(str1[i] - 48) > int(str2[i] - 48))
{
answerHold = '>';
break;
}
}
}
if (answerHold == ' ')
answerHold = '=';
return answerHold;
}
int main()
{
string str1;
string str2;
cin >> str1 >> str2;
cout << char{compareBigInterger(str1, str2)};
}
Aucun commentaire:
Enregistrer un commentaire