Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Function: Convert 'float number' as type string to integer and float type.
- // How to use: Create 2 int number and 1 array char type has number from 0 - 9 and ',' symbol.
- // Set 2 int number by call function: floatFromStringToInt(variable name of type char, int for int, int for float)
- // After call function, your variable will have value before ',' and after ','.
- // Hàm lấy phần nguyên và phần thực của số dạng chuỗi ký tự.
- // Cách dùng: Khai báo 2 biến thuộc kiểu số nguyên, 1 chuỗi ký tự.
- // 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)
- // 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 ','.
- void floatFromStringtoInt(char *charNumber, int *soNguyen, int *thapPhan) {
- char resultSoNguyen[30], resultThapPhan[30];
- int iLoop, iLength, iResult, iForThapPhan, intResultSoNguyen, intResultThapPhan;
- iLength = (int) strlen(charNumber);
- for (iLoop = 0; iLoop < iLength; iLoop++) {
- resultSoNguyen[iLoop] = charNumber[iLoop];
- if (charNumber[iLoop + 1] == ',') {
- iResult = iLoop;
- break;
- }
- if (charNumber[iLoop + 1] == '\0') {
- iResult = iLoop;
- break;
- }
- }
- resultSoNguyen[iResult + 1] = '\0';
- iForThapPhan = 0;
- for (iLoop = iResult + 2; iLoop < iLength; iLoop++) {
- resultThapPhan[iForThapPhan] = charNumber[iLoop];
- iForThapPhan++;
- }
- resultThapPhan[iForThapPhan] = '\0';
- if ((int)strlen(resultThapPhan) == 0) {
- resultThapPhan[0] = '0';
- resultThapPhan[1] = '\0';
- }
- iLength = (int) strlen(resultSoNguyen);
- intResultSoNguyen = 0;
- for (iLoop = 0; iLoop < iLength; iLoop++) {
- switch (resultSoNguyen[iLoop]) {
- case '0':
- intResultSoNguyen = (intResultSoNguyen * 10) + 0;
- break;
- case '1':
- intResultSoNguyen= (intResultSoNguyen * 10) + 1;
- break;
- case '2':
- intResultSoNguyen = (intResultSoNguyen * 10) + 2;
- break;
- case '3':
- intResultSoNguyen = (intResultSoNguyen * 10) + 3;
- break;
- case '4':
- intResultSoNguyen = (intResultSoNguyen * 10) + 4;
- break;
- case '5':
- intResultSoNguyen = (intResultSoNguyen * 10) + 5;
- break;
- case '6':
- intResultSoNguyen = (intResultSoNguyen * 10) + 6;
- break;
- case '7':
- intResultSoNguyen = (intResultSoNguyen * 10) + 7;
- break;
- case '8':
- intResultSoNguyen = (intResultSoNguyen * 10) + 8;
- break;
- case '9':
- intResultSoNguyen = (intResultSoNguyen * 10) + 9;
- break;
- default:
- break;
- }
- }
- iLength = (int) strlen(resultThapPhan);
- intResultThapPhan = 0;
- for (iLoop = 0; iLoop < iLength; iLoop++) {
- switch (resultThapPhan[iLoop]) {
- case '0':
- intResultThapPhan = (intResultThapPhan * 10) + 0;
- break;
- case '1':
- intResultThapPhan = (intResultThapPhan * 10) + 1;
- break;
- case '2':
- intResultThapPhan = (intResultThapPhan * 10) + 2;
- break;
- case '3':
- intResultThapPhan = (intResultThapPhan * 10) + 3;
- break;
- case '4':
- intResultThapPhan = (intResultThapPhan * 10) + 4;
- break;
- case '5':
- intResultThapPhan = (intResultThapPhan * 10) + 5;
- break;
- case '6':
- intResultThapPhan = (intResultThapPhan * 10) + 6;
- break;
- case '7':
- intResultThapPhan = (intResultThapPhan * 10) + 7;
- break;
- case '8':
- intResultThapPhan = (intResultThapPhan * 10) + 8;
- break;
- case '9':
- intResultThapPhan = (intResultThapPhan * 10) + 9;
- break;
- default:
- break;
- }
- }
- *soNguyen = intResultSoNguyen;
- *thapPhan = intResultThapPhan;
- }
- // 2012 Dino Phan
- /* Example/Mẫu:
- int main(void) {
- char myArrayChar[30];
- int nguyen, thapphan;
- printf("Enter float number: ");
- gets(myArrayChar);
- floatFromStringtoInt(myArrayChar, &nguyen, &thapphan);
- printf("eQual: %d.\n", nguyen + thapphan);
- return 0;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement