Good day to all, I write a term paper, but I’m stuck in one procedure, I just can’t solve it ... The problem is this:

At the press of a button, it is calculated from the amount of rew , how many bills should be issued from the available ones ( rub5000 , rub1000 , rub500 , rub100 , rub50 , rub10 ). The number of bills p strictly limited, that is, for example, if one 5000r note is missing, the program should replace it with an equivalent amount of other bills.

That's where I got a problem.

 procedure TForm2.Button3Click(Sender: TObject); begin label1.Caption := '5000 = 0 шт.'; label2.Caption := '1000 = 0 шт.'; label3.Caption := '500 = 0 шт.'; label4.Caption := '100 = 0 шт.'; label5.Caption := '50 = 0 шт.'; label6.Caption := '10 = 0 шт.'; while rew > 0 do begin if rew >= rub5000 then begin while (rew > rub5000) or (p > 0) do begin k := rew div rub5000; p := ini.ReadInteger('Купюры', '5000', p); p := p - k; label1.Caption := '5000 руб. = ' + inttostr(k) + ' шт.'; ini.WriteInteger('Купюры', '5000', p); ini.ReadString('Купюры', '5000', label12.Caption); rew := rew - (rub5000 * k); end; p := 1 end else if (rew >= rub1000) and (rew < 5000) then begin while (rew > rub1000) or (p > 0) do begin k := rew div rub1000; p := ini.ReadInteger('Купюры', '1000', p); p := p - k; label2.Caption := '1000 руб. = ' + inttostr(k) + ' шт.'; ini.WriteString('Купюры', '1000', inttostr(p)); ini.ReadString('Купюры', '1000', label13.Caption); rew := rew - (rub1000 * k); end; p := 1 end else if (rew >= rub500) and (rew < 1000) then begin while (rew > rub500) or (p > 0) do begin k := rew div rub500; p := strtoint(ini.ReadString('Купюры', '500', inttostr(p))); p := p - k; label3.Caption := '500 руб. = ' + inttostr(k) + ' шт.'; ini.WriteString('Купюры', '500', inttostr(p)); ini.ReadString('Купюры', '500', label14.Caption); rew := rew - (rub500 * k); end; p := 1 end else if (rew >= rub100) and (rew < 500) then begin while (rew > rub100) or (p > 0) do begin k := rew div rub100; p := ini.ReadInteger('Купюры', '100', p); p := p - k; label4.Caption := '100 руб. = ' + inttostr(k) + ' шт.'; ini.WriteString('Купюры', '100', inttostr(p)); ini.ReadString('Купюры', '100', label15.Caption); rew := rew - (rub100 * k); end; p := 1 end else if (rew >= rub50) and (rew < 100) then begin while (rew > rub50) or (p > 0) do begin k := rew div rub50; p := strtoint(ini.ReadString('Купюры', '50', inttostr(p))); p := p - k; label5.Caption := '50 руб. = ' + inttostr(k) + ' шт.'; ini.WriteString('Купюры', '50', inttostr(p)); ini.ReadString('Купюры', '50', label16.Caption); rew := rew - (rub50 * k); end; p := 1 end else if (rew >= 10) and (rew < 50) then begin while (rew > rub10) or (p > 0) do begin k := rew div rub10; p := strtoint(ini.ReadString('Купюры', '10', inttostr(p))); p := p - k; label6.Caption := '10 руб. = ' + inttostr(k) + ' шт.'; ini.WriteString('Купюры', '10', inttostr(p)); ini.ReadString('Купюры', '10', label17.Caption); rew := rew - (rub10 * k); end; end; end; label12.Caption := '5000 = ' + ini.ReadString('Купюры', '5000', '') + ' шт.'; label13.Caption := '1000 = ' + ini.ReadString('Купюры', '1000', '') + ' шт.'; label14.Caption := '500 = ' + ini.ReadString('Купюры', '500', '') + ' шт.'; label15.Caption := '100 = ' + ini.ReadString('Купюры', '100', '') + ' шт.'; label16.Caption := '50 = ' + ini.ReadString('Купюры', '50', '') + ' шт.'; label17.Caption := '10 = ' + ini.ReadString('Купюры', '10', '') + ' шт.'; end; 

It seems to me that the mistake here is banal, but I just can not figure out how to fix it. I hope for help.

  • @ Vladimir Archi Specify the line on which an error is issued. - VioLet
  • It can be any part of the count, if p := p - k; less than zero, the error is not issued, but the program freezes in a loop. And besides, he still writes that there will be a negative amount of bills (in particular, 5,000) in the remainder, but I need it to be not lower than zero (if I have 2 5,000 bills, I cannot issue 15 five thousandth bills). - Vladimir Archi

2 answers 2

 int ruble[] = {10, 10, 43, 3421, 76, 1000}; // кол-во купюр int nom[] = {10, 50, 100, 500, 1000, 5000}; // номиналы void func () { long int sum; std::cout << "Введите сумму" << std::endl; std::cin >> sum; for (i = 5; i >= 0; i--) { int x = sum / nom[i]; std::cout << "необходимое кол-во купюр номинала " << nom[i] << " руб. - " << x << std::endl; std::cout << "в наличии - " << ruble[i] << "купюр такого номинала" << std::endl; int vydano = (x > ruble[i]) ? ruble[i] : x; // выдано наименьшее кол-во из имеющегося и посчитанного кол-ва купюр std::cout << "будет выдано " << vydano << " купюр" << std::endl; sum = sum - vydano * nom[i]; // на следующую итерацию будет переведена невыданная сумма } if (sum > 0) std::cout << "в банкомате не хватает денег для проведения операции" << std::endl; } 

This is a kind of C-like pseudocode. It may not compile, but I hope the general idea is clear.

  • one
    It would be necessary to check that the amount entered is a multiple of the minimum nominal. - kot-da-vinci

Added. pancake, looked at the date, and realized that two years had passed, and maybe more

What about mistakes, I can say so, to the eye:

1) It is not clear where p is taken from. And as I understand it, p is the number of bills.

  if rew >= rub5000 then begin // например сначала загрузим // p := ini.ReadInteger('Купюры', '5000', p); while (rew > rub5000) or (p > 0) do begin k := rew div rub5000; p := ini.ReadInteger('Купюры', '5000', p); p := p - k; // Если k будет больше p, то выдадим больше чем купюр // if (k>p) then k:=p; // p:=p - k; label1.Caption := '5000 руб. = ' + inttostr(k) + ' шт.'; ini.WriteInteger('Купюры', '5000', p); // Не понятно что вы считываете в label12.Caption ini.ReadString('Купюры', '5000', label12.Caption); rew := rew - (rub5000 * k); end; p := 1 end else 

2) If there are not enough 5000th currencies, then there are no options

  // думаю можно было просто if (rew >= rub1000) then // ну и ниже те же ошибки что и в первом пункте if (rew >= rub1000) and (rew < 5000) then begin while (rew > rub1000) or (p > 0) do begin k := rew div rub1000; p := ini.ReadInteger('Купюры', '1000', p); p := p - k; label2.Caption := '1000 руб. = ' + inttostr(k) + ' шт.'; ini.WriteString('Купюры', '1000', inttostr(p)); ini.ReadString('Купюры', '1000', label13.Caption); rew := rew - (rub1000 * k); end; p := 1 end else 

3) I’ll also touch on the structure of the structure as a whole - you write 6 blocks of essentially performing the same thing, only with different numbers. Such things need to think better, or make a function. As an example, you can study the C-Podomny option in response.