财务上可能用得上整形数的读法

例如:1111,2222,3333

读作:一千一百一十一亿二千二百二十二万三千三百三十三

#pragma once


class CConvert
{
public:
	CConvert(void);
	virtual ~CConvert(void);
public:
	bool getoutcome(char* result, char* input);	
	static char table[10][4];
private:
	void remove0(char* input, char* output);
	bool convert9999(char* result, char* input);
	bool convert99999999(char* result, char* input);
	bool convert999999999999(char* result, char* input);
	void mid(char* dest, char* sour, int start, int len);	
};

#include "StdAfx.h"
#include "Convert.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"


char CConvert::table[10][4] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};


CConvert::CConvert(void)
{

}

CConvert::~CConvert(void)
{
}	

//把 souce 从 start 开始的 len 个字符拷贝到 dest 中来
void CConvert::mid(char* dest, char* sour, int start, int len)
{
	for(int i = 0; i < len; i++)
	{
		*(dest + i) = *(sour + start + i);
	}
	*(dest + len) = '\0';
	return;
}

//把一个字符串开头的'0'全部删除掉,如果全部都为0,就认为只有一个字符'0'
void CConvert::remove0(char* input, char* ouput)
{
	int zero = 0;
	char *pstr = input;
	while((*pstr) == '0')
	{
		zero++;
		pstr++;
	}
	if (zero == strlen(input))
	{
		strcpy_s(ouput, 50, "0");
	}
	else
	{
		strcpy_s(ouput, 50, pstr);
	}
}

//可以处理  最大数为 9999,9999,9999
//根据不同的区间,调用不同的函数转换
bool CConvert::getoutcome(char* result, char* input)
{
	bool ret = false;

	char output[50];
	memset(output, 0, 50);
	remove0(input, output);

	if (strlen(output) > 12) return false;

	unsigned int intvalue = atoi(output);
	if (intvalue >= 0 && intvalue < 10000)
	{
		ret = convert9999(result, output);
	}
	else if(intvalue >= 10000 && intvalue < 100000000)
	{
		ret = convert99999999(result, output);
	}
	else 
	{
		ret = convert999999999999(result, output);
	}
	return ret;
}


//1,0000 以内整形数的读法
//通过枚举的情况组合各种10000以内整形数的读法
//不知道有没有其他的办法
bool CConvert::convert9999(char* result, char* input)
{
	bool ret = false;
	remove(input);
	
	char output[50];
	memset(output, 0, 50);
	remove0(input, output);

	int length = strlen(output);
	if ( length > 4) return ret;

	unsigned short index[4];
	for(int i = 0; i < length; i++)
	{
		index[i] = output[i] - 48;
	}

	unsigned int intvalue = atoi(input);
	if (intvalue >= 0 && intvalue < 10) // range from 0 to 10
	{
		sprintf_s(result, 50, "%s",table[index[0]]);
	}
	else if(intvalue >= 10 && intvalue < 100) //range from 10 to 100
	{
		if (index[1] == 0)//10
		{
			sprintf_s(result, 50, "%s%s",table[index[0]],"十");
		}
		else//11
		{
			sprintf_s(result, 50, "%s%s%s",table[index[0]],"十", table[index[1]]);
		}
	}
	else if(intvalue >= 100 && intvalue < 1000)  //range from 100 to 1000
	{
		if (index[1] == 0 && index[2] == 0) //100
		{
			sprintf_s(result, 50, "%s%s",table[index[0]],"百");
		}
		else if(index[1] != 0 && index[2] == 0)//110
		{
			sprintf_s(result, 50, "%s%s%s%s",table[index[0]],"百", table[index[1]], "十" );
		}
		else if(index[1] == 0 && index[2] != 0)//101
		{
			sprintf_s(result, 50, "%s%s%s",table[index[0]],"百零",table[index[2]] );
		}
		else//111
		{
			sprintf_s(result, 50, "%s%s%s%s%s",table[index[0]],"百",table[index[1]],"十", table[index[2]]);
		}
	}
	else if(intvalue >=1000 && intvalue < 10000) //range from 1000 to 10000
	{
		//四位数 3 个 0 在其中
		if (index[1] == 0 && index[2] == 0 && index[3] == 0)//1000
		{
			sprintf_s(result, 50, "%s%s",table[index[0]],"千");
		}
		//四位数有两个 0 在 其中
		else if(index[1] == 0 && index[2] == 0 && index[3] != 0 )//1001
		{
			sprintf_s(result, 50, "%s%s%s",table[index[0]],"千零",table[index[3]] );
		}
		else if(index[1] != 0 && index[2] == 0 && index[3] == 0 )//1100
		{
			sprintf_s(result, 50, "%s%s%s%s",table[index[0]],"千",table[index[1]],"百" );
		}
		else if(index[1] == 0 && index[2] != 0 && index[3] == 0 )//1010
		{
			sprintf_s(result,50,  "%s%s%s%s",table[index[0]],"千零",table[index[2]],"十");
		}
		//四位数有且仅有一个0在其中
		else if(index[1] == 0 && index[2] != 0 && index[3] != 0 )//1011
		{
			sprintf_s(result,50,  "%s%s%s%s%s",table[index[0]],"千零",table[index[2]],"十", table[index[3]]);
		}
		else if(index[1] != 0 && index[2] == 0 && index[3] != 0 )//1101
		{
			sprintf_s(result,50, "%s%s%s%s%s",table[index[0]],"千",table[index[1]],"百零", table[index[3]]);
		}
		else if(index[1] != 0 && index[2] != 0 && index[3] == 0 )//1110
		{
			sprintf_s(result,50, "%s%s%s%s%s%s",table[index[0]],"千",table[index[1]],"百", table[index[2]],"十");
		}
		//四位数不存在0的情况
		else//1111
		{
			sprintf_s(result, 50, "%s%s%s%s%s%s%s",table[index[0]],"千",table[index[1]],"百",table[index[2]],"十", table[index[3]]);
		}
	}
	ret = true;
	return ret;
}


bool CConvert::convert99999999(char* result, char* input)
{
	bool ret = false;
	
	char output[50];
	memset(output, 0, 50);
	remove0(input, output);

	int length = strlen(output);
	if ( length > 8) return ret;

	
	//清空空间
	char localinput[2][5];
	char localresult[2][50];
	memset((char*)localinput, 0, sizeof localinput);
	memset((char*)localresult, 0, sizeof localresult);
	//*************************************************
	//按照中国人读数字的习惯,1111,2222
	//*************************************************
	if (strlen(output) > 4)
	{
		mid(localinput[0], output, length - 4, 4);
		convert9999(localresult[0], localinput[0]);

		mid(localinput[1], output, 0, length - 4);
		convert9999(localresult[1], localinput[1]);

		unsigned int intvalue0 = atoi(localinput[0]);
		if (intvalue0 == 0)
		{
			sprintf_s(result, 50, "%s%s", localresult[1],"万");	
		}
		else if(intvalue0 >= 1 && intvalue0 < 1000)
		{
			sprintf_s(result, 50, "%s%s%s", localresult[1],"万零",localresult[0]);	
		}
		else if(intvalue0 >= 1000)
		{
			sprintf_s(result, 50, "%s%s%s", localresult[1],"万",localresult[0]);		
		}
	}
	else
	{
		mid(localinput[0], output, 0, length);
		convert9999(localresult[0], localinput[0]);
		strcpy_s(result, 50, localresult[0]);	
	}
	
	ret = true;
	return ret;
}


bool CConvert::convert999999999999(char* result, char* input)
{
	bool ret = false;
	char output[50];
	memset(output, 0, 50);
	remove0(input, output);

	int length = strlen(output);
	if (length > 12) return ret;

	//清空空间
	char localinput[2][9];
	char localresult[2][50];

	memset((char*)localinput,  0, sizeof localinput);
	memset((char*)localresult, 0, sizeof localresult);

	//*************************************************
	//按照中国人读数字的习惯,1111,2222,3333
	//*************************************************

	mid(localinput[0], output, length - 8, 8);
	convert99999999(localresult[0], localinput[0]);

	mid(localinput[1], output, 0, length - 8);
	convert9999(localresult[1], localinput[1]);


	unsigned int intvalue0 = atoi(localinput[0]);
	if (intvalue0 == 0)
	{
		sprintf_s(result, 50, "%s%s", localresult[1],"亿");	
	}
	else if(intvalue0 >= 1 && intvalue0 < 10000000)
	{
		sprintf_s(result, 50, "%s%s%s", localresult[1],"亿零",localresult[0]);	
	}
	else if(intvalue0 >= 10000000)
	{
		sprintf_s(result, 50, "%s%s%s", localresult[1],"亿",localresult[0]);		
	}
	
	ret = true;
	return ret;
}



 


主函数:

#include "stdafx.h"
#include "Convert.h"
#include "string.h"


int _tmain(int argc, _TCHAR* argv[])
{
	CConvert con;
	char input[15]="1122223333";
	char result[50]="";
	memset(result, 0, 50);
	con.getoutcome(result, input);
	printf("%s\n", result);

	getchar();
	return 0;
}


 

你可能感兴趣的:(财务上可能用得上整形数的读法)