Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have tried this problem a lot .And I'm getting bored now.HERE is the link:
Modular Calculator - CodeAbbey[^]

Example:
510
* 5
* 9
+ 9
* 9932
* 1925
* 4
+ 918
* 7
* 9307
* 9
+ 96
* 1063
* 4
* 1380
* 100
+ 119
+ 8377
* 718
* 87
* 57
* 7548
* 52
+ 7227
+ 7
+ 1
+ 6204
+ 598
+ 48
+ 740
+ 4698
+ 4
* 81
+ 718
* 90
* 4
* 2065
+ 8741
* 244
+ 93
* 9
+ 8875
* 16
* 76
* 1
* 112
+ 1
+ 9
+ 4450
+ 8859
% 9602


What I have tried:

C++
 #include<stdio.h>
 #include<conio.h>
int main()
{
    int str[1];
    int i , j ,num1;
    char ch;
    scanf("%d",&str[0]);
    while (1)
    {
        ch=getche();
        scanf(" %d",&num1);
        if(ch=='+')
        {
            str[0]=str[0]+num1;
             printf("%d ",str[0]);
        }
        if(ch =='*')
        {
            str[0]=str[0]*num1;
            printf("%d ",str[0]);
        }
        if(ch =='%')
        {
            str[0]=str[0]%num1;
                break;
        }

    }

    return 0;
}
Posted
Updated 25-Mar-16 16:16pm
v3
Comments
jeron1 25-Mar-16 15:51pm    
What do mean by overflowing numbers?
Patrice T 25-Mar-16 16:40pm    
And you have a problem with your code ?
Member 12416732 25-Mar-16 17:03pm    
510
* 5
* 9
+ 9
* 9932
* 1925
* 4
+ 918
* 7
* 9307
* 9
+ 96
* 1063
* 4
* 1380
* 100
+ 119
+ 8377
* 718
* 87
* 57
* 7548
* 52
+ 7227
+ 7
+ 1
+ 6204
+ 598
+ 48
+ 740
+ 4698
+ 4
* 81
+ 718
* 90
* 4
* 2065
+ 8741
* 244
+ 93
* 9
+ 8875
* 16
* 76
* 1
* 112
+ 1
+ 9
+ 4450
+ 8859
% 9602

if i give it as input i m not getting the proper output.my output -274 but it should be 2218
Patrice T 25-Mar-16 22:21pm    
I updated your question with the example as it give more visibility.
If you want to update your question, use Improve question to update your question.
Sergey Alexandrovich Kryukov 25-Mar-16 17:18pm    
Did you look at your own post after it was rendered. The code starts with
#include
#include

Who do you think is supposed to fix it? You just need to escape HTML < and >...

And please properly explain "overflow". There should not be wrapping of int with so small numbers...

—SA

Many possibilities:
1) If you can know the last operation in advance, you can limit the size of values by continuously applying the modulo after each operation. Using type long should be enough.

2) Using type long long will push the overflowing limit but may not be large enough depending on data.

3) resorting to a bigint library or "infinite integers" will ensure that you will never have overflow.

Warning: depending on compiler version, size of int can be as small as 16 bits, which is too small, sizes of int, long and long long may vary.
 
Share this answer
 
v2
Try to the first 20 or calculation with your pocket calculator and you see that the intermediate result gets into the range of 10**20. That is too much for a 32-bit int and even for a 64-bit int.

But if you look at the task description on the given link you see that they ask for modulo operation. Note that you can do all those operations in the modulus given in the last line. Now, how do you know the modulus before you have read all the lines? Well, this is the task for you to solve. It is not that difficult, if you think about it for a minute.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900