- #include <assert.h>
- void main()
- {
- char* p = NULL;
- assert(p != NULL);
- Test a;
- }
- 执行时会提示错误
- 如果这样
- #include <iostream.h>
- #include "Test.h"
- #include <assert.h>
- void main()
- {
- char* p;
- assert(p != NULL);
- Test a;
- }
- 程序会照样执行,原因在于没有对p进行初始化,p的值不一定为NULL,这一点不能忘记;
- 类的封装调用时,以util中的类的调用为例,先要在Tools 中的options中的directories中进行设置,把各个文件加入;在test中进行调试;
- 8-20
- //在D盘中生成一个txt文件
- CFileWriter c_write("D://");
- c_write.SetFileName("D://", ".txt", "filename");
- c_write.Open();
- c_write.Append("hello, this is a new file");
- c_write.Close();
- /
- 为了防止头文件被重复引用,应当用ifndef/define/endif结构产生预处理块。
- ///
- strlen()
- 求出的是字符串的实际长度,不包括'/0'
- aTime::aTime(char* date_time):CTime(date_time)
- {
- }
- 调用基类的构造函数
- /
- 五分钟内,找出下面程序中的三处错误, (非定义和语法错误)
- unsigned char * Test(void)
- {
- unsigned char i=0;
- int j=1000;
- char* p=NULL;
- for(i=0;i <j;i++)
- {
- strncpy(p,"hello",5);
- }
- return &i;
- }
- 注意:不是定义和语法上的错误
- 1. p没有分配空间
- 2. i永远小于j,循环不会结束
- 3. 返回局部变量地址
- unsigned char * Test(void)
- {
- unsigned char i=0;
- int j=1000;
- char* p=NULL;
- for(i=0;i <j;i++) //char类型的i和int类的j做比较;实际编译器不报错!
- {
- strncpy(p,"hello",5); //copy字符串溢出
- }
- return &i; //返回了局部的变量地址
- }
- //
- char* test(char* p)
- {
- p++;
- return p;
- }
- void main()
- {
- char* p = "njau";
- cout << test(p) << endl;
- cout << p << endl;
- }
- 结果是
- jau
- njau
- test(p) 返回的是当前的指针,
- 在执行函数之后,p的值并没有改变,指向不变;
- 这种方式更容易理解
- int add(int t)
- {
- ++t;
- return t;
- }
- int a = 2;
- cout << add(a) << endl;
- cout << a << endl;
- 结果是
- 3
- 2
- /
- char* strcpy(char* strDest,const char* strSource)
- {
- assert((strDest!=NULL) &&(strSource!=NULL));
- while((*strDest++=*strSource++)!='/0');
- return strDest;
- }
- char* r_strcpy(char* strDest,const char* strSource)
- {
- assert((strDest!=NULL) &&(strSource!=NULL));
- char* newAdress=strDest;
- while((*strDest++=*strSource++)!='/0');
- return newAdress;
- }
- 第一个函数在执行语句
- char s1[10], s2[10] = "second", *s3 = "hello";
- strcpy(strcpy(s1,s2),s3);
- 时,就会出问题
- 可以这样理解,第一种方式返回的不是原变量,相当于没有返回值;
- /
- 2008-8-21
- 函数floor()的功能是取整数值,floor(2.7) = 2, floor(2.1) = 2
- 用floorv()来实现四舍五入
- int round(double x)
- {
- return floor(x + 0.5);
- }
- 一个类可以访问本类的任何对象的私有成员
- 下午
- 程序问题
- char c[16];
- char b[3] = "a";
- if (strcpy(c, b) != "a")
- {
- cout << "what's out!/n";
- }
- 结果输出了what's out!
- 原因在于 不能用 != 进行字符串的比较
- 注意"" 和'/0'的区别
- 一个是字符串的空,一个是字符结束标识
- //
- -> 的理解
- -> 综合了* 和 . 的功能
- p->data 等价于 (*p).data
- /
- 8-22
- typedef 不会产生新的类型,只是仅仅是产生已有的类型的别名
- /
- LogFile 对当前的exe文件设置日志记录log
- ///
- 8-23 下午
- 在一个Source.bat二进制文件中,有许多自然数,要求找出1000~2000之间的自然数,把他们进行排序,Source.bat有1GB大,要求把排序后的数存到Beat.bat中,要考虑到内存和效率问题。
- 一种方法:不用去排序,统计每个数字出现的次数就可以,即以数字的大小为对象去处理,
- 如a[0],表示1000出现的个数,a[1]表示1001出现的个数
- 有哈希方法的影子在其中
- //
- 8-25
- 对于赋值函数,应当用“引用传递”的方式返回String对象。如果用“值传递”的方式,虽然功能仍然正确,但由于return语句要把 *this拷贝到保存返回值的外部存储单元之中,增加了不必要的开销,降低了赋值函数的效率。例如:
- String a,b,c;
- …
- a = b; // 如果用“值传递”,将产生一次 *this 拷贝
- a = b = c; // 如果用“值传递”,将产生两次 *this 拷贝
- String的相加函数operate + 的实现如下:
- String operate+(const String &s1, const String &s2)
- {
- String temp;
- delete temp.data; // temp.data是仅含‘/0’的字符串
- temp.data = new char[strlen(s1.data) + strlen(s2.data) +1];
- strcpy(temp.data, s1.data);
- strcat(temp.data, s2.data);
- return temp;
- }
- 对于相加函数,应当用“值传递”的方式返回String对象。如果改用“引用传递”,那么函数返回值是一个指向局部对象temp的“引用”。由于temp在函数结束时被自动销毁,将导致返回的“引用”无效。例如:
- c = a + b;
- 此时 a + b 并不返回期望值,c什么也得不到,流下了隐患。
- ///
- 8-26 晚上
- 连接数据库时,其他的文件中要是发生了变化,要再编译,选中文件所在的大类文件,右键,build
- long j = Login("oracle");
- T_ACCT_BALANCE t_table;
- t_table.SetConn(DefaultConnect);
- t_table.SetBatSize(1);
- t_table.SetTableName("ACCT_BALANCE_A");
- //
- 8-27
- PL/SQL
- File -> new -> SQLWindow 打开查询界面
- ///
- j =t_table.Query();
- // t_table.Query("select balance from ACCT_BALANCE_A");
- int i = 0;
- do {
- cout << t_table.BALANCE.Float() << endl;
- i++;
- } while(t_table.Next() > 0 && i < 10);
- Logout();
- STRU_CONNECT_STRING stru;
- strcpy(stru.szConstr, "@HADEV");
- strcpy(stru.szPassword, "ams1");
- strcpy(stru.szUserName, "ams1");
- Connection con;
- con.Login(stru);
- con.ExecSql(" insert into person values('Bush');");
- con.Logout();
- 问题在哪里?留下这个疑问,怎么访问数据库
- ///
- 类的私有成员 在标记上有一把锁,要能够一眼看书来
- /
- long j = 0;
- char str[1024];
- j = Login("oracle");
- CRecords c_Records(DefaultConnect, 1024);
- strcpy(str, "select balance from ACCT_BALANCE_A where balance < 2");
- j = c_Records.Query(str);
- // j = DefaultConnect.ExecSql(str);
- while (j > 0 )
- {
- cout << c_Records.Field(0).Float() << endl;
- j = c_Records.Next();
- }
- Logout();
- //
- strcpy(str, "select balance, eff_date from ACCT_BALANCE_A where balance < 2");
- j = c_Records.Query(str);
- while (j > 0 )
- {
- cout << c_Records.Field(0).Float() << " ";
- cout << c_Records.Field(1).Char() << endl;
- j = c_Records.Next();
- }
- Logout();
- /
- select * from ACCT_BALANCE_A where balance < 2
- select * from person
- create table person(name char(20), age int);
- drop table person
- insert into person values('Mary',17)
- //
- 8-28
- 在插入insert操作时,由于元组有主键,再次插入同样的数据后,就会出问题,主键冲突;
- 再次插入时,主键应该不同;
- delete是根据主码进行识别删除的,基本的update也是根据主码去识别元组的;
- Connection cc;
- int j = cc.Login ("oracle") ;
- if (j > 0)
- {
- cout << "登录/n";
- }
- CRecords c_Records(cc, 1024);
- j = c_Records.Query("select * from person");
- cout << c_Records.Field(0).Char() << endl;
- cc.Logout();
- //
- 赋值函数不可忽略
- 如果不主动编写拷贝构造函数和赋值函数,编译器将以“位拷贝”的方式自动生成缺省的函数。倘若类中含有指针变量,那么这两个缺省的函数就隐含了错误。以类String的两个对象a,b为例,假设a.m_data的内容为“hello”,b.m_data的内容为“world”。
- 现将a赋给b,缺省赋值函数的“位拷贝”意味着执行b.m_data = a.m_data。这将造成三个错误:一是b.m_data原有的内存没被释放,造成内存泄露;二是b.m_data和a.m_data指向同一块内存,a或b任何一方变动都会影响另一方;三是在对象被析构时,m_data被释放了两次。
- ///
- 2008-8-29
- 关于表类的Query的参数问题
- j = t_table.Query(" balance < 2");
- 找到问题所在了,这里的参数表示查找的条件,但是不用加where这个词
- /
- Oracle数据库的使用
- 选择 运行SQL命令行
- SQL> conn/as sysdba
- 接下来就可以进行操作了
- 注意,语句结束要加上分号;
- SQL>
- SQL> create table person(name char(20), age int);
- 表已创建。
- SQL> insert into person values('Mary', 17);
- 已创建 1 行。
- SQL> select * from person;
- NAME AGE
- ---------------------------------------- ----------
- Mary 17
- SQL> save all
- 已创建 file all.sql
- SQL> shutdown abort
- ORACLE 例程已经关闭。
- /
- 2008-9-1
- D:/time>
- D:/time>cd D:/time/Mo/Debug
- D:/time/Mo/Debug>d
- /cygdrive/d/time/Mo/Debug/:
- rwx------ sun:None 168K Aug 04 02:40 Mo.exe
- rwx------ sun:None 190K Aug 04 02:40 Mo.ilk
- rwx------ sun:None 199K Aug 04 02:01 Mo.pch
- rwx------ sun:None 409K Aug 04 02:40 Mo.pdb
- rwx------ sun:None 168K Aug 04 02:40 myadd.exe
- rwx------ sun:None 33K Aug 04 02:46 vc60.idb
- rwx------ sun:None 44K Aug 04 02:46 vc60.pdb
- 7 regular files in directory, with a total size of 1.2M.
- D:/time/Mo/Debug>d 输入d,读取内容
- cd 进入路径
- cd.. 退回上一层目录
- ///
- 2008-9-2
- If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.
- ==========================================
- struct{
- int i;
- int a[];
- }a;
- sizeof(a);不包含最后数组的大小
- struct A{
- int id;
- char name[];
- };
- char *name = "csdn";
- A *a = (A *)malloc(sizeof(A) + sizeof(name));
- a->id = 23;
- strcpy(a->name, name);
- ///
- itoa()
- itoa itoa
- 功 能: 把一整数转换为字符串
- 用 法: char *itoa(int value, char *string, int radix);
- 详细解释:itoa是英文integer to string a(将整形数转化为一个字符串,并将值保存在a中)
- 的缩写.其中value为要转化的整数, radix是基数的意思,即先将value转化为几进制的数,之后在保存在a 中.
- 作用:实现数制之间的转化
- 比较:ltoa,其中l是long integer(长整形数)
- 备注:该函数的头文件是"stdlib.h"
- atoi()
- //
- 9-3
- /*
- int main(int argc, char** argv)
- {
- int a = atoi("2.1.");
- cout << a;
- // int b = atoi(argv[1] );
- // int c = atoi(argv[2] );
- // cout << b + c << endl;
- return 0;
- }
- */
- /
- 9-4
- 读文件
- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- void main()
- {
- // char a[1000];int i = 0;
- char filename1[256], filename2[256];
- cout<<"输入源文件名:"; / 如 D:/from.txt
- cin>>filename1;
- cout<<"输入目的文件名";
- cin>>filename2;
- ifstream infile(filename1);
- ofstream outfile(filename2);
- if (!infile)
- {
- cout<<"不能打开输入文件:"<<filename1<<'/n';
- exit(1);
- }
- if (!outfile)
- {
- cout<<"不能打开目的文件: "<<filename2<<'/n';
- exit(2);
- }
- char ch;
- while (infile.get(ch))
- {
- // a[i++] = ch;
- // ch = ch^*'1';
- // ch = ch - 1;
- outfile.put(ch);
- }
- // a[i] = '/0';
- infile.close();
- outfile.close();
- // cout<<a;
- }
- */
- ///
- 9-5
- sprintf中有分行时,不能加上逗号,
- sprintf(buf, "%s",
- "abc"
- "/nvv"
- "aaa");
- //
- 9-6
- 指针应该指向实在的空间
- int* p = new int a;
- 结构体的指针也一样
- 应用中,指针要指向实际存在的结点
- /
- 9-7
- class A
- {
- public:
- int value;
- A()
- {
- value = 0;
- }
- protected:
- private:
- };
- void add(A a)
- {
- a.value++;
- }
- void main()
- {
- A a;
- cout << a.value << endl;
- add(a);
- cout << a.value << endl;
- }
- 结果是
- 0
- 0
- 函数参数的问题,对于类同样如此
- //
- void insert(char *dtr, char *str, int locat)
- {
- dtr+=locat;
- locat=0;
- while(*str)
- {
- while(*str)
- {
- *dtr^=*str;
- *str^=*dtr;
- *dtr^=*str;//这里进行交换
- str++;
- dtr++;
- locat++;
- }
- str-=locat;
- locat=0;
- }
- }
- int main()
- {
- char str1[100]="1234567890"; //测试一下
- char str2[]="abcdefg";
- insert(str1,str2,10);
- puts(str1);
- return 0;
- }
- /
- 9-8
- int t_Update(T_ACCT_BALANCE table, int ACCT_BALANCE_ID, int BALANCE)
- {
- T_ACCT_BALANCE tableUpdate;
- // tableUpdate.SetConn(DefaultConnect);
- // tableUpdate.SetBatSize(1);
- // tableUpdate.SetTableName("ACCT_BALANCE_A");
- tableUpdate = table;
- tableUpdate.ACCT_BALANCE_ID = ACCT_BALANCE_ID;
- tableUpdate.BALANCE = BALANCE;
- long j = tableUpdate.Update();
- if (j > 0)
- {
- DefaultConnect.Commit();
- }
- else
- {
- j = -1;
- DefaultConnect.Rollback();
- cout << DefaultConnect.GetMsg();
- }
- return j;
- }
- 这样也是可以的
- 但是这个注释后却有问题,一个疑问
- int t_Update(T_ACCT_BALANCE table, int ACCT_BALANCE_ID, int BALANCE)
- {
- // table.SetConn(DefaultConnect);
- // table.SetBatSize(1);
- // table.SetTableName("ACCT_BALANCE_A");
- table.ACCT_BALANCE_ID = ACCT_BALANCE_ID;
- table.BALANCE = BALANCE;
- long j = table.Update();
- if (j > 0)
- {
- DefaultConnect.Commit();
- }
- else
- {
- j = -1;
- DefaultConnect.Rollback();
- cout << DefaultConnect.GetMsg();
- }
- return j;
- }
- 可能是形参修改了但是实参并没有改变
- long j = Login("oracle");//(DB_CONF_HEAD);
- if (j <= 0)
- {
- g_cLogFile.Error(0, "登录公用数据库失败,失败原因:[%s]!", GetMsg());
- return -1;
- }
- else
- {
- cout << "登录成功/n";
- }
- T_ACCT_BALANCE t_table;
- t_table.SetConn(DefaultConnect);
- t_table.SetBatSize(1);
- t_table.SetTableName("ACCT_BALANCE_A");
- j = t_table.Query(" balance < 2 and init_balance > 100");
- // j = t_table.Query();
- // t_table.Print();
- if (j <= 0)
- {
- return 1;
- }
- int i = 0;
- do {
- cout << t_table.BALANCE.Float() << " ";
- cout << t_table.OPT_DATE.Char() << endl;
- i++;
- } while(t_table.Next() > 0 && i < 10);//
- t_table.ACCT_BALANCE_ID = 1;
- t_table.PAYMENT_ID = 9;
- t_table.ACCT_ID = 1;
- t_table.SPE_PAYMENT_ID = 1;
- t_table.BALANCE_TYPE_ID = 9;
- t_table.INIT_BALANCE = 0;
- t_table.BALANCE = 1;
- t_table.EFF_DATE = "20080703203504";
- t_table.EXP_DATE = "20080703203505";
- t_table.OPT_DATE = "20080703203506";
- t_table.REGION_ID = "A";
- t_table.STAFF_ID = 0;
- t_table.OPT_CODE = "A";
- t_table.PAYED_PATH = "0";
- T_ACCT_BALANCE b_table, c_table;
- // t_Insert(t_table, 6, 1);
- t_Update(t_table, 3, -1);
- t_Delete(t_table, 6);
- cout << t_table.BALANCE.Float() << endl;
- Logout();
- DefaultConnect.Commit();
- DefaultConnect.Rollback();
- //
- 9-9
- 十叉树的查找操作
- inline short Lookup( const char* p_index, TYPE*& pout )
- {
- T_NODE10 *m_pcursor = m_proot;
- short j = 1 ;
- char c = '/0' ;
- for( unsigned i = 0 ; i < strlen( p_index ) && j == 1 ; i ++ )
- {
- c = p_index[ i ] ;
- if( m_pcursor -> p_pointer[ c - '0' ] != NULL )
- {
- m_pcursor = m_pcursor -> p_pointer[ c - '0' ] ;
- }
- else
- {//已经到结尾了
- j = 0 ;
- }
- } ;
- if( j > 0 )
- {
- if( m_pcursor -> p_data )
- {
- pout = m_pcursor -> p_data ;
- }
- else
- {
- pout = NULL ;
- j = 0 ;
- //Modify By June 20080725 出现此类情况可能正常,告警
- g_cLogFile.Warn(0, "节点值为空, INDEX=[%s]!", p_index ) ;
- }
- }
- else
- {
- pout = NULL ;
- }
- return j ;
- };
- //
- /**
- * 据电话号码查找出对应的区号,可用于电话号码区号的查询相类似的查询
- * Add by Rose 2002-03-26
- *
- * @param p_index 号码
- * @param pout 查询出的区号
- * @return >0-查询成功 <=0-查询失败
- */
- inline short AreaLookup( const char* p_index, TYPE*& pout )
- {
- T_NODE10 *m_pcursor = m_proot;
- short j = 0;
- char c = '/0';
- pout = NULL;
- for(unsigned i = 0; i < strlen(p_index); i++)
- {
- c = p_index[i];
- if(m_pcursor->p_pointer[c-'0'] != NULL)
- {
- m_pcursor = m_pcursor->p_pointer[c - '0'];
- if(m_pcursor->p_data)
- {
- j = 1;
- pout = m_pcursor->p_data;//这里进行了设置,但是并没有跳出循环,为超长的索引作准备,超长时,取前面的片段
- }
- }
- else
- {
- if(m_pcursor->p_data)
- {
- pout = m_pcursor->p_data;
- j = 1;
- }
- break;
- }
- }
- return j ;
- };
- 9-10
- 在使用自动生成工具时,要输入大写的名称,产生的文件在当前目录bin中
- cvs的使用,Update,可以先删除,再Update
- 9-11
- commit 将所有悬挂的修改保存
- RollBack 丢弃所有悬挂的修改
- 9-12
- 对生成的表进行测试
- /* long j = Login("oracle");//(DB_CONF_HEAD);
- if (j <= 0)
- {
- g_cLogFile.Error(0, "登录公用数据库失败,失败原因:[%s]!", GetMsg());
- return -1;
- }
- else
- {
- cout << "登录成功/n";
- }
- T_STUDENT t_table;
- t_table.SetConn(DefaultConnect);
- t_table.SetBatSize(1);
- t_table.SetTableName("STUDENT");
- j = t_table.Query();
- // t_table.Print();
- if (j <= 0)
- {
- return 1;
- }
- int i = 0;
- do {
- cout << t_table.SNAME.Char() << endl;
- i++;
- } while(t_table.Next() > 0 && i < 10);//
- */
- /**
- * 向数据管理类中添加索引
- * @param ind 索引对象
- */
- void AddIndex(INDEX_BASE* ind)
- {
- ind_list.AddTail(ind);
- ind->SetDbManager(this);
- }
- /
- 加载的文件更新要及时,可能里面的错误没有更新
- 迈出了第一步
- long j = Login("oracle");
- if (j <= 0)
- {
- g_cLogFile.Error(0, "登录公用数据库失败,失败原因:[%s]!", GetMsg());
- return 1;
- }
- T_STUDENT t_student;
- IND_STUDENT_BY_SNO ind_student_by_sno;
- DB_STUDENT db_student;
- db_student.AddIndex(&ind_student_by_sno);
- db_student.SetTableName("STUDENT");
- db_student.SetBatchSize(1);
- db_student.LoadByTable(DefaultConnect, "");
- STRU_STUDENT* stru_student;// = new STRU_STUDENT;
- // if (stru_student == NULL)
- // {
- // cout << "NULL/n";
- // return 1;
- // }
- ind_student_by_sno.Query("001", stru_student);
- cout << stru_student->SNAME << endl;
- db_student.Remove();
- ind_student_by_sno.RemoveAll();
- long j = Login("oracle");
- if (j <= 0)
- {
- g_cLogFile.Error(0, "登录公用数据库失败,失败原因:[%s]!", GetMsg());
- return 1;
- }
- T_STUDENT t_student;
- IND_STUDENT_BY_SNO ind_student_by_sno;
- DB_STUDENT db_student;
- db_student.AddIndex(&ind_student_by_sno);
- db_student.SetTableName("STUDENT");
- db_student.SetBatchSize(1);
- db_student.LoadByTable(DefaultConnect, "");
- STRU_STUDENT* stru_student;// = new STRU_STUDENT;
- ind_student_by_sno.Query("001", stru_student);
- if (stru_student == NULL)
- {
- cout << "NULL/n";
- return 1;
- }
- cout << stru_student->SNAME << endl;
- //实现数据更新
- strcpy(stru_student->SNAME, "Jam");
- db_student.Update(stru_student);
- //
- db_student.Save(DefaultConnect);
- // 输出内存中的内容
- T_LIST <STRU_STUDENT> *List_student = db_student.GetList();
- // STRU_STUDENT* p = db_student.GetList()->GetHeadPosition()->pItem;
- // = List_student->m_pHead->pItem;
- List_student->GoHead();
- while (List_student->Current() != NULL)
- {
- cout << List_student->Current()->SNAME << endl;
- List_student->Next();
- }
- cout << db_student.GetNodeNumber() << endl;
- //向内存中添加结点
- STRU_STUDENT* stru_stu = new STRU_STUDENT;
- strcpy(stru_stu->SNO, "003");
- strcpy(stru_stu->SNAME, "Google");
- db_student.Insert(stru_stu);
- db_student.Remove();
- ind_student_by_sno.RemoveAll();