#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
string bagi(string a, long long b)
{
long long nilai = 0;
string hasil;
bool belum = true;
for (long i = 0; i < a.length(); i++)
{
nilai *= 10;
nilai += a[i]-'0';
if (belum)
{
if (nilai/b)
{
belum = false;
hasil.push_back(('0' + nilai/b));
}
}
else
hasil.push_back(('0' + nilai/b));
nilai %= b;
}
if (belum) return "0";
return hasil;
}
long long mod(string a, long long b)
{
long long nilai = 0;
bool belum = true;
for (long i = 0; i < a.length(); i++)
{
nilai *= 10;
nilai += a[i]-'0';
nilai %= b;
}
return nilai;
}
int main()
{
long long b;
string a;
char c;
bool sudah = false;
while (cin >> a)
{
if(sudah) printf("\n");
else sudah = true;
scanf(" %c %lld",&c,&b);
if (c == '/')
cout << bagi(a,b);
else
cout << mod(a,b);
}
return 0;
}
r = r * 10 + n[i] - '0';
res[i] = r / d + '0';
r = r % d;
When d = 2147483647. After r = r % d opeation r can get value between 214748364..2147483646 and in the next iteration of for loop r = r * 10 + n - '0' value of r will be greater than 2147483647.
So use long long for r.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Next time post in the existing thread.
Don't make a function return a pointer to a local variable. In g++ that gives me a warning. Either print ans in your division function or make ans global.
Check input and AC output for thousands of problems on uDebug!