Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Showing posts with label Discrete Structure. Show all posts
Showing posts with label Discrete Structure. Show all posts

Sunday, 23 December 2012

[Java] Implement a Program to Perform Various Operations on Text Files

Implement a program to accomplish the following task:
  • Create a text file and store data in it.
  • Count number of lines and words in the file.
  • Copy contents of one file to another file.

import java.io.*;
class Copycon
{
int count1,count,len;
void getdata()
{
try
{
RandomAccessFile ff=new RandomAccessFile ("student.dat","rw");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String choice=new String();
String s="";
int countc=count1=len=0;

do{
countc++;
count1++;
System.out.print("Enter the String :");
s=in.readLine();
ff.writeUTF(s);
len=s.length();
s=s.trim();

for(int i=0;i<len;i++)
{
if(s.charAt(i)==' ')
countc++;
}
System.out.print("Insert next line(Y/N):");
choice=in.readLine();
}
while(choice.compareTo("Y")==0||choice.compareTo("y")==0);
System.out.println("Number of Lines:"+count1);
System.out.println("Number of words:"+countc);
}
catch(Exception e)
{   }
}
public static void main(String args[])
{
Copycon c=new Copycon();
c.getdata();
File inFile=new File("input.txt");
File outFile=new File("output.txt");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(inFile);
outs=new FileWriter(outFile);
int ch;
while((ch=ins.read())!=-1);
{
outs.write(ch);
}
}
catch(Exception e)
{
System.out.println(e);
System.out.println(-1);
}
finally
{
try
{
ins.close();
outs.close();
}
catch(IOException e)
{}
}

[Java] program to calculate and display area of Square and Rectangle using function overloading

Define a class Shape having overloading static member function area() to calculate and display area of Square and Rectangle.
class Shape
{
static int l,b,S,R;
static void area(int m)
{                                                                                                  //For Square
l=m;     
}

static void area(int x,int y)
{
l=x;                                                                                            //For Rectangle
b=y;
}

void cal()

{
S=l*l;
R=l*b;
}

void display()
{
System.out.println("Area of Square="+S);                       //For Display
System.out.println("Area of Rectangle="+R);
}

public static void main(String args[])
{
Shape s1=new Shape();                                                       //Object s1 of class Shape is created.
area(10);
area(10,2);                                                                             // area() is called .
s1.cal();
s1.display();
}
}

Tuesday, 18 December 2012

[PL 1]A Discrete Structure Program Using C Programming To Perform Various Set Operation


/*Program for various operations on set.
Operations covered :
1) Create() : for creating a new set with initial members
of the set
2) print() : diaplays all members of the set
3) Union() : finds union of two sets, set1[] and set2[] and
stores the result in set3[]
4) intersection() : finds intersection of two sets, set1[] and set2[]
and stores the result in set3[]
5) difference() :finds difference of two sets, set1[] and set2[]
and stores the result in set3[]
6) member() :function returns 1 or 0 ,depending onwhether the
element x belongs or not to a set.

7) symmdiff() : Finds Symmetric difference of two sets
Representation of a set
-----------------------
A set is representrd using an array of integers.
It may be declared as:
int set[30];
set[0] - gives number of elements in a set.
set[1] to set[29] are for storing set members.

Example :
A set,[2,11,3,5 6],when represebted will appear as:
[5][2][3][5][6][11][ ][ ][ ] 0 1 2 3 4 5 6 7 8 */

#define MAX 30
#include"STDIO.H"
#include"CONIO.H"
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set3[]);
void difference(int set1[],int set2[],int set3[]);
void symmdiff(int set1[],int set2[],int set3[]);
int member(int set[],int x);


void main()
{
int set1[MAX],set2[MAX],set3[MAX];
int x,op;
clrscr();
flushall();
set1[0]=set2[0]=set3[0]=0;
do
{
printf("\n1)Create\n2)Print\n3)Union\n4)Intersection\n5)Difference");
printf("\n6Symmetrec Difference \n7)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nCreting First Set*******");
create(set1);
printf("\nCreating Second Set*****");
create(set2);
break;
case 2: printf("\nFirst Set :\n");
print(set1);
printf("\n\nSecond Set :\n");
print(set2);
printf("\n\nThird Set :\n");
print(set3);
break;
case 3: Union(set1,set2,set3);print(set3);break;
case 4: intersection(set1,set2,set3);print(set3);break;
case 5: difference(set1,set2,set3);print(set3);break;
case 6: symmdiff(set1,set2,set3);print(set3);break;
}
printf("\npress a key............");
getch();
}while(op!=7);
}
/*creates set[] with initial elements*/
void create(int set[])
{ int n,i,x;
set[0]=0;/*make it a null set*/
printf("\n No. of elements in the set:");
scanf("%d",&n);
printf("\n enter set elements :");
for(i=1;i scanf("%d",&set[i]);
set[0]=n; //Number of elements.

}
void print(int set[])
{ int i,n;
n=set[0];/* number of elements in the set */
printf("\Members of the set :-->");
for(i=1;i printf("%d ",set[i]);
}
/* union of set1[] and set2[] is stored in set3[]*/
void Union(int set1[],int set2[],int set3[])
{ int i,n;
/* copy set1[] to set3[]*/
set3[0]=0;/*make set3[] a null set */
n=set1[0];/* number of elements in the set*/
//Union of set1,set2= set1 + (set2-set1)
for(i=0;i set3[i]=set1[i];

n=set2[0];
for(i=1;i if(!member(set3,set2[i]))
set3[++set3[0]]=set2[i]; // insert and increment no. of elements
}
/*function returns 1 or 0 depending on whether x belongs
to set[] or not */
int member(int set[],int x)
{ int i,n;
n=set[0]; /* number of elements in the set*/
for(i=1;i if(x==set[i])
return(1);

return(0);
}
/*intersection of set1[] and set2[] is stored in set3[]*/
void intersection(int set1[],int set2[],int set3[])
{
int i,n;
set3[0]=0; /* make a NULL set*/
n=set1[0];/* number of elements in the set*/
for(i=1;i if(member(set2,set1[i])) /* all common elements are inserted in set3[]*/
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
}
/*difference of set1[] and set2[] is stored in set3[]*/
void difference(int set1[],int set2[],int set3[])
{ int i,n;
n=set1[0];/* number of elements in the set*/
set3[0]=0;/*make it a null set*/
for(i=1;i if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
}
void symmdiff(int set1[],int set2[],int set3[])
{ int i,n;
n=set1[0];/* number of elements in the set*/
set3[0]=0;/*make it a null set*/
//Calculate set1-set2
for(i=1;i if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
//Calculate set2-set1
n=set2[0];
for(i=1;i if(!member(set1,set2[i]))
set3[++set3[0]]=set2[i]; // insert and increment no. of elements

}

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine