runtimeerror。。。。求指导。。。
程序代码:
#include<stdio.h> #include<string.h> #include<malloc.h> char* cal(char *p, char *q); int main() { char p[] = ""; int n = 0, i = 0; while(scanf("%s%d", p, &n) != EOF) { //初始检查,小数,位置记录 char * result = p; for(i = 0; i < n - 1; i++) { result = cal(result, p); } printf("%s", result); } } ///两个大数的乘法 char* cal(char *p, char *q) { // int pLength = strlen(p); int qLength = strlen(q); int m = pLength - 1, n = qLength - 1;//用于记录两个数的小数点所在位置 int i = 0, j = 0, k = 0; int isDot = 0; char* result; int * s = (int*)malloc(sizeof(int)*(pLength + qLength)); for(i = 0; i < pLength + qLength; i++) s[i] = 0; for(j = 0; j < pLength - 1; j++) { if(p[j] == '.') { isDot = 1; m = j; } if(isDot) { p[j] = p[j+1]; } } isDot = 0; for(j = 0; j < qLength - 1; j++) { if(q[j] == '.') { isDot = 1; n = j; } if(isDot) { q[j] = q[j+1]; } } for(i = 0; i < pLength; i++) { for(j = 0; j < qLength; j++) { s[i + j + 1] += (p[i] - '0') * (q[j] - '0'); } } m = pLength - m - 1;//得到小数点后位置 n = qLength - n - 1; for(k = pLength + qLength - 1; k > 0; k--) { if(s[k] >= 10) { s[k - 1] += s[k] / 10; s[k] = s[k] % 10; } } if(m + n > 0) { while(k < pLength + qLength - m - n) { s[k] = s[k + 1]; k++; } s[k] = -2; } result = (char*)malloc(sizeof(char)*(pLength + qLength)); i = 0; while(s[i] == 0) i++; for(k = 0; i < pLength + qLength; k++, i++) { result[k] = s[i] + '0'; } result[k] = '\0'; return result; }
老是runtimeerror
[ 本帖最后由 itma 于 2013-10-20 00:30 编辑 ]