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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
int length, index;
int digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8;
int translation;
char string[8];
cout << "Enter binary string: ";
gets(string); // enter string
length = strlen(string);
cout << length; // declare string size
int pwrs[8];
pwrs[0]=128; pwrs[1]=64; pwrs[2]=32; pwrs[3]=16;
pwrs[4]=8; pwrs[5]=4; pwrs[6]=2; pwrs[7]=1;
switch(length){
case '1': //The string size determines the switch
if(string[0]='1'){ //Each char is assigned to a var so that the last number always=digit8
digit8=pwrs[7]; //For example, with string 10, digit7=1 and digit8=0
}else digit8=0; //If digit8 is 1, digit8 = pwrs[7](1)
break; //If digit8 isn't 1, digit8 = 0
translation=digit8; //The translation is a sum of all the digits
case '2':
if(string[0]='1'){
digit7=pwrs[6];
}else digit7=0;
if(string[1]='1'){
digit8=pwrs[7];
}else digit8=0;
translation=digit7+digit8;
break;
case '3':
if(string[0]='1'){
digit6=pwrs[5];
}else digit6=0;
if(string[1]='1'){
digit7=pwrs[6];
}else digit7=0;
if(string[2]='1'){
digit8=pwrs[7];
}else digit8=0;
translation=digit6+digit7+digit8;
break;
case '4':
if(string[0]='1'){
digit5=pwrs[4];
}else digit5=0;
if(string[1]='1'){
digit6=pwrs[5];
}else digit6=0;
if(string[2]='1'){
digit7=pwrs[6];
}else digit7=0;
if(string[3]='1'){
digit8=pwrs[7];
}else digit8=0;
translation=digit5+digit6+digit7+digit8;
case '5':
if(string[0]='1'){
digit4=pwrs[3];
}else digit4=0;
if(string[1]='1'){
digit5=pwrs[4];
}else digit5=0;
if(string[2]='1'){
digit6=pwrs[5];
}else digit6=0;
if(string[3]='1'){
digit7=pwrs[6];
}else digit7=0;
if(string[4]='1'){
digit8=pwrs[7];
}else digit8=0;
translation = digit4+digit5+digit6+digit7+digit8;
case '6':
if(string[0]='1'){
digit3=pwrs[2];
}else digit3=0;
if(string[1]='1'){
digit4=pwrs[3];
}else digit4=0;
if(string[2]='1'){
digit5=pwrs[4];
}else digit5=0;
if(string[3]='1'){
digit6=pwrs[5];
}else digit6=0;
if(string[4]='1'){
digit7=pwrs[6];
}else digit7=0;
if(string[5]='1'){
digit8=pwrs[7];
}else digit8=0;
translation=digit3+digit4+digit5+digit6+digit7+digit8;
case '7':
if(string[0]='1'){
digit2=pwrs[1];
}else digit2=0;
if(string[1]='1'){
digit3=pwrs[2];
}else digit3=0;
if(string[2]='1'){
digit4=pwrs[3];
}else digit4=0;
if(string[3]='1'){
digit5=pwrs[4];
}else digit5=0;
if(string[4]='1'){
digit6=pwrs[5];
}else digit6=0;
if(string[5]='1'){
digit7=pwrs[6];
}else digit7=0;
if(string[6]='1'){
digit8=pwrs[7];
}else digit8=0;
translation=digit2+digit3+digit4+digit5+digit6+digit7+digit8;
case '8':
if(string[0]='1'){
digit1=pwrs[0];
}else digit1=0;
if(string[1]='1'){
digit2=pwrs[1];
}else digit2=0;
if(string[2]='1'){
digit3=pwrs[2];
}else digit3=0;
if(string[3]='1'){
digit4=pwrs[3];
}else digit4=0;
if(string[4]='1'){
digit5=pwrs[4];
}else digit5=0;
if(string[5]='1'){
digit6=pwrs[5];
}else digit6=0;
if(string[6]='1'){
digit7=pwrs[6];
}else digit7=0;
if(string[7]='1'){
digit8=pwrs[7];
}else digit8=0;
translation=digit1+digit2+digit3+digit4+digit5+digit6+digit7+digit8;
}
cout << "\n" << translation;
return 0;
}
|