【笔记】参照变量

通过使用参照变量,可以使得应用程序共享相同对象,从而降低占用空间。有两种参照变量:ref cursor 和ref obj_type。

1.ref cursor :当使用显式游标时,需要在定义显式游标时指定相应的select语句,这种显式游标称为静态游标。当使用游标变量时,在定义游标变量时不需要指定select语句,而是在打开游标时指定select语句,从而实现动态的游标操作。如:

  declare

   type c1 is ref cursor;

emp_cursor c1;

v_ename emp.ename%type;

v_sal emp.sal%type;

begin

open emp_cursor for

   selcet ename,sal from emp where depno=10;

loop

  fetch emp_cursor into v_ename,v_sal;

exit when emp_cursor%notfound;

dbms_output.put_line(v_ename);

endloop;

close emp_cursor;

end;

   如例所示:c1为ref cursor类型,而emp_cursor为游标变量,并且在打开游标变量时指定了其所对应的select语句。

2.ref obj_type:当编写对象类型应用时,为了共享相同对象,可以使用ref引用对象类型,ref实际是指向对象实例的指针。下面通过示例说明如何使用ref。首先建立对象类型home和对象表homes,然后插入数据。如下:

  create or replace type home_type is object (

   street varchar2(50),city varchar2(20),

   state varchar(20),zipcode varchar2(6),

    owner varchar2(10));

   /

  create table home of home_type;

 insert into homes values('呼伦北路12号',‘呼和浩特’,‘内蒙’,‘010010’,‘马鸣’);

insert into homes values('呼伦北路13号',‘呼和浩特’,‘内蒙’,‘010010’,‘秦斌’);

commit;

   如例所示,对象表homes存放着家庭所在地以及户主姓名。假定每个家庭有四口人,当进行人口统计时,为了使得同一家庭的每个家庭成员可以共享家庭地址,可以使用ref引用home_type对象类型,从而降低占用空间。如:

   create table person (

    id number(6) primary key,

   name varchar2(10),addr ref home_type );

insert into person select 1,'马鸣',ref(p)

   from homes p where p.owner='马鸣';

insert into person select 1,'马武',ref(p)

   from homes p where p.owner='马鸣';

insert into person select 1,'王敏,ref(p)

   from homes p where p.owner='马鸣';

commit;

  如例所示,当为person表插入数据是,addr列将会存放指向homes表响应数据的地址指针。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22786751/viewspace-619971/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22786751/viewspace-619971/

好的,您需要一个基于命令行界面的笔记管理系统,使用C语言实现。下面是一个简单的实现,您可以根据自己的需求进行修改和扩展。 ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NOTE_SIZE 10000 // 笔记最大长度 #define MAX_NOTE_NAME_SIZE 100 // 笔记名称最大长度 // 笔记结构体 typedef struct Note { char name[MAX_NOTE_NAME_SIZE]; // 笔记名称 char content[MAX_NOTE_SIZE]; // 笔记内容 struct Note *next; // 指向下一个笔记的指针 } Note; // 全局变量,指向笔记链表的头节点 Note *note_list = NULL; // 显示菜单 void show_menu() { printf("Welcome to Note Management System!\n"); printf("1. Open note\n"); printf("2. Create note\n"); printf("3. Edit note\n"); printf("4. Search note\n"); printf("5. Save note\n"); printf("6. Close note\n"); printf("7. Help\n"); printf("8. Exit\n"); } // 创建新笔记 Note *create_note() { Note *new_note = (Note *)malloc(sizeof(Note)); new_note->next = NULL; printf("Please enter the name of the note: "); scanf("%s", new_note->name); printf("Please enter the content of the note: "); getchar(); // 消耗掉之前输入的回车符 fgets(new_note->content, MAX_NOTE_SIZE, stdin); return new_note; } // 打开笔记 void open_note() { char name[MAX_NOTE_NAME_SIZE]; printf("Please enter the name of the note you want to open: "); scanf("%s", name); Note *p = note_list; while (p != NULL) { if (strcmp(p->name, name) == 0) { printf("%s\n", p->content); return; } p = p->next; } printf("Note not found.\n"); } // 编辑笔记 void edit_note() { char name[MAX_NOTE_NAME_SIZE]; printf("Please enter the name of the note you want to edit: "); scanf("%s", name); Note *p = note_list; while (p != NULL) { if (strcmp(p->name, name) == 0) { printf("Please enter the new content of the note: "); getchar(); // 消耗掉之前输入的回车符 fgets(p->content, MAX_NOTE_SIZE, stdin); printf("Note updated.\n"); return; } p = p->next; } printf("Note not found.\n"); } // 查找笔记 void search_note() { char keyword[MAX_NOTE_SIZE]; printf("Please enter the keyword to search: "); scanf("%s", keyword); Note *p = note_list; int count = 0; while (p != NULL) { if (strstr(p->content, keyword) != NULL) { printf("%s\n", p->name); count++; } p = p->next; } if (count == 0) { printf("No note found.\n"); } } // 保存笔记 void save_note() { char name[MAX_NOTE_NAME_SIZE]; printf("Please enter the name of the note you want to save: "); scanf("%s", name); Note *p = note_list; while (p != NULL) { if (strcmp(p->name, name) == 0) { FILE *fp = fopen(name, "w"); if (fp == NULL) { printf("Failed to save note.\n"); return; } fputs(p->content, fp); fclose(fp); printf("Note saved.\n"); return; } p = p->next; } printf("Note not found.\n"); } // 关闭笔记 void close_note() { printf("Note closed.\n"); } // 帮助信息 void show_help() { printf("Note Management System\n"); printf("Usage:\n"); printf("1. Open note: display the content of a note\n"); printf("2. Create note: create a new note\n"); printf("3. Edit note: edit the content of an existing note\n"); printf("4. Search note: search for notes containing a keyword\n"); printf("5. Save note: save the content of a note to a file\n"); printf("6. Close note: close the current note\n"); printf("7. Help: display this help message\n"); printf("8. Exit: exit the program\n"); } int main() { int choice = -1; while (choice != 8) { show_menu(); printf("Please enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: // 打开笔记 open_note(); break; case 2: // 创建笔记 Note *new_note = create_note(); new_note->next = note_list; note_list = new_note; printf("Note created.\n"); break; case 3: // 编辑笔记 edit_note(); break; case 4: // 查找笔记 search_note(); break; case 5: // 保存笔记 save_note(); break; case 6: // 关闭笔记 close_note(); break; case 7: // 帮助信息 show_help(); break; case 8: // 退出程序 printf("Goodbye!\n"); break; default: printf("Invalid choice.\n"); } } // 释放笔记链表的内存 Note *p = note_list; while (p != NULL) { Note *temp = p; p = p->next; free(temp); } return 0; } ``` 这个笔记管理系统具有基本的功能,可以进行笔记的创建、编辑、保存、打开、关闭和查询。您可以根据自己的需求进行修改和扩展,比如添加更多的菜单选项,支持多种文件格式的导入和导出等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值