I made a program which is like a CUI notepad. There you can insert a character, delete a line and replace a character.
the whole code is like this:
/*
.............. Character User Interface Notepad (CUI Notepad).............
this is a program which uses the concepts of the pointers, data file handling, classes.
this program is made by sanmveg saini and no rights are reserved
*/
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct node{
char c; node * next, *prev;
};
class chain{
node *start, *last;
public:
int length;
chain(){
start=last=nullptr; length=0;
};// end of the constructor
chain(chain *cha){
length=cha->length;
this->start= (cha)->start;
this->last = (cha)->last;
};
void _insert (char, int);
void _remove(int, int);
void _replace(int, char);
void show();
node *firstNode();
~chain();
};
chain fragmentor(char[]);
void writer(char[], chain);
node*create(char);
// now defining all the functions
void chain::_insert(char c, int where = -1){
if(c == '\0'){}else{length++;
node*newptr = create(c);
if(start==nullptr){start=last=newptr;}else{
if(where==-1){ // if someone wants to insert at the last
last->next=newptr;newptr->prev=last;last=newptr;
}else{
node*bef, *aft, *t; t=start;
for(int i=1; i<=where;i++){bef = t; t=t->next; aft=t;};
newptr->next=aft;
aft->prev=newptr;
newptr->prev=bef;
bef->next=newptr;
};// this is the end of the else block
};// this is the end of the outer else block
};
}; // this is the end of the insert function
void chain::_remove(int from, int to=0){
if(to<from){
cout<<"\n\t.....[1] aborting......";
return;
};
node *bef, *aft, *curr, *t; t=start;
for(int i=1; i<=from;i++){
t=t->next;
if(i==from){
bef=t->prev;aft=t->next;curr=t;
};
};// this is the end of the for loop
if(to==0){delete curr;}else{
for(int j= from;j<=to;j++){
delete curr; t=t->next;curr=t;
};// end of the j-for loop
aft=curr; curr=nullptr;
delete curr;
};// end of the last else statement
length--;
bef->next=aft;
aft->prev=bef;
};// this is the end of the remove function
void chain::show(){
node *intermediate = start;
cout<<"\n\t";
if(length == 1){cout<<intermediate->c;}else{
while(intermediate->next!=nullptr){
cout<<intermediate->c;
intermediate=intermediate->next;
if(intermediate->next==nullptr){
cout<<intermediate->c;
};
};// end of the while loop
};// this is the end of the else block
};// this is the end of the show function
chain::~chain(){
node *intermediate = start;
if(length == 1){delete intermediate;}else{
while(intermediate->next != nullptr){
intermediate = start->next;
delete start;
start = intermediate;
};// this is the end of the while loop
delete start;
};// this is the end of the else block
};// this is the end of the chain destructor
void chain::_replace(int pos, char ch){
_remove(pos); // first remove the character which is at that place
_insert(ch,pos); // now add the new character to that place
}; // this is the end of the _replace function
node *chain::firstNode(){
return start;
};
chain fragmentor(char arr[]){
char ch; chain cha;
ifstream ifs(arr, ios::in);
if(!ifs){cout<<"\n.....[2] File couldn't be opened......";}else{
while( !ifs.eof() ){
ifs.get(ch);
cha._insert(ch);
};// end of the file
ifs.close();
};
return cha;
};// this is the end of the fragmentor function
void writer(char arr[], chain cha){
ofstream of(arr, ios::out);
node *start = cha.firstNode();
if(start==nullptr){cout<<"\n....null";};
while(start != nullptr) {
of.put(start->c);
start=start->next;
};// this is the end of the while statement
of.close();
};// this is the end of the writer function
node *create(char ch){
node *ptr= new node;
ptr->c =ch;
ptr->next=nullptr;
ptr->prev=nullptr;
return ptr;
};// this is the end of the create function
// this is the end of the defining of the function and
// from here starts the main function
int main(){
char arr[31], ch; int op, pos;
cout<<"\n\t Enter the file name with extension(eg. abc.txt): ";
gets(arr);
if(ifstream(arr)){}else{ofstream o(arr, ios::out);o.close();};
chain f_chain(fragmentor(arr));
f_chain.show();
writer(arr,f_chain);
start:
cout<<"\n\n\t 1. insert 2. delete 3. replace 4.exit : "; cin>>op;
switch(op){
case 1: cout<<"\n\tEnter the character you want to insert: "; cin>>ch;
cout<<"\n\tEnter the position (-1 to insert at last): "; cin>>pos;
f_chain._insert(ch, pos);
break;
case 2: int to, from;
cout<<"\n\tEnter position where you want to delete: "; cin>>from;
cout<<"\n\tEnter upto which you want to delete(0 to delete one character): ";cin>>to;
f_chain._remove(from, to);
break;
case 3: cout<<"\n\tEnter the position of the element which has to be replaced: ";pos=0; cin>>pos;
cout<<"\n\treplace character by: ";cin>>ch;
f_chain._replace(pos, ch);
break;
case 4: writer(arr, f_chain);
exit(0);
};// this is the end of the switch statement
f_chain.show();
goto start;
};
// this is the end of the program which is intended to make a character user interface notepad using the pointers, data file handler and classes
// this program is made by sanmveg saini and no rights are reserved.
the problem is coming in insert function and delete function.
what my insert function does
-
if the passed character is null then don't insert other wise continue
-
create a node of a chain
-
now find the nodes which will be before and after this new node
-
after finding the nodes connect the new node, the node one before this node and the node after this new node.
no suppose i have a file, named abc.txt
, containing text this is a tet
. now i want to add x
at the position 12, but i don't know why my console crashes. It says that CUI_notepad.exe
has stop working and the same thing is with remove function and because the replace function is using insert and replace function hence it is also showing the same error.
so what is the mistake i have done?
thanks.
Aucun commentaire:
Enregistrer un commentaire