google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.

Friday, January 18, 2019

Write a command line program for line editor of practcal slip



/*
    slip-7
1. Write a command line program for line editor. The file to be edited is taken
 as command line argument, an empty  file is opened for editing if no argument
 is supplied.  It should display '$' prompt to accept the line editing commands.
Implement the following commands.
i)    a                 - to append
ii)    i  n                 - to insert after nth line
iii)    c    n1   n2              - to copy line n1 at  n2 position
iv)    c    n1, n2    n3   - to copy range of line at n3        [30 marks]

*/




#include<stdio.h>
#include<conio.h>
#include<string.h>
#define newnode (struct node *)malloc(sizeof(struct node))

struct node
{
    char line[80];
    struct node *next,*prev;
}*head=NULL;

int  length();
void print();
void append(char *line);
void load(char *);
void add();
void insert(char *,int);
void copy(int,int,int,int);





void load(char *fname)
{
    FILE *fp;
    char line[80];
    fp=fopen(fname,"r");
    if(fp==NULL)
    {
        printf("\nFile cannot boe opened..");
        getch();
        exit(0);
    }
    while(!feof(fp))
    {
        fgets(line,80,fp);
        append(line);
    }

}

void append(char *line)
{
    struct node *f,*s;
    f=newnode;
    strcpy(f->line,line);
    f->next=NULL;
    f->prev=NULL;
    if(head==NULL)
        head=f;
    else
    {
        for(s=head;s->next!=NULL;s=s->next);
        s->next=f;
        f->prev=s;
        s=s->next;
    }
}

void print()
{
    struct node *f;
    int i=1;
    for(f=head;f!=NULL;f=f->next)
        printf("%d %s",i++,f->line);
}

int length()
{
    int cnt=0;
    struct node *s;
    for(s=head;s!=NULL;s=s->next)
        cnt++;
    return cnt;
}

void insert(char *line,int from)
{
    struct node *f,*s;
    int len,i;
    if(from-1>length())
    {
        printf("\nError..");
        return;
    }

    f=newnode;
    f->next=NULL;
    f->prev=NULL;
    strcpy(f->line,line);

    if(from==1)
    {
        f->next=head;
        head->prev=f;
        head=head->prev;
        return;
    }
    else
    {
        s=head;
        for(i=1;i<(from-1);i++)
            s=s->next;
        f->next=s->next;
        s->next->prev=f;
        s->next=f;
        f->prev=s;
    }
}






main(int argc,char *argv[])
{
    int ch;
    int i,notok,from,to,range;
    char cmd[80],tok[4],line[80];
    clrscr();
    for(i=0;i<4;i++)
        tok[i]=(char )malloc(20);

    if(argc==2)
    {
        head=NULL;
        load(argv[1]);
    }
    while(1)
    {
        printf("\n?");
        flushall();
        gets(cmd);
        notok=sscanf(cmd,"%s%s%s%s",tok[0],tok[1],tok[2],tok[3]);
        switch(cmd[0])
        {
            case 'q':exit(1);

            case 'p':print();
                   break;
            case 'i':from=atoi(tok[1]);
                    printf("Input Line:");
                    flushall();
                    fgets(line,80,stdin);
                    insert(line,from);
                    break;

            case 'c':from=atoi(tok[1]);
                   to=atoi(tok[2]);
                   range=atoi(tok[3]);
                   copy(notok,from,to,range);
                   break;



        }
    }
}

void copy(int notok,int from,int to,int range)
{
    int len,i;
    struct node *s,*f,*temp;
    char line[80];
    len=length();
    if(from>len)
    {
        printf("\nError...");
        return;
    }

    if(notok==3)
    {
        s=head;
        for(i=0;i<(from-1);i++)
            s=s->next;
        insert(s->line,to);
    }

    if(notok==4)
    {
        if(to>len || range-1>len)
        {
            printf("\nError,,,");
            return;
        }
        s=head;
        for(i=0;i<(from-1);i++)
            s=s->next;
        for(i=from;i<=to && s!=NULL;i++)
        {
            insert(s->line,range++);
            s=s->next;
        }
    }
}

RoundRobin Program of practical slip



 /* Implementation of RoundRobin method for CPU scheduling using Circular LL */
 


#include<stdlib.h>
#define newnode (struct node *)malloc(sizeof(struct node))
struct node
{ int jobno,time;
  struct node *next;
}*JOBQ=NULL;


void erase()
{ struct node *S,*T;
  if(JOBQ!=NULL)
  { for(T=JOBQ;T->next!=JOBQ; T=T->next) ;  // go to last node
    T->next=NULL;

    while(JOBQ!=NULL)  { S=JOBQ;  JOBQ=JOBQ->next;  free(S);  }
  }
}

void CreateJOBQ()
{ struct node *S;   int i,n;
  printf("\nHow many jobs?"); scanf("%d",&n);


  if(JOBQ!=NULL) erase(JOBQ);
  JOBQ=newnode;
  printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&JOBQ->jobno,&JOBQ->time);

  S=JOBQ;
  for(i=2;i<=n;i++)
  { S->next=newnode;
    S=S->next;
    printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&S->jobno,&S->time);
  }
  S->next=JOBQ;
}

void DisplayJOBQ()
{struct node *T=JOBQ;
  if(JOBQ==NULL) printf("\nJOB Q is Empty!");
  else
  { printf("\nJOB  Rem. Time");
    do{ printf("\n J%d : %d ",T->jobno,T->time);
    T=T->next;
      }while(T!=JOBQ);
  }
}

void ExecuteJOBQ()
{ struct node *T=JOBQ,*S=NULL,*t1;  int flag,timeslot=1,time=0; char choice;
  printf("\nEnter Time Slot (in sec.) "); scanf("%d",&timeslot);
  printf("\nclock CPU   JOBQ ");
  while(JOBQ!=NULL)
  {
    do{ flag=0;
    if(T->time<=timeslot)  // job remaining time < time slot
    { printf("\n%3d  J%d(%d) |--",time,T->jobno,T->time); // cpu allocated
      for(t1=T->next; t1!=T; t1=t1->next) printf(" J%d(%d) ",t1->jobno,t1->time);  // print JOBQ
      time+=T->time; // increment clock
      T->time=0; // job finished, delete from JOBQ
      if(T==JOBQ) // 1st job
      { if(JOBQ->next==JOBQ) // only 1 job in Q
        { JOBQ=NULL; free(T); T=NULL; } // JOBQ became empty
        else  // more jobs in Q
        {  for(t1=JOBQ;t1->next!=JOBQ; t1=t1->next) ;  // go to last node
           JOBQ=JOBQ->next;   t1->next=JOBQ;
           free(T);     T=JOBQ; flag=1;
        }
      }
      else // job other than 1st position
      {   S->next=T->next; free(T);  T=S->next;
      }

    }
    else  // job is executed for given time slot
    {  printf("\n%3d  J%d(%d) |--",time,T->jobno,T->time); // cpu allocated
       for(t1=T->next; t1!=T; t1=t1->next) printf(" J%d(%d) ",t1->jobno,t1->time); // print JOBQ

       T->time-=timeslot;
       time+=timeslot;
       S=T; T=T->next;  // go to next job
    }

    printf(" --|  Add New Job(y/n)? ");  choice=getche();
    if(choice=='y' || choice=='Y')
    { struct node *S1;
      if(JOBQ==NULL) // JOBQ empty
      { JOBQ=newnode; JOBQ->next=JOBQ;    // 1st job in Q
        printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&JOBQ->jobno,&JOBQ->time);
        T=JOBQ; flag=1;
      }
      else
      { for(S1=S;S1->next!=S; S1=S1->next) ;  // go to last node of JOBQ
        S1->next=newnode; S1=S1->next; S1->next=S;    // add new job at the end of JOBQ
        printf("\nEnter JOB No. & execution time in sec. "); scanf("%d %d",&S1->jobno,&S1->time);
      }
    }
      }while(T!=JOBQ || flag);
  }
  printf("\n%3d  -idle-",time);
}

char menu()
{ char choice='a';
  clrscr();
  printf("----- Round Robin ------");
  printf("\nC: Create New JOB Queue");
  printf("\nD: Display JOB Queue");
  printf("\nE: Execute JOB Queue");
  printf("\nX: exit");
  printf("\nEnter your choice : ");
  choice=getche();
  return toupper(choice);
}

void main()
{
  while(1)
  { switch(menu())
    { case 'X' : erase(); exit(0);  // release memory allocated to JOBQ
      case 'C' : CreateJOBQ();  break;
      case 'D' : DisplayJOBQ(); break;
      case 'E' : ExecuteJOBQ(); break;
      default  : printf("\7 Invalid Choice!");
    }
    printf("\npress any key to continue...");
    getch();
  }

}

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...