Guest User

Untitled

a guest
Mar 5th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. /*********************************
  2. * Jorge Ceballos
  3. * CS 430
  4. * HW1
  5. * Q2
  6. **********************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <oraca.h>
  12.  
  13. void myTrim(char* );
  14. /*** header file for ORACLE definitions ***/
  15. exec sql include sqlca;
  16.  
  17. /*** Variable Declaration Block ***/
  18. exec sql begin declare section;
  19. char qAorB[10], condA[20];
  20. char login[20], password[20];
  21. float pcPrinter;
  22. float minSpeed = 5.0;
  23. char maker[12];
  24. char pType[12];
  25. int model;
  26. int pcNum;
  27. int printerNum;
  28. int colorNum = 0;
  29. float tempPrice;
  30. float speed;
  31. float ram;
  32. float hd;
  33. float screen;
  34. float price;
  35. char color[1];
  36. char printerType[10];
  37. char SQLSTATE[6];
  38. exec sql end declare section;
  39.  
  40. int print_sql_state(char *code){
  41. printf("\nSQLSTATE=%s\n",code);
  42. }
  43.  
  44. int main()
  45. {
  46. /*** error handler ***/
  47. exec sql whenever sqlerror goto report_error;
  48. exec sql whenever not found goto notfound;
  49.  
  50. /*** Prompt user for login name and password ***/
  51. printf("Username=");
  52. gets(login);
  53. printf("Password=");
  54. char *pass_string = malloc(100);
  55. pass_string=getpass("");
  56. strcpy(password, pass_string);
  57.  
  58. /*** Connect to DB ***/
  59. exec sql connect :login
  60. identified by :password;
  61.  
  62. /*** cursor declaration ***/
  63. exec sql declare maker_type cursor for
  64. select maker, type, model from Product;
  65.  
  66.  
  67. /*** Prompt user for question ***/
  68. printf("\nFor Q2.a Type = a, \nFor Q2.b Type = b");
  69. gets(qAorB);
  70.  
  71. if(strcmp(qAorB, "a") == 0) {
  72. printf("\nChoose Maker");
  73. scanf("%s",&condA);
  74. exec sql open maker_type;
  75. /*** TRAVERSE RESULT SET WITH CURSOR ***/
  76. while(1) {
  77. EXEC SQL
  78. FETCH maker_type into :maker, :pType, :model;
  79.  
  80. myTrim(pType);
  81. myTrim(maker);
  82. if(strcmp(pType, "PC") == 0 && strcmp(maker, condA) == 0) {
  83. EXEC SQL
  84. SELECT speed, ram, hd, price
  85. INTO :speed, :ram, :hd, :price
  86. FROM PC
  87. WHERE model = :model;
  88. printf("\nModel # = %d, Product type = %s, Speed = %f, Ram = %f, Hd = %f, Price = %f", model, pType, speed, ram, hd, price);
  89. }
  90. if(strcmp(pType, "laptop") == 0 && strcmp(maker, condA) == 0) {
  91. EXEC SQL
  92. SELECT speed, ram, hd, screen price
  93. INTO :speed, :ram, :hd, :screen, :price
  94. FROM Latop
  95. WHERE model = :model;
  96. printf("\nModel # = %d, Product type = %s, Speed = %f, Ram = %f, Hd = %f, Screen = %f Price = %f", model, pType, speed, ram, hd, screen, price);
  97. }
  98. if(strcmp(pType, "printer") == 0 && strcmp(maker, condA) == 0) {
  99. EXEC SQL
  100. SELECT color, type, price
  101. INTO :color, :printerType, :price
  102. FROM Printer
  103. WHERE model = :model;
  104. printf("\nModel # = %d, Product type = %s, Color = %s, Type = %s, Price = %f", model, pType, color, ram, hd, price);
  105. }else {
  106. printf("\nUnspected Error Encountered");
  107. }
  108. }
  109. }
  110. if(strcmp(qAorB, "b") == 0) {
  111. printf("\nEnter Max Budget = ");
  112. scanf("%f",&pcPrinter);
  113. printf("\nEnter Min PC speed = ");
  114. scanf("%f",&minSpeed);
  115. float result = pcPrinter;
  116. float pc = 0;
  117. float printer = 0;
  118. int i;
  119. int j;
  120. char hasColor[20];
  121.  
  122. /*** cursor declaration ***/
  123. exec sql declare pc_crs cursor for
  124. select price from PC where speed >= :minSpeed;
  125. printf("\n1 = %f.......", minSpeed);
  126.  
  127. EXEC SQL
  128. SELECT COUNT(*) INTO :pcNum FROM PC WHERE speed >= :minSpeed;
  129. printf("\n2.......");
  130. for(j = 0; j < pcNum; j++) {
  131. printf("\n3.......");
  132.  
  133. exec sql declare colorP_crs cursor for
  134. select price from Printer where color = 't';
  135. printf("\n4.......");
  136. EXEC SQL
  137. SELECT COUNT(*) INTO :printerNum FROM Printer;
  138.  
  139. printf("\n5.......");
  140.  
  141. exec sql declare printer_crs cursor for
  142. select price from Printer;
  143. printf("\n6.......");
  144. EXEC SQL
  145. SELECT COUNT(*) INTO :colorNum FROM Printer WHERE color = 't';
  146. printf("\n7.......");
  147.  
  148. EXEC SQL
  149. FETCH pc_crs into :price;
  150. printf("\n8.......");
  151. if(colorNum > 0) {
  152. printf("\n9.......");
  153. strcpy(hasColor, "Yes");
  154. printf("\n10.......");
  155. for(i = 0 ; i < colorNum; i++) {
  156. printf("\n11.......");
  157. EXEC SQL
  158. FETCH colorP_crs INTO :tempPrice;
  159.  
  160. if((tempPrice + price) <= result) {
  161. result = price + tempPrice;
  162. pc = price;
  163. printer = tempPrice;
  164. }
  165. }
  166. }else if(colorNum == 0 && (pc == 0 || printer == 0)) {
  167. strcpy(hasColor, "No");
  168. for(i = 0 ; i < printerNum; i++) {
  169. EXEC SQL
  170. FETCH printer_crs INTO :tempPrice;
  171.  
  172. if((tempPrice + price) <= result) {
  173. result = price + tempPrice;
  174. pc = price;
  175. printer = tempPrice;
  176. }
  177. }
  178. }
  179. }
  180. printf("\nCheapest System (pc + printer): \nColor Printer = %s, \nPC = %f, \nPrinter = %f, \nTotal price = %f", hasColor, pc, printer, result);
  181. }
  182. notfound:
  183. /* END OF DATA */
  184. printf("\nEND OF DATA");
  185. print_sql_state(SQLSTATE);
  186. exec sql close maker_type;
  187. exec sql close pc_crs;
  188. exec sql close printer_crs;
  189. return 0;
  190. report_error:
  191. printf("\nERROR OCCURRED");
  192. print_sql_state(SQLSTATE);
  193. return 1;
  194. }
  195.  
  196. void myTrim(char* str) {
  197.  
  198. int i = strlen(str) - 1;
  199.  
  200. while(i >= 0) {
  201. if(str[i] == '\0') continue;
  202. if(str[i] == 32) str[i] = '\0';
  203. i--;
  204. }
  205.  
  206. }
Add Comment
Please, Sign In to add comment