2017-05-05 66 views
-5

印度货币格式的任意数量的印度货币风格是12345→“12345”(对于奇数长度)和123456→“1,23,456”(即使长度)。我已经包括了所有的可能性,如格式用C语言

1.减号: “-12,345”

2.小数点:“-12,345.345”或“12,345.123”

3.零条件000000.123→ “0.123”

4.Minus和零条件 '-000000.123' - > “-0.123”

int currencyFormatter(char av_currency[], int av_strLen, char *ap_formattedNumber) 
     { 
      char flag = 'N';    //Taking a Flag to know whether thier is a decimal Point in Currency or not 
      int  lengthOf = 0, index = 0, i = 0, j = 0; 
      char *decAr = NULL; 
      char *tmpCurrency = NULL;//Taking two Pointers one for Array with Commas(tmpCurrency) and decAr pointer for decimal Point array 
      char *s = NULL; 

      s = strstr(av_currency, ".");//Checking for decimal Point in array 

      if (s > 0) 
      { 
       flag = 'D';     // Changing Flag to show Decimal Point is Present in Array 
       s  = strchr(av_currency, '.'); 
       index = s - av_currency;   //Index at which Decimal Point is present 
       av_strLen = strlen(av_currency) - index;  // calculated formula to know length of an array needed to contain decimal point and Numbers after that 
       decAr = (char*)malloc(av_strLen*sizeof(char*));//allocated Memory using malloc 
       decAr[av_strLen] = '\0'; 
       memmove(decAr, &av_currency[index], av_strLen); //memmove from decimal till end of array. 
       av_currency[index] = '\0'; 

       if (!decAr)//Handled Null Condition for Pointer 
       { 
        return -1;//All errors for Negative Number 
       } 
      } 


      lengthOf = strlen(av_currency) + (strlen(av_currency)/2); // Derived Formula(It Works for Indian Currency Format) to know the length of an array is needed to contain numbers and Commas Together. 
      tmpCurrency = (char*)malloc(lengthOf*sizeof(char*)); 
      strrev(av_currency); //Reversed Array as commas comes at multiple of 3. eg=12345 reverse=54321 wdComma=543,21 index is 3 if number would had been bigger commas would had come at 3,6. 
      while (av_currency[i] != '\0') 
      { 
       if (j % 3 == 0 && j >= 3 && av_currency[i] != '-')//all Commas come at multiple of 3 when you reverse an amount 
       { 
        tmpCurrency[j] = ',';//If an , is found Increment only J as 
    is used as index number to store in tmpcurrency 
        j++; 
        continue; 
       } 
       tmpCurrency[j] = av_currency[i];//storing the Value in tmpCurrency 
       i++;//Incrementing 

       j++;//Incrementing 
      } 
      tmpCurrency[j] = '\0';//Null Condition 

      if (!tmpCurrency) // Checking for NULL Pointer 
      { 
       return -2; //all errors for Negative value 
      } 

      flag == 'D' ? strcpy(av_currency, (strcat(strrev(tmpCurrency), decAr))) : strcpy(av_currency, (strrev(tmpCurrency)));//Ternary Operator 

      strcpy(ap_formattedNumber,av_currency);//Copying formated number into original array 
      free(tmpCurrency);//Releasing the memory 
      free(decAr);//Releasing the Memory 
      return 0; 
     } 
+1

** I **时,出现了问题,阅读这个烂摊子。在发布和正确格式化代码之前阅读[问]怎么样。 – Olaf

+0

这是你的问题中最少的。您如何看待您刚发布的自己的帖子。如果你认为它看起来像一个完整的混乱,然后编辑它并修复格式。 – Lundin

+1

你能否更详细地解释一下你的问题和你的代码的作用? –

回答

0

只做正与你的函数值!

检查值是调用函数之前负;以正值调用函数;如果开始时为负值,请在后面添加负号。

int needssign = 0; 
if (val < 0) needssign = 1; 
indianformat(res, abs(val)); 
if (needssign) sprintf(res, "-%s", res); 

或者让你当前的函数成为辅助函数,并使用上面的代码为新的改进的函数进行格式化为印度格式。

+1

拐角案例:这是'val == INT_MIN'的问题,因为'abs(INT_MIN)'通常是UB。 – chux

+0

谢谢你我做的是我已经检查它是否是一个减号或不Help.What并添加Comma.Previously我的号用来获取 - ,23123。所以现在它只会通过首先检查货币[i]!=' - '来放置逗号。 – Devious

1

我对上述问题的解决方案。 请尝试此代码。

#include <stdio.h> 
#include <conio.h> 
#include <string.h> 
#include <stdlib.h> 

char *printComma(double input_number,char *demo,char ap_it[],char ap_type[]) 
{ 
    char *result = NULL; 
    char *lp_decimal_number = NULL; 
    char *main_number = NULL; 
    char *decimal_pos = NULL; 
    char zero[1] = {0}; 
    int  i = 0; 
    int  j = 0; 
    int  z = 0; 
    int  cnt = 0; 
    int  decimal_index = 0; 
    int  lp_decimal_numberLen = 0 ; 
    int  flag_dec = 0; 
    int  flag_minus = 0 ; 
    int  length_main; 
    int  k = 0; 
    int  length_demo=0; 
    sprintf(demo,"%lf",input_number); 
    if(strcmp(ap_type,"P") == 0) 
     { 
       if(strcmp(ap_it,"A") == 0 || strcmp(ap_it,"B") == 0) 
       { 
        sprintf_s(demo,40,"%0.4lf",input_number); 
       } 
       else 
       { 
        sprintf_s(demo,40,"%0.2lf",input_number); 
       } 
     } 
     else 
     { 
       sprintf_s(demo,40,"%.0lf",input_number); 
     } 
    length_demo = strlen(demo); // finds the length of original string 
    result = (char *)malloc((length_demo+10)*sizeof(char)); 
    main_number = (char *)malloc((length_demo+50)*sizeof(char)); 
    z = strspn(demo[0] == '-' ? (demo + 1) : demo , "0"); 
    if(z != 0) 
    { 
     if(demo[0] != '-') 
     { 
      memcpy(main_number,&demo[z],length_demo); 
      main_number[length_demo]='\0'; 
     } 
     else 
     { 
      puts(main_number); 
      main_number[length_demo]='\0'; 
      flag_minus=1; 
     } 
    } 
    else 
    { 
     memcpy(main_number,&demo[0],length_demo); 
     main_number[length_demo]='\0'; 
    } 
    length_main=strlen(main_number); 
    decimal_pos = strstr(main_number,"."); 
    if(decimal_pos > 0) 
    { 
     decimal_index = decimal_pos - main_number ; // Getting postion of decimal 
     lp_decimal_numberLen = length_main - decimal_index; // Calculating the endpoint for decimal number 
     if(length_main > 3) //Copying the decimal part to a separate array 
     { 
      lp_decimal_number = (char *) malloc(lp_decimal_numberLen+1); 
      memcpy(lp_decimal_number, &main_number[decimal_index], lp_decimal_numberLen); 
      lp_decimal_number[lp_decimal_numberLen] = '\0'; 
      flag_dec=1; 
      main_number[decimal_index]='\0'; 
     } 
    } 
    //logic for comma starts here 
    strrev(main_number); 
    i = 0; 
    while(main_number[i] != '\0') 
    { 
     if (j%3 == 0 && j>=3 && main_number[i]!='-' && main_number[i]!='$') 
     { 
      result[j] = ','; 
      cnt++; 
      j++; 
      continue; 
     } 
     else if(cnt==1 || cnt==2) 
     { 
      result[j] = main_number[i]; 
     } 
     else 
     { 
      result[j] = main_number[i]; 
     } 
     i++; 
     j++; 
    } 
    result[j] = '\0'; 
    if(flag_dec==0) 
    { 
     if(flag_minus==0) 
      return(strrev(result)); 
     else 
     { 
      strcat(result,"-"); 
      return(strrev(result)); 
     } 
    } 
    else 
    { 
     if(flag_minus==0) 
      return(strcat(strrev(result),lp_decimal_number)); 
     else 
     { 
      strcat(result,"-"); 
      return(strcat(strrev(result),lp_decimal_number)); 
     } 
    } 
} 
int main() 
{ 
    double number; 
    char num[25] = {0}; 
    char it_type[] = "A"; 
    char ap_type[] = "P"; 
    char *formattedNumber = NULL; 
    printf("\n Enter the number n: "); 
    scanf("%lf",&number); 
    formattedNumber=printComma(number,num,it_type,ap_type); 
    printf("\n Final Result = %s ",formattedNumber); 
    getch(); 
    return 0; 
} 
+0

在您的代码中,它们是使用Count的数据,以便在将数据从一个数组复制到另一个数组时不会丢失数据,但保持计数不是一个好主意,因为随着数量的增加,您必须写入(cnt == 1 | | cnt == 2 || cnt == 3 || cnt == 4 ......)等等。您可以使用三元运算符来声明语句。 – Devious

+0

@Devious雅我认为这是一个可变的场景,但它不会伤害任何东西,因为从实际的数值可以达到8或10以上。除此之外,没有考虑到实际数量。 关于if else,你可以使用三元运算符替代。 – 2017-05-12 05:15:01