-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·124 lines (109 loc) · 4.32 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
nclude <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
/* global data */
typedef struct dlink /* double linked list for text */
{ char input;
struct dlink *prev;
struct dlink *next;
}dnode;
dnode *begin, *cursor, *new;
const char csor='|'; /* cursor character */
const char LF=0xa; /* line feed character */
void print_String(){
dnode *cur = begin;
while(cur!=NULL){
printf("%c",cur->input);
cur=cur->next;
}
}
void move_Left() {
dnode *cur = cursor;
if(cur->prev != NULL){
cur = cur->prev;
cursor->input = cur->input;
cur->input = csor;
cursor = cur;
}
print_String();
}
void move_Right(){
dnode *cur = cursor;
if(cur->next != NULL){
cur = cur->next;
cursor->input = cur->input;
cur->input = csor;
cursor = cur;
}
print_String();
}
char cget() // get command character, skip LF
{ char c;
do {c=getchar();} while(c==LF); // skip LFs
return c;
}
void ins_Char(){
dnode *newNextNode = (dnode*)malloc(sizeof(dnode));
char c = cget();
newNextNode->input = c;
newNextNode->prev = cursor;
newNextNode->next = cursor->next;
cursor->next= newNextNode;
print_String();
}
void ini_String(){
new=(dnode*)malloc(sizeof(dnode));
new->prev = NULL;
new->next = NULL;
begin=new;
cursor = new;
char c;
while(1) {
c = getchar();
if(c=='\n')
break;
new->input = c;
dnode *temp= (dnode*) malloc(sizeof (dnode));
new->next = temp;
temp->prev = new;
temp->next = NULL;
new = temp;
}
new->input = csor;
cursor= new;
}
void del_Char(){
if(cursor->next != NULL){
dnode *delNode = cursor->next;
cursor->next = delNode->next;
free(delNode);
print_String();
}
}
void menu() /*Menu choice and switch function*/
{ char option;
printf("\nEnter option (v=view l=left r=right i=ins d=del x=exit): ");
do
{
option=(char)tolower( (int)cget() );
switch(option)
{ case 'v': print_String();break;
case 'l': move_Left(); break;
case 'r': move_Right(); break;
case 'i': ins_Char(); break;
case 'd': del_Char(); break;
case 'x': printf("Exit\n"); break;
default: printf("Invalid option=%x\n", option);
}
} while (option!='x');
}
int main()
{
printf("Enter initial string: ");
ini_String();
menu(); /* command Entry menu loop */
return(EXIT_SUCCESS);
} /*End main*/