大数相乘的c代码

#include "stdafx.h" #include <string.h> // 字符串反转 void reverse_string(char* pszBuffer) { unsigned int length = strlen(pszBuffer); for (size_t i = 0; i < length / 2; i++) { char temp = pszBuffer[i]; pszBuffer[i] = pszBuffer[length - i - 1]; pszBuffer[length - i - 1] = temp; } } // 个位数与大整数的乘法 void multiply_one_digits(const char* numToMultiple, char* pResultBuffer, unsigned char num) { unsigned int length = strlen(numToMultiple); unsigned int adjust = 0; unsigned int index = 0; unsigned int i = 0; while (i < length) { unsigned char charToMultiply = numToMultiple[i]; unsigned int numToMultiply = charToMultiply - 0x30; unsigned int result = numToMultiply * num + adjust; adjust = result / 10; result = result % 10; pResultBuffer[index++] = (char)(result + 0x30); i++; } if (adjust != 0) { pResultBuffer[index] = (char)(adjust + 0x30); } } //去除小数点前无效的0,如0.1234最后会转化为1234 void remove_zero(char* number) { size_t i = 0; while (number[i] == '0') { size_t len = strlen(number); for (int j = 0; j < len - 1; j++) { number[j] = number[j+1]; } number[j] = '/0'; } } // 去除乘数里的小数点 void remove_dot(char* number) { remove_zero(number); size_t remove_index = 0; bool need_remove = false; size_t len = strlen(number); for (size_t i = 0; i < len; i++) { if (number[i] == '.') { need_remove = (i == 0 ? true : false); while (i < len - 1) { char ch_next = number[i+1]; number[i] = ch_next; i++; } number[len - 1] = '/0'; break; } } if (need_remove) { remove_zero(number); } } // 去掉小数点后无效的0 void trim_zero(char* number) { unsigned int len = strlen(number); if (number[0] == '0') { for (size_t i = 0; i < len - 1; i++) { number[i] = number[i+1]; } number[i] = '/0'; } } // 初始化被乘数 void setup_num(char* result, const char* num_multiplyed, unsigned int pos) { strcpy(result, num_multiplyed); while (pos > 0) { unsigned int len = strlen(result); for (size_t i = len; i > 0; i--) { result[i] = result[i-1]; } result[0] = '0'; result[len + 1] = '/0'; pos--; } } // 保存一次计算的结果 void save_result(char* pResultBuffer, char* added) { unsigned int adjust = 0; if (strlen(pResultBuffer) < strlen(added)) { unsigned append_len = strlen(added) - strlen(pResultBuffer); for (size_t j = 0; j < append_len; j++) { strcat(pResultBuffer, "0"); } } for (size_t i = 0; i < strlen(pResultBuffer); i++) { unsigned int current = pResultBuffer[i] - 0x30; char to_add = '0'; if (i < strlen(added)) { to_add = added[i]; } else { int test = 0; } unsigned int value = current + adjust + to_add - 0x30; adjust = value / 10; pResultBuffer[i] = value % 10 + 0x30; } if (adjust != 0) { pResultBuffer[i] = adjust + 0x30; pResultBuffer[i+1] = '/0'; } } // 如果乘1000,则移动3位,该函数通过补0达到移位的目的 void insert_zero(char* pResultBuffer, unsigned int zero_num) { size_t i = zero_num; while (i) { size_t len = strlen(pResultBuffer); for (size_t j = len; j > 0; j--) { pResultBuffer[j] = pResultBuffer[j-1]; } pResultBuffer[0] = '0'; i--; } } // 调整计算结果:在正确的位置插入小数点 void adjust_result(char* pResultBuffer, unsigned int move_right_bits) { unsigned int len = strlen(pResultBuffer); if (move_right_bits > 0) { if (move_right_bits > len) { insert_zero(pResultBuffer, move_right_bits - len); len = strlen(pResultBuffer); } for (size_t i = len; i > len - move_right_bits ; i--) { pResultBuffer[i] = pResultBuffer[i-1]; } pResultBuffer[len - move_right_bits] = '.'; } if (pResultBuffer[0] == '.') { unsigned result_len = strlen(pResultBuffer); for (size_t j = result_len; j > 0; j--) { pResultBuffer[j] = pResultBuffer[j-1]; } pResultBuffer[0] = '0'; } } // 大数相乘的函数 void multiply_huge( char* num_be_multiplyed, char* multiplyer, char* pResultBuffer) { unsigned int dot_pos_num1 = 0; unsigned int dot_pos_num2 = 0; char ch = '.'; char* pdest1 = strchr(num_be_multiplyed, ch); char* pdest2 = strchr(multiplyer, ch); unsigned int move_right_1 = 0; if (pdest1 != NULL) { move_right_1 = (strlen(num_be_multiplyed) - (pdest1 - num_be_multiplyed) - 1); } unsigned int move_right_2 = 0; if (pdest2 != NULL) { move_right_2 = (strlen(multiplyer) - (pdest2 - multiplyer) - 1); } remove_dot(num_be_multiplyed); remove_dot(multiplyer); trim_zero(num_be_multiplyed); trim_zero(multiplyer); reverse_string(num_be_multiplyed); reverse_string(multiplyer); char* num_multiplyed = num_be_multiplyed; for (size_t i = 0; i < strlen(multiplyer); i++) { char* num_to_multiplied = new char[strlen(num_be_multiplyed) + i + 1]; memset(num_to_multiplied, 0, strlen(num_be_multiplyed) + i + 1); char* pTempResult = new char[strlen(num_be_multiplyed) + i + 2]; memset(pTempResult, 0, strlen(num_be_multiplyed) + i + 2); setup_num(num_to_multiplied, num_multiplyed, i); multiply_one_digits(num_to_multiplied, pTempResult, multiplyer[i] - 0x30); save_result(pResultBuffer, pTempResult); delete [] pTempResult; delete [] num_to_multiplied; } reverse_string(num_be_multiplyed); reverse_string(multiplyer); reverse_string(pResultBuffer); unsigned int move_right = move_right_1 + move_right_2; adjust_result(pResultBuffer, move_right); } // Test Sample int main() { char* pBuffer = new char[100]; memset(pBuffer, 0, 100); char multiplied[] = "111111111110.2"; char multiplyer[] = "22222222222.3"; multiply_huge(multiplied, multiplyer, pBuffer); printf("%s/r/n", pBuffer); delete [] pBuffer; pBuffer = NULL; return 0; }

 

 

你可能感兴趣的:(c,String,null,delete,insert,include)