C语言基础_上课代码
2012-12-06 12:10
239 查看
2010年 ================================================================================= 0325 1、类和函数名以大写字母开头,按照匈牙利命名法命名 2、变量名以小写字母开头,按照匈牙利命名法命名 3、有从属关系的语句,遵守缩进规则,即一个tab,占用4格空格位 4、花括号本身不要缩进 5、左花括号右边置空,不要再跟语句 6、右花括号单独列在一行 Ctrl+Tab 子窗口之间进行切换 Alt+Tab 在应用程序之间进行切换 Ctrl+Home 定位到文档头 Ctrl+End 定位到文档尾 Home 定位到行首 End 定位到行尾 PageUp 向上翻页 PageDown 向下翻页 Ctrl+g num 定位到第num行 Ctrl+箭头 单词间跳动 Ctrl+Shift+箭头 选中一个单词 Shift+Home 选中到行首 Shift+End 选中到行尾 ??????????????? 存盘 Ctrl+f 查找 Ctrl+h 替换 Tab 缩进 Shift+Tab 向左缩进 ================================================================================= 0326 a //理解编译链接原理 #include "stdafx.h" void MyPrintf(char *a); int main(int argc, char* argv[]) { MyPrintf("zhang"); return 0; } b // 03026b.cpp : Defines the entry point for the console application. //编译调试与重复包涵 #include "stdafx.h" #include "0306b.h" #include "0306b.h" #include "0306b.h" int main(int argc, char* argv[]) { //printf("Hello World!\n"); int num,x,y; int i=0; scanf("%d",&i); printf("%d",i); printf("输入两个数:"); scanf("%d,%d",&x,&y); num=add(x,y); printf("%d",num); return 0; } int add(int x,int y) { return x+y; } // 03026b.h int add(int x,int y); c #include <stdio.h> //格式化输出 #define HH 111 int main(int argv,char *args[]) { int i=123; float f=4.123f; double d=3.14; char c='a'; printf("%d\t",i); printf("%f\t",f); printf("%d",d); printf("%0.2f\t",f); printf("%8f\t",f); printf("%x\t",c); printf("%u\n",c); #ifdef __DEBUG__ printf("DEBUG is on\t"); #else printf("DEBUG is off\t"); #endif printf("%d\t",HH); printf("%s\t",__FILE__); // printf("%s\t",__cplusplus); return 0; } d // 03026d.cpp : Defines the entry point for the console application. //查看内存存储原理 #include "stdafx.h" int main(int argc, char* argv[]) { int i; char c; /* i=0x80; c=i; printf("%x\n",c); */ c=0x40; i=c; i=0x40; printf("%x\n",i); return 0; } //fun.c #include "stdio.h" int fun() { return 0; } //fun.h #ifndef _XX_ #define _XX_ int fun(); #endif ================================================================================= 0330 a // 0330a.cpp : Defines the entry point for the console application. //查看内存和彷汇编代码 #include "stdafx.h" int main(int argc, char* argv[]) { int i; int *pi; pi=&i; i=1; *pi=2; return 0; } b // 0330b.cpp : Defines the entry point for the console application. //指针变量强制存放整行变量 #include "stdafx.h" int main(int argc, char* argv[]) { int i=0xaabbccdd; int j; char *pch; pch=(char*)i; j=(int)pch; printf("j:%x",j); return 0; } c // 0330c.cpp : Defines the entry point for the console application. //整形指针变量指向字符变量 #include "stdafx.h" int main(int argc, char* argv[]) { char ch; int *pi; char*pch; pi=(int*)&ch; pch=(char*)pi; *pch='a'; return 0; } d // 0330d.cpp : Defines the entry point for the console application. //内存访问越界问题(整形指针变量访问字符变量) //系统不报错并不代表没有错误 #include "stdafx.h" int main(int argc, char* argv[]) { char ch; int *pi; pi=(int*)&ch; *pi='a';//整形变量的其它位被0填充 61 00 00 00 C0 a.... return 0; } f // 0330f.cpp : Defines the entry point for the console application. //void指针与特定内存指针的区别 // 可以指向任意类型变量 #include "stdafx.h" int main(int argc, char* argv[]) { char ch='a'; void *p=&ch; //*p='b';//不能表示内存单元,作用于中间结果 char *pch; pch=(char*)p; *pch='b'; return 0; } g // 0330g.cpp : Defines the entry point for the console application. //空指针NULL #include "stdafx.h" int main(int argc, char* argv[]) { int *pi; //*pi=3;//未初始化指针报错 int *pj=NULL; //*pj=5;//空指针不允许访问 int *pk=NULL; pk=(int*)0x23435588; //危险指定内存地址 *pk=123; return 0; } h // 0330h.cpp : Defines the entry point for the console application. //指针的传递 #include "stdafx.h" void Func(int *pi) { //pi=NULL; int j=7; pi=&j; } int main(int argc, char* argv[]) { int i; int *pi=&i; /*printf("%p\n",pi); Func(pi); printf("%p\n",pi);*/ printf("%p\n",pi); Func(pi); printf("%p\n",pi); return 0; } i // 0330i.cpp : Defines the entry point for the console application. //指针的移动 #include "stdafx.h" int main(int argc, char* argv[]) { int n; /* int *pi1=(int*)0x0012ff60; int *pi2=(int*)0x0012ff70; n=pi2-pi1; printf("%d",n); */ char *pi1=(char*)0x0012ff60; char*pi2=(char*)0x0012ff70; n=pi2-pi1; printf("%d",n); char *p1=(char*)32; char*p2=(char*)23; n=p2-p1; printf("%d",n); /* void*pi1=(void*)0x0012ff60; void *pi2=(void*)0x0012ff70; n=pi2-pi1; printf("%d",n); */ return 0; } o // 0330o.cpp : Defines the entry point for the console application. //数组编译时确定大小 #include "stdafx.h" int main(int argc, char* argv[]) { //int array[];不允许 const int iCount = 4; int *pi =(int*)&iCount; *pi=5; printf("%d",*pi); int array[iCount]={0};//编译时已经确定为4 array数组 return 0; } p // p.cpp : Defines the entry point for the console application. //数组转换和使用 #include "stdafx.h" void Func(int array[]) { //数组作为型参实际是当指针使用 int i=sizeof(array);//4 *array++=60; } int main(int argc, char* argv[]) { int array[4] = {0};//数组初始化为0 /* int *pi; pi = array; *(pi+2) = 2; pi[3] = 3;//指针当数组使用 *array = 9;//数组当指针使用 *(array+2) = 10;//移动 sizeof(array);//??? sizeof(pi);//?? *pi++=50; //*array++=60; 错误 */ Func(array); return 0; } x // 0330x.cpp : Defines the entry point for the console application. //实现aabbccdd--ddccbbaa倒序 #include "stdafx.h" /* int ByteReverse(int num)//自己倒序 { char *res=(char*)# char temp; temp=*res; *res=*(res+3); *(res+3)=temp; temp=*(res+1); *(res+1)=*(res+2); *(res+2)=temp; return num; } */ //借助中间变量 int ByteReverse(int num) { int ret; char *rsu=(char*)# char *set=(char*)&ret; for(int i=0;i<4;i++) //*(set+i)=*(rsu+3-i); set[i] = rsu[3-i];//字符数组当指针 return ret; } int main(int argc, char* argv[]) { int j,k; scanf("%x",&j); k=ByteReverse(j); printf("%x\n",k); return 0; } ================================================================================= 0329 a // 0329a.cpp : Defines the entry point for the console application. // #include "stdafx.h" int main(int argc, char* argv[]) { /*unsigned int i=1; int j=-5; //if(i&&j) if(i+j>0) printf("ok");//is ok else printf("no");*/ /*int i=0; if(i=1) printf("ok"); else printf("no");*/ /*int i=5; i|=1<<15;//32位第15位置位1 i&=~(1<<19);//32位19位置为0 printf("%x",i);*/ return 0; } b // 0329b.cpp : Defines the entry point for the console application. // #include "stdafx.h" int main(int argc, char* argv[]) { int sum=0; int i=2; for(;i<=100;i+=2) { sum+=i; } printf("%d\n",sum); while(i<=100) { sum+=i; i+=2; } printf("%d\n",sum); do{ sum+=i; i+=2; }while(i<=100); printf("%d\n",sum); return 0; } c // 03029c.cpp : Defines the entry point for the console application. //递归函数 #include "stdafx.h" int add(int i) { if(i<100) return i+add(i+2); else return i; } int add2(int i) { if(i>2) return i+add2(i-2); else return i; } int main(int argc, char* argv[]) { int sum=0; sum=add(2); printf("%d\n",sum); int sum2=0; sum2=add2(100); printf("%d\n",sum2); return 0; } d // 03029d.cpp : Defines the entry point for the console application. // #include "stdafx.h" int func(int i) { if(i<100) return i*i+func(i+2); else return i*i; } int func2(int i) { if(i>2) return i*i+func2(i-2); else return i*i; } int main(int argc, char* argv[]) { int sum=0; sum=func(2); printf("%d\n",sum); int sum2=0; sum2=func2(100); printf("%d\n",sum2); return 0; } e // 03029e.cpp : Defines the entry point for the console application. //可变参数传值 #include "stdafx.h" #include <stdarg.h> void Myprint(int i,...) { int j; char *str=NULL; va_list ls;//定义 va_start(ls,i);//初始化 j=va_arg(ls,int);//取值 j=va_arg(ls,int); j=va_arg(ls,int); str=va_arg(ls,char*); str=va_arg(ls,char*); va_end(ls);//结束 printf("%d",j); } /* void Myprint(char fmt,...) { int j; char *str=NULL; va_list ls; va_start(ls,fmt); while(fmt里面还有格式化字符) { if(格式化字符是%d) j=va_arg(ls,int); else if(格式化字符是%c) j=va_arg(ls,int); else if(格式化是%s) { str=va_arg(ls,char*); } } va_end(ls); }*/ int main(int argc, char* argv[]) { Myprint(1,2,3,"abc","hello"); return 0; } f // 0329f.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdarg.h> int Min(int i,...) { int j,arg,min; va_list ls;//定义 va_start(ls,i);//初始化 min=va_arg(ls,int); //printf("%d\n",min); for(j=1;j<i;j++) { arg=va_arg(ls,int); if(arg<min) min=arg; } va_end(ls); return min; } int main(int argc, char* argv[]) { int iMin; iMin=Min(5,33,45,16,60,9); printf("%d\n",iMin); return 0; } ================================================================================= 0331 a // 0331a.cpp : Defines the entry point for the console application. //4p #include "stdafx.h" #include <stdlib.h> void Func() { /* char *pch=(char *)malloc(1); int *pi= (int *)malloc(4); int *pj= (int *)malloc(40*1024*1024); */ char *pch1=NULL; free(pch1); //free(pch1);//不能重复释放 char *pch2; free(pch2);//不能释放站空间 call tack 查看 /*char ch; char *pch3=&ch; free(pch3); */ char *pch4=(char*)malloc(0); //char *pch4=(char*)malloc(size); free(pch4); char *pch=(char *)malloc(1); *pch='a'; *pch=0xa; printf("%x/n",*pch); free(pch); if(pch==NULL){ puts("free success"); }else{ puts("free error"); } } int main(int argc, char* argv[]) { Func(); return 0; } b // 0331b.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdlib.h> void Func() { int i,iSize=3; char *pch=(char*)malloc(iSize); for(i=0;i<iSize;i++) pch[i]='a'+i; //*pch++'a'+i;//不能释放 *(pch+0)='a'; *(pch+1)='b'; *(pch+2)='c'; pch[0]='a'; pch[1]='b'; pch[2]='c'; free(pch); } int main(int argc, char* argv[]) { Func(); return 0; } c // 0331c.cpp : Defines the entry point for the console application. //a7 #include "stdafx.h" #include <stdlib.h> #include <string.h> void Func() { const int COUNT=8; int i; char *pch=(char*)malloc(COUNT);//int...COUNT*4 char *pch2=(char*)malloc(COUNT); for(i=0;i<COUNT;i++) pch[i]='a'+i; memset(pch,0,COUNT);//...COUNT*4 初始化元素位0 memcpy(pch2,pch,COUNT);//把pch里的值拷贝到pch2 free(pch2); free(pch); } int main(int argc, char* argv[]) { Func(); return 0; } d // 0331d.cpp : Defines the entry point for the console application. //扩充分配大小 //pch,8查看全部数组(pch[0,pch[1]...]) #include "stdafx.h" #include <stdlib.h> void Func() { int i; char *pch=(char*)malloc(3); for(i=0;i<3;i++) pch[i]='a'+i; pch=(char*)realloc(pch,8);//扩充函数 for(i=3;i<8;i++) pch[i]='a'+i; free(pch); } int main(int argc, char* argv[]) { Func(); return 0; } e // 0331e.cpp : Defines the entry point for the console application. //函数外部分配,内部使用 #include "stdafx.h" #include <stdlib.h> void Func(char *pch,int iCount) { int i; for(i=0;i<iCount;i++) pch[i]='a'+i; } int main(int argc, char* argv[]) { char *pch=(char*)malloc(8); Func(pch,8); free(pch); return 0; } f // 0331f.cpp : Defines the entry point for the console application. //函数内部分配,外部使用 #include "stdafx.h" #include <stdlib.h> void Func(char *pch) { pch=(char*)malloc(8);//内存泄露 } int main(int argc, char* argv[]) { char *pch=NULL; int i; Func(pch); for(i=0;i<8;i++) pch[i]='a'+i;// 报错 free(pch); return 0; } g // 0331g.cpp : Defines the entry point for the console application. //函数分配空间,返回指针 #include "stdafx.h" #include <stdlib.h> char *Func() { char *pch=(char*)malloc(8); return pch; } int main(int argc, char* argv[]) { char *pch=NULL; int i; pch=Func(); for(i=0;i<8;i++) pch[i]='a'+i; free(pch); return 0; } j // 0331j.cpp : Defines the entry point for the console application. //二级指针 #include "stdafx.h" #include <stdlib.h> int main(int argc, char* argv[]) { /* char **ppch; ppch=(char**)malloc(4); *ppch=(char*)malloc(1); **ppch='a'; free(*ppch); free(ppch); */ char **ppch; char *array[4];//数组指针 ppch=array; //array[1]=(char*)malloc(1); //*array[1]='a'; //*(ppch+1)=(char*)malloc(1); ppch[1]=(char*)malloc(1); *ppch[1]='b'; free(*(ppch+1)); //free(ppch[1]); //free(ppch); //free(ppch+1); //free(*ppch[1]); return 0; } k #include "stdafx.h" #include <stdlib.h> int main(int argc, char* argv[]) { return 0; } l // 0331l.cpp : Defines the entry point for the console application. // #include "stdafx.h" void Func(char **ppch) { //@ *ppch=NULL;//错误试图访问不存在的值 // ppch=NULL; //ppch=NULL; *ppch=NULL; } int main(int argc, char* argv[]) { //@char **ch; // Func(ch); char *pch; Func(&pch); return 0; } m // 0331m.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdlib.h> #include <string.h> void Func(char **ppch) { *ppch=(char*)malloc(1);//pch=(char*)malloc(1); **ppch='a'; } int main(int argc, char* argv[]) { char *pch; Func(&pch); printf("%c\n",*pch); free(pch); return 0; } x // 0331X.cpp : Defines the entry point for the console application. //从键盘输入整数存放在字符数组里,打印出来(个数,数值) #include "stdafx.h" #include <stdlib.h> int main(int argc, char* argv[]) { int i,n; printf("输入整数个数: "); scanf("%d",&n); int *pi=(int*)malloc(4*n); printf("输入数值: "); for(i=0;i<n;i++) scanf("%d",&pi[i]); printf("打印输入的数值: "); for(i=0;i<n;i++) printf("%d ",pi[i]); return 0; } y // 0331y.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdlib.h> int main(int argc, char* argv[]) { int **pi; int *ary[2]; pi=ary; int array1[]={1,2}; int array2[]={3,4}; *pi=(int*)malloc(8); *pi=array1; *(pi+1)=array2; return 0; } ================================================================================= 0401 a b c // 0401c.cpp : Defines the entry point for the console application. //函数指针 #include "stdafx.h" typedef void *(*FN)(int);//定义数据类型 void Func1() { puts("Func1"); } void *Func2(int i) { puts("Func2"); return NULL; } void *Func3(int i) { puts("Func3"); return NULL; } int main(int argc, char* argv[]) { void *p=Func1;//void *p=Func1; 函数指针取地址 Func1(); //不带参函数指针 void (*pfn)()=Func1;//返回void 无参数 Func1(); pfn();//直接跳转到pfn指向的地址 //带参函数指针 void *(*pfn2)(int)=Func2;//返回为void* 带参数 的函数指针 Func2(2); pfn2(4); //定义数据类型,实例化指针 FN pfn3=Func2;//实例化指针 Func2(2); pfn3(5); //函数指针的数组 FN pfn4[2]; pfn4[0]=Func2; pfn4[1]=Func3; pfn4[0](5); pfn4[1](6); return 0; } d e // 0401e.cpp : Defines the entry point for the console application. //字符串:以0结尾的字符数组 (0 是必须的,但数组大小可以为0) #include "stdafx.h" int main(int argc, char* argv[]) { char szBuf1[]="he\nllo"; //不可打印字符的字符串 \n char *pszBuf2="hello"; //指向字符串数组地址 char *pszBuf3=szBuf1; char szBuf4[8]={0}; char szBuf5[8]=""; char *pszBuf6=""; char szBuf7[8]={'a','b','c',0}; char szBuf8[8]={'a','b','c',0,'d','e','f',0}; int i=0x636261; char *pszBuf9=(char*)&i; char *pszBuf10=szBuf1+2; puts(pszBuf9); char *psaBuf11=(char *)malloc(4); psaBuf11="abc";//不会释放 puts(pszBuf11); // char szBuf11[8]; // char szBuf12[5]="hello"; return 0; } f // 0401f.cpp : Defines the entry point for the console application. //strlen #include "stdafx.h" #include <string.h> #include <stdlib.h> int main(int argc, char* argv[]) { int size; char str1[] = "hello world"; char str2[] = ""; size = strlen(str1); printf("%d\n",size); strcpy(str2,str1); puts(str2); int i = strcmp(str1,str2);//比较字符串大小 printf("\n%d\n",i); /* char *ch = strchr(str1,'e'); int result = ch - str1 + 1; if(ch!=NULL) printf("%d\n",result); else puts("no"); char *pch = strstr(str1,"world"); int ret = ch - str1 + 1; if(ch!=NULL) printf("%d\n",ret); else puts("no"); */ strcat( str1, " haha " ); printf( "%s\n", str1 ); char *s = "9885 aa"; int k; k = atoi(s); printf( "string: %s\t\tinteger: %d\n", s, k ); char buffer[] = "buffer"; char *newstring; printf( "%s\n", buffer ); newstring = _strdup( buffer ); printf( "%s\n", newstring ); free( newstring ); return 0; } x // testX.cpp : Defines the entry point for the console application. //模仿strlen strcmp #include "stdafx.h" size_t Mystrlen(char *pzBuf) { size_t len=0; while(*pzBuf++) len++; return len; } char* Mystrcpy(char *strDst,const char* strRes) { char *pszRet=strDst; while(*strDst++=*strRes++) ; return pszRet; } int main(int argc, char* argv[]) { char str1[]="hello"; int num=Mystrlen(str1); printf("%d\n",num); char str[]=""; Mystrcpy(str,str1); puts(str); return 0; } ================================================================================= 0402 a // 0402a.cpp : Defines the entry point for the console application. //strcmp 按英文字典,逐个字母比较,区分大小写 // strchr atoi #include "stdafx.h" #include <string.h> int main(int argc, char* argv[]) { char szBuf1[]="hello"; char szBuf2[]="Hello"; int n=strcmp(szBuf1,szBuf2); printf("%d\n",n); char *pszBuf; pszBuf=strchr(szBuf1,'e'); if(pszBuf!=NULL) printf("e是hello里的第%d个字符\n",pszBuf-szBuf1); else printf("is no char 1"); int i=1234; char szNum[32]; sprintf(szNum,"%d",i);//数字转换为字符串 return 0; } b // 0402b.cpp : Defines the entry point for the console application. //字符串返回 #include "stdafx.h" #include <string.h> void Func(char *pszBuf) { pszBuf="hello";//1 只是拷贝一份地址 /* pszBuf=(char*)malloc(8); strcpy(pszBuf,"hello"); */ } int main(int argc, char* argv[]) { char *pszBuf; Func(pszBuf); puts(pszBuf); return 0; } c // 0402c.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdlib.h> #include <string.h> void Func(char **pszBuf) { *pszBuf=(char*)malloc(8); strcpy(*pszBuf,"hello"); } int main(int argc, char* argv[]) { char *pszBuf; Func(&pszBuf); puts(pszBuf); free(pszBuf); return 0; } d // 0403d.cpp : Defines the entry point for the console application. //p05字符串作为返回值 #include "stdafx.h" char *Func() { // char szBuf[]="hello";//返回时局部变量释放 static char szBuf[8]; scanf("%7s",szBuf);//建议键盘输入字符限制个数 return szBuf; } int main(int argc, char* argv[]) { char *pszBuf=Func(); printf("%s\n",pszBuf); return 0; } e // 0402e.cpp : Defines the entry point for the console application. //字符串作为返回值 #include "stdafx.h" #include <stdlib.h> #include <string.h> char *Func() { /*char szBuf[8]; char *pszBuf; scanf("%7s",szBuf); pszBuf=(char*)malloc(8); return strcpy(pszBuf,szBuf); */ char szBuf[8]; scanf("%7s",szBuf); return strdup(szBuf);//... } int main(int argc, char* argv[]) { char *pszBuf=Func(); printf("%s\n",pszBuf); free(pszBuf); return 0; } f // 0402f.cpp : Defines the entry point for the console application. //strtokj分隔符取字符串 #include "stdafx.h" #include <string.h> int main(int argc, char* argv[]) { char szBuf[128]; char *pszWord; fgets(szBuf,sizeof(szBuf),stdin); pszWord=strtok(szBuf," ,\n");//可以是分隔符集合 while(pszWord){ puts(pszWord); pszWord=strtok(NULL," "); } return 0; } x // 0401c.cpp : Defines the entry point for the console application. //从键盘上输入字符串,创建一个数组指针 //指向单个字符串,用函数获取输出 #include "stdafx.h" #include <stdlib.h> #include <string.h> void Func(int iWordCount,char *pszWords[]) { int i; for(i=0; i<iWordCount; i++) printf("word %d:%s\n",i,pszWords[i]); } int main() { char szBuf[128]; char *pszWord; char **szWords =NULL; int i=0; fgets(szBuf,sizeof(szBuf),stdin); pszWord=strtok(szBuf," \n,"); while(pszWord){ szWords=(char**)realloc(szWords,(i+1)*4); szWords[i++]=pszWord; pszWord=strtok(NULL," \n,"); } Func(i,szWords); free(szWords); return 0; } y // 0401c.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdlib.h> #include <string.h> char *GetWord(char **ppszSrc,const char *pszDelim) { char *pszRet; char *pszHead,*pszTail; pszHead=*ppszSrc; while(*pszHead && strchr(pszDelim,*pszHead)) pszHead++; if(*pszHead == '\0') return NULL; pszTail=pszHead+1; while(*pszTail && !strchr(pszDelim,*pszTail)) pszTail++; *ppszSrc=pszTail; pszRet=(char *)malloc(pszTail-pszHead+1); memcpy(pszRet,pszHead,pszTail-pszHead); pszRet[pszTail-pszHead]='\0'; return pszRet; } char **Split(const char *pszSrc,const char *pszDelim) { char **ppszWords=NULL; char *pszWord; int i=1; pszWord=GetWord((char **)&pszSrc,pszDelim); ppszWords=(char **)malloc(4); while(pszWord){ ppszWords=(char **)realloc(ppszWords,++i*4); ppszWords[i-2]=pszWord; pszWord=GetWord((char **)&pszSrc,pszDelim); } ppszWords[i-1]=NULL; return ppszWords; } void PrintWords(char *pszWords[]) { int i=0; while(pszWords[i]){ printf("word %d:%s\n",i,pszWords[i]); i++; } } void FreeWords(char *pszWords[]) { int i=0; while(pszWords[i]){ free(pszWords[i]); i++; } free(pszWords); } int main() { char szBuf[128]; char **ppszWords; fgets(szBuf,sizeof(szBuf),stdin); ppszWords=Split(szBuf," \n,"); PrintWords(ppszWords); FreeWords(ppszWords); return 0; } ================================================================================= 0406 a //a.c #include <stdio.h> typedef struct tagINFO { /*1 int i;//内存依次存放 char c; int j; */ char c1:2;//使用2个比特位 char c2:3; char c3:3; /* unsigned char c1:2; unsigned char c2:3; unsigned char c3:3; */ }INFO,*pINFO; int main(int argc, char* argv[]) { /*1 INFO info={1,'a',2};//结构体赋值 info.i=1; info.c='b'; info.j=2; */ INFO info; info.c1=2;//-2 info.c2=3;//3 info.c3=4;//-4 return 0; } b // 求结构体的大小 输出 #include <stdio.h> #pragma pack(8) //#pragma pack() 设置缺省对齐方式 typedef struct tagINFO { int i; char c; }INFO,*PINFO; int main(int argc, char* argv[]) { INFO info={1,'a'}; PINFO pInfo=&info; printf("sizeof(struct):%d",sizeof(info)); //printf("i:%d,c:%d\n",(*pInfo).c,(*pInfo).c); printf("i:%d,c:%c\n",pInfo->i,pInfo->c); return 0; } c //结构体之间赋值 #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma pack() typedef struct tagINFO { int i; char szName[8]; }INFO,*PINFO; int main(int argc, char* argv[]) { INFO info={1,"zhang"}; // INFO info2; // info2=info; PINFO pInfo=(PINFO)malloc(sizeof(INFO)); pInfo->i=2; strcpy(pInfo->szName,"zhang"); *pInfo=info; // memcpy(pInfo,&info,sizeof(info)); free(pInfo); printf("sizeof(info):%d",sizeof(info)); return 0; } d //内存布局查看 结构体与函数 #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct tagINFO { int i; char szName[8]; char *pazBuf; }INFO,*PINFO; void Func(PINFO pInfo) { strcpy(pInfo->szName,"sum"); pInfo->pazBuf="zhang"; } int main(int argc, char* argv[]) { //INFO info={1,"zhang","abc"}; INFO info; Func(&info);//指针传递‘ printf("%s,%s\n",info.szName,info.pazBuf); return 0; } e // 0406a.cpp : Defines the entry point for the console application. //结构体与函数 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct tagINFO { int i; char szName[8]; char *pszBuf; }INFO,*PINFO; void Func(INFO info) { strcpy(info.szName,"sun"); strcpy(info.pszBuf,"zhao"); } int main() { char chBuf[8]; INFO info; info.pszBuf=chBuf; Func(info); printf("%s,%s\n",info.szName,info.pszBuf);//??? return 0; } x // 0406c.cpp : Defines the entry point for the console application. // #include <stdio.h> typedef struct tagPerson { int iId; char szName[16]; char szMajor[16]; }Person,*PPerson; void WritePerson(const char *pszFileName) { Person person; FILE *fp=fopen(pszFileName,"wb"); if(fp==NULL){ printf("open file %s failed\n",pszFileName); return; } while(1){ scanf("%d %s %s",&person.iId, person.szName,person.szMajor); if(person.iId<1)//控制台输入无效的ID,就表示结束输入 break; fwrite(&person,1,sizeof(person),fp); } fclose(fp); } void ReadPerson(const char *pszFileName) { Person person; FILE *fp=fopen(pszFileName,"rb"); if(fp==NULL){ printf("open file %s failed\n",pszFileName); return; } while(1){ if(fread(&person,sizeof(person),1,fp)!=1) break; printf("id:%d,name:%s,major:%s\n", person.iId,person.szName,person.szMajor); } fclose(fp); } int main() { //WritePerson("./student.dat"); ReadPerson("./student.dat"); return 0; } ================================================================================= 0407 a // 0407a.cpp : Defines the entry point for the console application. //结构体包涵结构体 #include "stdafx.h" #include "b.h"//使用者置声明使用相关的头文件 #include "c.h" int main(int argc, char* argv[]) { struct B b; struct C c; b.j=1; b.a.i=2; c.k=3; // c.pa->i=4;//没有实例化的指针 return 0; } //a.h #ifndef __HA__ #define __HA__ struct A { int i; }; #endif //b.h #ifndef __HB__ #define __HB__ #include "a.h"//包涵结构体对象使用头文件 struct B { int j; struct A a; }; #endif //c.h #ifndef __HC__ #define __HC__ struct A;//包涵结构体指针声明结构体 struct C { int k; struct A *pa; }; #endif b // 0407b.cpp : Defines the entry point for the console application. //单向链表 赋值和动态分配内存 #include "stdafx.h" #include <stdlib.h> struct Node { int iData; struct Node *pNext; }; void Func(struct Node * pHead) { int i=1; while(pHead) { pHead->iData=i++; pHead=pHead->pNext; } } void Free(struct Node * pHead) { struct Node * pDelete; while(pHead) { pDelete=pHead; pHead=pHead->pNext; free(pDelete); } } int main(int argc, char* argv[]) { /* struct Node *pHead,node,node2,node3; pHead=&node; node.pNext=&node2; node2.pNext=&node3; node3.pNext=NULL; Func(pHead); */ Node *pHead; pHead=(struct Node *)malloc(sizeof(struct Node)); pHead->pNext=(struct Node *)malloc(sizeof(struct Node)); pHead->pNext->pNext=(struct Node *)malloc(sizeof(struct Node)); pHead->pNext->pNext->pNext=(struct Node *)malloc(sizeof(struct Node)); pHead->pNext->pNext->pNext=NULL; Free(pHead); return 0; } x // 0407b.cpp : Defines the entry point for the console application. //链表实现学生信息储存 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct tagPerson { int iId; char szName[16]; char szMajor[16]; struct *pNext; }Person,*PPerson; PPerson GetPerson() { PPerson pPerson=(PPerson)malloc(sizeof(Person)); scanf("%d %s %s",&pPerson->iId, pPerson->szName,pPerson->szMajor); if(pPerson->iId<1){ free(pPerson); return NULL; } return pPerson; } PPerson GetPersonList() { PPerson pNode,pHead=NULL; while(pNode=GetPerson()){ pNode->pNext=pHead; pHead=pNode; } return pHead; } void FreePersonList(PPerson pNode) { PPerson pDelete; while(pNode != NULL){ pDelete=pNode; pNode=pNode->pNext; free(pDelete); } } int main() { PPerson pHead=GetPersonList(); FreePersonList(pHead); return 0; } y // 0407YYY.cpp : Defines the entry point for the console application. //单向链表插入和弹出数据,实现调用和功能实现分开 #include "stdafx.h" void BuildData()//输入数据 { Person data; while(1){ scanf("%d %s %s",&data.iId, data.szName,data.szMajor); if(data.iId<1) break; Push(&data); } } void PrintData()//读取数据 { Person data; while(Pop(&data)){ printf("id:%d,name:%s,major:%s\n", data.iId,data.szName,data.szMajor); } } int main() { BuildData(); PrintData(); return 0; } //stack.cpp #include "stdafx.h" static Node *g_pHead=NULL; void Push(const Person *pDate)//插入数据 { Node *pNode=(Node*)malloc(sizeof(Node));//分配空间 pNode->date=*pDate;//赋值 pNode->pNext=g_pHead; g_pHead=pNode; } bool Pop(Person *pDate)//弹出数据 { Node *pDelete=g_pHead; if(g_pHead==NULL) return false; *pDate=g_pHead->date; g_pHead=g_pHead->pNext; free(pDelete); return true; } //stack.h #if !defined __STACK_H__ #define __STACK_H__ #include "stdafx.h" struct Person { int iId; char szName[16]; char szMajor[16]; }; struct Node { Person date; Node *pNext; }; void Push(const Person *pDate);//插入数据 bool Pop(Person *pDate);//弹出数据 #endif //stdafx.h #include <stdio.h> #include <stdlib.h> #include <string.h> #include "stack.h" ================================================================================= 0408 a // 0408a.cpp : Defines the entry point for the console application. //双链表 #include "stdafx.h" int main(int argc, char* argv[]) { Person data1={1,"li","shuxue"}; Person data2={2,"wang","hanyu"}; Person data3={3,"zheng","computer"}; void *pPos1,*pPos2; pPos1=AddTail(&data1); pPos2=AddTail(&data2); InsertBefore(pPos2,&data3); Delete(pPos1); return 0; } //stack.cpp #include "stdafx.h" static Node *g_pHead = NULL; static Node *g_pTail = NULL; void *AddTail(const Person *pData)//从尾部增加节点 { Node *pNode=(Node *)malloc(sizeof(Node)); pNode->data=*pData; if(g_pHead) g_pTail->pNext=pNode; else g_pHead=pNode; pNode->pPrev=g_pTail; pNode->pNext=NULL; g_pTail=pNode; return pNode; } void *InsertBefore(void *pPos,const Person *pData)//指定节点前插入节点 { Node *pCurNode=(Node *)pPos; Node *pNewNode=(Node *)malloc(sizeof(Node)); pNewNode->data=*pData; if(g_pHead==pCurNode){ //说明当前节点是头节点 pNewNode->pNext=pCurNode; pNewNode->pPrev=NULL;//pCurNode->pPrev pCurNode->pPrev=pNewNode; g_pHead=pNewNode; }else{ pNewNode->pNext=pCurNode; pNewNode->pPrev=pCurNode->pPrev; pCurNode->pPrev->pNext=pNewNode; pCurNode->pPrev=pNewNode; } return pCurNode; } void Delete(void *pPos)//删除节点 { Node *pDel=(Node*)pPos; if(g_pHead==pDel) g_pHead=pDel->pNext; else pDel->pPrev->pNext=pDel->pNext; if(pDel == g_pTail) g_pTail = pDel->pPrev; else pDel->pNext->pPrev = pDel->pPrev; } //stack.h #include "stdafx.h" struct Person { int iId; char namme[20]; char Major[20]; }*pPerson; struct Node { Person data; struct Node *pNext; struct Node *pPrev;; }; void *AddTail(const Person *pDate); void *InsertBefore(void *pPos,const Person *pData); void Delete(void *pPos); //stdafx.h #include <stdio.h> #include <stdlib.h> #include <string.h> #include "stack.h" b // 0408b.cpp : Defines the entry point for the console application. //环链表 #include "stdafx.h" struct Node { int data; Node *pNext; }; bool IsRing(Node *pHead)//判断是否是环链表 { Node *pFlow,*pFast; pFlow=pFast=pHead; while(pFast&&pFast->pNext) { pFlow = pFlow->pNext; pFast = pFast->pNext->pNext; if(pFlow==pFast) return true; } return false; } int main(int argc, char* argv[]) { Node n1,n2,n3,n4; n1.data=1; n1.pNext=&n2; n2.data=2; n2.pNext=&n3; n3.data=3; n3.pNext=&n4; n4.data=4; n4.pNext=&n1; bool n=IsRing(&n1); return 0; } ================================================================================= 0409 a // 0409a.cpp : Defines the entry point for the console application. //引用 #include "stdafx.h" #include <stdlib.h> int main(int argc, char* argv[]) { /* int i; int &iRef=i;//常量引用 iRef=1; */ /* int array[5]={0,1,2,3,4}; int &iRefar=array[1];//数组引用 iRefar=20; */ /* int *p; int *&piRef=p;//指针引用 piRef=NULL; */ int *pi=(int*)malloc(4); int &iRef=*pi;//引用指向内存 iRef=0xab; free(pi); return 0; } b // 0409b.cpp : Defines the entry point for the console application. //引用函数参数传递与返回值的传递 #include "stdafx.h" #include <stdlib.h> /* void Func(int &iRef) { iRef=1; } int main(int argc, char* argv[]) { int i; Func(i);//引用函数参数传递 return 0; } */ int &Func() //引用返回值的传递 { // static int i=123; // return i; int *pi=(int*)malloc(4); *pi=234; return *pi; } int main(int argc, char* argv[]) { // int &iRef=Func(); int &iRef=Func(); free(&iRef); return 0; } c // 0409c.cpp : Defines the entry point for the console application. //引用函数的传递 #include "stdafx.h" #include <stdlib.h> void Func(int *&pi) { pi=(int *)malloc(4); *pi=456; } int main(int argc, char* argv[]) { int *pi; Func(pi); free(pi); return 0; } x // 0409X.cpp : Defines the entry point for the console application. // 使用递归遍历、释放、插入二叉数 #include "stdafx.h" #include <stdlib.h> struct Node { int iDate; Node *pLeft; Node *pRight; }; void PrintTree(const Node *pBoot) { if(pBoot==NULL) return; printf("node:%d\n",pBoot->iDate); PrintTree(pBoot->pLeft); PrintTree(pBoot->pRight); } void FreeTree(Node *pBoot) { if(pBoot==NULL) return; FreeTree(pBoot->pLeft); FreeTree(pBoot->pRight); printf("free:%d\n",pBoot->iDate);//34251 free(pBoot); } void InsertTree(Node *&pRoot,int data) { if(pRoot == NULL) { pRoot=(Node*)malloc(sizeof(Node)); pRoot->iDate=data; pRoot->pLeft=pRoot->pRight=NULL; return; } if(pRoot->iDate<data) InsertTree(pRoot->pRight,data); else InsertTree(pRoot->pLeft,data); } int main(int argc, char* argv[]) { /* Node *pn1=(Node*)malloc(sizeof(Node)); Node *pn2=(Node*)malloc(sizeof(Node)); Node *pn3=(Node*)malloc(sizeof(Node)); Node *pn4=(Node*)malloc(sizeof(Node)); Node *pn5=(Node*)malloc(sizeof(Node)); pn1->iDate=1; pn2->iDate=2; pn3->iDate=3; pn4->iDate=4; pn5->iDate=5; pn1->pLeft=pn2; pn1->pRight=pn5; pn2->pLeft=pn3; pn2->pRight=pn4; pn3->pLeft=NULL; pn3->pRight=NULL; pn4->pLeft=NULL; pn4->pRight=NULL; pn5->pLeft=NULL; pn5->pRight=NULL; PrintTree(pn1); FreeTree(pn1); */ Node *pRoot=NULL; InsertTree(pRoot,5); InsertTree(pRoot,8); InsertTree(pRoot,3); InsertTree(pRoot,4); InsertTree(pRoot,2); PrintTree(pRoot); FreeTree(pRoot); return 0; } y // order.cpp : Defines the entry point for the console application. //排序 #include <stdio.h> #include <stdlib.h> void Swap(int &num1,int &num2) { int temp=num1; num1=num2; num2=temp; } //选择排序法 void SelectSort(int nums[],int count) { int i,j,k; for(i=0; i<count-1; i++){ k=i;//初始最小数的索引值 for(j=i+1; j<count; j++){ if(nums[j]<nums[k]) k=j; } if(k!=i) Swap(nums[i],nums[k]); } } //选择排序 void SelectSort2(int array[],int n) { int i,j,temp; for(i=1;i<n;i++) { temp = array[i]; for(j=i ; j>0 && temp < array[j-1] ; j--) { array[j]=array[j-1]; } array[j]=temp; } } //选择排序法 void Order(int order[],int count) { static int icount=0; int i,j,temp; for (i=0;i<count;i++) for (j=i;j<count;j++){ if(order[i]>order[j+1]){ temp=order[i]; order[i]=order[j+1]; order[j+1]=temp; icount++; } } } //冒牌排序 void Bubble (int order[],int count) { int i,j,temp; for(i=0;j<count-1;i++) for(j=count-1;j>i;j--) if (order[j]<order[j-1]) { temp=order[j]; order[j]=order[j-1]; order[j-1]=temp; } /* for (j=0;j<count-i;j++) if (order[j]>order[j+1]){ temp=order[j+1]; order[j+1] = order[j]; order[j]=temp; }*/ for(i=0;i<count;i++) printf("%d ",order[i]); } //插入排序 void InsertSort(int nums[],int count) { int i,j,k; int temp; for(i=0; i<count-1; i++){ for(j=i; j>=0; j--){ if(nums[j]<nums[i+1]){ break; } } temp=nums[i+1]; for(k=i; k>j; k--){ nums[k+1]=nums[k]; } nums[j+1]=temp; } } int main(int argc, char* argv[]) { int nums[]={5,4,1,3,9,6}; //SelectSort(nums,sizeof(nums)/sizeof(nums[0])); //SelectSort2(nums,sizeof(nums)/sizeof(nums[0])); //Order(nums,sizeof(nums)/sizeof(nums[0])); Bubble (nums,sizeof(nums)/sizeof(nums[0])); return 0; } ================================================================================= a // 0412a.cpp : Defines the entry point for the console application. // #include "stdafx.h" //连接符 #define MY_TYPE(x,y) x##y //把一个字符转发为字符串 #define MY_DATA(z) #z #define Min(x,y) ((x)<(y)?(x):(y)) int main(int argc, char* argv[]) { //续行符号 char ch[]="the entry point for \ the console application" ; char ch2[]="the entry point for" "the console application" ; MY_TYPE(ch,ar) info[8]=MY_DATA(hello); // 相当于char info[8]="hello" Min(2,5); return 0; } b // 0412b.cpp : Defines the entry point for the console application. //共同体(联合体) 枚举类型 #include "stdafx.h" //共同占用一块内存 union IP { unsigned char bIP[4]; int iIP; }; enum ERRNO //类型 { ERRNO1, //常量 从0开始 ERRNO2, //可以赋初始值,依次递增 ERRNO3, }; ERRNO MyLogin() { // if() return ERRNO1; // if() return (ERRNO)1;//整形变量赋给枚举类型要强制转换 } int main() { IP ip; ip.bIP[0]=0xc0; ip.bIP[1]=0xa8; ip.bIP[2]=0x00; ip.bIP[3]=0x01; // ip.iIP=0x010008c0; //查看内存分布情况 int ret=MyLogin(); //枚举类型可以赋给整形变量 return 0; } c // 0412c.cpp : Defines the entry point for the console application. //new Delete 的使用 #include "stdafx.h" struct INFO { char c; int i; }; int main(int argc, char* argv[]) { char *pc = new char('a'); int *pi = new int(4); int *pi2 = new int[4]; INFO *pInfo= new INFO(); delete pc; delete pi; delete []pi2;//数组释放 delete pInfo; return 0; } =================================================================================
相关文章推荐
- C语言基础之宏定义(附代码)
- C语言学习基础代码记录
- 程序语言基础 8.14课堂代码
- C语言基础之常用文件操作2(附代码)
- 学习java基础语言代码
- 【分享】一些经典的C/C++语言基础算法及代码(二)
- Java学习笔记之语言基础――Java代码安全的概念
- C语言基础之常用文件操作1(附代码)
- 易语言基础教程之简单代码的编写
- Go语言基础学习二-简单的代码分析
- 【分享】一些经典的C/C++语言基础算法及代码(三)
- 机器学习基础(二十)—— 数学语言与 Python 代码
- iOS开发之 Objective-C语言基础 :代码块
- 【分享】一些经典的C/C++语言基础算法及代码(四)
- Java语言基础(关键字、标识符、注释、常量与变量、进制、数据类型、类型转换、代码示例)
- java语言基础之构造函数深入以及构造函数和构造代码块比较-作者:逝秋
- java语言基础之代码块详解
- 黑马程序员——Java语言基础(代码示例篇)
- 例题:计算运费。c#语言基础,比较简单。看代码输入格式和方法。同样方法可以做一个 出租车打车的程序
- Beginning C# Objects从概念到代码——1.3 C#语言基础