Advertisement
dinophanhk

[C] Lấy Phần Thập Phân và Số Tự Nhiên

Jun 20th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "string.h"
  3.  
  4. int main(void) {
  5.     char charNumber[30], resultSoNguyen[30], resultThapPhan[30];
  6.     int iLoop, iLength, iResult, iForThapPhan;
  7.    
  8.     printf("Nhap vao mot so thap phan: ");
  9.     scanf("%s", charNumber);
  10.    
  11.     iLength = (int) strlen(charNumber);
  12.    
  13.     for (iLoop = 0; iLoop < iLength; iLoop++) {
  14.         resultSoNguyen[iLoop] = charNumber[iLoop];
  15.         if (charNumber[iLoop + 1] == ',') {
  16.             iResult = iLoop;
  17.             break;
  18.         }
  19.         if (charNumber[iLoop + 1] == '\0') {
  20.             iResult = iLoop;
  21.             break;
  22.         }
  23.     }
  24.     resultSoNguyen[iResult + 1] = '\0';
  25.    
  26.     iForThapPhan = 0;
  27.     for (iLoop = iResult + 2; iLoop < iLength; iLoop++) {
  28.         resultThapPhan[iForThapPhan] = charNumber[iLoop];
  29.         iForThapPhan++;
  30.     }
  31.    
  32.     resultThapPhan[iForThapPhan] = '\0';
  33.    
  34.     if ((int)strlen(resultThapPhan) == 0) {
  35.         resultThapPhan[0] = '0';
  36.         resultThapPhan[1] = '\0';
  37.     }
  38.    
  39.     printf("So nguyen la: %s.\nPhan thap phan la: %s.\n", resultSoNguyen, resultThapPhan);
  40.    
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement