2017-05-05 60 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