资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

#include
#include
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;
  stacknValue;
  for (int i = 0; i < str_value.length(); i++)
  {
   const char cc = str_value.at(i);
   nValue.push((cc - '0'));
  }
  stackcn;
  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();
  }
  //删除零
  stackcn2;
  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]);
   }
   
  }
  stackcn3;
  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;
}

本文名称:C++实现整数值转中文大写
本文来源:http://cdkjz.cn/article/peopeh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220