PDA

View Full Version : تغییر برنامه از C++ به C



delphi_bir
سه شنبه 06 آذر 1386, 21:27 عصر
با سلام !
من برنامه زیر که مربوط به حرکت در هزارتو هستش رو دارم که دستورات آن در C++ نوشته شده است، حال میخواهم آنرا به C برگردونم . دوستان ممنون میشم راهنمایی نمایید که چه تغییراتی لازم هست که در زیر بدم؟
(نیز اگه در مورد اشکالات موجود در زیر راهنمایی نمایید خیلی ممنون میشم)
بسیار ممنونم.

struct offsets
{
Int a,b ;
};
Enum directions {N,NE,E,SE,S,SW,W,NW};

Offsets move[8] ;

While (stack is not empty)
{
(i,j,dir) = coordinates and direction deleted from top of stack;
While (there are more moves )
{
(g,h) = coordinates of next move;
if ((g == m)&&(h == p)) success;
if ((!maze [g][h]) // legal move
&& (!mark [g][h]) // have not been here before
{
Mark [g][h] = 1;
Dir = next direction to try;
Add (i,j,dir)to top of stack;
i=g; j=h; dir=north;
}
}
}
Cout <<"no path found "<<end1;

Void path(int m,int p)
// output a path(if any) in the maze ; maze [o][i] = maze [m+1][i] =
// maze [j][o] = maze [j][p+1] =1,0 ≤ i≤ p+1,0 ≤ j ≤ m+1.
{
// start at (1,1)
Mark [1][1] = 1;
Stack < items > stack ( m* p);
Items temp;
Temp.x = 1; temp.y =1; temp.dir = E;
Stack.Add(temp);
While (!stack.IsEmpty()) // stack not empty
{
Temp = *stack.delete(temp);//umstack
int i = temp.x; int j = temp.y;int d = temp.dir;
while(d<8)//move forward
{
int g = i + move [d] .a; int h = j +move [d] .b;
if ((g == m ) &&(h == p)) { // reached exit
// output path
cout << stack;
cout << i<<" "<< j<< end1; // last two squares on the path
cout << m<<" "<< p << end1;
return;
}


if ((!maze [g][h]) &&(!mark [g][h])){ // new position
mark [g][h] = 1;
temp.x = i; temp.y = j; temp.dir = d+1;
stack.Add(temp); // stack it
i = g; j = h; d = N; // move to ( g,h)
}
else d ++ ; // try next direction
}
}
Cout << " no path in maze " << end1;


Ostream & operator <<(ostream & os,stack < keyType>& s)
{
Os<<"top="<<s.top<<end1;
For(int i=o; i<=s.top; i++)
Os<<i<<":"<<s.stack[i]<< end1;
Return os;
}
Ostream& operator<<(ostream& os, items& item)
{
Return os<<item.x<<","<<item.y<<","<< item.dir;
}


نیاز فوری!

powerboy2988
سه شنبه 06 آذر 1386, 22:41 عصر
struct offsets
{
Int a,b ;
};
Enum directions {N,NE,E,SE,S,SW,W,NW};

Offsets move[8] ;

While (stack is not empty)
{
(i,j,dir) = coordinates and direction deleted from top of stack;
While (there are more moves )
{
(g,h) = coordinates of next move;
if ((g == m)&&(h == p)) success;
if ((!maze [g][h]) // legal move
&& (!mark [g][h]) // have not been here before
{
Mark [g][h] = 1;
Dir = next direction to try;
Add (i,j,dir)to top of stack;
i=g; j=h; dir=north;
}
}
}
Cout <<"no path found "<<end1;

Void path(int m,int p)
// output a path(if any) in the maze ; maze [o][i] = maze [m+1][i] =
// maze [j][o] = maze [j][p+1] =1,0 ≤ i≤ p+1,0 ≤ j ≤ m+1.
{
// start at (1,1)
Mark [1][1] = 1;
Stack < items > stack ( m* p);
Items temp;
Temp.x = 1; temp.y =1; temp.dir = E;
Stack.Add(temp);
While (!stack.IsEmpty()) // stack not empty
{
Temp = *stack.delete(temp);//umstack
int i = temp.x; int j = temp.y;int d = temp.dir;
while(d<8)//move forward
{
int g = i + move [d] .a; int h = j +move [d] .b;
if ((g == m ) &&(h == p)) { // reached exit
// output path
cout << stack;
cout << i<<" "<< j<< end1; // last two squares on the path
cout << m<<" "<< p << end1;
return;
}


if ((!maze [g][h]) &&(!mark [g][h])){ // new position
mark [g][h] = 1;
temp.x = i; temp.y = j; temp.dir = d+1;
stack.Add(temp); // stack it
i = g; j = h; d = N; // move to ( g,h)
}
else d ++ ; // try next direction
}
}
Cout << " no path in maze " << end1;


Ostream & operator <<(ostream & os,stack < keyType>& s)
{
Os<<"top="<<s.top<<end1;
For(int i=o; i<=s.top; i++)
Os<<i<<":"<<s.stack[i]<< end1;
Return os;
}
Ostream& operator<<(ostream& os, items& item)
{
Return os<<item.x<<","<<item.y<<","<< item.dir;
}

اول برنامه رو مرتب بنویس

powerboy2988
سه شنبه 06 آذر 1386, 22:48 عصر
دستوراتی که باید تو c رو بنویسی رو معادلشو جلوش نوشتم
اگه غلطی داشتم شرمنده و از دوستان می خوام که درست کنند


#include <stdio.h>

struct offsets
{
Int a,b ;
};
Enum directions {N,NE,E,SE,S,SW,W,NW};

Offsets move[8] ;

While (stack is not empty)
{
(i,j,dir) = coordinates and direction deleted from top of stack;
While (there are more moves )
{
(g,h) = coordinates of next move;
if ((g == m)&&(h == p)) success;
if ((!maze [g][h]) // legal move
&& (!mark [g][h]) // have not been here before
{
Mark [g][h] = 1;
Dir = next direction to try;
Add (i,j,dir)to top of stack;
i=g; j=h; dir=north;
}
}
}
Cout <<"no path found "<<end1; ////// printf("no path found");

Void path(int m,int p)
// output a path(if any) in the maze ; maze [o][i] = maze [m+1][i] =
// maze [j][o] = maze [j][p+1] =1,0 ≤ i≤ p+1,0 ≤ j ≤ m+1.
{
// start at (1,1)
Mark [1][1] = 1;
Stack < items > stack ( m* p);
Items temp;
Temp.x = 1; temp.y =1; temp.dir = E;
Stack.Add(temp);
While (!stack.IsEmpty()) // stack not empty
{
Temp = *stack.delete(temp);//umstack
int i = temp.x; int j = temp.y;int d = temp.dir;
while(d<8)//move forward
{
int g = i + move [d] .a; int h = j +move [d] .b;
if ((g == m ) &&(h == p)) { // reached exit
// output path
cout << stack; //////printf(stack);
cout << i<<" "<< j<< end1; // last two squares on the path ////// printf(i," ",j);
cout << m<<" "<< p << end1; ////// printf(m ," ",p);
return;
}


if ((!maze [g][h]) &&(!mark [g][h])){ // new position
mark [g][h] = 1;
temp.x = i; temp.y = j; temp.dir = d+1;
stack.Add(temp); // stack it
i = g; j = h; d = N; // move to ( g,h)
}
else d ++ ; // try next direction
}
}
Cout << " no path in maze " << end1; ////// printf("no path found");


Ostream & operator <<(ostream & os,stack < keyType>& s)
{
Os<<"top="<<s.top<<end1;
For(int i=o; i<=s.top; i++)
Os<<i<<":"<<s.stack[i]<< end1;
Return os;
}
Ostream& operator<<(ostream& os, items& item)
{
Return os<<item.x<<","<<item.y<<","<< item.dir;
}

delphi_bir
چهارشنبه 07 آذر 1386, 07:29 صبح
ممنون از پاسخ شما.
در اجرای برنامه از عبارت

while <stack is not empty>
اشکال میگیره . میشه بگین مشکل کجاست.
ممنونم

powerboy2988
چهارشنبه 07 آذر 1386, 08:37 صبح
ممکنه اون مشکلاتی رو کع میگیره رو بگی؟/

delphi_bir
چهارشنبه 07 آذر 1386, 08:55 صبح
Undefined symbol 'stack'

Nima_NF
پنج شنبه 08 آذر 1386, 18:47 عصر
من خوب متوجه نشدم تابع stack را چطوری و از کجا در زبان C دارید استفاده می کنید ، چون stack جز توابع C++ run-time هست که از سال 1995 به آن اضافه شده است و header فایل آن هم <stack> هست و نمونه های آن هم با s کوچک تعریف می شوند و این توابع یکی از مهم ترین تفاوت های C++ , C است. شما از چه در C استفاده کردید ؟
ضمنا متد های stack دارای push و pop هست نه Add, پس احتمالا از توابع غیر استاندارد دیگری دارید استفاده می کنید.

ضمنا جناب delphi_bir جمله ( While (stack is not empty اصلا در زبان وجود ندارد و شبهه کد است و شما باید آن را با تست پر یا خالی بودن stack انجام دهید.

delphi_bir
جمعه 09 آذر 1386, 19:39 عصر
با تشکر از شما!
این برنامه یک قطعه برنامه هست . به همین جهت تعریف نشده است.
اگه میشه راجع به این دستور بیشتر توضیح بدین .
ممنونم.
چطوری میشه تعریف کنم؟