bhaincodedotcom.files.wordpress.com€¦ · web viewwap to create a pointer to a pointer and...

33
INDEX S.No . Practical Name Page No. Remarks 1. WAP to create a pointer using it display the contents of variable and memory address of variable. 1 2. WAP to create a pointer to a pointer and display the contents of that pointer and memory address of that pointer. 2 3. WAP to create a linked list. 3 4. Introduction to DOS Editor 5 5. Introduction to VI Editor 11 6. Introduction to File Handling 15 7. Write a program for reading and writing a file. 16 8. Write a program for copying contents from one file to another file. 18 9. Write a program to count characters, vowels ,blank 20 1

Upload: tranthu

Post on 12-Apr-2019

235 views

Category:

Documents


0 download

TRANSCRIPT

INDEX

S.No. Practical Name Page No. Remarks

1. WAP to create a pointer using it display

the contents of variable and memory

address of variable.

1

2. WAP to create a pointer to a pointer and

display the contents of that pointer and

memory address of that pointer.

2

3. WAP to create a linked list. 3

4. Introduction to DOS Editor 5

5. Introduction to VI Editor 11

6. Introduction to File Handling 15

7. Write a program for reading and writing a

file.

16

8. Write a program for copying contents

from one file to another file.

18

9. Write a program to count characters,

vowels ,blank space and full stop from a

given file

20

10. Write a program to check balanced

parenthesis from given expression.

24

Que. WAP to create a pointer using it display the contents of variable and memory address of variable.

#include<iostream.h>#include<conio.h>void main(){ clrscr(); int n, *p;

1

cout<<"Enter any no... "; cin>>n; p=&n; cout<<"Contents of variable... "<<*p<<"\n"; cout<<"No. is stored at "<<p; getch();}

Output:

Que. WAP to create a pointer to a pointer and display the contents of that pointer and memory address of that pointer.

#include<iostream.h>#include<conio.h>void main(){ clrscr(); int n, *p, **q;

2

cout<<"Enter any no... "; cin>>n; p=&n; q=&p; cout<<"Contents of variable... "<<**q<<"\n"; cout<<"variable is stored at "<<*q<<"\n"; cout<<"No. is stored at "<<q; getch();}

Output:

Que. WAP to create a linked list.

#include<iostream.h>#include<conio.h>class linklist{ private: struct node {

3

int data; node *link; }*p; public: linklist(); void append(int num); void display();};//initilize the data memberslinklist::linklist(){ p=NULL;}//add a nodeat the end of the listvoid linklist::append(int num){ node*temp,*r; //if list is empty create first node if(p==NULL) { p=new node; p->data=num; p->link=NULL;}else{ //go to last node temp=p; while(temp->link!=NULL) temp=temp->link; //add node at the end r=new node; r->data=num; r->link=NULL; temp->link=r;}}

//display the content of the linklistvoid linklist::display(){ node *temp=p;//traverse list while(temp!=NULL) { cout<<temp->data<<"\t";

4

temp=temp->link; }}void main(){ clrscr(); int n,a[20]; linklist l; cout<<"Enter total no. of elements... "; cin>>n; for(int i=1; i<=n; i++) { cin>>a[i]; l.append(a[i]); } cout<<"\nElements in list are: "; l.display(); getch();}

Output:

Introduction to DOS Editor

DOS menu options

5

1. File

i. New

New command creates a new file.

ii. Open

Open command loads the existing files from the disk.

iii. Save

Save command saves the current file.

iv. Save As

Save As command saves current file with new name.

v. Close

Close command closes current file.

6

2. Edit

i. Cut

Cut command deletes selected text and copies it to buffer.

ii. Copy

Copy command copies selected text to buffer.

iii. Paste

Paste command inserts buffer contents at current location.

iv. Clear

7

Clear command deletes selected text without copying it to buffer.

3. Search

i. Find

Find command finds specified text.

ii. Repeat Last Find

Repeat Last Find command finds next occurrence of text specified in previous search.

iii. Replace

Replace command finds and changes specified text.

8

4. View

i. Split Window

Split Window command opens a second edit window.

ii. Size Window

Size Window command resizes the edit windows.

9

iii. Close Window

Close Window command closes the second edit window.

5. Options

i. Settings

Settings command changes editor settings.

ii. Colors

10

Colors command changes the editor screen colors.

Introduction to VI Editor

11

VI editor is an example of a screen editor. A screen editor is one where a portion of the file is displayed on the terminal screen, and the cursor can be moved around the screen to indicate where to make changes. Screen editor are also called display editors or visual editors. “Vi” editors are implemented in UNIX. “Vi” is a display-oriented, interactive text editor the result of the Vi commands can be immediately viewed on the screen.

How to open the vi editor?

Start Run cmd command prompt telnet server ip vi new file name

CREATING A FILE:-

There are various “vi” commands for performing various operations of editing on files. To be able to try each command of ‘vi’ editor a file is needed with some text. To create a file the following command has to be entered:

$ vi xyz

Here “xyz” is the name of the file you are creating. The filename is printed at the bottom of the screen and a tilde (~) is printed at the beginning of each line on the screen. The tilde is special character that “vi” uses to mark the end of the text in a file that already exists or in the case of a

12

new file to show that there is currently no text in the file. The tildes are simply marks that are used for user convenience; these are not part of the text in file. To enter text in the file type “a” (for append).When text has been entered in the file, press escape and exit the editor by pressing ZZ (in upper case).

INVOKING “Vi”:- The file created can now be used for editing as

13

$vi xyz

Now vi prints out either a screen full of text from or the entire contents of “xyz” followed by tilde (~) each remaining empty line, if “xyz” does not contain enough text to fill the screen. The cursor is position at the beginning of the first line of the file. The state of Vi can be determined by pressing escape or delete. If you press escape and “Vi” responds by ringing the bell, then “Vi” is in quiescent state (state in which “Vi” enters a do-nothing state and wait for a command). Otherwise, it is busy executing a command.

SCROLLING AND PAGING:-

14

The “(control) d” command scrolls down in the file, leaving several lines of continuity between the previous screen full of text and the new screen full of text.

The “(control) u” command scrolls up in the file, leaving several lines of continuity on the screen. More control is required over the scrolling process, the “(control) e” command exposes one more line at the bottom of the screen and the “(control) y” command exposes one more line at the top. There are two paging commands, “(control) f” and “(control) b” which move forward and backward one page in the respectively.

Introduction to File Handling

15

A File is the collection of related information normally representing programs; both source and object forms and data. Data may be numeric alphabetic or alphanumeric. A file can also be defined as a sequence of bits, bytes, lines or records whose meaning is defined by the programs operations such as create, open, read, and close are performed on files. Files are used to save time for data entry and to reduce its processing time also. These are also used for storage and subsequent retrieval of data. Files are used in testing and debugging of programs. Using files, data can be stored on many devices like floppy disks, hard disks, magnetic tapes, magnetic drum etc, which help in the permanent storage and distributed data processing. Data can easily access though files.

Stream Classes Used In File Handling

ofstream class:- ofstream class used for writing information to the file. The functions linked with this class are open (), put(), seekp() and write().

ifstream Class:- ifstream class used for reading information from a file. It contain open() with default input made get(), getline(), read(), seekg() and tellg().

fstream Class:- fstream class is used for reading and writing of data simultaneously. It contain open () with default input mode and also stores all the functions linked with istream and ostream.

filebuf:- filebuf sets the file buffer to read and write. It contain close() and open() functions as members.

fstream base:- It provides operations common to the file stream. It also contain open() and close() functions.

Que. Write a program for reading and writing a file.

16

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

void main()

{

clrscr();

ofstream out;

ifstream in;

char ch;

out.open("b.txt");

out<<"welcome,this is my first program";

out.close();

in.open("b.txt");

while(in)

{

in.get(ch);

cout<<ch;

}

in.close();

getch();

}

Output:

17

Que. Write a program for copying contents from one file to another file.

#include<iostream.h>

18

#include<conio.h>

#include<fstream.h>

void main()

{

ofstream out;

ifstream in;

char ch;

out.open("c.txt");

in.open("b.txt");

while(in)

{

in.get(ch);

out.put(ch);

}

out.close();

in.close();

cout<<"copied";

clrscr();

in.open(“c.txt”);

while(in)

{

in.get(ch);

cout<<ch;

}

in.close();

19

getch();

}

Output:

Que. Write a program to count characters, vowels ,blank space and full stop from a given file.

#include<iostream.h>

20

#include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

int count=0;

charch;

ofstream out;

ifstream in;

in.open("b.txt");

while(in)

{

in.get(ch);

cout<<ch<<endl;

}

in.close();

in.open("b.txt");

cout<<"Total character with blank space=> "<<endl;

while(in)

{

in.get(ch);

count=count+1;

}

cout<<count<<endl;

int v=0;

21

in.close();

in.open("b.txt");

while(in)

{

in.get(ch);

if((ch=='a')||(ch=='A')

||(ch=='e')||(ch=='E')

||(ch=='i')||(ch=='I')

||(ch=='o')||(ch=='O')

||(ch=='u')||(ch=='U'))

{

v=v+1;

}

}

cout<<"Number of vowels are=>"<<v<<endl;

in.close();

intnum=0;

in.open("b.txt");

while(in)

{

in.get(ch);

if(ch=='.')

{

num=num+1;

}

22

}

cout<<"Numbers of full stop are=> "<<num<<endl;

in.close();

intbs=0;

in.open("b.txt");

while(in)

{

in.get(ch);

if(ch==' ')

{

bs=bs+1;

}

}

cout<<"Number of blank spaces=>"<<bs<<endl;

cout<<"Total number of character excluding blank spaces=>"<<endl;

cout<<count-bs;

in.open("b.txt");

while(in)

{

in.get(ch);

cout<<"Content of b file: "<<ch;

}

in.close();

getch();

}

23

Output:

Que. Write a program to check balanced parenthesis from given expression.

#include<iostream.h>

#include<conio.h>

24

void main()

{

clrscr();

charch[10];

int count=0,count1=0,count2=0,n;

cout<<"Enter the value of n"<<endl;

cin>>n;

cout<<"Enter expresion"<<endl;

for(int j=1;j<=n;j++)

{

cin>>ch[j];

}

for(j=1;j<=n;j++)

{

cout<<endl<<ch[j];

}

for(j=1;j<=n;j++)

{

if((ch[j]=='('))

{

count=count+1;

}

else if ((ch[j]==')'))

{

count=count-1;

25

}

}

for(j=1;j<=n;j++)

{

if((ch[j]=='{'))

{

count1=count1+1;

}

else if ((ch[j]=='}'))

{

count1=count1-1;

}

}

for(j=1;j<=n;j++)

{

if((ch[j]=='['))

{

count2=count2+1;

}

else if ((ch[j]==']'))

{

count2=count2-1;

}

}

if(count==0 && count1==0 && count2==0)

26

{

cout<<"Brackets are balanced"<<endl;

}

else

{

cout<<endl<<"Brackets are unbalanced"<<endl;

}

getch();

}

27