怎样用递归代替多层for循环
我要创建一个PROJECT,下面有多个ACTIVITY,每个ACTIVITY下面又有多个ACTIVITY,要实现这种模式的PROJECT可以使用多层for循环,代码如下:for(i=3;i<=3;i++)
{
sprintf(firstName,"%d",i);
sprintf(userName,"%s%d",lastName,i);
addUser(userName,password,confirmPassword,firstName,lastName);
sprintf(jobName,"Project_%s",userName);
sprintf(projectCode,"P%d",i);
tempId=addProject(jobName,projectCode,responsibleStr,respGroupStr,startTime,duration);
printf("tempID: %d",tempId);
for(j=1;j<=2;j++)
{
sprintf(tAName,"P_V_%d_%d",i,j);
sprintf(pUName,"admin");
sprintf(rUName,"admin");
tParentId=tempId;
tRootId=tempId;
tempId2=addActivity(tAName,pUName,rUName,tParentId,tRootId,tPreId,tPreAId,tAStartDate,tADurDate);
for(k=1;k<=3;k++)
{
sprintf(tAName2,"P_V_%d_%d_%d",i,j,k);
sprintf(pUName2,"admin");
sprintf(rUName2,"admin");
tParentId2=tempId2;
tRootId2=tempId;
tempPreId2=addActivity(tAName2,pUName2,rUName2,tParentId2,tRootId2,tPreId2,tPreAId2,tAStartDate2,tADurDate2);
tPreId2=LogicID;
tPreAId2=tempPreId2;
}
}
但是如果ACTIVITY下面的层数很多的时候,用FOR循环就不好做了,这个时候可以用递归的方面,把层数和每层下面有几个ACTIVITY作为参数传进去,这个递归程序应该如何实现呢? 3Q