Advertisement
dinophanhk

[C] Function get number from string (before and after ',')

Jun 20th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1. // Function: Convert 'float number' as type string to integer and float type.
  2. // How to use: Create 2 int number and 1 array char type has number from 0 - 9 and ',' symbol.
  3. // Set 2 int number by call function: floatFromStringToInt(variable name of type char, int for int, int for float)
  4. // After call function, your variable will have value before ',' and after ','.
  5.  
  6. // Hàm lấy phần nguyên và phần thực của số dạng chuỗi ký tự.
  7. // Cách dùng: Khai báo 2 biến thuộc kiểu số nguyên, 1 chuỗi ký tự.
  8. // Gọi hàm bằng cách: floatFromStringtoInt(chuỗi ký tự vào, số nguyên thứ 1, số nguyên thứ 2)
  9. // Sau khi gọi hàm, số nguyên thứ nhất sẽ lấy được phần nguyên (trước dấu ','), số nguyên thứ 2 sẽ lấy giá trị sau dấu ','.
  10.  
  11. void floatFromStringtoInt(char *charNumber, int *soNguyen, int *thapPhan) {
  12.     char resultSoNguyen[30], resultThapPhan[30];
  13.     int iLoop, iLength, iResult, iForThapPhan, intResultSoNguyen, intResultThapPhan;
  14.    
  15.     iLength = (int) strlen(charNumber);
  16.    
  17.     for (iLoop = 0; iLoop < iLength; iLoop++) {
  18.         resultSoNguyen[iLoop] = charNumber[iLoop];
  19.         if (charNumber[iLoop + 1] == ',') {
  20.             iResult = iLoop;
  21.             break;
  22.         }
  23.         if (charNumber[iLoop + 1] == '\0') {
  24.             iResult = iLoop;
  25.             break;
  26.         }
  27.     }
  28.     resultSoNguyen[iResult + 1] = '\0';
  29.    
  30.     iForThapPhan = 0;
  31.     for (iLoop = iResult + 2; iLoop < iLength; iLoop++) {
  32.         resultThapPhan[iForThapPhan] = charNumber[iLoop];
  33.         iForThapPhan++;
  34.     }
  35.    
  36.     resultThapPhan[iForThapPhan] = '\0';
  37.    
  38.     if ((int)strlen(resultThapPhan) == 0) {
  39.         resultThapPhan[0] = '0';
  40.         resultThapPhan[1] = '\0';
  41.     }
  42.    
  43.  
  44.     iLength = (int) strlen(resultSoNguyen);
  45.     intResultSoNguyen = 0;
  46.     for (iLoop = 0; iLoop < iLength; iLoop++) {
  47.         switch (resultSoNguyen[iLoop]) {
  48.             case '0':
  49.                 intResultSoNguyen = (intResultSoNguyen * 10) + 0;
  50.                 break;
  51.             case '1':
  52.                 intResultSoNguyen= (intResultSoNguyen * 10) + 1;
  53.                 break;
  54.             case '2':
  55.                 intResultSoNguyen = (intResultSoNguyen * 10) + 2;
  56.                 break;
  57.             case '3':
  58.                 intResultSoNguyen = (intResultSoNguyen * 10) + 3;
  59.                 break;
  60.             case '4':
  61.                 intResultSoNguyen = (intResultSoNguyen * 10) + 4;
  62.                 break;
  63.             case '5':
  64.                 intResultSoNguyen = (intResultSoNguyen * 10) + 5;
  65.                 break;
  66.             case '6':
  67.                 intResultSoNguyen = (intResultSoNguyen * 10) + 6;
  68.                 break;
  69.             case '7':
  70.                 intResultSoNguyen = (intResultSoNguyen * 10) + 7;
  71.                 break;
  72.             case '8':
  73.                 intResultSoNguyen = (intResultSoNguyen * 10) + 8;
  74.                 break;
  75.             case '9':
  76.                 intResultSoNguyen = (intResultSoNguyen * 10) + 9;
  77.                 break;
  78.                
  79.             default:
  80.                 break;
  81.         }
  82.     }
  83.    
  84.     iLength = (int) strlen(resultThapPhan);
  85.     intResultThapPhan = 0;
  86.     for (iLoop = 0; iLoop < iLength; iLoop++) {
  87.         switch (resultThapPhan[iLoop]) {
  88.             case '0':
  89.                 intResultThapPhan = (intResultThapPhan * 10) + 0;
  90.                 break;
  91.             case '1':
  92.                 intResultThapPhan = (intResultThapPhan * 10) + 1;
  93.                 break;
  94.             case '2':
  95.                 intResultThapPhan = (intResultThapPhan * 10) + 2;
  96.                 break;
  97.             case '3':
  98.                 intResultThapPhan = (intResultThapPhan * 10) + 3;
  99.                 break;
  100.             case '4':
  101.                 intResultThapPhan = (intResultThapPhan * 10) + 4;
  102.                 break;
  103.             case '5':
  104.                 intResultThapPhan = (intResultThapPhan * 10) + 5;
  105.                 break;
  106.             case '6':
  107.                 intResultThapPhan = (intResultThapPhan * 10) + 6;
  108.                 break;
  109.             case '7':
  110.                 intResultThapPhan = (intResultThapPhan * 10) + 7;
  111.                 break;
  112.             case '8':
  113.                 intResultThapPhan = (intResultThapPhan * 10) + 8;
  114.                 break;
  115.             case '9':
  116.                 intResultThapPhan = (intResultThapPhan * 10) + 9;
  117.                 break;
  118.                
  119.             default:
  120.                 break;
  121.         }
  122.     }
  123.    
  124.     *soNguyen = intResultSoNguyen;
  125.     *thapPhan = intResultThapPhan;
  126. }
  127. // 2012 Dino Phan
  128.  
  129.  
  130.  
  131.  
  132. /* Example/Mẫu:
  133. int main(void) {
  134.     char myArrayChar[30];
  135.     int nguyen, thapphan;
  136.     printf("Enter float number: ");
  137.     gets(myArrayChar);
  138.     floatFromStringtoInt(myArrayChar, &nguyen, &thapphan);
  139.     printf("eQual: %d.\n", nguyen + thapphan);
  140.     return 0;
  141. }
  142. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement