散分,散分,求指点

strive_x 2011-02-25 11:59:56
前2天发的帖子由于高亮问题,,沉底了~~再发一遍,希望能获得收获~
初学C语言,为了练手,写了个大数加法/乘法。第一个是为了练手没有考虑效率问题。请大家帮看看语法,结构还有其他各个方面,尽量提意见。
第二个程序是参照了别人的思想写的大数乘法。
中肯的意见我会多给分。不够可以追加 ^^
另外,还想请高人指点下。我下一步再如何往下学。


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define CHAR_STEP 50
#define CHAR_MAX_NUM 1000
#define DEBUG 0

char *dynamicState(char *p); //动态开内存地址
int multiplication(char *, char*, char*); //大数乘法
int addition(const char *, const int, const char *, const int, char *); //大数加法
int char2int(char *); //char数组转换int数组
int int2char(char *, int); //int数组转换char数组

int main(){
char *s1, *s2, *result;
int len=0;

printf("x=");
s1 = dynamicState(s1);

printf("y=");
s2 = dynamicState(s2);

len = strlen(s1)+strlen(s2);

result = (char*)malloc(sizeof(char)*len);
multiplication(s1, s2, result);

printf("result=%s\n", result);
return 0;
}

int multiplication(char *s1, char *s2, char *mul){
int templen=0, len1, len2;
int i=0, j=0, k=0;
len1 = char2int(s1);
len2 = char2int(s2);

char *temp_mult;
temp_mult = malloc(sizeof(char)*(len1+len2));

for(i=0; i<len1; i++)
for(j=0; j<len2; j++){
//补0.
for(k=0; k<i+j; k++){
temp_mult[k] = 0;
}
temp_mult[i+j] = (s1[i] * s2[j])%10;
temp_mult[i+j+1] = (s1[i] * s2[j])/10;
//循环调用加法
templen = addition(temp_mult, i+j+2, mul, templen, mul);
}
int2char(mul, len1+len2+1);
free(temp_mult);
return templen;
}

int addition(const char *s1, const int ns1, const char *s2, const int ns2, char *sum){
int i=0, len=0;
int temp_sum=0;
int remainder=0, carry=0;
int add1=0, add2=0;
len = (ns1 > ns2) ? ns1 : ns2;
for(i=0; i<len+1; i++){
add1 = (i>ns1-1) ? 0 : s1[i];
add2 = (i>ns2-1) ? 0 : s2[i];
temp_sum = add1 + add2;
remainder = temp_sum%10;
sum[i] = (remainder + carry)%10;
carry = ((remainder + carry) > 9) ? temp_sum/10 + 1 : temp_sum/10;
}
return len+1;
}

int int2char(char *p, int len){
int i=0;
char *temp = p;

while(i < len)
p[i++] = p[i] + '0';
if(p[len-2] == '0'){
len += -1;
i++;
}
i=0;
//倒序排列
while(i < len-2-i){
p[i] = p[i] + temp[len-2-i];
temp[len-2-i] = p[i] - temp[len-2-i];
p[i++] = p[i] - temp[len-2-i];
}
p[len-1] = '\0';
return len;
}

int char2int(char *p){
int i=0;
char *temp = p;
int len = strlen(temp);
while(i < len)
p[i++] = p[i] - '0';
i=0;
//倒序排列
while(i < len-1-i){
p[i] = p[i] + temp[len-1-i];
temp[len-1-i] = p[i] - temp[len-1-i];
p[i++] = p[i] - temp[len-1-i];
}
p[len] = '\0';
return len;
}

char *dynamicState(char *p){
int ch=0, i=0;
int char_max_num = CHAR_MAX_NUM;
p = (char *)malloc(sizeof(char)*(char_max_num + 1));
while((ch = getchar()) != '\n'){
if(i > char_max_num){
char_max_num += CHAR_STEP;
p = realloc(p, sizeof(char) * (char_max_num + 1));
}
p[i++] = ch;
}
p[i]='\0';
return p;
}




第二个程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define CHAR_STEP 50
#define CHAR_MAX_NUM 1000

char *dynamicState(char *p);
int multiplication(const char *,const char*, char*);

int main(){
char *s1, *s2, *result;
int len=0;
int t1=0, t2=0;

t1 = clock();
printf("x=");
s1 = dynamicState(s1);

printf("y=");
s2 = dynamicState(s2);

len = strlen(s1)+strlen(s2);

result = (char*)malloc(sizeof(char)*len);
multiplication(s1, s2, result);

printf("result=%s\n", result);
t2=clock();
printf("time=%d\n", t2-t1);
return 0;
}

int multiplication(const char *s1,const char *s2, char *mul){
int *p;
int i=0,j=0;
int len1=0, len2=0, len=0;

len1 = strlen(s1);
len2 = strlen(s2);
len = len1+len2;

p = (int*)calloc(len, sizeof(int));
for(i=0; i<len1; i++)
for(j=0;j<len2;j++)
p[i+j] += (s1[len1-1-i] - '0') * (s2[len2-1-j] - '0');

i=0;
while(i<len){
if(p[i] > 9){
p[i+1] += p[i]/10;
}
p[i] = p[i]%10;
i++;
}
i=0;
while(i < len){
mul[len-1-i] = p[i] + '0';
i++;
}
mul[len] = '\0';
mul[0] = (mul[0] == '0') ? 32 : mul[0];
free(p);
return 0;
}

char *dynamicState(char *p){
int ch=0, i=0;
int char_max_num = CHAR_MAX_NUM;
p = (char *)malloc(sizeof(char)*(char_max_num + 1));
while((ch = getchar()) != '\n'){
if(i > char_max_num){
char_max_num += CHAR_STEP;
p = realloc(p, sizeof(char) * (char_max_num + 1));
}
p[i++] = ch;
}
p[i]='\0';
return p;
}


...全文
148 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2011-02-25
  • 打赏
  • 举报
回复
随便怎么写,写出来就行了,模拟笔算而已.
wyfwx 2011-02-25
  • 打赏
  • 举报
回复
for(i=0; i<len1; i++)
for(j=0; j<len2; j++){
//补0.
for(k=0; k<i+j; k++){
temp_mult[k] = 0;
}
temp_mult[i+j] = (s1[i] * s2[j])%10;
temp_mult[i+j+1] = (s1[i] * s2[j])/10;
//循环调用加法
templen = addition(temp_mult, i+j+2, mul, templen, mul);
}
不是我喜欢的代码风格
yjjlyyj151 2011-02-25
  • 打赏
  • 举报
回复
bdmh 2011-02-25
  • 打赏
  • 举报
回复
if(i > char_max_num){ //应该是>= 吧?,不该是吧,数组下标从0开始,char_max_num位应该是结束符了
昵称很不好取 2011-02-25
  • 打赏
  • 举报
回复
char *dynamicState(char *p){
int ch=0, i=0;
int char_max_num = CHAR_MAX_NUM;
p = (char *)malloc(sizeof(char)*(char_max_num + 1));
while((ch = getchar()) != '\n'){
if(i > char_max_num){ //应该是>= 吧?
char_max_num += CHAR_STEP;
p = realloc(p, sizeof(char) * (char_max_num + 1));
}
p[i++] = ch;
}
p[i]='\0';
return p;
}

昵称很不好取 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 strive_x 的回复:]
引用 8 楼 thefirstz 的回复:
引用 2 楼 bdmh 的回复:
if(i > char_max_num){ //应该是>= 吧?,不该是吧,数组下标从0开始,char_max_num位应该是结束符了


char_max_num作为下标时就越界了啊

我开的数组长度是char_max_num + 1啊。用char_max_num当下标时,不会越界吧?
[/Quote]
+1不会,没注意到+1,抱歉~~
strive_x 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wyfwx 的回复:]
for(i=0; i<len1; i++)
for(j=0; j<len2; j++){
//补0.
for(k=0; k<i+j; k++){
temp_mult[k] = 0;
}
temp_mult[i+j] = (s1[i] * s2[j])%10;
temp_mult[i+j+1] = (s1[i] * s2[j])/10;
//循环调用……
[/Quote]
那你喜欢的是什么样呢?可以发来看看嘛。
strive_x 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 thefirstz 的回复:]
引用 2 楼 bdmh 的回复:
if(i > char_max_num){ //应该是>= 吧?,不该是吧,数组下标从0开始,char_max_num位应该是结束符了


char_max_num作为下标时就越界了啊
[/Quote]
我开的数组长度是char_max_num + 1啊。用char_max_num当下标时,不会越界吧?
昵称很不好取 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]
if(i > char_max_num){ //应该是>= 吧?,不该是吧,数组下标从0开始,char_max_num位应该是结束符了
[/Quote]

char_max_num作为下标时就越界了啊

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧