Powered By Blogger

Tuesday, May 8, 2012

Unix Commands...

  • Clear - Used to clear the screen
  • ls- Used to list all the files and folders
  • mkdir- to create a new directory (folder)
  • cat><File name> - used to create a new file
  • cat <File name>- To view file contents
  • cd <Folder name>- to Enter into the directory(folder) created
  • cd <Space Bar Key> - To come out of the directory (folder)
  • rmdir- To remove the directory(folder)
  • who- tell who all are connected to the server
  • date- displays the date and time (of the system)
  • cal- displays the present year calendar of all the months
  • cal <month(in number)> <Year(in number)>- displays the specified month and year
  • pwd- This command is provided to know the current working directory.
  • cp <Old File> <New File> - To copy the contents of old file to a new file
  • mv <Old File> <New File>- to move the data of old file into new file
  • wc <File Name>- To count the number of words lines and characters in a given file
  • bc- Calculator  to come out of it Press CTRL+d
  • who am i- Identify the user and lists the user name, Terminal line, the date and time of login.
  • Semi column(;) - to run multiple commands at a time.
  • vi editor- visual editor used to write C Programs.
  • sort <File Name>- it will sort the file according to the alphabetic order.
  • rm <File Name>- To delete the file which is already created.

How to run a C file in UNIX???

First open a file using VI editor (vi <File name.c>) after entering into it then press the key 'i' so that we can insert the data into the file which we created. after writing the program press escape key and the ':wq' to save and quit the program. After that use the command 'gcc <File Name.c>' to compile u r C program. If no errors then just give './a.out' to see the output.

 Sample Program for addition of 2 numbers:
$vi add.c
press 'i' key to type the program.
#include<stdio.h>
int main()
{
 int a,b,c;
 printf("\n Enter the values of A and B:");
 scanf("%d %d",&a,&b);
 c=a+b;
 printf("The Result is %d",c);
 return c;
}
 press the 'escape key' and ':wq'
after that compile using gcc or cc.
$gcc add.c
To view the output.
$./a.out  
finally u r result will be displayed if u don't have any errors.

Friday, April 27, 2012

Program : 19


PROGRAM - 19
Write a program to implement stack operations using:
  •  Arrays.
  • Pointers.
Program to implement stack operations using arrays:

#include<stdio.h>
#include<conio.h>
#define max 5
static stack(max);
int top;
void push(int t)
{
   if(top>=max)
   {
     printf("stack is full");
     return;
   }
   stack[top++]=t;
}
int pop()
{
   top--;
   if(top<0)
   {
   printf("stack is empty");
   }
   return(stack[top]);
}
main()
{
 int condition,element,ret;
 clrscr();
 while(1)
 {
      printf("enter-1:push\n");
      printf("enter-2:pop\n");
      printf("enter-3:exit\n");
      scanf("%d",&condition);
      switch(condition)
      {
      case 1:printf("enter the element to push");
          scanf("%d",&elements);
          push(element);
          break;
      case 2:ret=pop();
          printf("popped element is %d",ret);
          break;
      case 3:return 0;
      default:printf("\n choose & give correct option");
      }
 }
 getch();
 return();
}


Program to implement stack operations using pointers:

#include<stdio.h>
#include<conio.h>
struct stack
{
       int data;
       struct stack *next;
};
typedef struct stack node;
node *start=NULL;
node *top=NULL;
node *getnode();
int menu();
void push(node*);
void pop();
void display();
void main()
{
      node *newnode;
      int ch;
      clrscr();
      do
      {
        ch=menu();
        switch(ch)
        {
           case 1:{
                  newnode=getnode();
                  push(newnode);
                  }
                  break;
           case 2:pop();
                  break;
           case 3:display();
                  break;
           case 4:exit(1);
           default:printf("invalid choice");
        }
      }while(1);
}
int menu()
{
     int ch;
     printf("menu\n");
     printf("1.push\n");
     printf("2.pop\n");
     printf("3.display\n");
     printf("4.exit\n");
     printf("enter your choice");
     scanf("%d",&ch);
     return ch;
}
node *getnode()
{
     node *newnode;
     newnode=(node*)malloc(sizeof(node));
     printf("enter the data");
     scanf("%d",&newnode->data);
     newnode->next=NULL;
     return(newnode);
}
void push(node *newnode)
{
     node *temp;
     if(newnode==NULL)
     {
       printf("overflow");
     }
     else
     {
       if(start==NULL)
       {
          start=newnode;
          top=newnode;
       }
       else
       {
           temp=start;
           while(temp->next!=NULL)
           {
             temp=temp->next;
           }
           temp->next=newnode;
           top=newnode;
       }
     }
}
void pop()
{
     node *temp;
     if(start==NULL)
     {
      printf("underflow");
     }
     else
     {
     if(start->next==NULL)
     {
           printf("popped element is %d",top->data);
           start=NULL;
           free(top);
           top=NULL;
     }
     else
     {
           temp=start;
           while(temp->next!=top)
           {
                  temp=temp->next;
           }
           temp->next=NULL;
           printf("popped element is %d",top->data);
           free(top);
           top=temp;
     }
     }
}
void display()
{
     node *temp;
     temp=start;
     while(temp!=NULL)
     {
          printf("%d",temp->data);
          temp=temp->next;
     }
}

Program : 18


PROGRAM - 18

Write a program to print the output in the following format by giving the customer_ID as an input.

Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
     FILE *fp;
     char line[200],ch,id[10],*f,fname[20];
     int i=0;
     clrscr();
     printf("enter source file name:");
     scanf("%s",&fname);
     fp=fopen(fname,"r");
     if(fp==NULL)
     {
           printf("file can't be opened");
           exit(0);
     }
     while((ch=fgetc(fp))!=EOF)
     {
           line[i]=ch;
           i++;
     }
     line[i]='\0';
     printf("content in file is:\n");
     printf("................................");
     printf("\nid name price");
     printf("\n...............................\n");
     printf("%s",line);
     printf("\nenter id:");
     scanf("%s",&id);
     f=strstr(line,id);
     i=(f-line);
     if(i<=strlen(line))
     {
          printf("...........\nid name price\n............\n");
           for( ;line[i]!='\n'&&line[i]!='\0';i++)
           {
                printf("%c",line[i]);
           }
     }
     else
     printf("invalid id");
     getch();
     fclose(fp);
}
/*
INPUT:enter source file name:item.txt
content in file is:
.... .......  ......
id    name   price
.... .......  ......
1.   c01     10
2.   c02     50
3.   c03     20
4.   c04     30
OUTPUT:
enter id:c02
..   .....   ....
id   item    price
..   .....   ....
c02    i2     50
*/