标题:怎样用递归代替多层for循环
只看楼主
SolomHu
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-5-20
 问题点数:0 回复次数:3 
怎样用递归代替多层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
搜索更多相关主题的帖子: 递归 ACTIVITY PROJECT 代替 模式 
2008-06-11 18:17
lingluoz
Rank: 2
来 自:苏州科技学院
等 级:新手上路
威 望:4
帖 子:749
专家分:0
注 册:2008-2-2
得分:0 
严重鄙视楼上的。。做广告行为。学习资料应该免费共享。。。。

Murphy's Law :
If there are two or more ways to do something, and one of those ways can result in a catastrophe, then someone will do it.
2008-06-11 19:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-218662-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.231032 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved