Bonjour .Je veux traduire cette fonction VB en C++.
La fonction VB renvoie une chaine de caractères. Collez cette fonction dans un module Openoffice, cela revoi un Texte box. Le résultat est dans: strRes.
Code VB : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 Public Function CombineAJako() Dim strRes As String fnCombine "12", 2, "", 0, 2, strRes ''''Testez fnCombine "1234", 4, "", 0, 4, strRes MsgBox strRes End Function Sub fnCombine(ByVal strVal As String, ByVal iLgStrVal As Integer, strWord As String, ByVal iLg As Integer, ByVal iTargetLg As Integer, ByRef strRes As String) Dim i As Integer Dim c As String If (iLg = iTargetLg) Then strRes = strRes + strWord + ", " Exit Sub End If For i = 1 To iLgStrVal c = Mid(strVal, i, 1) fnCombine strVal, iLgStrVal, strWord + c, iLg + 1, iTargetLg, strRes Next i End Sub
Voici maintenant la fonction en C++, elle ne fonctionne pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <cstdlib> #include <iostream> #include <stdio.h> #include <cstring> #include <string> #include <fstream> using namespace std; void fnCombine(string strVal, int iLgStrVal, string strWord, int iLg, int iTargetLg, string strRes); int main(int argc, char *argv[]) { string strRes; strRes = ""; fnCombine("12", 2, "", 0, 2, strRes); /// ++--Testez---++//// fnCombine("1234", 4, "", 0, 4, strRes); cout<<strRes; system("PAUSE"); return EXIT_SUCCESS; } void fnCombine(string strVal, int iLgStrVal, string strWord, int iLg, int iTargetLg, string strRes) { int i ; string c ; if (iLg == iTargetLg) { strRes = strRes + strWord + ", "; return ; } for (i = 1; i < iLgStrVal; i++) { c = strVal.substr (i- 1, 1); fnCombine(strVal, iLgStrVal, strWord + c, iLg + 1, iTargetLg, strRes); } }
Partager