Powered By Blogger

Friday, April 27, 2012

Program : 17


PROGRAM - 17

a)  Write a program which copies one text file to another.
b)  Write a program to reverse the first N characters of a given text file.

Note: The file name and N are specified through command line.

Program which copies one text file to another file:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<process.h>
void main(int argc,char *argv[])
{
 FILE *fs,*ft;
 char ch;
 clrscr();
 if(argc!=3)
 {
  puts("Invalid number of Arguments:");
  exit(0);
 }
 fs=fopen(argv[1],"r");
  if(fs==NULL)
  {
   puts("Source file cannot be opened");
   exit(0);
  }
  ft=fopen(argv[2],"w");
   if(ft==NULL)
   {
    puts("Target file cannot be opened");
    fclose(fs);
    exit(0);
   }
  while(1)
  {
   ch=fgetc(fs);
    if(ch==EOF)
     break;
    else
     fputc(ch,ft);
  }
  printf("File Copied Successfully.....");
  fclose(fs);
  fclose(ft);
  getch();
}
/*
Sample out:keep all the files including .exe in a same folder copy the source file(Which contain some data) and target file(Which is an empty file) in the same folder and then compile and in cmd prompt set the path where all these files are available give input as
 <filename> <sourcefile> <targetfile>
*/

Program to reverse the first N Charecters of a given text file:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
 char a[15],s[20],n;
 int k,j=0,i,len;
 FILE *fp;
 if(argc!=3)
 {
  puts("Invalid no.of argv");
  exit(0);
 }
 fp=fopen(argv[1],"r");
  if(fp==NULL)
  {
   puts("File Cannot be opened");
   exit(0);
  }
  k=*argv[2]-48;
  n=fread(a,1,k,fp);
  a[n]='\0';
  len=strlen(a);
  for(i=len-1;i>=0;i--)
  {
   s[j]=a[i];
   printf("%c",s[j]);
   j=j+1;
  }
  s[j+1]='\0';
  getch();
}

/* Output: Copy all the files in the same folder and for this only source file is enough, the source file should contain same text, then in CMD set the path where all these files are copied and give  <filename> <sourcefile> <no of charecters(in number)> */

Program : 16


PROGRAM - 16

a)  Write a program to accept the element of the structure as: Employee-name and Basic pay.
Display the same structure along with the DA, CCA and Gross Salary for 5 employees.
Note: DA=51% of basic pay, CCA= Rs.100. Consolidated.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
     int i;
     struct emp
     {
        char name[20];
        int basic;
        int cca;
        int da;
        float gross;
     };
     struct emp e[5];
     clrscr();
     printf("\n enter emp name and basic:");
     for(i=0;i<5;i++)
     {
       scanf("%s %d",e[i].name,&e[i].basic);
       e[i].da=e[i].basic*0.51;
       e[i].cca=100;
       e[i].gross=e[i].basic+e[i].da+e[i].cca;
     }
     for(i=0;i<5;i++)
     {
       printf("\nName=%s\nBasics=%d\nCca=%d\nda=%d \nGross=%f",e[i].name,                          e[i].basic,e[i].cca,e[i].da,e[i].gross);
     }
     getch();
}
/*
INPUT:
 enter emp name and basic:
MICHAEL
1000
OUTPUT:
Name=MICHAEL
Basics=1000
Cca=100
da=510
Gross=1610.000000
*/


b)  Define a structure to store employee’s data with the following specifications: Employee-number, Employee-Name, Basic Pay, and Date of joining.

  •   Write a function to store 10 employee details.
  •   Write a function to implement the following rules while revising the basic pay.
i) If basic pay<= Rs. 5000 then increase it by 15%.
ii) If basic pay > Rs. 5000 and <=Rs. 25000 then      it    increase by 10%.
iii) If basic pay > Rs. 25000 then there is no change in basic pay.
  Write a function to print the details of employee who have completed 20 years of service from the date of joining.
Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
struct emp
{
     int eid;
     char ename[20];
     int basic;
     int day;
     int month;
     int year;
};
struct emp e[10];
int i;
void main()
{
     void empdetail();
     void basicpay();
     void display();
     clrscr();
     empdetail();
     basicpay();
     display();
     getch();
}
void empdetail()
{
     for(i=0;i<2;i++)
     {
        printf("\n enter id,name,basic,doj,enter day,month,year seperatly:");
        scanf("%d %s %d %d %d %d",&e[i].eid,&e[i].ename,&e[i].basic,&e[i].day,&e[i].month,&e[i].year);
     }
}
void basicpay()
{
     for(i=0;i<2;i++)
     {
        if(e[i].basic<=5000)
        e[i].basic=e[i].basic+((15*e[i].basic)/100);
        else if(e[i].basic>5000&&e[i].basic<=25000)
        e[i].basic=e[i].basic+((10*e[i].basic)/100);
        else
        e[i].basic=e[i].basic;
     }
}
void display()
{
     int year=2012;
     for(i=0;i<2;i++)
     {
         if((year-(e[i].year))>=20)
         printf("\nempid:%d\nname:%s\nsalary:%d\nday:%d\nmonth:%d\nyear:%d",
               e[i].eid,e[i].ename,e[i].basic,e[i].day,e[i].month,e[i].year);
     }
}
/*
INPUT:
enter id,name,basic,enter day,month,year seperatly of doj:
11       bittu       30000      13   04   1986

OUTPUT:

emp id:11
name:bittu
salary:30000
day:13
month:4
year:1986
*/

Program : 15


PROGRAM - 15

Write a program that uses the functions to perform the following:
a)  Reading a complex number.
b)  Writing a Complex number.
c)  Addition of two complex numbers.
d)  Multiplication of two Complex numbers.

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void arth(int);
struct comp
{
     double rp;
     double ip;
};
void main()
{
     int op;
     clrscr();
     printf("\nMAIN MENU");
     printf("select \n 1. ADDTION\n 2. MULTIPLICATION\n
                                        0. EXIT\n");
     scanf("%d",&op);
     switch(op)
     {
          case 0:exit(0);
          case 1:
          case 2:
          arth(op);
          break;
          default:
          printf("\n invalid statement");
          exit(0);
     }
}
void arth(int op)
{
     struct comp w1,w2,w;
     printf("enter the first complex number:");
     scanf("%lf",&w1.rp);
     scanf("%lf",&w1.ip);
     printf("enter the second complex number:");
     scanf("%lf",&w2.rp);
     scanf("%lf",&w2.ip);
     switch(op)
     {
        case 1:
        w.rp=w1.rp+w2.rp;
        w.ip=w1.ip+w2.ip;
        break;
        case 2:
        w.rp=(w1.rp*w2.rp)-(w1.ip*w2.ip);
        w.ip=(w1.rp*w2.ip)+(w1.ip*w2.rp);
        break;
     }
     if(w.ip>0)
     printf("answer=%lf+i%lf",w.rp,w.ip);
     else
     printf("answer=%lf-i%lf",w.rp,w.ip);
     getch();
}
/*
IPNPUT:MAIN MENUSELECT
 1. ADDTION
 2. MULTIPLICATION
 0. EXIT
your option is:2
enter the first complex number:2     3
enter the second complex number:4    5
answer=-7.000000+i22.000000
MAIN MENUselect
 1. ADDTION
 2. MULTIPLICATION
 0. EXIT
 1
enter the first complex number:3     4
enter the second complex number:5    6
answer=8.000000+i10.000000
*/