博文

C++个人回顾小结: 指针编写的个人实例(2006-11-23 21:26:00)

摘要:#include<iostream.h>
main()
{
 int b[2][3]={1,2,3,4,5,6};
 int (*q)[3];
 q=b;
 cout<<(*q)[5]<<endl; 
 cout<<"hello\b\b\b\a\a\a\a\a\a\a\a\a\a\t\t\t\thow are you\0how do you do ok\b\b\b\0\\d"<<endl; }
......

阅读全文(2791) | 评论:0

C++个人回顾小结: 函数(一)个人实践小结(2006-11-23 21:25:00)

摘要:#include<iostream.h>
int add(int a, int b, int c);
typedef int array[4];
void swap1(int a, int b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;
}
void swap2(int *a, int *b)
{
 int temp;
 temp=*a;
 *a=*b;
 *b=temp;
}
void arrayfun1(int b[],int n)
{
 b[n-1]=8;
 cout<<b[n-1]<<endl;
} void arrayfun2(int *p,int n)
{
 for(int i=0;i<n-2;i++)
 {
  p++;
 }
 *p=8;
 cout<<*p<<endl;
} /*void arrayfun3(int &b[],int n)
{
 b[n-1]=8;
 cout<<b[n-1]<<endl;
} */ void arrayfun4(array &b,int n)
{
 b[n-1]=8;
 cout<<b[n-1]<<endl;
} void printArrayA(int b[], int n)
{
 for(int i=0;i<4;i++)
 {
  cout<<b[i]<<" ";
 }
 cout<<endl;
}  
void main()
{
 ......

阅读全文(4112) | 评论:0

C++个人回顾小结: 函数(一)(2006-11-23 20:50:00)

摘要:今天复习了类型定义和函数。 先所类型定义: 举个例子 typedef double wages,bonus; 以后可以用wages,bonus来定义周薪和月薪了 比如: wages weekly; bonus monthly; 其他的定义方法: 例如: typedef char * string; typedef string months[3]; months spring = {"February","March","April"}; 关于函数: 函数的有定义和说明的两件工作,如果定义在先,调用在后,调用前可以不用说明;如果定义在后,调用在先,就需要说明: 例如: #include<iostream.h> void fun1(); void main() {      cout<<"This is fun1"<<endl;     fun1(); } void fun1() {     cout<<"fun1 is executed."<<endl; } 或者这样 #include<iostream.h> void fun1() {     cout<<"fun1 is executed."<<endl; } void main() {      cout<<"This is fun1"<<endl;     fun1(); } 函数的传值调用,函数的传址调用,函数的引用调用 分别如下: void swap(int x, int y) {     int temp;     temp=x;     x=y;     y=temp; }   void swap(int *x, int* y) {  &nbs......

阅读全文(3912) | 评论:0

C++个人回顾小结:指针(2006-11-22 21:16:00)

摘要:最近在复习C++,想把每次复习的东西挑重点的写在我的BLOG上面,以来最为个人复习和总结。二来把这些东西和大家共同分享一下。 指针 int a(5); int *p=&a; &a代表变量a的地址。指针p的赋值必须指向某个变量的地址。   int b[10]; int *p=b; b相当于b数组第一个元素的地址&b[0] 所以上面可以这样直接赋值。 对于二维数组b[2][4] 可以这样通过指针调用元素:*(*(b+i)+j;  *(b[i]+j); *(b+i)[j]; *(&b[0][0]+4*i+j); 由此可以拓展到三维数组 int (*p)[3]; 表示p所指向的是3个元素的一维数组 int b[2][3]={1,2,3,4,5,6};
 int (*q)[3];
 q=b; 可以通过
cout<<(*q)[i]<<endl;
输出数组b当中的第i个元素 函数的指针: double sin(double x); double(*pf)(); pf=sin; 表明pf是一个指向函数的指针,它所指向的函数是sin,这里用sin给pf赋值,实际上是让pf指针指向sin函数在内存中的入口地址。 另外指针比较具体的理解为 &表示取地址 *表示取内容 &a表示取变量a的地址 *p=&a就表示把a的地址放到指针p的内存单元的内容里面去。 当然,大家记住了在比如输出cout<<*p<<endl;当中,输出的可是*p的所指向内容而不是变量a的地址了哦。  ......

阅读全文(4082) | 评论:1

一个静态初始化的例子(2006-05-29 21:28:00)

摘要:欲编写一个"总统"类,其构造器中可以包含一系列的创建"总统"的步骤: class President {     public President(){         //...     } } 这样做的一个问题是"总统"只能有一个,但其他程序使用这个类时却可以用new President()创建多个不同的"总统"。为了防止多个“总统”被创建出来,可以将President的构造器由public类型改为private类型,根据前面的访问控制规则,这样该构造器就只能在类President内部使用,而在类之外就不可使用了。 class President {     private President(){         //...     } } 这样,在其他类中就无法用President创建对象了,但为了仍能使用President对象,可以在类President中通过显示初始化创建好,如下 class President{     String name;     static President x=new President();     private President(){         name="xxx";     } class Test{     public static void main (String args[ ]){         //President x= new President();         President p=President.x;  &......

阅读全文(3778) | 评论:0

模拟多线程程序(C++实现)(2005-10-14 23:52:00)

摘要:#include<iostream.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
  class multithread   //多线程类
{
      private:
              char s[1000];       //某线程工作状态字符型记录数组
              int sum;        //某个对象存储状态的字符串型数组的个数
             
      public:
       multithread(){xpiot=0;for(int i=0;i<1000;i++)s[i]=' ';}; 
              ~multithread(){};
              void randcpu();          &nb......

阅读全文(5181) | 评论:3

重新认识struct(2)(2005-08-15 11:43:00)

摘要:5.函数对结构的使用
eg.:
/* ptinrect: return 1 if p in r, 0 if not */
int ptinrect(struct point p, struct rect r)
{
    return p.x >= r.pt1.x && p.x < r.pt2.x && p.y >= r.pt1.y && p.y < r.pt2.y;
}
以上种种显示,结构体和一般的变量没有本质的区别,其不同仅仅是在定义上.
由5中函数对rectangle的判断,可以分析出在处理长方形问题上的方法,一个长方形可以用其对角的两个点表示,并且可由这两个点解决很多实际的问题,比如上面的判断一个点是否在长方形内部.
在这里我对几个概念做一下总结性描述:
structure:结构体,通常一个结构体通过一个结构体标记表示(structure tag)
structure varialbe:结构体变量.像一般变量一样,不同的是其是由结构体定义(申明).
虽然结构体和类在c++中没有本质的区别,但是我们依然不混用他们,其原因就是他们内在含义的不同,而这几个概念就是他们含义的一种体现.

6.结构体使用技巧,传递指针而非结构体变量本身.
If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure.(K&R 《the c programming language》)
这里的高效,体现在传递指针不需要为变元的副本拷贝提供过大的开销,这样的问题特别出现在巨大的结构体和类上面,对于一个小的结构,虽然从本质上来说使用指针来提高效率作用颇小,但是却因为一些微妙的原因值得我们去做.关于函数参数的传递问题,在函数参数的按引用传递和按值传递上表现的很明显,相关内容建议参考<<c++ how to program>>......

阅读全文(431) | 评论:0

重新认识struct(1)(2005-08-15 11:40:00)

摘要:建议结合使用《the c programming language》来阅读本文,相关书籍在网上均有电子版.

在描述struct前,我们先来了解一下what is a type?
A type is a concrete representation of a concept.
For example,the c++ built-in type float with its operations +,-,*,etc.,provides a concrete approximation of the mathematical concept of a real number. (摘于《the c++ programming language》chapter 10)
由此,我们可以明确的知道,a type is a concept.这个type包含的不仅仅是一个像int,double这样的一个符号,更多的是在表示一种concept。一个类型的内涵,还包括其运算方式,其使用范围,其用法等等一系列的问题。

现在进行分析,struct是什么:
A structure is a collection of one or more variables, possibly of different types.(K&R 《the c programming language》)
由此可以明确的是,struct组合的是一组concept并把它表示为一个concept.由于这种组合的特殊性,虽然方便但在作为一个整体使用,必然要在一定程度上限定其应用.所以有下:
structures may be copied and assigned to, passed to functions,and returned by functions.(K&R 《the c programming language》)
描述struct的使用方式。
以上具体的描述了struct,为之后理解struct有很重要的意义。

struct实际应用的意义:
通常情况下,我们会用一个struct来定义一个树,定义一个线形表,而不会使用一个类来表示,因为,类是具体对象的抽象,也就......

阅读全文(3760) | 评论:0