Question d’entretien chez Microsoft

Write a function to turn a string into an integer and test it

Réponses aux questions d'entretien

Utilisateur anonyme

24 nov. 2010

public int AtoI(string str) { bool isNeg = false; int index = 0; if (str[0] == '-') { isNeg = true; index++; } int result = 0; // "123" 123 // 1 * 10 + 2 12 * 10 + 3 int multiple = 1; int number = 0; for (int i = index; i < str.Length; i++) { number = str[i] - '0'; result = result * multiple + number; multiple = 10; } if (isNeg) { result = result * -1; } return result; }

1

Utilisateur anonyme

8 oct. 2010

CString csNum; int nNum = 2; csNum.format("%d", nNum);

Utilisateur anonyme

8 oct. 2010

oops wrong one CString csNum = "1"; int nNum = atoi(csNum);