gogl的专栏

佛曰:一枯一荣,皆有定数。圣经上说:欠著我的,我会记下。梁朝伟说:出来混,总归是要还的。主席说:哪里有压迫,哪里就有反抗。小平同志说:贫穷不是社会主义。

原创  c语言学习笔记 收藏

c语言学习笔记
2005.8.27
1.变量类型
 int
 char
 float
 double
 void
2.修饰符
 signed
 unsigned
 long
 short
3.十六进制表示方法
 0x001,0x002
4.转义字符
 \\,\',\"
 \r  回车
 \n  回车换行
 \f  换页
 \t  table 
 \v  垂直制表
 \b  退格
 \a  响铃
5.格式输出转换说明符
 o  octal
 d  decimal
 x  hex
 f  float
 c  char
 s  string
6.标识符和关键字
 auto,break,case,char,const,continue,default,do
 double,else,enum,extern,float,for,goto,if,int
 long,register,return,short,signed,sizeof,static,struct
 switch,typedef,union,unsigned,volatile,while 
7.变量的存储属性
 auto
 register
 static
 extern
                            2005.8.27 雨 15:16
8.输入输出
 printf(),scanf(),puts(),gets(),
 getch(int a),putch(int a),
 注意,用vc6的编译器,输入时,要规定char数组长度
9.多维数组 
 例如:
 char week[7][9]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
10.算术运算符
 +,-,*,/,%,
11.条件运算
 a1?a2:a3
12.逻辑运算
 &&,||,!
13.位操作
 1.二进制位操作
 ~,&,|,^
 2.整型数的位操作
 ~,&,|,^,<<,>>
14.数据类型的转换
 a.赋值符两侧的数据类型不一致,结果同左边变量的类型
 b.算术符两侧的数据类型不一致,自动转到精度高的类型
 c.强制转换
  (char)x
  (int)x
  (float)x
  (double)x
15.运算顺序和结合性
 用括号解决
16.控制语句
 a.
  if(){
  }
  else if(){
  }
  ...
  else{
  }
 b.
 switch(){
  case 值1:
   语句1;
   break;
  case 值2:
   语句2
   break;
  ...
  default:
   语句n;
   break;
 } 
 c.
 while(){
 }
 d.
 do{
 }
 while();
 e.
 int i;
 for(i=0;i<10;i++){
 }
17.转移语句
 break,continue,都是配合while,和for,switch使用
 转移语与为goto
 void testGoto(){
 int i=0;
 goto abc;
 for(i=0;i<10;i++){
  printf("a");
 }
 abc:printf("\nhere is abc:\n");
}
2005.8.28
18.宏定义#define
example:
 #define PI 3.1415926
 #define SQR(x,y) x*y
19.函数
 主要是在使用前注意要声明;
20.指针
 指针类型 int,char,float,double,
 指针的所对应的内存值得引用 *pointer
 注意多级指针的使用
 example:
 void testPP(){
 int i,j;
 int a[3][4]={{11,12,13,14},{21,22,23,24},{31,32,33,34}};
 int *ap[3];
 int **app;
 ap[0]=a[0];
 ap[1]=a[1];
 ap[2]=a[2];
 app=ap;
 for(i=0;i<3;i++){
  for(j=0;j<4;j++){
   printf("%d,",*( *(app+i)+j)  );
  }
  printf("\n");
 }
}
21.用地址做实际参数
22.利用地址量返回结果
23.参数不定的函数
 int printf(const char*,...);
24.利用全局变量传递参数
25.将数组传递到函数中
 example:
  void test_print2(){
 void print2(char *);
 char str[10]={"abc"};
 print2(str);
}
void print2(char *str){
 while(*str){
  printf("str[%d]=%c\n",str,*str);
  str++;
 }
 printf("str[%d]=%d\n",str,*str);
 printf("str[%d]=%d\n",str,*str);
}
26.将字符串传递到函数中
27.多维数组传递到函数中
28.指针型函数
 int add(int x,int y);//int型函数
 void pnt();//void型函数;
 如果返回的是指针,那么就称为指针型函数
29.递归函数
 example:
  double recursion(double x,int y){
   if(y==0){
    return (1+x);
   }
   else{
    y--;
    return (1+x)*recursion(x,y);
   }
  }
30.函数指针
 声明指针函数的格式:
 存储类型 数据类型 (*函数指针名称)([参数]);
 example:
  void test_fnp(){
   char press;
   void fn1(char);
   void fn2(char);
   void fnp(char,void(*)(char));
   printf("press keyboard c will execute fn1\npress other key  will execute fn2\n");
   press=getch();
   if(press=='c'){
    fnp(press,fn1);
   }
   else{
    fnp(press,fn2);
   }
  }
  void fn1(char q){
   printf("this is in fn1 q=%c\n",q);
  }
  void fn2(char q){
   printf("this is in fn2 q=%c\n",q);
  }
  void fnp(char q,void (*fnx)(char)){
   (*fnx)(q);
  }
31.库函数
 常用库函数:
 #include<math.h>
 sin(); cos(); tan(); sinh();求双曲正弦 tanh();
 asin(); acos(); exp();求指数 log(); log10(); 
 pow(x,y);求x的y次方  ;sqrt(x);求平方根 abs();
 #include<string.h>
 strcat(str1,str2); 
 strcpy(str1,str2);
 strlen(str);
 strcmp(str1,str2);
32.输入输出函数的格式
 转换说明符6个
  % + - m.n l d
  +-:如果取-号,表示输入的内容紧靠左端
  0: 表示遇到空格填0
  m.n:制定字符长度,例如,6.2,表示字长为6,其中2位是小数;
  l、d:l和d通常联合表示某种含义。d代表:d,x,f,c,s,e,u,o,g
     g表示从e和f中选一种形式显示,选简单的
     l表示(long)
     e,f,g,表示采用float
     le,lf,lg, 表示采用double
 33.文件的打开
  fopen("文件名","模式");   
  模式:r(read),w(write),rw(read & write),a(append);
 #include<stdlib.h>
 void test_fopen(){
 char c;
 FILE *fp;
 if((fp=fopen("a.txt","r"))==NULL){
  printf("can't open file\n");
  exit(0);
 }
 while((c=getc(fp))!=EOF){
  putc(c,stdout);
 }
 fclose(fp);
 getch();
}
34.getc()&putc()
 example:
  void test_putc(){
 char c=65;
 putc(c,stdout);
 while(1){
  c=getc(stdin);
  printf("%d",c);
 }
}
35.命令行参数
 main的参数
  #include<stdlib.h>
  void main(int argc,char *argv[]){
   char c;
   FILE * fp;
   printf("argv[0]=%s\n",argv[0]);
   printf("argv[1]=%s\n",argv[1]);
   if((fp=fopen(argv[1],"r"))==NULL){
    printf("can't open %s\n",argv[1]);
    exit(0);
   }
   else{
    while((c=getc(fp))!=EOF){
     putc(c,stdout);
    }
   }
   fclose(fp);
   printf("int argc=%d",argc); 
  }
36.文件操作
 文件复制
 #include<stdlib.h>
 主要有,fopen(),fclose();结构FILE;
 读取: getc(),putc()
    fgets(),fputs();
    fscanf(),fprintf();  
 EXAMPLE:
 void copyfile(){
 char sourceFile[]={"e:\\a.txt"};
 char destFile[]={"e:\\b.txt"};
 char ch;
 FILE *fs,*fd;
 fs=fopen(sourceFile,"r");
 fd=fopen(destFile,"a");
 if(fd==NULL){
  printf("%s can't open!\n",destFile);
 }
 else{
  printf("%s can open!\n",destFile);
 }
 if(fs==NULL){
  printf("%s can't open!\n",sourceFile);
  exit(0);
 }
 else{
  //fd=fopen(destFile,"w");
  while((ch=getc(fs))!=EOF){
   putc(ch,stdout);
   putc(ch,fd);
  }
 }
 fclose(fs);
}
37.结构和联合
 1.结构struct
  struct person{
   int no;
   char name[30];
   char sex[7];
   int age;
   char add[80];
   char tel[20];
   float salary;
  };
   struct person{
   int no;
   char name[30];
   char sex[7];
   int age;
   char add[80];
   char tel[20];
   float salary;
  } zhang,li;
  定义时可以省略结构名字
   struct{
   int no;
   char name[30];
   char sex[7];
   int age;
   char add[80];
   char tel[20];
   float salary;
  };zhang,li;
  2.结构成员的访问:
   zhang.name;  li.name;
   struct person zhang{1,"张三","male",66,"Shanghai","88888888666",999.99};
  3.结构数组
   struct person worker[]={
    1,"ZhangSan","male",25,"shanghai","13162501778",1000.00,
    2."XiaoLan","female",23,"Beijing","13245625123",1250.00.
    
   }
   4.结构指针
   struct person *p;
   p=field;
   int no=p->no;
   5.将结构传递到函数中去
    一般将要传递的结构作为全局变量处理
   6.结构型函数
    #include<stdio.h>
    struct data{
     char item;
     int a;
     int b;
     float x;
     float y;
    };
    void main(){
     struct data fn(),ab;
     ab=fn();
     printf("%c  %d  %d  %f  %f  \n",ab.item,ab.a,ab.b,ab.x,ab.y);
    }
    struct data fn(){
     struct data abc={'A',11,12,1.1,1.2};
     return(abc);
    }
    7.结构指针型函数
    struct data *ab();
38.联合
 在某一时刻只能有一个数据成员存储在联合体中
 union data1{
  char a1;
  int b1;
  float x1;
  double y1;
 };
 union data1 abc1;
39.枚举(enumeration)
 enum city{bj,sh,tj,cq,gz,sy};
 enum city{bj=2,tj,cq,gz,sy};
40.类型定义(typedef)
 typedef int RenShu;
 typedef char *  STRING;
 typedef struct person{
  int no;
  char name[20];
  int age;
  char *add;
 }STUDENT;
 typedef struct person Student;
 2005.9.3
 rand()同余法
     在visual c中rand()函数能产生大小为多少的随机序列。
     对于同余法中的wichmann-hill算法
     即:
     x=(171*x)mod(30269)
     y=(172*y)mod(30307)
     z=(170*z)mod(30323)
     U=(x/303269)+(y/30307)+(z/30323)
     中x,y,z的初始值的选取有什么好办法?
41.条件编译
 //example 1:(#if 和 #else 的使用)
  #define ABC 0;
  #if ABC
   static char CC=5;
  #else
   static char CC=10;
  #endif
 
 //example 2:(#ifdef & #ifndef)
  #define ABC 0
  #ifdef ABC
   char str[]="ABC is defined!";
  #else
   char str[]="ABC is not defined!";
  #endif
 
 //example 3:(#undef 语句取消用#define 的定义)
 #define ABC 0
 #undef ABC
 #ifndef ABC
  char str[]="ABC is undefined!";
 #else
  char str[]="ABC is defined!";
 #endif
42.动态存储分配(malloc---memory allocate)
 注:声明的指针以后要分配内存空间,再赋值
 void *pv;
 int *pa;
 char *pc;
 pa=(int) pv;
 
 example:
  #include<stdio.h>
  #include<malloc.h>
  test_malloc(){
   int *pi;
   if(pi==NULL){
    printf("Insufiicient memory avilable\n");
   }
   else{
    printf("Memory space allocated for pi");
    pi=malloc(sizeof(int));
   }
  }
the end of chapter 2
c++
c++和c的主要差别
1.增加了输入输入操作
 int a;
 cout<<"aabc";
 cin>>a;
2.变量的定义比较宽松
 A:
  cout<<"abc";
  int a;
  cin>>a;
 B:
  for(int c=0;c<10;c++){
  cout<<c;
 }
3.必须使用函数原型
 为了重载方法
4.函数可以采用默认值
 void t2_1(){
  int t2(int a=222);
  int a=9;
  t2();
 }
 int t2(int a){
  printf("%d",a);
  return a;
 }
5.通过"引用"传递函数的参数
void t3(int& a){
 printf("[%x]=%d\n",&a,a);
 a=90;
 printf("[%x]=%d\n",&a,a);
}
void t3_1(){
 void t3(int& a);
 int b=88;
 printf("[%x]=%d\n",&b,b);
 t3(b);
 printf("[%x]=%d\n",&b,b);
}
6.内联函数
关键字 inline
例如:
 #include<iostream.h>
 inline void repla()
 void main(){
  repla();
 }
 inline void repla(){
  printf("This is a inline Test\n");
 }
7.函数的重载
void t5(){
 void t5_a(int);
 void t5_b(char []);
 //int a=10;
 t5_a(10);
 t5_b("hello!! ");
}
void t5_a(int a){
 printf("function t5_a(int a) int a=%d\n",a);
}
void t5_b(char b[]){
 printf("function t5_b(char b[]) b=%s\n",b);
}
8.作用域运算符::
 通过::可以访问被隐藏的变量
 i=9000;//it is gloable
 void t6(){
 int i=900;
 for(int j=0;j<3;j++){
  int i=0;
  cout<<i++<<endl;
  cout<<"gloable i="<<::i<<endl;
 }
}
9.无参数函数的说明
 int fn(void);
10.new & delete 操作
 对应于c的malloc()和free()
11.c++中类的定义 
 可以分两个文件
 point.h   类的声明,有点类似java中的抽象方法
 point.cpp  对类的中抽象方法的实现(implement)
 应用是,只要#include "point.h"
 
12.构造函数
 放在public:中,没有返回值 连void返回值都没有
13.建立动态对象
 Cpoint *A = new Cpoint();
14.类的继承
 单一继承
 class Cpoint{
 private: 
  int x,y;
 public:
  ~Cpoint(void);
  Cpoint(void);
  Cpoint(int x0,int y0);
  void point(int x0,int y0);
  int getX(void);
  int getY(void);
 };
 
 class Cpoint1: public Cpoint{//如果是private 的话就不能访问Cpoint 的x,y了
 public:
  int pls(int,int);
  int getX();
 };
 
 多重继承
 ...
 class Cpoint1: public Cpoint,private Cpoint2{//如果是private 的话就不能访问Cpoint 的x,y了
 public:
  int pls(int,int);
  int getX();
 }; 
15.友元函数(friend)
 在类的内部的成员函数能够访问private 的成员变量
 
 file:point.h
 class point{
 private:
  int x,y;
 public:
  friend int plus(point& u);
    
 }
 file:point.cpp
 #include "point.h"
 int plus(point& u){
  return (u.x+u.y);
 }
16.多态性和虚函数
多态性:c++中允许同名函数,因此相同的函数名调用时可能得到不同的函数,而得到不同的结果.(函数的重载)
容易混淆:
 test.h
 class CA{
 public:
  void show();
 }
 class CB:public CA{
 public:
  void show();
 }
 
 test.cpp
 #include<iostream.h>
 #include "test.h"
 void CA::show(){
  cout<<"this is class CA"<<endl;
 }
 void CB:show(){
  cout<<"this is class CB"<<endl;
 }
 start.cpp
 #include "test.h"
 viod main(){
  CB s;
  CA *ca = &s;
  ca->show();//结果是:this is class CA;java 中也有这种情况
 }
 用虚函数解决此问题(virtual)
 test.h
 class CA{
 public:
  virtual void show();
 }
 class CB:public CA{
 public:
  void show();
 }
 
 test.cpp
 #include<iostream.h>
 #include "test.h"
 void CA::show(){
  cout<<"this is class CA"<<endl;
 }
 void CB:show(){
  cout<<"this is class CB"<<endl;
 }
 start.cpp
 #include "test.h"
 viod main(){
  CB s;
  CA *ca = &s;
  ca->show();//结果是:this is class CA;java 中也有这种情况
 } 
17.头文件和原文件
 Head Files  头文件,其中包含声明(declaration)。头文件又称为类的接口(interface of the class)。
 Source File 源文件,其中包含对类的成员函数的定义。原文件又称为类的实现(implementation of the class)。

发表于 @ 2006年01月14日 17:05:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:Udp内网穿透终于测试成功 | 新一篇:Coze 2005 简介

  • 发表评论
  • 评论内容:
  •  
Copyright © gogl
Powered by CSDN Blog