Guest User

Untitled

a guest
Mar 7th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 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. /*** Prompt user for question ***/
  67. printf("\nFor Q2.a Type = a, \nFor Q2.b Type = b");
  68. gets(qAorB);
  69.  
  70. if(strcmp(qAorB, "a") == 0) {
  71. printf("\nChoose Maker");
  72. scanf("%s",&condA);
  73. exec sql open maker_type;
  74. /*** TRAVERSE RESULT SET WITH CURSOR ***/
  75. while(1) {
  76. EXEC SQL
  77. FETCH maker_type into :maker, :pType, :model;
  78.  
  79. myTrim(pType);
  80. myTrim(maker);
  81. if(strcmp(pType, "PC") == 0 && strcmp(maker, condA) == 0) {
  82. EXEC SQL
  83. SELECT speed, ram, hd, price
  84. INTO :speed, :ram, :hd, :price
  85. FROM PC
  86. WHERE model = :model;
  87. printf("\nModel # = %d, Product type = %s, Speed = %f, Ram = %f, Hd = %f, Price = %f", model, pType, speed, ram, hd, price);
  88. }
  89. if(strcmp(pType, "laptop") == 0 && strcmp(maker, condA) == 0) {
  90. EXEC SQL
  91. SELECT speed, ram, hd, screen, price
  92. INTO :speed, :ram, :hd, :screen, :price
  93. FROM Laptop
  94. WHERE model = :model;
  95. printf("\nModel # = %d, Product type = %s, Speed = %f, Ram = %f, Hd = %f, Screen = %f Price = %f", model, pType, speed, ram, hd, screen, price);
  96. }
  97. if(strcmp(pType, "printer") == 0 && strcmp(maker, condA) == 0) {
  98. EXEC SQL
  99. SELECT color, type, price
  100. INTO :color, :printerType, :price
  101. FROM Printer
  102. WHERE model = :model;
  103. printf("\nModel # = %d, Product type = %s, Color = %s, Type = %s, Price = %f", model, pType, color, printerType, price);
  104. }
  105. }
  106. }
  107. if(strcmp(qAorB, "b") == 0) {
  108. printf("\nEnter Max Budget = ");
  109. scanf("%f",&pcPrinter);
  110. printf("\nEnter Min PC speed = ");
  111. scanf("%f",&minSpeed);
  112. float result = pcPrinter;
  113. float pc = 0;
  114. float printer = 0;
  115. int i;
  116. int j;
  117. char hasColor[20];
  118.  
  119. /*** cursor declaration ***/
  120. exec sql declare pc_crs cursor for
  121. select price from PC where speed >= :minSpeed;
  122.  
  123. printf("\n1 = %f.......", minSpeed);
  124.  
  125. EXEC SQL
  126. SELECT COUNT(*) INTO :pcNum FROM PC WHERE speed >= :minSpeed;
  127. printf("\n2.......pcNum = %d", pcNum);
  128. while(1) {
  129. printf("\n3.......");
  130.  
  131. /*** cursor declaration ***/
  132. exec sql declare colorP_crs cursor for
  133. select price from Printer where color = 't';
  134. printf("\n4.......");
  135. EXEC SQL
  136. SELECT COUNT(*) INTO :printerNum FROM Printer;
  137.  
  138. printf("\n5.......printerNum = %d", printerNum);
  139.  
  140. exec sql declare printer_crs cursor for
  141. select price from Printer;
  142. printf("\n6.......");
  143. EXEC SQL
  144. SELECT COUNT(*) INTO :colorNum FROM Printer WHERE color = 't';
  145. printf("\n7.......colorNum = %d", colorNum);
  146.  
  147. EXEC SQL
  148. FETCH pc_crs INTO :price;
  149. printf("\n8.......problem?");
  150.  
  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