红皮书 - C语言程序设计实用教程参考答案(10)

2020-02-22 12:04

printf(\读取合并后的新文件:\\n\ //ch=fgetc(p3); while(!feof(p3)){ printf(\ ch=fgetc(p3); }

fclose(p3); getchar(); return 0; }

19.请预先在c:\\\\1000\\\\中准备文本文件ok1.txt。

#include int main(){ FILE *p1,*p2; char ch;

p1=fopen(\ p2=fopen(\ printf(\读取原文件: \ ch=fgetc(p1); while(!feof(p1)){

printf(\ ch=fgetc(p1); }

putchar('\\n');

rewind(p1); //将读写位置定位到文件的开头 while(!feof(p1)){

putc(ch^'a',p2); //用字母a与ok1中的字符进行异或运算加密文件,将密文送入文件ok2 ch=fgetc(p1); }

fclose(p1);

rewind(p2); //将读写位置定位到文件的开头 printf(\读取加密后的新文件:\\n\ while(!feof(p2)){ printf(\ ch=fgetc(p2); }

fclose(p2);

getchar(); return 0; }

习题15

1.D

2.本题使用尾插法

#include #include #include typedef

struct node { char ch;

struct node * next; } NODE; int main(){ int i = 0;

NODE * head = NULL; //空链表 NODE * newNode ;

NODE * foundEndNode;

printf(\请输入10个字符,用回车分隔: \\n\ for(i=0;i<10;i++){ newNode = malloc(sizeof(NODE)); //创建新节点 newNode->ch = getchar(); //设置新节点中包含的字符数据 getchar();//吃掉回车符 if(head == NULL){ newNode->next = head; //设置新节点是尾节点 head = newNode; //将头节点指向新节点 } else{ foundEndNode = head; while((foundEndNode->next) != NULL){ //寻找最后的节点 foundEndNode = foundEndNode->next; } foundEndNode->next = newNode ; //将最后一个节点的中的next指针指向新节点 newNode->next = NULL; //新节点设置成尾节点 } }

printf(\节点中的字符依次是:\\n\ while(head!=NULL){ printf(\ head = head->next; }

return 0; }

3.

#include #include #include typedef

struct node { int ch;

struct node * next; } NODE; int main(){ int i = 0;

NODE * head = NULL; //空链表

printf(\请输入10个整数,用回车分隔: \\n\ for(i=0;i<10;i++){ NODE * newNode = malloc(sizeof(NODE)); //创建新节点 scanf(\ //设置新节点中的数据 getchar(); //消耗掉回车符 newNode->next = head; //设置新节点指向的后继节点 head = newNode; //将新节点设置为链表的头节点 }

printf(\节点中的整数依次是:\\n\ while(head!=NULL){ printf(\ head = head->next; }

return 0; } 4.

#include #include #include typedef

struct node { int ch;

struct node * next; } NODE; int main(){ int i = 0;

NODE * head = NULL; //空链表 NODE * newNode ;

NODE * foundEndNode;

printf(\请输入10个整数,用回车分隔: \\n\ for(i=0;i<10;i++){

} 5.

newNode = malloc(sizeof(NODE)); //创建新节点 scanf(\ //设置新节点中包含的字符数据 getchar();//吃掉回车符 if(head == NULL){ newNode->next = head; //设置新节点是尾节点 head = newNode; //将头节点指向新节点 } else{ foundEndNode = head; while((foundEndNode->next) != NULL){ //寻找最后的节点 foundEndNode = foundEndNode->next; } foundEndNode->next = newNode ;//节点中的next指针指向新节点 newNode->next = NULL; //新节点设置成尾节点 } }

printf(\节点中的整数依次是:\\n\while(head!=NULL){ printf(\ head = head->next; }

return 0;

#include #include #include typedef

struct node { int number; struct node * next; } NODE; int main(){ int a[] ={12,34,22,15,36,66}; int i = 0,n=0; NODE * head = NULL; //空链表 NODE * newNode ; NODE * foundEndNode; int N = sizeof(a)/sizeof(int); for(i=0;inumber = a[i]; //设置新节点中包含的字符数据 if(head == NULL){

newNode->next = head; //设置新节点是尾节点 head = newNode; //将头节点指向新节点 } else{ foundEndNode = head; while((foundEndNode->next) != NULL){ //寻找最后的节点 foundEndNode = foundEndNode->next; } foundEndNode->next = newNode ; //节点中的next指针指向新节点 newNode->next = NULL; //新节点设置成尾节点 } } printf(\请输入一个学号: \\n\ scanf(\ getchar();

foundEndNode = head; while(1){ if( foundEndNode->number==n ){ printf(\这个学号在此链表中.\\n\ break; } if( foundEndNode->next==NULL){ printf(\这个学号不在此链表中.\\n\ break; }

foundEndNode = foundEndNode->next; } return 0; }


红皮书 - C语言程序设计实用教程参考答案(10).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:项目融资计划书模板

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: