Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *********************************
- * Jorge Ceballos
- * CS 430
- * HW1
- * Q2
- **********************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <oraca.h>
- void myTrim(char* );
- /*** header file for ORACLE definitions ***/
- exec sql include sqlca;
- /*** Variable Declaration Block ***/
- exec sql begin declare section;
- char condA[20];
- char login[20], password[20];
- float pcPrinter;
- float minSpeed;
- int model;
- int model1;
- int model2;
- int pcNum;
- int printerNum;
- int colorNum;
- float tempPrice;
- float price;
- float result;
- float pc = 0;
- float printer;
- int pcModel;
- int printerModel;
- char hasColor[20];
- char color[1];
- char SQLSTATE[6];
- exec sql end declare section;
- int print_sql_state(char *code){
- printf("\nSQLSTATE=%s\n",code);
- }
- int main()
- {
- /*** error handler ***/
- exec sql whenever sqlerror goto report_error;
- exec sql whenever not found goto notfound;
- /*** Prompt user for login name and password ***/
- printf("Username=");
- gets(login);
- printf("Password=");
- char *pass_string = malloc(100);
- pass_string=getpass("");
- strcpy(password, pass_string);
- /*** Connect to DB ***/
- exec sql connect :login
- identified by :password;
- printf("\nEnter Max Budget = ");
- scanf("%f",&pcPrinter);
- printf("\nEnter Min PC speed = ");
- scanf("%f",&minSpeed);
- result = pcPrinter;
- pc = 0;
- printer = 0;
- int i;
- int j = 0;
- /*** cursor declaration ***/
- exec sql declare pc_crs cursor for
- select price, model from PC where speed >= :minSpeed;
- printf("\n%d = ", j);
- j++;
- EXEC SQL
- SELECT COUNT(*) INTO :pcNum FROM PC WHERE speed >= :minSpeed;
- printf("\n%d = ", j);
- j++;
- EXEC SQL OPEN pc_crs;
- printf("\n%d = ", j);
- j++;
- while(1) {
- printf("\n%d = ", j);
- j++;
- /*** cursor declaration ***/
- exec sql declare colorP_crs cursor for
- select price, model1 from Printer where color = 't';
- printf("\n%d = ", j);
- j++;
- EXEC SQL
- SELECT COUNT(*) INTO :printerNum FROM Printer;
- printf("\n%d = ", j);
- j++;
- exec sql declare printer_crs cursor for
- select price, model2 from Printer;
- printf("\n%d = ", j);
- j++;
- EXEC SQL
- SELECT COUNT(*) INTO :colorNum FROM Printer WHERE color = 't';
- printf("\n%d = ", j);
- j++;
- EXEC SQL FETCH pc_crs INTO :price, :model;
- printf("\n%d = ", j);
- j++;
- if(colorNum > 0) {
- printf("\n%d = ", j);
- j++;
- strcpy(hasColor, "Yes");
- EXEC SQL OPEN colorP_crs;
- printf("\n%d = ", j);
- j++;
- for(i = 0 ; i < colorNum; i++) {
- printf("\n%d = ", j);
- j++;
- EXEC SQL
- FETCH colorP_crs INTO :tempPrice, :model1;
- printf("\n%d = ", j);
- j++;
- if((tempPrice + price) <= result) {
- printf("\n%d = ", j);
- j++;
- result = price + tempPrice;
- printerModel = model1;
- pcModel = model;
- pc = price;
- printer = tempPrice;
- }
- printf("\n%d = ", j);
- j++;
- }
- printf("\n%d = ", j);
- j++;
- EXEC SQL CLOSE colorP_crs;
- }else if(colorNum == 0 && !(pc == 0 || printer == 0)) {
- strcpy(hasColor, "No");
- EXEC SQL OPEN printer_crs;
- for(i = 0 ; i < printerNum; i++) {
- EXEC SQL
- FETCH printer_crs INTO :tempPrice, model2;
- if((tempPrice + price) <= result) {
- result = price + tempPrice;
- printerModel = model2;
- pcModel = model;
- pc = price;
- printer = tempPrice;
- }
- }
- EXEC SQL CLOSE printer_crs;
- }
- }
- EXEC SQL CLOSE pc_crs;
- notfound:
- /* END OF DATA */
- 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);
- printf("\nEND OF DATA");
- print_sql_state(SQLSTATE);
- exec sql close pc_crs;
- exec sql close printer_crs;
- return 0;
- report_error:
- printf("\nERROR OCCURRED");
- print_sql_state(SQLSTATE);
- return 1;
- }
- void myTrim(char* str) {
- int i = strlen(str) - 1;
- while(i >= 0) {
- if(str[i] == '\0') continue;
- if(str[i] == 32) str[i] = '\0';
- i--;
- }
- }
Add Comment
Please, Sign In to add comment