|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include <math.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +int main() |
| 7 | +{ |
| 8 | + // taking input string |
| 9 | + char input[1000]; |
| 10 | + printf("Input format <number>$<unit in lowercase>\n"); |
| 11 | + scanf("%s", input); |
| 12 | + |
| 13 | + // taking to which input string |
| 14 | + char toUnit[100]; |
| 15 | + printf("To which unit\n"); |
| 16 | + scanf("%s", toUnit); |
| 17 | + |
| 18 | + // in = input number (float) , unit is unit of input number |
| 19 | + float in = 0; |
| 20 | + char unit[100] = {0}; |
| 21 | + |
| 22 | + // a for detecting $ sign |
| 23 | + int a = 1; |
| 24 | + |
| 25 | + // just for iteration |
| 26 | + int j = 0; |
| 27 | + int k = 0; |
| 28 | + |
| 29 | + // string storing the number |
| 30 | + char forn[100]; |
| 31 | + |
| 32 | + for (int i = 0; i < strlen(input); i++) |
| 33 | + { |
| 34 | + if (a) |
| 35 | + { |
| 36 | + if (input[i] == '$') |
| 37 | + { |
| 38 | + a = 0; |
| 39 | + continue; |
| 40 | + } |
| 41 | + forn[k++] = input[i]; |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + unit[j++] = input[i]; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // convertign string to float |
| 50 | + char *endPtr; |
| 51 | + in = strtof(forn, &endPtr); |
| 52 | + |
| 53 | + // terminating unit as its length was 100 |
| 54 | + unit[j] = '\0'; |
| 55 | + |
| 56 | + // iur = input unit row |
| 57 | + int iur = -1; |
| 58 | + int iuc = -1; |
| 59 | + |
| 60 | + int numOfCols = 16; |
| 61 | + char *DISTANCE_UNITS[][16] = {{"pm","/","/","nm","/","/","um","/","/","mm", "cm", "dm", "m", "dam", "hm", "km"}, |
| 62 | + {"pg","/","/","ng","/","/","ug","/","/","mg", "cg", "dg", "g", "dag", "hg", "kg"}, |
| 63 | + {"pl","/","","nl","/","/","ul","/","/","ml", "cl", "dl", "l", "dal", "hl", "kl"}}; |
| 64 | + |
| 65 | + for (int i = 0; i < 3; i++) |
| 66 | + { |
| 67 | + for (int j = 0; j < numOfCols; j++) |
| 68 | + { |
| 69 | + if (!strcmp(unit, DISTANCE_UNITS[i][j])) // strcmp return 0 for same strings |
| 70 | + { |
| 71 | + iur = i; |
| 72 | + iuc = j; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + // the ouput float out; |
| 77 | + float out = in * 1.0; |
| 78 | + |
| 79 | + // our = output unit row |
| 80 | + int our = -1; |
| 81 | + int ouc = -1; |
| 82 | + |
| 83 | + for (int i = 0; i < 3; i++) |
| 84 | + { |
| 85 | + for (int j = 0; j < numOfCols; j++) |
| 86 | + { |
| 87 | + if (!strcmp(toUnit, DISTANCE_UNITS[i][j])) |
| 88 | + { |
| 89 | + our = i; |
| 90 | + ouc = j; |
| 91 | + if (iuc != -1) |
| 92 | + { |
| 93 | + out = in * pow(10, (iuc - ouc)); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + printf("%f %s",out,toUnit); |
| 99 | +} |
0 commit comments