# I nclude "stdio. H"
# I nclude "stdlib. H"
Struct line {
Char ''''' text [81];
Int num;/* row number */
Struct line * Next;/* pointer to the next input project */
Struct line * Prior;/* refers to the pointer to the previous project */
};
Struct line * start;/* pointer to the first item in the table */
Struct line * last;/* pointer to the table as the next item */
Struct line * Find (INT), * dls_store (struct line *);
Void patchup (INT, INT), delete_text (), list (), save (char *), load (char *);
Menu_select ();
Enter (INT linenum );
Void main (INT argc, char * argv [])
{
Char s [80], choice, fname [80];
// Struct line * Info;
Int linenum = 1;
Start = NULL;
Last = NULL;
If (argc = 2) load (argv [1]);/* load the file on the command line */
Do {
Choice = menu_select ();
Switch (choice ){
Case 1: printf ("/T row number :");
Gets (s );
Linenum = atoi (s );
Enter (linenum );
Break;
Case 2: delete_text ();
Break;
Case 3: List ();
Break;
Case 4: printf ("/T file name :");
Gets (fname );
Save (fname );
Break;
Case 5: printf ("/T file name :");
Gets (fname );
Load (fname );
Break;
Case 6: exit (0 );
}
} While (1 );
}
/* Display menu for users to choose */
Menu_select ()
{
Char s [80];
Int C;
Printf ("/T/T1. input/N ");
Printf ("/T/T2. delete a row/N ");
Printf ("/T/T3. display a row/N ");
Printf ("/T/T4. file storage/N ");
Printf ("/T/T5. Load file/N ");
Printf ("/T/T6. exit/N ");
Do {
Printf ("/n/T :");
Gets (s );
C = atoi (s );
} While (C <0 | C> 6 );
Return (C );
}
/* Insert the text at the end of the specified row */
Enter (INT linenum)
{
Struct line * Info;
// Char T [81];
For (;;){
/**/
Info = (struct line *) malloc (sizeof (struct line ));
If (! Info ){
Printf ("/T! Insufficient memory! /N ");
Return (null );
}
Printf ("% d:", linenum );
Gets (Info-> text );
Info-> num = linenum;
If (* Info-> text ){
If (find (linenum) patchup (linenum, 1 );
If (* Info-> text) Start = dls_store (Info );
}
Else break;
Linenum ++;
}
Return (linenum );
}
/* When the text content is inserted in the middle of the file, the line number of the content must be increased by 1, while */
/* The row number after the deleted text must be reduced by 1 */
Void patchup (int n, int incr)
{
Struct line * I;
I = find (N );
While (I ){
I-> num = I-> num + incr;
I = I-> next;
}
}
/* Sort by row number and insert it */
Struct line * dls_store (struct line * I)
{
Struct line * Old, * P;
If (last = NULL ){
I-> next = NULL;
I-> prior = NULL;
Last = I;
Return (I );
}
P = start;
Old = NULL;
While (p ){
If (p-> num ){
Old = P;
P = p-> next;
}
Else {
If (p-> prior ){
P-> prior-> next = I;
I-> next = P;
P-> prior = I;
Return start;
}
I-> next = P;
I-> prior = NULL;
P-> prior = I;
Return (I );
}
}
Old-> next = I;
I-> next = NULL;
I-> prior = old;
Last = I;
Return start;
}
/* Delete a row */
Void delete_text ()
{
Struct line * Info;
Char s [80];
Int linenum;
Printf ("/T row number :");
Gets (s );
Linenum = atoi (s );
Info = find (linenum );
If (Info ){
If (START = info ){
Start = Info-> next;
If (start) Start-> prior = NULL;
Else last = NULL;
}
Else {
Info-> prior-> next = Info-> next;
If (info! = Last)
Info-> next-> prior = Info-> prior;
Else last = Info-> prior;
}
Free (Info );
Patchup (linenum + 1,-1 );
}
}
/* Search for a line of text */
Struct line * Find (INT linenum)
{
Struct line * Info;
Info = start;
While (Info ){
If (linenum = Info-> num) Return (Info );
Info = Info-> next;
}
Return (null );
}
/* Display text */
Void list ()
{
Struct line * Info;
Info = start;
While (Info ){
Printf ("% d: % s/n", info-> num, info-> text );
Info = Info-> next;
}
Printf ("/n ");
}
/* Save the file */
Void save (char * fname)
{
Struct line * Info;
Char * P;
File * FP;
If (FP = fopen ("text.txt", "W") = NULL ){
Printf ("/T file cannot be opened! /N ");
Exit (0 );
}
Printf ("/t saving file:/N ");
Info = start;
While (Info ){
P = Info-> text;
While (* P) putc (* P ++, FP );
// Putc ('/R', FP );
Putc ('/N', FP );
Info = Info-> next;
}
Fclose (FP );
}
/* Load the file */
Void load (char * fname)
{
Struct line * info, * temp;
Char * P;
File * FP;
// Int T, size, INCT;
Int size, INCT;
If (FP = fopen ("text.txt", "R") = NULL ){
Printf ("/T file cannot be opened! /N ");
Exit (0 );
}
While (start ){
Temp = start;
Start = start-> next;
Free (temp );
}
Printf ("/n/T dress file! /N ");
Size = sizeof (struct line );
Start = (struct line *) malloc (size );
If (! Start ){
Printf ("/n/T memory used up! ");
Return;
}
Info = start;
P = Info-> text;
INCT = 1;
While (* P = GETC (FP ))! = EOF ){
P ++;
While (* P = GETC (FP ))! = '/N') P ++;
// GETC (FP);/* discard '/N '*/
* P = '/0 ';
Info-> num = INCT ++;
Info-> next = (struct line *) malloc (size );
If (! Info-> next ){
Printf ("/n/T memory used up! ");
Return;
}
Info-> prior = temp;
Temp = Info;
Info = Info-> next;
P = Info-> text;
}
Temp-> next = NULL;
Last = temp;
Free (Info );
Start-> prior = NULL;
Fclose (FP );
}