PDA

View Full Version : سوال: مشکل این کد چیه ؟



vb8334
شنبه 23 مرداد 1389, 18:49 عصر
با سلام به تمامی دوستان

دوستان مشکل این کد چیه ؟




#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{


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;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}




ممنون

حامد مصافی
شنبه 23 مرداد 1389, 19:32 عصر
main بيشتر از يك بار تعريف شده
به چيز ديگري توجه نكردم، همين براي كار نكردن كافيست

vb8334
شنبه 23 مرداد 1389, 19:50 عصر
من main اضافه رو پاک کردم ولی باز ابراد میگیره

حامد مصافی
شنبه 23 مرداد 1389, 19:55 عصر
متن خطا و شماره سطر مرتبط را ذكر كنيد.

vb8334
شنبه 23 مرداد 1389, 23:31 عصر
از این خط ارور میگیره


nodeptr* insert_num(){

Go To Line :46

این خط رو هم پاک کردم :
system("PAUSE");
return EXIT_SUCCESS;
}

developing
یک شنبه 24 مرداد 1389, 10:18 صبح
با سلام.

خطاهایی که من برطرف کردم و برنامه در CBuilder 2009 اجرا شد اینها هستند:

1- اضافه کردن سرآیند conio
2- حذف خطوط 6 و 7 از برنامه
3- حذف 3 خط آخر برنامه

اگه درست نشد دقیقا خطا رو بنویس تا برطرفش کنیم.

امیدوارم جوابت رو بگیری.

vb8334
یک شنبه 24 مرداد 1389, 14:34 عصر
ممنون دوست عزیز میشه کدی رو که Debug کردین اینجا بزارین

چون من با dev کار میکنم ولی باز یه ایراداتی داره

ممنون

developing
یک شنبه 24 مرداد 1389, 19:31 عصر
با سلام.

کد به صورت زیر در CBuider 2009 استفاده شده

نکته : قسمت هایی که با فونت قرمز نوشتم تغییرات هست. یعنی فقط همین قسمت‎ها تغییر کرده‎اند تا برنامه شما اجرا شده است.



#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;

//int main(int argc, char *argv[])
//{


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;
}
}
// system("PAUSE");
// return EXIT_SUCCESS;
//}

عکس زیر هم در زمان اجرای برنامه گرفته شده.

vb8334
یک شنبه 24 مرداد 1389, 21:57 عصر
ممنون دوست عزیز که پاسخ میدید

برای من از این قسمت ایراد میگیره البته اون قسمت هایی رو که گفتید حذف کردم



void main(){


چون حجم عکس از حجم آپلودی زیاد بود من زیپ کردم

ممنون

drstrike
دوشنبه 25 مرداد 1389, 05:03 صبح
برای من از این قسمت ایراد میگیره البته اون قسمت هایی رو که گفتید حذف کردم

دوست عزیز این اررورها که داره فارسی باهات حرف می زنه! :چشمک:
دو تا اررور اول میگه تابع main باید یه مقدار int برگردونه. به جای void main از int main استفاده کن، آخر برنامه هم یه return 0 بزار.
اررور سوم هم میگه تابع clrscr اعلان نشده. سالار تو یه تاپیک گفته بود که از این تابع تو Dev چطوری باید استفاده کرد.

vb8334
دوشنبه 25 مرداد 1389, 11:28 صبح
ممنون دوستان مشکل برطرف شد