问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
#include
#include
#include
using namespace std;
double round(double r);
void printf(int);
string word[10] = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
int main()
{
double money;
cout << "please enter the number,the number is retained annd rounded to "
<< "tow decimal places : ";// retain(保留),round(把...四舍五入),decimal(n.小数)
cin >> money;
cout << endl;
double dmoney = round(money);
long long imoney = dmoney;
long long temp = imoney;
long long i = imoney;
int n = 0;
do
{
i = i / 10;
n++;
} while (i != 0);
while (n >= 9)
{
long long a = imoney / int(pow(10, 8));
cout << a << endl;
printf(a);
cout << "亿";
imoney = imoney - a * int(pow(10, 8));
n = 8;
}
while (n >= 5)
{
int a = imoney / int(pow(10, 4));
printf(a);
cout << "万";
imoney = imoney - a * int(pow(10, 4));
n = 4;
}
printf(imoney);
cout << "元";
if (dmoney - temp != 0)
{
int aa = (dmoney - imoney) * 100;
int rr = aa / 10;
if (rr != 0)
cout << word[rr] << "角";
rr = aa - rr * 10;
if (rr != 0)
cout << word[rr] << "分";
}
return 0;
}
double round(double r)
{
int temp = r * 100 + 0.5;
return double(temp) / 100;
}
void printf(int a)
{
int i = a;
int k = 0;
do
{
i = i / 10;
k++;
} while (i != 0);
while (k >= 1)
{
int gg = a / int(pow(10, k - 1)) - a / int(pow(10, k)) * 10;//Very important
if (gg != 0)
{
cout << word[gg];
switch (k)
{
case 4:
cout << "仟";
break;
case 3:
cout << "佰";
break;
case 2:
cout << "拾";
break;
}
}
if (gg == 0)
cout << word[0];
k--;
}
}