Help on simplifying this?

The code below has too much repeated code.
How do I Write a function which will return an integer value for a letter
then multiply it in the equation.

Apparently I am not allowed to repeat this...
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


  for (int i =  convertTheLength - 1; i >= 0; i--) {
    
    switch(convert.at(i)) {
    case '0':
     
      newCoor +=0 * pow(base, exp);
      break;

    case '1':

      newCoor +=1 *pow(base, exp);
      break;

    case '2':

      newCoor+=2 *pow (base, exp);
      break;

    case '3':

      newCoor+=3 * pow (base, exp);
      break;

 case '4':

      newCoor+=4 * pow (base, exp);
      break;

    case '5':

      newCoor+=5 * pow (base, exp);
      break;

    case '6':

      newCoor+=6 * pow (base, exp);
      break;

    case '7':

      newCoor+=7 * pow (base, exp);
      break;
 case '8':

      newCoor+=8 * pow (base, exp);
      break;

    case '9':

      newCoor+=9 * pow (base, exp);
      break;

    case 'A':

      newCoor+=10 * pow (base, exp);
      break;

    case 'B':
      newCoor+=11 * pow (base, exp);
      break;

    case 'C':

      newCoor+=12 * pow (base, exp);
      break;
 case 'D':

      newCoor+= 13 * pow (base, exp);
      break;

    case 'E':

      newCoor+= 14 * pow (base, exp);
      break;

    case 'F':

      newCoor+= 15 * pow (base, exp);
      break;
    }
    exp++;
  } 
  return newCoor;
}

Last edited on
Check this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// your char:
char c = 'B'; // (for example B)

// If you write this:
int myNumber = c - 'A' + 10; //myNumer is 11
/* You do the following:
If you look up A in ascii it's 65 and B is 66
=> 'B' - 'A' = 66 - 65 = 1
=> add 10 to that => 11

another 2 exmaples:
'A' - 'A' = 65 - 65 = 0
=> add 10 to that => 10

'D' - 'A' = 68 - 65 = 3
=> add 10 to that => 13

 */


Have fun ! ;)
Last edited on
Topic archived. No new replies allowed.