zoukankan      html  css  js  c++  java
  • 数字0到9999转换大写汉字整数程序(C语言实现)

    输入:9001

    输出:九千零一




    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    
    #define DEBUG printf
    /*========================================================*/
    
    int len_num(int num);   //to judge the length of the number
    char* my_itoa(int num, int len); //to transform the number to a string
    char* handle_str(char* str_num, int len); //transform num to NUM
    
    /*========================================================*/
    
    int main()
    {
    	float num_test;
    	int num, len;
    	char* str_num;
    	char* str_result;
    	
    	while(1)
    	{		
    		printf("Please input a number:  ");
    		
    		if(!(scanf("%f", &num_test)))
    		{
    			printf("Invalid:data type not correct.\n");
    			break;
    		}	
    		else
    		{
    			num = (int)num_test;
       		
    			if (num_test != num)
    			{
    				printf("Invalid:number type not correct.\n");
    				printf("number: %f\n", num_test);
    			}
    			else if (num == 0)
    			{
    				printf("Number: 0\nLength: 1\nResult: 零\n");
    			}
    			else if (num_test > 9999 || num_test < 0)
    			{
    				printf("Invalid:number overflow.\n");
    				printf("number: %d\n", num);
    			}
    			else
    			{
    				len = len_num(num);
    		
    				str_num = my_itoa(num, len);
    
    				str_result = handle_str(str_num, len);
    	
    				printf("Number: %d\nLength: %d\n", num, len);
    				printf("Result: %s\n", str_result);
    	
    			}
    		}
    	}
    	return 0;
    }
    
    /*========================================================*/
    
    /*
      function:  to get the length of the number
      input : the input number, int num
      output: the length of the number, int num_len
    */
    
    int len_num(int num)
    {
    	int num_len;
    
    	if(num <= 9999 && num >= 1000)
    		num_len = 4;
    	else if(num <= 999 && num >= 100)
    		num_len = 3;
    	else if(num <= 99 && num >= 10)
    		num_len = 2;
    	else if(num <=9 && num >=0)
    		num_len = 1;
    	return num_len;
    }
    
    /*
      function:  to transform number to a string
      input : int num, int len
      output: char* point to a string
    */
    
    char *my_itoa(int num, int len)
    {
    	char *temp;
    	int i;
    	temp =(char*)malloc(sizeof(char)*len);
    
    	for (i = 1; i <= 5; i++)
    	{
    		if(len-i != -1)
    		{
    			temp[len-i] = num % 10 + '0';
    			//		DEBUG("temp%d %c",i,temp[len-i]);
    			num  = num / 10;
    		}
    		else
    			return temp;
    	
    	}
    }
    
    
    /*
      function:  to transform num to NUM
      input : char* str_num 
      output: None
    */
    
    char* handle_str(char* str_num, int len)
    {
    	int i=0, k;
    	char *str_result;
    	
        static char num_trans[10][4] = {"零","一","二","三","四","五","六","七","八","九"};
    	static char num_value[4][4] = {"千","百","十",'\0'};
    	
    	str_result = (char*)malloc(sizeof(char)*32);
    	
    	for(k = 4-len; k <=3 ; k++)
    	{
    		int j;
    		j = str_num[i]-'0';
    		if(!(str_num[i] == '0' && (str_num[i+1] == '0' || i ==len-1)))
    			strcat(str_result, num_trans[j]);
    					
    		if(j != 0)
    		{
    			strcat(str_result, num_value[k]);
    		}
    		i++;
    	}
    	return str_result;
    }
    


  • 相关阅读:
    [Canvas]英雄可以射箭了
    [Canvas]人物型英雄出现(前作仅为箭头)
    day33_Spring学习回顾_01
    day33_Spring学习笔记_01
    MyEclipse中,当我们写一个类实现一个接口时,会自动生成重写该接口的方法,但是,方法的参数提示不够好,是什么原因导致的呢?该如何解决呢?
    使用Junit测试一个 spring静态工厂实例化bean 的例子,所有代码都没有问题,但是出现java.lang.IllegalArgumentException异常
    MyEclipse中如何变换查看项目文件夹
    实战网卡bond
    Centos7上使用官方YUM源安装Mysql
    CentOS 7上安装WordPress详细步骤
  • 原文地址:https://www.cnblogs.com/suzhou/p/3639004.html
Copyright © 2011-2022 走看看