1 /*
2 本程序用来将输入的制表符替换为\t, 而将退格替换为\b,
3 将反斜杠替换为\\
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 typedef struct node
10 {
11 char Input;
12 struct node* next;
13 }NODE;
14
15
16 int GetLine(NODE *head);
17 int Transfer(NODE *head);
18 void EchoLine(NODE *head);
19
20 int main(int argc,char* argv[],char* env[])
21 {
22 NODE* head;
23
24
25 if(NULL==(head=(NODE*)malloc(sizeof(NODE))))
26 {
27 puts("No enough space,then will quit");
28 exit(0);
29 }
30
31
32 GetLine(head);
33 Transfer(head);
34 EchoLine(head);
35
36 getchar();
37 return 0;
38 }
39
40
41 /*
42 函数功能:
43 读取字符,并用动态分配的内存存储起来
44 函数原型:
45 char* GetLine(char* head)
46 函数参数:
47 char* head:存储的首地址
48 返回值:
49 0:成功执行
50 1:没有足够的内存空间分配
51 异常:
52 传递空指针参数
53 */
54 int GetLine(NODE* head)
55 {
56 NODE* end;
57 NODE* temp;
58
59 short int Input;
60
61 if(NULL==(head=(NODE*)malloc(sizeof(NODE))))
62 exit(0);
63
64 end=head;
65 while((Input=getchar())!= EOF)
66 {
67 end->Input =Input;
68 if(NULL==(temp=(NODE *)malloc(sizeof(NODE))))
69 exit(0);
70 temp->next =NULL;
71 end->next =temp;
72 end=temp;
73 }
74
75 return 0;
76 }
77
78 /*
79 函数功能:
80 '\t' ——> ‘\\’##‘t’
81 ‘\b' ——> '\\'##'b'
82 '\\' ——> '\\'##'\\'
83 函数原型:
84 int Transfer(NODE *)
85 函数参数:
86 NODE* head:待处理文本流首地址
87 函数返回值:
88 0: 成功执行
89 1: 没有足够的空间存储分配
90 异常:
91 传递空指针参数
92 */
93 int Transfer(NODE* head)
94 {
95 NODE* temp;
96 NODE* NewNode;
97
98 NewNode=head;
99 while(NewNode->next != NULL)
100 {
101 if(NewNode->Input == '\t')
102 {
103 if(NULL==(temp=(NODE*)malloc(sizeof(NODE))))
104 return 1;
105 //1、先将temp的Input成员设置成‘t’
106 temp->Input ='t';
107 //2、然后将temp->next指向NewNode的下一个节点
108 temp->next =NewNode->next ;
109 //3、然后将NewNode->next指向temp节点
110 NewNode->next =temp;
111 //4、设置NewNode->input为反斜杠 ‘\\’
112 NewNode->Input ='\\';
113 }
114 else if(NewNode->Input == '\b')
115 {
116 if(NULL==(temp=(NODE*)malloc(sizeof(NODE))))
117 return 1;
118 //1、先将temp的Input成员设置成‘t’
119 temp->Input ='b';
120 //2、然后将temp->next指向NewNode的下一个节点
121 temp->next =NewNode->next ;
122 //3、然后将NewNode->next指向temp节点
123 NewNode->next =temp;
124 //4、设置NewNode->input为反斜杠 ‘\\’
125 NewNode->Input ='\\';
126 }
127 else if(NewNode->Input == '\\')
128 {
129 if(NULL==(temp=(NODE*)malloc(sizeof(NODE))))
130 return 1;
131 //1、先将temp的Input成员设置成‘t’
132 temp->Input ='\\';
133 //2、然后将temp->next指向NewNode的下一个节点
134 temp->next =NewNode->next ;
135 //3、然后将NewNode->next指向temp节点
136 NewNode->next =temp;
137 //4、设置NewNode->input为反斜杠 ‘\\’
138 NewNode->Input ='\\';
139 }
140 }
141
142 return 0;
143 }
144
145
146 /*
147 函数功能:
148 输出字符流
149 函数原型:
150 int EchoLine(NODE *head)
151 函数参数:
152 NODE* head:存储字符的首地址
153 返回值:
154 无返回值
155 异常:
156 传递空指针参数
157 */
158
159 void EchoLine(NODE* head)
160 {
161 NODE* temp;
162 temp=head;
163 while(NULL != temp->next )
164 {
165 putchar(temp->Input);
166 temp=temp->next ;
167
168 //free(temp);
169 }
170 }