温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++实现整数值转中文大写

发布时间:2020-07-27 21:25:25 来源:网络 阅读:466 作者:哈夫猿 栏目:编程语言
#include<iostream>
#include<stack>
using namespace std;
string zhCN[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
//正常记账范围内数值
string zhUNIT[] = { "拾","佰","仟","万","拾","佰","仟","亿" ,"拾","佰","仟","万", "拾","佰","仟"};
int main()
{
 //12,345 ,678, 902
 while (1) {
  char cc[32];
  scanf("%s", cc);
  string str_value(cc);
  cout << "input value is " << str_value.c_str() << endl;
  stack<int>nValue;
  for (int i = 0; i < str_value.length(); i++)
  {
   const char cc = str_value.at(i);
   nValue.push((cc - '0'));
  }
  stack<string>cn;
  int ii = 0;
  int zcount = 0;
  while (!nValue.empty())
  {
   int value = nValue.top();
   cn.push(zhCN[value]);
   cn.push(zhUNIT[ii]);
   nValue.pop();
   ii++;
  }
  if (!cn.empty())
  {
   cn.pop();
  }
  //删除零
  stack<string>cn2;
  while (!cn.empty())
  {
   while (cn.top() != zhCN[0])
   {
    cn2.push(cn.top());
    cn.pop();
    if (cn.empty())
    {
     break;
    }
   }
   if (cn.empty())
   {
    break;
   }
   //找到零结束
   while (cn.top() == zhCN[0])
   {
    
    cn.pop();
    if (cn.empty())
    {
     break;
    }
    if (cn.top() == zhUNIT[3])//保留万
    {
     cn2.push(zhUNIT[3]);
    }
    if (cn.top() == zhUNIT[7])//保留亿
    {
     cn2.push(zhUNIT[7]);
    }
    cn.pop();
   }//找到零结束
   if (!cn.empty())
   {
    cn2.push(zhCN[0]);
   }
   
  }
  stack<string>cn3;
  string newstr = "";  
  while (!cn2.empty())
  {
   cn3.push(cn2.top());
   cn2.pop();
  }
  while (!cn3.empty())
  {   
   newstr += cn3.top();
   cn3.pop();
  }  
  cout << "newstr is " << newstr.c_str() << endl;
 }
 return 0;
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI