C语言复习资料.doc_第1页
C语言复习资料.doc_第2页
C语言复习资料.doc_第3页
C语言复习资料.doc_第4页
C语言复习资料.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

头插法/list16.2_dynamic linklist _insert from head.c#include #include struct node /结点数据类型 int data; struct node *next; ;main() struct node *head=NULL, *p;int n=0;/采用相同的方式创建4个结点 while(nnext = NULL;printf(input data:);scanf(%d,&p-data);/挂链p-next = head;head = p;n+;/遍历p= head;while(p)printf(%4d, p-data);p = p-next;putchar(n);/释放资源while(head)p = head;head = head -next;free(p);尾插法/list16.3_dynamic linklist _insert from tail.c#include #include struct node /结点数据类型 int data; struct node *next; ;main() struct node *head=NULL, *p,*q;int n=0;/采用相同的方式创建4个结点 while(nnext = NULL;printf(input data:);scanf(%d,&p-data) ;/挂链if(head = NULL)head = p;else/寻找链尾q=head;while(q-next!=NULL)q=q-next;q-next = p; /挂入链尾n+;/遍历p= head;while(p)printf(%4d, p-data);p = p-next;putchar(n);/释放资源while(head)p = head;head = head -next;free(p);文件1.*File: list16.1_write char to file.c#includemain()FILE *fp;/文件结构类型指针变量声明char ch;/*打开文件*/ if (fp=fopen(test.txt,w)=NULL) /*打开文件失败*/printf(can not open this filen);exit(0); /*字符写方式访问文件:输入字符,并存储到指定文件中*/ printf(input some characters here:n); for( ; (ch=getchar() != ; ) fputc(ch,fp); /*输入字符并存储到文件中*/*关闭文件*/ fclose(fp);2.*File: list16.2_read char to file.c*/#include stdio.hmain() FILE *fp;char ch;/*打开文件*/if (fp=fopen(test.txt,r)=NULL) printf(can not open source filen);exit(0);/*顺序输出文件的内容*/for(; (ch=fgetc(fp)!=EOF; ) /读文件操作putchar(ch); /*将读取的字符回显在屏幕上*/printf(n);fclose(fp); /*关闭打开的文件*/3.*File: list16.3_read write string to file.c*/#include stdio.hmain() FILE *fp;char string81;/*字符数组用于暂存输入输出的字符串*/if (fp=fopen(teststring.txt,w)=NULL) /*打开文件失败*/ printf(can not open this filen);exit(0);/*从键盘上输入字符串,并存储到指定文件中*/printf(Input a string: ); gets(string);/*从键盘上输入字符串*/fputs(string, fp);/*存储到指定文件*/fclose(fp);/*重新打开文件,读出其中的字符串,并输出到屏幕上*/if (fp=fopen(teststring.txt, r)=NULL) /*打开文件失败*/ printf(can not open this filen);exit(0);fgets(string, strlen(string)+1, fp);/*从文件中读一个字符串*/printf(Output the string: ); puts(string);/*将字符串输出到屏幕上*/fclose(fp);结构体1.*File: list17.3_struct array.c*/#include /*定义结构类型*/typedef struct int month; int day; int year;DATE;/*定义结构类型*/typedef struct char no7; char name10; char sex3; DATE birthday;STD_INFO;/*声明并初始化一个全局记录数组stu3 */STD_INFO stu3= 000102,张三,男,9,20,1980, 000105,李四,男,8,15,1980, 000112,王五,女,3,10,1980 ;/*主函数main()*/main() int i; /*打印表头: 表示1个空格字符*/ printf( No. Name Sex Birthdayn); /*输出三个学生的基本情况*/ for(i=0; i3; i+) printf(%-8s,stui.no); printf(%-10s,); printf(%-5s,stui.sex); printf(%d-%d-%dn,stui.birthday.year, stui.birthday.month, stui.birthday.day); 2.*File: list17.1_access members of struct type viaralbe.c#include #include main()struct int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; char * pName = Zhang Xin; char * pAddr = Shanghai; int i; /stu1 stu1.num=10010; strcpy(, pName); stu1.sex=M; stu1.age=19; stu1.score=90.5; strcpy(stu1.addr, pAddr); /stu2 printf(please input info of stu2:n); printf(stu2s name:); gets(); printf(stu2s studentID,sex,age,score:); scanf(%d,%c,%d,%f,&stu2.num,&stu2.sex,&stu2.age,&stu2.score); /*读掉用于激活缓冲机制的回车换行符,否则下面的gets将读取该字符而得不到用户输入*/getchar(); printf(please input stu2s address:); gets(stu2.addr); /output printf(n%6d, %-20s, %2c, %3d, %7.1f, %-30sn, stu1.num,,stu1.sex,stu1.age,stu1.score,stu1.addr); printf(%6d, %-20s, %2c, %3d, %7.1f, %-30sn, stu2.num,,stu2.sex,stu2.age,stu2.score,stu2.addr); 指针与二维数组1.数组元素的指针 /*list14.2_point to element*/#include main() int a34=1,3,5,7,9,11,13,15,17,19,21,23; int * pa, i; pa=*a; /不能是p=a;想想为什么 /*解释:pa指向数据元素*a的等价表示:*(a+0)+0, &a00, 即第0行第0列元素 a表示数组起始地址,a+0表示第0行起始地址,均不能表示某个元素的地址*/ printf(多维数组a34中元素如下表:n); for(i=0; i12; i+) if(pa-*a)%4=0) printf(n); printf(%5d,*pa+); printf(nn详细地址信息与元素值如下表:n); pa=*a; /此处没有该语句,程序会如何? for(i=0; i12; i+,pa+) if(pa-*a)%4=0) printf(n); printf(addr=%x, value=%dn,pa,*pa); printf(n);2.行指针*list14.3_point to array.c#include #define M 3#define N 4main() int aMN=1,3,5,7,9,11,13,15,17,19,21,23; int (*pa)N; /pa是指向有N个元素的一维数组的指针,括号千万不可省略 int i,j; pa=a; /不能是pa=*a为什么?/*解释:pa为一维数组指针此处a的含义是:a+0, &a0, 表示第0行起始地址,即一维数组1,3,5,7的起始地址*a实际为*(a+0)+0的简化写法表示,表示第0行第0列元素地址,指向元素,而非数组起始地址*/ printf(npa=%xn,pa); /第0行首 printf(*pa=%xn,*pa); /第0行第0列首 printf(*pa=%dnn,*pa); /第0行第0列元素值 printf(pa+1=%xn,pa+1); /第1行首 printf(*(pa+1)=%xn,*(pa+1); /第1行第0列首 printf(*(pa+1)=%dnn,*(pa+1); /第1行第0列

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论