Arash m
یک شنبه 16 تیر 1387, 00:09 صبح
سلام.
یه برنامه برای ضرب اعداد خیلی بزرگ با استفاده از لیست پیوندی نوشتم که برای شما گذاشتم.
امیوارم به کارتون بیاد.
این هم کد کامل برنامه:
#include<iostream.h>
#include<conio.h>
const int null='\0';
struct nodeptr{
int data;
struct nodeptr *left;
struct nodeptr *right;
};
void del_nodeptr(nodeptr*);
nodeptr* add_node(int,nodeptr**,int=0,int=1);
nodeptr* insert_num();
nodeptr* sum_nodeptr(nodeptr*,nodeptr*);
nodeptr* multiplication_nodeptr(nodeptr*,nodeptr*);
void display_nodeptr(nodeptr*);
void main(){
clrscr();
struct nodeptr *a,*b,*c;
cout<<"Welcome to arash matinkhah's multiplication & sum project.\n\n\nplease enter number a : ";
a=insert_num();
cout<<"\n\nplease enter number b : ";
b=insert_num();
clrscr();
cout<<"a*b = ";
c=multiplication_nodeptr(a,b);
display_nodeptr(c);
del_nodeptr(c);
getch();
clrscr();
cout<<"a+b = ";
c=sum_nodeptr(a,b);
display_nodeptr(c);
getch();
del_nodeptr(a);
del_nodeptr(b);
del_nodeptr(c);
}
nodeptr* insert_num(){
int counter=0;
struct nodeptr *rtn;
char ch;
while(((ch=getch())!='\r')||counter==0)
{
if(ch>=48 && ch<=57){
putch(ch);
add_node(ch-48,&rtn,0,counter);
counter++;
}
}
cout<<"\n";
return rtn;
}
//...........................
nodeptr* sum_nodeptr(nodeptr *a,nodeptr *b){
int counter=0,sum,tmp1,tmp2,help=0,end_a=0,end_b=0;
nodeptr *rtn;
while(a->right!=null){a=a->right;}
while(b->right!=null){b=b->right;}
while((end_a==0)||(end_b==0)){
if(end_a==0){
if(a->left==null)
end_a=1;
tmp1=a->data;
}else tmp1=0;
if(end_b==0){
if(b->left==null)
end_b=1;
tmp2=b->data;
}else tmp2=0;
if((tmp1+tmp2+help)<=9){
sum=tmp1+tmp2+help;
help=0;
}
else{
sum=(tmp1+tmp2+help)%10;
help=(tmp1+tmp2+help)/10;
}
rtn=add_node(sum,&rtn,1,counter);
counter++;
if(end_a==0)
a=a->left;
if(end_b==0)
b=b->left;
}
if(help>0)
rtn=add_node(help,&rtn,1,counter);
return rtn;
}
nodeptr* multiplication_nodeptr(nodeptr *a,nodeptr *b){
int mltpli,i,zcntr=0,counter=0,tmp,help=0,end_a=0,end_ b=0;
nodeptr *rtn_tmp,*rtn,*b_tmp,*del_rtn;
add_node(0,&rtn,0,0);
while(a->right!=null){a=a->right;}
while(b->right!=null){b=b->right;}
while(end_a==0){
if(a->left==null)
end_a=1;
for(i=0;i<zcntr;i++){
rtn_tmp=add_node(0,&rtn_tmp,1,counter);
counter++;
}
end_b=0;
b_tmp=b;
while(end_b==0){
if(b_tmp->left==null)
end_b=1;
tmp=((a->data)*(b_tmp->data))+help;
if(tmp>9){
mltpli=tmp%10;
help=tmp/10;
}
else{
mltpli=tmp;
help=0;
}
rtn_tmp=add_node(mltpli,&rtn_tmp,1,counter);
counter++;
if(end_b==0)
b_tmp=b_tmp->left;
}
if(help>0){
rtn_tmp=add_node(help,&rtn_tmp,1,counter);
help=0;
}
counter=0;
zcntr++;
del_rtn=rtn;
rtn=sum_nodeptr(rtn,rtn_tmp);
del_nodeptr(del_rtn);
del_nodeptr(rtn_tmp);
if(end_a==0)
a=a->left;
}
return rtn;
}
nodeptr* add_node(int data,nodeptr **start,int lr,int counter){
static nodeptr *newptr;
nodeptr *end;
end=*start;
newptr=new nodeptr;
newptr->data=data;
if(counter==0){
*start=newptr;
(*start)->left=null;
(*start)->right=null;
}
else{
if(lr==0){
while(end->right!=null)
end=end->right;
end->right=newptr;
newptr->right=null;
newptr->left=end;
}
else{
while(end->left!=null)
end=end->left;
end->left=newptr;
newptr->left=null;
newptr->right=end;
}
}
return newptr;
}
void del_nodeptr(nodeptr *start){
nodeptr *tmp;
int end=0;
while(end==0){
if(start->right==null)
end=1;
else
tmp=start->right;
delete start;
start=tmp;
}
}
void display_nodeptr(nodeptr *p){
int end=0;
while(end==0){
if(p->right==null)
end=1;
cout<<p->data;
p=p->right;
}
}
اگه سوالی داشتین حتما بگین.
یه برنامه برای ضرب اعداد خیلی بزرگ با استفاده از لیست پیوندی نوشتم که برای شما گذاشتم.
امیوارم به کارتون بیاد.
این هم کد کامل برنامه:
#include<iostream.h>
#include<conio.h>
const int null='\0';
struct nodeptr{
int data;
struct nodeptr *left;
struct nodeptr *right;
};
void del_nodeptr(nodeptr*);
nodeptr* add_node(int,nodeptr**,int=0,int=1);
nodeptr* insert_num();
nodeptr* sum_nodeptr(nodeptr*,nodeptr*);
nodeptr* multiplication_nodeptr(nodeptr*,nodeptr*);
void display_nodeptr(nodeptr*);
void main(){
clrscr();
struct nodeptr *a,*b,*c;
cout<<"Welcome to arash matinkhah's multiplication & sum project.\n\n\nplease enter number a : ";
a=insert_num();
cout<<"\n\nplease enter number b : ";
b=insert_num();
clrscr();
cout<<"a*b = ";
c=multiplication_nodeptr(a,b);
display_nodeptr(c);
del_nodeptr(c);
getch();
clrscr();
cout<<"a+b = ";
c=sum_nodeptr(a,b);
display_nodeptr(c);
getch();
del_nodeptr(a);
del_nodeptr(b);
del_nodeptr(c);
}
nodeptr* insert_num(){
int counter=0;
struct nodeptr *rtn;
char ch;
while(((ch=getch())!='\r')||counter==0)
{
if(ch>=48 && ch<=57){
putch(ch);
add_node(ch-48,&rtn,0,counter);
counter++;
}
}
cout<<"\n";
return rtn;
}
//...........................
nodeptr* sum_nodeptr(nodeptr *a,nodeptr *b){
int counter=0,sum,tmp1,tmp2,help=0,end_a=0,end_b=0;
nodeptr *rtn;
while(a->right!=null){a=a->right;}
while(b->right!=null){b=b->right;}
while((end_a==0)||(end_b==0)){
if(end_a==0){
if(a->left==null)
end_a=1;
tmp1=a->data;
}else tmp1=0;
if(end_b==0){
if(b->left==null)
end_b=1;
tmp2=b->data;
}else tmp2=0;
if((tmp1+tmp2+help)<=9){
sum=tmp1+tmp2+help;
help=0;
}
else{
sum=(tmp1+tmp2+help)%10;
help=(tmp1+tmp2+help)/10;
}
rtn=add_node(sum,&rtn,1,counter);
counter++;
if(end_a==0)
a=a->left;
if(end_b==0)
b=b->left;
}
if(help>0)
rtn=add_node(help,&rtn,1,counter);
return rtn;
}
nodeptr* multiplication_nodeptr(nodeptr *a,nodeptr *b){
int mltpli,i,zcntr=0,counter=0,tmp,help=0,end_a=0,end_ b=0;
nodeptr *rtn_tmp,*rtn,*b_tmp,*del_rtn;
add_node(0,&rtn,0,0);
while(a->right!=null){a=a->right;}
while(b->right!=null){b=b->right;}
while(end_a==0){
if(a->left==null)
end_a=1;
for(i=0;i<zcntr;i++){
rtn_tmp=add_node(0,&rtn_tmp,1,counter);
counter++;
}
end_b=0;
b_tmp=b;
while(end_b==0){
if(b_tmp->left==null)
end_b=1;
tmp=((a->data)*(b_tmp->data))+help;
if(tmp>9){
mltpli=tmp%10;
help=tmp/10;
}
else{
mltpli=tmp;
help=0;
}
rtn_tmp=add_node(mltpli,&rtn_tmp,1,counter);
counter++;
if(end_b==0)
b_tmp=b_tmp->left;
}
if(help>0){
rtn_tmp=add_node(help,&rtn_tmp,1,counter);
help=0;
}
counter=0;
zcntr++;
del_rtn=rtn;
rtn=sum_nodeptr(rtn,rtn_tmp);
del_nodeptr(del_rtn);
del_nodeptr(rtn_tmp);
if(end_a==0)
a=a->left;
}
return rtn;
}
nodeptr* add_node(int data,nodeptr **start,int lr,int counter){
static nodeptr *newptr;
nodeptr *end;
end=*start;
newptr=new nodeptr;
newptr->data=data;
if(counter==0){
*start=newptr;
(*start)->left=null;
(*start)->right=null;
}
else{
if(lr==0){
while(end->right!=null)
end=end->right;
end->right=newptr;
newptr->right=null;
newptr->left=end;
}
else{
while(end->left!=null)
end=end->left;
end->left=newptr;
newptr->left=null;
newptr->right=end;
}
}
return newptr;
}
void del_nodeptr(nodeptr *start){
nodeptr *tmp;
int end=0;
while(end==0){
if(start->right==null)
end=1;
else
tmp=start->right;
delete start;
start=tmp;
}
}
void display_nodeptr(nodeptr *p){
int end=0;
while(end==0){
if(p->right==null)
end=1;
cout<<p->data;
p=p->right;
}
}
اگه سوالی داشتین حتما بگین.