0% found this document useful (0 votes)
69 views

C/C++ Source Codes: Programme For E.G. of Width and Setew Functions

The document contains code snippets in C/C++ demonstrating various programming concepts like classes, inheritance, constructors, strings, file handling, and system functions. The code examples include programs to count words and vowels, sort an array, reboot the system, and demonstrate the copy constructor.

Uploaded by

Ismail Wizzy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

C/C++ Source Codes: Programme For E.G. of Width and Setew Functions

The document contains code snippets in C/C++ demonstrating various programming concepts like classes, inheritance, constructors, strings, file handling, and system functions. The code examples include programs to count words and vowels, sort an array, reboot the system, and demonstrate the copy constructor.

Uploaded by

Ismail Wizzy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

C/C++ Source Codes

programme for e.g. of width and setew functions

//e.g. of width and setew function..


#include <iostream.h>
#include <iomanip.h>
void main ()
{
int i;
cout << "A list of numbers :" << endl;
for (i = 1; i <= 1024; i *= 2)
{
cout.width (7);
cout << i << endl;
}
cout << "A table of numbers :" << endl;
for (i = 0; i <= 4; i++)
{
cout << setw(3) << i << setw(5) << i * i * i << endl;
}
}

A program that reads from a character string

//A program that reads from a character string :


#include <iostream.h>
#include <strstrea.h>
#include <string.h>
void main ()
{
char a[1024];
istrstream b(a, 1024);
strcpy (a, "45.656");
double k, p;
b.seekg(0); // Start from first character.
b >> k;
k = k + 1;
cout << k << endl;
strcpy (a, "444.23 56.89");
b.seekg(0);
b >> k >> p;
cout << k << ", " << p + 1 << endl;
}

Lecturer: Mr. Serunjogi Ismail Page 1


program that reads from a file :(fstream.h is used.)

//Here is a program that reads from a file :


#include <iostream.h>
#include <fstream.h>
void main ()
{
fstream f;
char c;
cout << "What's inside the test.txt file from";
cout << "the C: hard disk root " << endl;
cout << endl;
f.open("c:\\test.txt", ios::in);
while (! f.eof() )
{
f.get(c); // Or c = f.get()
cout << c;
}
f.close();
}

double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double surface()
{
return fabs (x * y);
}
};
class number
{
public:
double z;
number (double a)
{
z = a;
}
int is_negative ()
{
if (z < 0) return 1;
else return 0;
}
};
class trivector : public vector, public number
{

Lecturer: Mr. Serunjogi Ismail Page 2


public:
trivector(double a=0, double b=0, double c=0) : vector(a,b), number(c)
{
} // The trivector constructor calls the vector
// constructor, then the number constructor,
// and in this example does nothing more.
double volume()
{
return fabs (x * y * z);
}
};
void main()
{
trivector a(2, 3, -4);
cout << a.volume() << endl;
cout << a.surface() << endl;
cout << a.is_negative() << endl;
}

A class may be DERIVED from another class.

//A class may be DERIVED from another class. The new class INHERITS
//the variables and methods of the BASE CLASS. Additional variables
// and/or methods can be added :
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x*x + y*y);
}
double surface()
{
return x * y;
}
};
class trivector : public vector // trivector is derived from vector
{
public:
double z; // added to x and y from vector
trivector (double m=0, double n=0, double p=0) : vector (m, n)
{

Lecturer: Mr. Serunjogi Ismail Page 3


z = p; // Vector constructor will
} // be called before trivector
// constructor, with parameters
// m and n.
trivector (vector a) // What to do if a vector is
{ // cast to a trivector
x = a.x;
y = a.y;
z = 0;
}
double module () // define module() for trivector
{
return sqrt (x*x + y*y + z*z);
}
double volume ()
{
return this->surface() * z; // or x * y * z
}
};
void main()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3) *r = b" << endl << endl;
cout << "Surface of a : " << a.surface() << endl;
cout << "Volume of b : " << b.volume() << endl;
cout << "Surface of base of b : " << b.surface() << endl;
cout << "Module of a : " << a.module() << endl;
cout << "Module of b : " << b.module() << endl;
cout << "Module of base of b : " << b.vector::module() << endl;
trivector k;
k = a; // thanks to trivector(vector) definition
// copy of x and y, k.z = 0
vector j;
j = b; // copy of x and y. b.z leaved out
vector *r;
r = &b;
cout << "Surface of r : " << r->surface() << endl;
cout << "Module of r : " << r->module() << endl;
}

print the equivalent English of a number

# include <ctype.h>
# include <stdio.h>
#include<conio.h>

char *ot[3][9] = {
{ " One", " Two", " Three", " Four", " Five",
" Six", " Seven", " Eight", " Nine" },

Lecturer: Mr. Serunjogi Ismail Page 4


{ " Ten", " Twenty", " Thirty", " Forty", " Fifty",
" Sixty", " Seventy", " Eighty", " Ninety" },
{ " Eleven", " Twelve", " Thirteen", " Fourteen",
" Fifteen"," Sixteen"," Seventeen"," Eighteen", " Nineteen"}
};

char *a[5] = { " Hundred", " Thousand", " Lakhs", " Crore", " Arab" } ;
char result[250] = "" ;
char *t[50] ;

main( )
{
int i, j, ind = 0, c, r, pr = -1, e = 0 ;
unsigned long n ;
unsigned long q ;

clrscr( ) ;
printf ( "\nEnter a number : " ) ;
scanf ( "%ld", &n ) ;
printf ( "%ld\n", n ) ;
q=n;
if ( n == 0 )
strcpy ( result, "Zero" ) ;
else
{
for( i = 0 ; q > 0 ; i++ )
{
if( ( i % 2 ) && i > 2 )
e++ ;
r = q % 10 ;
q /= 10 ;
if ( r != 0 )
{
if ( i == 0 )
t[++ind] = ot[i][r-1];
else if ( i == 1 )
{
if ( r == 1 && pr == 0 )
t[++ind] = ot[i][r-1] ;
else if ( r == 1 && pr > 0 )
t[--ind] = ot[2][r-1] ;
else
t[++ind] = ot[i][r-1] ;
}
else if ( i >= 2 )
{

Lecturer: Mr. Serunjogi Ismail Page 5


if ( i == 2 )
{
t[++ind] = a[e] ;
t[++ind] = ot[0][r-1] ;
}
else
{
if ( i % 2 )
{
t[++ind] = a[e] ;
t[++ind] = ot[0][r-1] ;
}
else
{
if ( pr == 0 )
{
t[++ind] = a[e] ;
t[++ind] = ot[1][r-1] ;
}
else if ( r == 1)
t[ind] = ot[2][pr-1] ;
else
t[++ind] = ot[1][r-1] ;
}
}
}
}
pr = r ;
}
}/* End for */

for ( i = ind ; i >= 0 ; i-- )


strcat ( result, t[i] ) ;

printf( "\n%s", result ) ;

getch();
} /* End main */

count words and vowels in a line

// program to count number of words in a given string


#include<stdio.h>
#include<conio.h>

void main()

Lecturer: Mr. Serunjogi Ismail Page 6


{
int wc,c,i,flag,v,w,space;
wc=c=i=v=w=space=0;
flag=1;
clrscr();
printf("enter a string press enter when you have done\n");
for(i=0 ; (c=getchar()) !='\n' ; i++)
{
if(c==' '''c=='\t')
flag=1;
else if(flag)
{
wc++;
flag=0;
}
if(c=='a'''c=='e' ''c=='i' ''c=='o' ''c=='u')
v++;
if(c!=' '&&c!='\t')
w++;
if(c==' ')
space++;
}
printf("\n\nnumber of words are %d \nno of vovels are %d",wc,v);
printf("\nno of char are %d\nno of space are %d",w,space);
getch();
}

shell sort

#include<iostream.h>
#include<conio.h>
void main()
{
int k,i,temp,n,a[20],swap,gap;
clrscr();
cout<<"enter the no. of elements: ";
cin>>n;
cout<<"enter elements: \n";
for(i=1;i<=n;i++)
cin>>a[i];
gap=n/2;
k=0;
do{
do{
swap=0;
k++;
for(i=1;i<=n-gap;i++)
if(a[i]>a[i+gap])

Lecturer: Mr. Serunjogi Ismail Page 7


{
temp=a[i];
a[i]=a[i+gap];
a[i+gap]=temp;
swap=1;
}
}while(swap);
gap=gap/2;
}while(gap);
cout<<"sorted list is: \n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
getch();
}

reboot the system

 #include <dos.h>

void bootme(int want_warm) /* arg 0 = cold boot, 1 = warm */


{
union REGS reg;

void (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;


unsigned far* boottype = (unsigned far*)0x00400072UL;
char far* shiftstate = (char far*)0x00400017UL;
unsigned ticks;
int time_to_waste;

/* Simulate reception of Ctrl-Alt-Del: */


for (;;)
{
*shiftstate '= 0x0C; /* turn on Ctrl & Alt */
reg.h.ah = 0x4F; /* see notes below */
reg.h.al = 0x53; /* 0x53 = Del's scan code */
reg.x.cflag = 1; /* sentinel for ignoring key */
int86(0x15, &reg, &reg);

/* If carry flag is still set, we've finished. */


if(reg.x.cflag)
break;

/* Else waste some time before trying again: */


reg.h.ah = 0;
int86(0x1A, &reg, &reg); /* system time into CX:DX */

ticks = reg.x.dx;
for (time_to_waste = 3; time_to_waste > 0; )
{
reg.h.ah = 0;
int86(0x1A, &reg, &reg);

Lecturer: Mr. Serunjogi Ismail Page 8


if (ticks != reg.x.dx)
ticks = reg.x.dx , --time_to_waste;
}
}

/* Issue a DOS disk reset request: */


reg.h.ah = 0x0D;
int86(0x21, &reg, &reg);

/* Set boot type and boot: */


*boottype = (want_warm ? 0x1234 : 0);
(*boot)( );
}

void main()
{
bootme(0);
}

a programme for starting knowledge about The COPY CONSTRUCTOR

#include<iostream.h>
#include <string.h>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char[100];
strcpy (name, n);
age = a;
}
person (person &s) // The COPY CONSTRUCTOR
{
strcpy (name, s.name);
age = s.age;
}
~person ()
{
delete (name);
}
};

void main ()
{
person p;
cout << p.name << ", age " << p.age << endl << endl;
person k ("John", 56);
cout << k.name << ", age " << k.age << endl << endl;

Lecturer: Mr. Serunjogi Ismail Page 9


p = k;
cout << p.name << ", age " << p.age << endl << endl;
p = person ("Bob", 10);
cout << p.name << ", age " << p.age << endl << endl;
}

even no. program


int main()
{
int n;
cout<<"enter the number";
cin>>n;
if(n%2==0)
cout<<"number entered is even";
else
cout<<"number is not even";
getch();
return 0;
}

programme to reverse a string


#include <string.h>
#include <stdio.h>

int main(void)
{
char *forward = "string";

printf("Before strrev(): %s\n", forward);


strrev(forward);
printf("After strrev(): %s\n", forward);
return 0;
}

to convert a string of upper case to lower case


 #include <stdio.h>
#include <string.h>

int main(void)
{
char *string = "Borland International";

printf("string prior to strlwr: %s\n", string);


strlwr(string);
printf("string after strlwr: %s\n", string);
return 0;
}

Lecturer: Mr. Serunjogi Ismail Page 10

You might also like