模数在C ++中有问题?

时间:2016-11-21 17:17:06

标签: c++

我正在为大学做一个“项目”。短期计划,将任何金额与硬币交换,价值从1到0.01。

#include <iostream>

using namespace std;

int main()
{
    double C;
    int rest;
    cout << "Wpisz kwote w zl. " << endl;
    cin >> C;
    double c1=1 ,c2=0.75 ,c3=0.50 ,c4=0.20 ,c5=0.10 ,c6=0.05 ,c7=0.02 ,c8=0.01;
    int coin1=c1*100 ,coin2=c2*100 ,coin3=c3*100 ,coin4=c4*100 ,coin5=c5*100 ,coin6=c6*100 ,coin7=c7*100 ,coin8=c8*100;
    int ile1=0, ile2=0, ile3=0, ile4=0, ile5=0, ile6=0, ile7=0, ile8=0;
    int cash= C*100;
    double check;


        if (cash-coin1>=0){rest=cash%coin1;ile1= cash/coin1; cash = rest; }
        cout << cash << " " << 28083%coin1 ;
        if (cash-coin2>=0){rest=cash%coin2;ile2= cash/coin2; cash = rest; }
        cout << cash << endl;
        if (cash-coin3>=0){rest=cash%coin3;ile3= cash/coin3; cash = rest; }
        cout << cash << endl;
        if (cash-coin4>=0){rest=cash%coin4;ile4= cash/coin4; cash = rest; }
        cout << cash << endl;
        if (cash-coin5>=0){rest=cash%coin5;ile5= cash/coin5; cash = rest; }
        cout << cash << endl;
        if (cash-coin6>=0){rest=cash%coin6;ile6= cash/coin6; cash = rest; }
        cout << cash << endl;
        if (cash-coin7>=0){rest=cash%coin7;ile7= cash/coin7; cash = rest; }
        cout << cash << endl;
        check=ile1*c1+ile2*c2+ile3*c3+ile4*c4+ile5*c5+ile6*c6+ile7*c7+ile8*c8;

我知道结构本身非常凌乱,但我正在学习。我会稍后尝试缩短它并使其更清晰。但这不是我的问题。

问题是看起来%操作数无法正常工作。

当我为C输入值280.83时,程序读取现金%coin1(coin1等于100)为82.随机1在哪里?另外,为了检查,我写了28083%coin1,结果是837.很奇怪。

我该如何解决这个问题?

编辑:

double C=280.83, c1=1;
int cash = C*100, coin1=c1*100, ile1=0;
rest=cash%coin1;ile1= cash/coin1; cash = rest;

休息= 82.应该是83。

0 个答案:

没有答案