{
......
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX2;j++)
{
......
}
}
......
}
fun2()
{
......
for(i=MAX;i>0;i--)
{
for(j=0;j<MAX2;j++)
{
......
}
}
......
}
fun3()
{
......
for(i=MAX;i>0;i--)
{
for(j=MAX2;j>0;j--)
{
......
}
}
......
}
我的Fun1到Fun8都只有For里面的条件不一样,其它地方代码完全一样.
有没有办法只写一个函数就行了?比如宏定义之类的去代替两个For() ?
前提是宏定义能不能随着变量动态改变的?
15 个解决方案
#1
#define funx(offset_x, offset_y) do{\
int i, j; \
for(i=0; i<MAX; i+=offset_x;) { \
for(j=0; j<MAX2; j+=offset_y;) { \
/* do something you want */ \
} \
} \
} while(0);
#define fun1() funx(1, 1)
#define fun2() funx(-1, 1)
这样试试
#2
将for循环里结束条件提出来,设为函数的参数不就行了?!
#3
看你传的参数类型,进行重载就是,可以用继承等方式
#4
funX( int M )
{
......
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX2;j++)
{
switch(M)
{
case 0:..........; break;
case 1:..........;break;
case 2:..........等等
}
}
}
......
}
{
......
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX2;j++)
{
switch(M)
{
case 0:..........; break;
case 1:..........;break;
case 2:..........等等
}
}
}
......
}
#5
对不起,没看清楼主的意思就盲目回帖了。
#6
这样可以???
#7
#include"stdio.h"
#include"conio.h"
#define MAX 10
#define _FOR_I_(off_x,off_y,off_z) for(i = off_x; i<off_y; i+=off_z)
#define _FOR_J_(off_x,off_y,off_z) for(j = off_x; j>off_y; j+=off_z)
int fun();
int main(int argc,char *argv[])
{
int num = 0;
num = fun();
printf("%d\n",num);
return 0;
}
int fun()
{
int i = 0;
int j = 0;
int sum = 0 ;
_FOR_I_(0,MAX,1)
_FOR_J_(MAX,0,-1)
sum = sum+i;
return sum;
}
这个可以,如果你想把#define _FOR_I_(off_x,off_y,off_z) for(i = off_x; i<off_y; i+=off_z)
#define _FOR_J_(off_x,off_y,off_z) for(j = off_x; j>off_y; j+=off_z)
定位一个宏的话,可以讲for中间的判断条件也定义为宏
#8
那倒要看你.......是什麼
#9
我的......前后有联系的,已经不能再分割成小的函数了
#10
有些我是i=MAX; i>0; i-- 前面两项怎么办?
#11
for(i=MAX; i>0; i--)这个又得另外写个宏了?
#12
将循环里面的语句定义成宏更好!
#13
foo(iStart, iEnd, jStart, jEnd)
{
...
i = iStart;
j = jStart;
while (iStart != iEnd)
{
while (jStart != jEnd)
{
...
if (jStart < jEnd)
j++;
else
j--;
}
if (iStart < iEnd)
i++;
else
i--;
}
...
}
foo(0, MAX, 0, MAX2);
foo(MAX, 0, 0, MAX2);
foo(MAX, 0, MAX2, 0);
#14
#include"stdio.h"
#include"conio.h"
#define MAX 10
#define _FOR_I_(off_x,off_y,off_z) for(i = off_x; i<off_y; i+=off_z)
#define _FOR_J_(off_x,off_y,off_z) for(j = off_x; j>off_y; j+=off_z)
#define _FOR_R_(M,N) do{\
for(int k=0; k<N; ++k){M+=k;}\
}while();
int fun();
int main(int argc,char *argv[])
{
int num = 0;
num = fun();
printf("%d\n",num);
return 0;
}
int fun()
{
int i = 0;
int j = 0;
int sum = 0 ;
_FOR_I_(0,MAX,1)
_FOR_J_(MAX,0,-1)
sum = sum+i;
return sum;
}
#15
受大家的启发才找到了方法,谢谢各位.
我的方法片断如下
相比八个For循环,写下面这些确实花了不少时间,但以后围护会容易一点吧,呵呵
(实际上这段代码应该也不用经常改的,算是给码农特有的执着找一个可爱的借口吧!)
我的方法片断如下
相比八个For循环,写下面这些确实花了不少时间,但以后围护会容易一点吧,呵呵
(实际上这段代码应该也不用经常改的,算是给码农特有的执着找一个可爱的借口吧!)
// rectangle traversal order(order relative with autoFillColCheck() ) (矩阵遍历顺序枚举类型)
enum RectTraversalOrder
{
RTO_LT_ROW_F, // start from left top , row first, find m_colCheck[0] (edge top)
RTO_RT_COL_F, // start from right top , column first, find m_colCheck[1] (edge right)
RTO_RB_ROW_F, // start from right bottom, row first, find m_colCheck[2] (edge bottom)
RTO_LB_COL_F, // start from left bottom, column first, find m_colCheck[3] (edge left)
RTO_RT_ROW_F, // start from right top , row first, find m_colCheck[0] (edge top)
RTO_RB_COL_F, // start from right bottom, column first, find m_colCheck[1] (edge right)
RTO_LB_ROW_F, // start from left bottom, row first, find m_colCheck[2] (edge bottom)
RTO_LT_COL_F, // start from left top , column first, find m_colCheck[3] (edge left)
RTO_ORDER_MAX,
RTO_RANDOM // random
};
#define FOR_FOR(starti,endi,offseti,start,end,offset) for(i=starti; i != endi; i += offseti)\
for(j=start; j != end; j += offset)
long starti,endi,offseti,start,end,offset;
bool isJIexchange=false;
switch(OrderType) // 借助这个Switch 给宏FOR_FOR赋参数
{
case RTO_LT_ROW_F:
{
starti=0; start=0;
endi =m_pGetColor->getHigh();
end =m_pGetColor->getWidth();
}
break;
case RTO_LT_COL_F:
{
starti=0; start =0; isJIexchange=true;
end =m_pGetColor->getHigh();
endi =m_pGetColor->getWidth();
}
break;
case RTO_RT_ROW_F:
{
starti=0; end =-1;
endi =m_pGetColor->getHigh();
start =m_pGetColor->getWidth()-1;
}
break;
case RTO_RT_COL_F:
{
start=0; endi =-1; isJIexchange=true;
end =m_pGetColor->getHigh();
starti=m_pGetColor->getWidth()-1;
}
break;
case RTO_RB_ROW_F:
{
end =-1; endi =-1;
starti=m_pGetColor->getHigh() -1;
start =m_pGetColor->getWidth()-1;
}
break;
case RTO_RB_COL_F:
{
end =-1; endi =-1; isJIexchange=true;
start =m_pGetColor->getHigh() -1;
starti=m_pGetColor->getWidth()-1;
}
break;
case RTO_LB_ROW_F:
{
endi =-1; start =0;
starti=m_pGetColor->getHigh() -1;
end =m_pGetColor->getWidth();
}
break;
case RTO_LB_COL_F:
{
end =-1; starti =0; isJIexchange=true;
start =m_pGetColor->getHigh() -1;
endi =m_pGetColor->getWidth();
}
break;
case RTO_RANDOM:
return autoFillByRandom(index);
default:
return false;
}
if(starti<endi) offseti= 1;
else offseti=-1;
if(start <end ) offset = 1;
else offset =-1;
unsigned long color;
long i,j;
if( isJIexchange )
FOR_FOR(starti, endi, offseti, start, end, offset)
{
color = m_pGetColor->getColor(i,j);
if (color>0xFFFFFF) return false;
if ( m_transparenceColor==color || isInColCheck(index, color) ) continue;
if ( getColorH()*getColorW()>index && isInColCheck(index,i,j) ) continue;
goto SUCCESS_;
}
else
FOR_FOR(starti, endi, offseti, start, end, offset)
{
color = m_pGetColor->getColor(j,i);
if (color>0xFFFFFF) return false;
if ( m_transparenceColor==color || isInColCheck(index, color) ) continue;
if ( getColorH()*getColorW()>index && isInColCheck(index,j,i) ) continue;
goto SUCCESS_;
}
#1
#define funx(offset_x, offset_y) do{\
int i, j; \
for(i=0; i<MAX; i+=offset_x;) { \
for(j=0; j<MAX2; j+=offset_y;) { \
/* do something you want */ \
} \
} \
} while(0);
#define fun1() funx(1, 1)
#define fun2() funx(-1, 1)
这样试试
#2
将for循环里结束条件提出来,设为函数的参数不就行了?!
#3
看你传的参数类型,进行重载就是,可以用继承等方式
#4
funX( int M )
{
......
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX2;j++)
{
switch(M)
{
case 0:..........; break;
case 1:..........;break;
case 2:..........等等
}
}
}
......
}
{
......
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX2;j++)
{
switch(M)
{
case 0:..........; break;
case 1:..........;break;
case 2:..........等等
}
}
}
......
}
#5
对不起,没看清楼主的意思就盲目回帖了。
#6
这样可以???
#7
#include"stdio.h"
#include"conio.h"
#define MAX 10
#define _FOR_I_(off_x,off_y,off_z) for(i = off_x; i<off_y; i+=off_z)
#define _FOR_J_(off_x,off_y,off_z) for(j = off_x; j>off_y; j+=off_z)
int fun();
int main(int argc,char *argv[])
{
int num = 0;
num = fun();
printf("%d\n",num);
return 0;
}
int fun()
{
int i = 0;
int j = 0;
int sum = 0 ;
_FOR_I_(0,MAX,1)
_FOR_J_(MAX,0,-1)
sum = sum+i;
return sum;
}
这个可以,如果你想把#define _FOR_I_(off_x,off_y,off_z) for(i = off_x; i<off_y; i+=off_z)
#define _FOR_J_(off_x,off_y,off_z) for(j = off_x; j>off_y; j+=off_z)
定位一个宏的话,可以讲for中间的判断条件也定义为宏
#8
那倒要看你.......是什麼
#9
我的......前后有联系的,已经不能再分割成小的函数了
#10
有些我是i=MAX; i>0; i-- 前面两项怎么办?
#11
for(i=MAX; i>0; i--)这个又得另外写个宏了?
#12
将循环里面的语句定义成宏更好!
#13
foo(iStart, iEnd, jStart, jEnd)
{
...
i = iStart;
j = jStart;
while (iStart != iEnd)
{
while (jStart != jEnd)
{
...
if (jStart < jEnd)
j++;
else
j--;
}
if (iStart < iEnd)
i++;
else
i--;
}
...
}
foo(0, MAX, 0, MAX2);
foo(MAX, 0, 0, MAX2);
foo(MAX, 0, MAX2, 0);
#14
#include"stdio.h"
#include"conio.h"
#define MAX 10
#define _FOR_I_(off_x,off_y,off_z) for(i = off_x; i<off_y; i+=off_z)
#define _FOR_J_(off_x,off_y,off_z) for(j = off_x; j>off_y; j+=off_z)
#define _FOR_R_(M,N) do{\
for(int k=0; k<N; ++k){M+=k;}\
}while();
int fun();
int main(int argc,char *argv[])
{
int num = 0;
num = fun();
printf("%d\n",num);
return 0;
}
int fun()
{
int i = 0;
int j = 0;
int sum = 0 ;
_FOR_I_(0,MAX,1)
_FOR_J_(MAX,0,-1)
sum = sum+i;
return sum;
}
#15
受大家的启发才找到了方法,谢谢各位.
我的方法片断如下
相比八个For循环,写下面这些确实花了不少时间,但以后围护会容易一点吧,呵呵
(实际上这段代码应该也不用经常改的,算是给码农特有的执着找一个可爱的借口吧!)
我的方法片断如下
相比八个For循环,写下面这些确实花了不少时间,但以后围护会容易一点吧,呵呵
(实际上这段代码应该也不用经常改的,算是给码农特有的执着找一个可爱的借口吧!)
// rectangle traversal order(order relative with autoFillColCheck() ) (矩阵遍历顺序枚举类型)
enum RectTraversalOrder
{
RTO_LT_ROW_F, // start from left top , row first, find m_colCheck[0] (edge top)
RTO_RT_COL_F, // start from right top , column first, find m_colCheck[1] (edge right)
RTO_RB_ROW_F, // start from right bottom, row first, find m_colCheck[2] (edge bottom)
RTO_LB_COL_F, // start from left bottom, column first, find m_colCheck[3] (edge left)
RTO_RT_ROW_F, // start from right top , row first, find m_colCheck[0] (edge top)
RTO_RB_COL_F, // start from right bottom, column first, find m_colCheck[1] (edge right)
RTO_LB_ROW_F, // start from left bottom, row first, find m_colCheck[2] (edge bottom)
RTO_LT_COL_F, // start from left top , column first, find m_colCheck[3] (edge left)
RTO_ORDER_MAX,
RTO_RANDOM // random
};
#define FOR_FOR(starti,endi,offseti,start,end,offset) for(i=starti; i != endi; i += offseti)\
for(j=start; j != end; j += offset)
long starti,endi,offseti,start,end,offset;
bool isJIexchange=false;
switch(OrderType) // 借助这个Switch 给宏FOR_FOR赋参数
{
case RTO_LT_ROW_F:
{
starti=0; start=0;
endi =m_pGetColor->getHigh();
end =m_pGetColor->getWidth();
}
break;
case RTO_LT_COL_F:
{
starti=0; start =0; isJIexchange=true;
end =m_pGetColor->getHigh();
endi =m_pGetColor->getWidth();
}
break;
case RTO_RT_ROW_F:
{
starti=0; end =-1;
endi =m_pGetColor->getHigh();
start =m_pGetColor->getWidth()-1;
}
break;
case RTO_RT_COL_F:
{
start=0; endi =-1; isJIexchange=true;
end =m_pGetColor->getHigh();
starti=m_pGetColor->getWidth()-1;
}
break;
case RTO_RB_ROW_F:
{
end =-1; endi =-1;
starti=m_pGetColor->getHigh() -1;
start =m_pGetColor->getWidth()-1;
}
break;
case RTO_RB_COL_F:
{
end =-1; endi =-1; isJIexchange=true;
start =m_pGetColor->getHigh() -1;
starti=m_pGetColor->getWidth()-1;
}
break;
case RTO_LB_ROW_F:
{
endi =-1; start =0;
starti=m_pGetColor->getHigh() -1;
end =m_pGetColor->getWidth();
}
break;
case RTO_LB_COL_F:
{
end =-1; starti =0; isJIexchange=true;
start =m_pGetColor->getHigh() -1;
endi =m_pGetColor->getWidth();
}
break;
case RTO_RANDOM:
return autoFillByRandom(index);
default:
return false;
}
if(starti<endi) offseti= 1;
else offseti=-1;
if(start <end ) offset = 1;
else offset =-1;
unsigned long color;
long i,j;
if( isJIexchange )
FOR_FOR(starti, endi, offseti, start, end, offset)
{
color = m_pGetColor->getColor(i,j);
if (color>0xFFFFFF) return false;
if ( m_transparenceColor==color || isInColCheck(index, color) ) continue;
if ( getColorH()*getColorW()>index && isInColCheck(index,i,j) ) continue;
goto SUCCESS_;
}
else
FOR_FOR(starti, endi, offseti, start, end, offset)
{
color = m_pGetColor->getColor(j,i);
if (color>0xFFFFFF) return false;
if ( m_transparenceColor==color || isInColCheck(index, color) ) continue;
if ( getColorH()*getColorW()>index && isInColCheck(index,j,i) ) continue;
goto SUCCESS_;
}