need help with recursion

hello everyone ,
I've been trying to implement some sort of code based on recursion and since I have serious issues with recursion i couldn't make it happen so here I am asking for your help
the use of the code :
the code is supposed to generate every process that enables to acheive a result :
so you have 6 numbers and a result , the point is to get that result using those numbers or some of them (by the means of + - * /)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<string>
#include<iostream>
#include<vector>
#include<list>
#include<map>
#include<set>

using namespace std ;

int calc (string op1, string operation , string op2)
{
    cout<<"calc"<<endl;
	if(operation=="+")
	{
		return stoi(op1)+stoi(op2);
	}
	else if(operation=="*")
	{
		return stoi(op1)*stoi(op2);
	}
	else if(operation=="-")
	{
		return stoi(op1)-stoi(op2);
	}
	else if(operation=="/")
	{
		return stoi(op1)/stoi(op2);
	}
}


string demarchetostring (vector<string>& demarche)
{
    string s ="";
	for (int i=2;i<demarche.size();i+=3)
	{
             cout<<"demarchetostring"<<endl;
			s+=demarche[i-2]+demarche[i]+demarche[i-1]+"=";
			int res=calc(demarche[i-2],demarche[i],demarche[i-1]);
			s+=to_string(res)+'\n' ;

	}
 return s ;
}

void compte_est_bon(vector<int>& vectnum , int resfinale ,vector<string>& demarche ,const string& operation, string& resultat,vector<string>& vectres)
{
/*	if (demarche.size()!=0)
	{
		demarche.push_back(operation);
	}*/
	if (vectnum.back()==resfinale)
	{
	    resultat+=demarchetostring(demarche);
		vectres.push_back(resultat);
		//demarche.clear();
		return ;
	}
	else if (vectnum.size()==1)
	{
		return ;
	}
	else
	{
		int op1 = vectnum.back();
		demarche.push_back(to_string(op1));
		int op2 = vectnum[vectnum.size()-2];
		demarche.push_back(to_string(op2));
		if((op1>op2)&&(op2!=0))
        {
		vector<int>vectnum1=vectnum;
		vectnum1.pop_back();
		vectnum1.pop_back();
		vectnum1.push_back(op1+op2);
        demarche.push_back("+");
	compte_est_bon(vectnum1,resfinale,demarche,"+",resultat,vectres);
        demarche.pop_back();
        }
        if((op1!=1)&&(op2!=1))
        {
		vector<int>vectnum2=vectnum;
        vectnum2.pop_back();
		vectnum2.pop_back();
		vectnum2.push_back(op1*op2);
                demarche.push_back("*");
		compte_est_bon(vectnum2,resfinale,demarche,"*",resultat,vectres);
                demarche.pop_back();
        }
		if((op1>op2)&&(op2!=0))
		{

			vector<int>vectnum3=vectnum;
            vectnum3.pop_back();
            vectnum3.pop_back();
			vectnum3.push_back(op1-op2);
			demarche.push_back("-");
			compte_est_bon(vectnum3,resfinale,demarche,"-",resultat,vectres);
			demarche.pop_back();
		}
		if((op1%op2==0)&&(op2!=1))
		{
			vector<int>vectnum4=vectnum;
            vectnum4.pop_back();
            vectnum4.pop_back();
			vectnum4.push_back(op1/op2);
			demarche.push_back("/");
			compte_est_bon(vectnum4,resfinale,demarche,"/",resultat,vectres);
			demarche.pop_back();
		}
	}
}




int main ( int argc , char** argv )
{
vector<int> vectnum={1,2,50,25,3,9};
int resultatfinale=353;
vector<string> demarche ;
vector<string> vectres ;
string svid="" ;
compte_est_bon(vectnum,resultatfinale,demarche,"",svid,vectres);
/*for (int i=0;i<vectnum.size();i++)
{
    cout << vectnum[i]<<endl ;
}
for (int i=0;i<demarche.size();i++)
{
    cout << demarche[i]<<endl ;
}*/
	return 0 ;
}


I know that it's a horrible mess but I wish to find some help
Thaks for reading this anyway :)
u can have a look at the algorithm graphe in this page
http://eternitygames.free.fr/LeCompteEstBon.html
(i m sorry that i haven't found an english one but u just need to have a look at the graphe)
Last edited on
Topic archived. No new replies allowed.