#include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <string.h> char *MulLargeNum(char *mulA ,char *mulB, char *result) { if(mulA == NULL || mulB == NULL) return NULL; int lengthA = strlen(mulA); int lengthB = strlen(mulB); char *rBuf = new char[lengthA + lengthB + 1]; memset(rBuf,0 ,lengthA + lengthB + 1); memset(result,0 ,lengthA + lengthB + 1); for (int i = 0; i < lengthA; i++) { for (int j = 0; j < lengthB; j++) { rBuf[i+j+1] += (mulA[i] -'0')*(mulB[j]-'0'); } } for(int i = lengthA + lengthB - 1; i>0 ;i--) { rBuf[i-1] += rBuf[i]/10; rBuf[i] = rBuf[i]%10+'0'; } if(rBuf[0] > 0) { memcpy(result,rBuf,strlen(rBuf)); } else { memcpy(result,&rBuf[1],strlen(&rBuf[1])); } printf("%s * %s = %s ",mulA,mulB,result); delete rBuf; return result; } int _tmain(int argc, _TCHAR* argv[]) { char *a = "1234"; char *b = "4567"; char buf[1000]; MulLargeNum(a,b,buf); return 0; }
本题主要考察点 1:函数返回值,为何返回值为char*,因为链式表达式可能使用
2:参数检查
3:基本算法功力