Memory Drive

반응형

  1. /*
  2. *  sample1.pc
  3. *
  4. *  Prompts the user for an employee number,
  5. *  then queries the emp table for the employee's
  6. *  name, salary and commission.  Uses indicator
  7. *  variables (in an indicator struct) to determine
  8. *  if the commission is NULL.
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. /* Define constants for VARCHAR lengths. */
  14. #define     UNAME_LEN      20
  15. #define     PWD_LEN        40
  16. /* Declare variables.No declare section is needed if MODE=ORACLE.*/
  17. VARCHAR     username[UNAME_LEN]
  18. /* VARCHAR is an Oracle-supplied struct */
  19. varchar     password[PWD_LEN];   
  20. /* varchar can be in lower case also. */
  21. /*
  22. Define a host structure for the output values of a SELECT statement.
  23. */
  24. struct {
  25.   VARCHAR   emp_name[UNAME_LEN];
  26.   float     salary;
  27.   float     commission;
  28. } emprec;
  29. /*
  30. Define an indicator struct to correspond to the host output struct. */
  31. struct
  32. {
  33.   short     emp_name_ind;
  34.   short     sal_ind;
  35.   short     comm_ind;
  36. } emprec_ind;
  37. /*  Input host variable. */
  38. int         emp_number;
  39. int         total_queried;
  40. /* Include the SQL Communications Area.
  41.   You can use #include or EXEC SQL INCLUDE. */
  42. #include <sqlca.h>
  43. /* Declare error handling function. */
  44. void sql_error();
  45. main()
  46. {
  47.   char temp_char[32];
  48. /* Connect to ORACLE--
  49. * Copy the username into the VARCHAR.
  50. */
  51.   strncpy((char *) username.arr, "SCOTT", UNAME_LEN);
  52. /* Set the length component of the VARCHAR. */
  53.   username.len = strlen((char *) username.arr);
  54. /* Copy the password. */
  55.   strncpy((char *) password.arr, "TIGER", PWD_LEN);
  56.   password.len = strlen((char *) password.arr);
  57. /* Register sql_error() as the error handler. */
  58.   EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
  59. /* Connect to ORACLE.  Program will call sql_error()
  60. * if an error occurs when connecting to the default database.
  61. */
  62.   EXEC SQL CONNECT :username IDENTIFIED BY :password;
  63.   printf("\nConnected to ORACLE as user: %s\n", username.arr);
  64. /* Loop, selecting individual employee's results */
  65.   total_queried = 0;
  66.   for (;;)
  67.   {
  68. /* Break out of the inner loop when a
  69. * 1403 ("No data found") condition occurs.
  70. */
  71.        EXEC SQL WHENEVER NOT FOUND DO break;
  72.        for (;;)
  73.        {
  74.            emp_number = 0;
  75.            printf("\nEnter employee number (0 to quit): ");
  76.            gets(temp_char);
  77.            emp_number = atoi(temp_char);
  78.            if (emp_number == 0)
  79.                break;
  80.            EXEC SQL SELECT ename, sal, NVL(comm, 0)
  81.                INTO :emprec INDICATOR :emprec_ind
  82.                FROM EMP
  83.                WHERE EMPNO = :emp_number;
  84. /* Print data. */
  85.            printf("\n\nEmployee\tSalary\t\tCommission\n");
  86.            printf("--------\t------\t\t----------\n");
  87. /* Null-terminate the output string data. */
  88.            emprec.emp_name.arr[emprec.emp_name.len] = '\0';
  89.            printf("%-8s\t%6.2f\t\t",
  90.                emprec.emp_name.arr, emprec.salary);
  91.            if (emprec_ind.comm_ind == -1)
  92.                printf("NULL\n");
  93.            else
  94.                printf("%6.2f\n", emprec.commission);
  95.            total_queried++;
  96.        }  /* end inner for (;;) */
  97.        if (emp_number == 0) break;
  98.        printf("\nNot a valid employee number - try again.\n");
  99.   } /* end outer for(;;) */
  100.   printf("\n\nTotal rows returned was %d.\n", total_queried);
  101.   printf("\nG'day.\n\n\n");
  102. /* Disconnect from ORACLE. */
  103.   EXEC SQL COMMIT WORK RELEASE;
  104.   exit(0);
  105. }
  106. void sql_error(msg)
  107. char *msg;
  108. {
  109.   char err_msg[128];
  110.   int buf_len, msg_len;
  111.   EXEC SQL WHENEVER SQLERROR CONTINUE;
  112.   printf("\n%s\n", msg);
  113.   buf_len = sizeof (err_msg);
  114.   sqlglm(err_msg, &buf_len, &msg_len);
  115.   printf("%.*s\n", msg_len, err_msg);
  116.   EXEC SQL ROLLBACK RELEASE;
  117.   exit(1);
  118. }
 
반응형

'Computer_IT > C++' 카테고리의 다른 글

[VC] 인라인 ASM 으로 작성한 연산  (1) 2006.08.14
[VC] MMX support 여부  (0) 2006.08.14
Const Member변수 초기화 특성  (0) 2006.04.24
LinkedList 자료  (0) 2006.04.06
VC++ 단축키 모음  (2) 2006.03.19

반응형
[CODE type="c"]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "d:\\student.dat"
FILE * fpStd;
typedef struct node NODE;
struct node {
NODE * flink;
NODE * blink;
char id[12];
char name[30];
};
typedef struct anchor ANCHOR;
struct anchor {
NODE * flink;
NODE * blink;
};
ANCHOR anc;
NODE * makeNode(char * id, char * name);
NODE * makeEmptyNode();
void printData(void);
char showMenu();
void enqAft(NODE * newNode, NODE * oldNode);
void enqBef(NODE * newNode, NODE * oldNode);
NODE * findNode(char * id);
void delNode(NODE * node);
int getListSize(void);
void writeFile(void);
void readFile();
void clearList(void);
int main(void) {
 // 1. anchor가 자신의 주소를 가르키게 함
anc.flink = (NODE *) &anc;
anc.blink = (NODE *) &anc;
 char choice;
char input[80];
NODE * newNode;
NODE * tempNode;
char id[12];
char name[30];
 choice = showMenu();
 while (choice != '0') {
  if (choice == '1') {
 
  printf("학생의 정보를 입력하세요 (아이디 이름) : ");
  gets(input); // aaa 홍길동
  sscanf(input, "%s %s", id, name);
  newNode = makeNode(id, name);
   tempNode = findNode(id);
  if (tempNode == NULL) {
   enqBef(newNode, (NODE *)&anc);
  } else {
   printf("%s 아이디는 존재합니다. \n", id);
  }
  } else if (choice == '2') {
   printf("찾으려는 학생의 아이디를 입력하세요 : ");
  gets(input); // aaa 홍길동
  sscanf(input, "%s", id);
   newNode = findNode(id);
   if (newNode == NULL) {
   printf("%s 아이디는 존재하지 않습니다. \n", id);
  } else {
   printf("학생의 정보를 입력하세요 (이름) : ");
   gets(input); // aaa 홍길동
   sscanf(input, "%s", newNode->name);    
  }

 } else if (choice == '3') {
  printf("찾으려는 학생의 아이디를 입력하세요 : ");
  gets(input); // aaa 홍길동
  sscanf(input, "%s", id);
   newNode = findNode(id);
   if (newNode == NULL) {
   printf("%s 아이디는 존재하지 않습니다. \n", id);
  } else {
   delNode(newNode);
  }
  } else if (choice == '4') {
   printf("찾으려는 학생의 아이디를 입력하세요 : ");
  gets(input); // aaa 홍길동
  sscanf(input, "%s", id);
   newNode = findNode(id);
   if (newNode == NULL) {
   printf("%s 아이디는 존재하지 않습니다. \n", id);
  } else {
   printf("찾을 학생 %s의 정보 : %s, %s\n", id, newNode->id, newNode->name);
  }

 } else if (choice == '5') {
   printData();
  } else if (choice == '6') {
   printf("총 학생의 수 : %d명 입니다.\n", getListSize());
  } else if (choice == '7') {
   writeFile();
  } else if (choice == '8') {
   readFile();
  } else if (choice == '9') {
   clearList();
  } else {
 }
 
 printf("\n<계속 진행하려면 엔터를 치세요 >\n");
 gets(input);
 
 system("cls");
 choice = showMenu();  
}
 return 0;
}
NODE * makeNode(char *id, char *name) {
NODE * newNode = (NODE *) malloc(sizeof(NODE));
strcpy(newNode->id, id);
strcpy(newNode->name, name);
return newNode;
}
NODE * makeEmptyNode() {
NODE * newNode = (NODE *) malloc(sizeof(NODE));
return newNode;
}
void enqAft(NODE * newNode, NODE * oldNode) {
newNode->flink = oldNode->flink;
newNode->blink = oldNode;
 oldNode->flink->blink = newNode;
oldNode->flink = newNode;
}
void enqBef(NODE * newNode, NODE * oldNode) {
newNode->flink = oldNode;
newNode->blink = oldNode->blink;
 oldNode->blink->flink = newNode;
oldNode->blink = newNode;
}
NODE * findNode(char * id) {
NODE * curNode = anc.flink;
NODE * findNode = NULL;
 while(curNode != (NODE *) &anc) {
 if (strcmp(curNode->id, id) == 0) {
  findNode = curNode;
 }
 curNode = curNode->flink;
}
 return findNode;
}
void delNode(NODE * node) {
 node->blink->flink = node->flink;
node->flink->blink = node->blink;
 free(node);
}
int getListSize() {
NODE * curNode = anc.flink;
int count = 0;
 while(curNode != (NODE *) &anc) {
 count++;
 curNode = curNode->flink;
}
 return count;
}

void printData(void) {
NODE * curNode = anc.flink;
 while(curNode != (NODE *) &anc) {
 printf("학생의 아이디 : %s, 이름 : %s.\n", curNode->id, curNode->name);
 curNode = curNode->flink;
}
}
void clearList() {
NODE * curNode = anc.flink;
NODE * tempNode = NULL;
 while(curNode != (NODE *) &anc) {
 tempNode = curNode->flink;
 delNode(curNode);
 curNode = tempNode;
}
}

void readFile() {
fpStd = fopen(FILE_NAME, "r");
 if (fpStd == NULL) {
 printf("파일 열기에 실패함.\n");
} else {
  clearList();
  NODE * newNode = makeEmptyNode();
 while (fread(newNode, sizeof(NODE), 1, fpStd) > 0) {
  enqBef(newNode, (NODE *) &anc);
  newNode = makeEmptyNode();
 }
  fclose(fpStd);
}
}
void writeFile() {
fpStd = fopen(FILE_NAME, "w");
 if (fpStd == NULL) {
 printf("파일 열기에 실패함.\n");
} else {
  NODE * curNode = anc.flink;
  while(curNode != (NODE *) &anc) {
  fwrite(curNode, sizeof(NODE), 1, fpStd);
  curNode = curNode->flink;
 }
  fclose(fpStd);
}
}
char showMenu() {
char choice, dummy;
 printf("************************************\n");
printf("* 1. 학생 추가                      \n");
printf("* 2. 학생 수정                      \n");
printf("* 3. 학생 삭제                      \n");
printf("* 4. 학생 찾기                      \n");
printf("* --------------------------------- \n");
printf("* 5. 학생 목록                      \n");
printf("* 6. 학생 수 출력                   \n");
printf("* --------------------------------- \n");
printf("* 7. 파일 저장                      \n");
printf("* 8. 파일 읽기                      \n");
printf("* --------------------------------- \n");
printf("* 9. 모두 지우기                    \n");
printf("* --------------------------------- \n");
printf("* 0. 종료                           \n");
printf("************************************\n");
printf(">> ");
 scanf("%c%c", &choice, &dummy);
 return choice;
}

[/HTML][/CODE]
반응형

'Computer_IT > C++' 카테고리의 다른 글

Example Program: A Simple Query  (0) 2006.08.07
Const Member변수 초기화 특성  (0) 2006.04.24
VC++ 단축키 모음  (2) 2006.03.19
Console - 바둑소스  (0) 2006.02.22
Console Codepage 설정.  (0) 2006.02.15