Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #include<string.h>
  2. #include<stdio.h>
  3. #include<math.h>
  4.  
  5. //calculating resistance based on first three bands
  6. int resistance_calc(char a[], char b[], char c[], char d[]){
  7.  
  8. int first, second, third, fourth;
  9.  
  10.  
  11. if((strcmp(a, "Brown") || strcmp(a, "BROWN") || strcmp(a, "brown"))==1)
  12. first = 1;
  13. if((strcmp(a, "Red") || strcmp(a, "RED") || strcmp(a, "red"))==1)
  14. first = 2;
  15. if((strcmp(a, "Orange") || strcmp(a, "ORANGE") || strcmp(a, "orange"))==1)
  16. first = 3;
  17. if((strcmp(a, "Yellow") || strcmp(a, "YELLOW") || strcmp(a, "yellow"))==1)
  18. first = 4;
  19. if((strcmp(a, "Green") || strcmp(a, "GREEN") || strcmp(a, "green"))==1)
  20. first = 5;
  21. if((strcmp(a, "Blue") || strcmp(a, "BLUE") || strcmp(a, "blue"))==1)
  22. first = 6;
  23. if((strcmp(a, "Violet") || strcmp(a, "VIOLET") || strcmp(a, "violet"))==1)
  24. first = 7;
  25. if((strcmp(a, "Gray") || strcmp(a, "GRAY") || strcmp(a, "gray"))==1)
  26. first = 8;
  27.  
  28. if((strcmp(a, "White") || strcmp(a, "WHITE") || strcmp(a, "white"))==1)
  29. first = 9;
  30. else
  31. printf("Please input a valid first band color (red, orange, yellow, green, violet, gray or white)");
  32. scanf("%s", a);
  33.  
  34.  
  35. if((strcmp(b, "Black") || strcmp(b, "BLACK") || strcmp(b, "black"))==1)
  36. second = 0;
  37. if((strcmp(b, "Brown") || strcmp(b, "BROWN") || strcmp(b, "brown"))==1)
  38. second = 1;
  39. if((strcmp(b, "Red") || strcmp(b, "RED") || strcmp(b, "red"))==1)
  40. second = 2;
  41. if((strcmp(b, "Orange") || strcmp(b, "ORANGE") || strcmp(b, "orange"))==1)
  42. second = 3;
  43. if((strcmp(b, "Yellow") || strcmp(b, "YELLOW") || strcmp(b, "yellow"))==1)
  44. second = 4;
  45. if((strcmp(b, "Green") || strcmp(b, "GREEN") || strcmp(b, "green"))==1)
  46. second = 5;
  47. if((strcmp(b, "Blue") || strcmp(b, "BLUE") || strcmp(b, "blue"))==1)
  48. second = 6;
  49. if((strcmp(b, "Violet") || strcmp(b, "VIOLET") || strcmp(b, "violet"))==1)
  50. second = 7;
  51. if((strcmp(b, "Gray") || strcmp(b, "GRAY") || strcmp(b, "gray"))==1)
  52. second = 8;
  53. if((strcmp(b, "White") || strcmp(b, "WHITE") || strcmp(b, "white"))==1)
  54. second = 9;
  55. else
  56. printf("Please input a valid second band color (black, red, orange, yellow, green, violet, gray or white)");
  57. scanf("%s", b);
  58.  
  59.  
  60. if((strcmp(c, "Black") || strcmp(c, "BLACK") || strcmp(c, "black"))==1)
  61. third = 0;
  62. if((strcmp(c, "Brown") || strcmp(c, "BROWN") || strcmp(c, "brown"))==1)
  63. third = 1;
  64. if((strcmp(c, "Red") || strcmp(c, "RED") || strcmp(c, "red"))==1)
  65. third = 2;
  66. if((strcmp(c, "Orange") || strcmp(c, "ORANGE") || strcmp(c, "orange"))==1)
  67. third = 3;
  68. if((strcmp(c, "Yellow") || strcmp(c, "YELLOW") || strcmp(c, "yellow"))==1)
  69. third = 4;
  70. if((strcmp(c, "Green") || strcmp(c, "GREEN") || strcmp(c, "green"))==1)
  71. third = 5;
  72. if((strcmp(c, "Blue") || strcmp(c, "BLUE") || strcmp(c, "blue"))==1)
  73. third = 6;
  74. if((strcmp(c, "Violet") || strcmp(c, "VIOLET") || strcmp(c, "violet"))==1)
  75. third = 7;
  76. if((strcmp(c, "Gray") || strcmp(c, "GRAY") || strcmp(c, "gray"))==1)
  77. third = 8;
  78. if((strcmp(c, "White") || strcmp(c, "WHITE") || strcmp(c, "white"))==1)
  79. third = 9;
  80. else
  81. printf("Please input a valid third band color (black, red, orange, yellow, green, violet, gray or white)");
  82. scanf("%s", c);
  83.  
  84.  
  85. if((strcmp(d, "Silver") || strcmp(d, "SILVER") || strcmp(d, "silver"))==1)
  86. fourth = pow(10,-2);
  87. if((strcmp(d, "Gold") || strcmp(d, "GOLD") || strcmp(d, "gold"))==1)
  88. fourth = pow(10, -1);
  89. if((strcmp(d, "Black") || strcmp(d, "BLACK") || strcmp(d, "black"))==1)
  90. fourth = pow(10, 0);
  91. if((strcmp(d, "Brown") || strcmp(d, "BROWN") || strcmp(d, "brown"))==1)
  92. fourth = pow(10, 1);
  93. if((strcmp(d, "Red") || strcmp(d, "RED") || strcmp(d, "red"))==1)
  94. fourth = pow(10, 2);
  95. if((strcmp(d, "Orange") || strcmp(d, "ORANGE") || strcmp(d, "orange"))==1)
  96. fourth = pow(10, 3);
  97. if((strcmp(d, "Yellow") || strcmp(d, "YELLOW") || strcmp(d, "yellow"))==1)
  98. fourth = pow(10, 4);
  99. if((strcmp(d, "Green") || strcmp(d, "GREEN") || strcmp(d, "green"))==1)
  100. fourth = pow(10, 5);
  101. if((strcmp(d, "Blue") || strcmp(d, "BLUE") || strcmp(d, "blue"))==1)
  102. fourth = pow(10, 6);
  103. if((strcmp(d, "Violet") || strcmp(d, "VIOLET") || strcmp(d, "violet"))==1)
  104. fourth = pow(10, 7);
  105. if((strcmp(d, "Gray") || strcmp(d, "GRAY") || strcmp(d, "gray"))==1)
  106. fourth = pow(10, 8);
  107. if((strcmp(d, "White") || strcmp(d, "WHITE") || strcmp(d, "white"))==1)
  108. fourth = pow(10, 9);
  109. else
  110. printf("Please input a valid fourth band color (silver, gold, black, red, orange, yellow, green, violet, gray or white)");
  111. scanf("%s", d);
  112.  
  113. return (((first*100)+(second*10)+(third))*fourth);
  114. }
  115.  
  116. int main(void)
  117. {
  118. char str[100]="";
  119. char *bands[6];
  120. int i;
  121. float resistance;
  122. //Reads in a string
  123. fgets(str, 100, stdin);
  124.  
  125. //Seperates the string, by space character, into an array
  126. bands[0]=strtok(str," ");
  127. if(bands[0]==NULL)
  128. {
  129. printf("Please input the six color bands from you resistor.\n");
  130. return (0);
  131. }
  132.  
  133. for(i=1;i<6;i++)
  134. {
  135. bands[i]=strtok(NULL," ");
  136. if(bands[i]==NULL)
  137. break;
  138. }
  139.  
  140. resistance = resistance_calc(bands[0], bands[1], bands[2], bands[3]);
  141.  
  142. printf("%10.4f Ohms\n", resistance);
  143.  
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement