Guest User

Untitled

a guest
Mar 7th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 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 condA[20];
  20. char login[20], password[20];
  21. float pcPrinter;
  22. float minSpeed;
  23. int model;
  24. int model1;
  25. int model2;
  26. int pcNum;
  27. int printerNum;
  28. int colorNum;
  29. float tempPrice;
  30. float price;
  31. float result;
  32. float pc = 0;
  33. float printer;
  34. int pcModel;
  35. int printerModel;
  36. char hasColor[20];
  37. char color[1];
  38. char SQLSTATE[6];
  39. exec sql end declare section;
  40.  
  41. int print_sql_state(char *code){
  42. printf("\nSQLSTATE=%s\n",code);
  43. }
  44.  
  45. int main()
  46. {
  47. /*** error handler ***/
  48. exec sql whenever sqlerror goto report_error;
  49. exec sql whenever not found goto notfound;
  50.  
  51. /*** Prompt user for login name and password ***/
  52. printf("Username=");
  53. gets(login);
  54. printf("Password=");
  55. char *pass_string = malloc(100);
  56. pass_string=getpass("");
  57. strcpy(password, pass_string);
  58.  
  59. /*** Connect to DB ***/
  60. exec sql connect :login
  61. identified by :password;
  62.  
  63. printf("\nEnter Max Budget = ");
  64. scanf("%f",&pcPrinter);
  65. printf("\nEnter Min PC speed = ");
  66. scanf("%f",&minSpeed);
  67. result = pcPrinter;
  68. pc = 0;
  69. printer = 0;
  70. int i;
  71. int j = 0;
  72.  
  73. /*** cursor declaration ***/
  74. exec sql declare pc_crs cursor for
  75. select price, model from PC where speed >= :minSpeed;
  76. printf("\n%d = ", j);
  77. j++;
  78. EXEC SQL
  79. SELECT COUNT(*) INTO :pcNum FROM PC WHERE speed >= :minSpeed;
  80. printf("\n%d = ", j);
  81. j++;
  82.  
  83. EXEC SQL OPEN pc_crs;
  84. printf("\n%d = ", j);
  85. j++;
  86.  
  87. while(1) {
  88. printf("\n%d = ", j);
  89. j++;
  90.  
  91. /*** cursor declaration ***/
  92. exec sql declare colorP_crs cursor for
  93. select price, model1 from Printer where color = 't';
  94. printf("\n%d = ", j);
  95. j++;
  96.  
  97. EXEC SQL
  98. SELECT COUNT(*) INTO :printerNum FROM Printer;
  99. printf("\n%d = ", j);
  100. j++;
  101.  
  102. exec sql declare printer_crs cursor for
  103. select price, model2 from Printer;
  104. printf("\n%d = ", j);
  105. j++;
  106.  
  107. EXEC SQL
  108. SELECT COUNT(*) INTO :colorNum FROM Printer WHERE color = 't';
  109. printf("\n%d = ", j);
  110. j++;
  111.  
  112. EXEC SQL FETCH pc_crs INTO :price, :model;
  113. printf("\n%d = ", j);
  114. j++;
  115.  
  116. if(colorNum > 0) {
  117. printf("\n%d = ", j);
  118. j++;
  119.  
  120. strcpy(hasColor, "Yes");
  121. EXEC SQL OPEN colorP_crs;
  122. printf("\n%d = ", j);
  123. j++;
  124.  
  125. for(i = 0 ; i < colorNum; i++) {
  126. printf("\n%d = ", j);
  127. j++;
  128.  
  129. EXEC SQL
  130. FETCH colorP_crs INTO :tempPrice, :model1;
  131. printf("\n%d = ", j);
  132. j++;
  133.  
  134. if((tempPrice + price) <= result) {
  135. printf("\n%d = ", j);
  136. j++;
  137.  
  138. result = price + tempPrice;
  139. printerModel = model1;
  140. pcModel = model;
  141. pc = price;
  142. printer = tempPrice;
  143. }
  144. printf("\n%d = ", j);
  145. j++;
  146.  
  147. }
  148. printf("\n%d = ", j);
  149. j++;
  150.  
  151.  
  152. EXEC SQL CLOSE colorP_crs;
  153.  
  154. }else if(colorNum == 0 && !(pc == 0 || printer == 0)) {
  155. strcpy(hasColor, "No");
  156.  
  157. EXEC SQL OPEN printer_crs;
  158.  
  159. for(i = 0 ; i < printerNum; i++) {
  160. EXEC SQL
  161. FETCH printer_crs INTO :tempPrice, model2;
  162.  
  163. if((tempPrice + price) <= result) {
  164. result = price + tempPrice;
  165. printerModel = model2;
  166. pcModel = model;
  167. pc = price;
  168. printer = tempPrice;
  169. }
  170. }
  171. EXEC SQL CLOSE printer_crs;
  172.  
  173. }
  174. }
  175.  
  176. EXEC SQL CLOSE pc_crs;
  177.  
  178. notfound:
  179. /* END OF DATA */
  180. printf("\nCheapest System (pc + printer): \nPC Model = %d, Printer Model = %d \nColor Printer = %s, \nPC = %f,\nPrinter = %f, \nTotal price = %f",pcModel, printerModel, hasColor, pc, printer, result);
  181. printf("\nEND OF DATA");
  182. print_sql_state(SQLSTATE);
  183. exec sql close pc_crs;
  184. exec sql close printer_crs;
  185. return 0;
  186. report_error:
  187. printf("\nERROR OCCURRED");
  188. print_sql_state(SQLSTATE);
  189. return 1;
  190. }
  191.  
  192. void myTrim(char* str) {
  193.  
  194. int i = strlen(str) - 1;
  195.  
  196. while(i >= 0) {
  197. if(str[i] == '\0') continue;
  198. if(str[i] == 32) str[i] = '\0';
  199. i--;
  200. }
  201.  
  202. }
Add Comment
Please, Sign In to add comment