Unit 5
Unit 5
Asn"i7pd
1. ds6%duin-b9iw
(Standard Libraries Function)
c++i 1 6 Q n f i ~ n u m l i ~ l # 3 ~ ~ $l a. ~ui ~~ ~a n?" $ ~ i 1 4iiulnnlnu
a8unirl~siu~sn"$u~0sniui ~
T ~ ~ ~ : ~ ~ ~ ~ ~ $ I P I ~ ~ ~ ~ Y ~ ~$d1;~~.6f
" $ ~ C ~ I U ~ ~ ! ~ ~ - ~ I ~ I ~ ~ I ~ ~ ~ ~
662304 - 05 Page 1 of 28
int isspace ( int character);
ex: +
isspace(' ') value > 0
aiaz~inlai~Suiosiisoz1Xdii~u
O
int isupper ( int character); idu~s6$unirmsaono9~$aGn6usziii8~6aGnp1s1~$
ex: isupper('D') 3 value > 0
F~uozai~d1dwi8~6aam
biwinigu6adnpIs%w$~zdu
dw w w w
nnnnaod 5-1 m~~$~iufianaununaonvsz~au
Library ctype
#include <iostream>
#include <string>
using namespace std;
int main ( )
I
char Ch;
string Message;
cout << "Enter character : ";
cin >> Ch;
cout << endl;
if (isalnum(Ch)) {
if (isalpha(Ch)) (
if (islower(Ch)) Message = "lower character.";
else Message = "upper character.";
1
else if (isdigit(Ch)) Message = "digit.";
1
else Message = "special character.";
cout << "\"' << Ch << "\"' << " is " << Message << endl;
if (isalpha(Ch)) {
if (islower(Ch)) {
cout << "\'" << Ch << "\'" << " convert to upper \ ' " ;
cout << (char)toupper (Ch) << " \ "';
1
else (
cout << " \ ' " << Ch << " \ ' " << " to lower \ '
'I;
662304 - 05 Page 2 of 28
Stdlib Library (std1ib.h or cstdlib) do~3J"od?n"~auzu
namespace std
I double atof ( const char *string) ; ~utudsdiI
I 18uJs6$unisudaadi$onaiu1~u6;1"a~avwa~~
1 ex: atof("45.6") + 45.6
~ua8~6;1"a~avq/la6u~~~b3~'~1
double I
1 int I ~8uJsn"~unis~~nlasdi$ona1ua8u6;1"a~avBiuau1G~~
Fmer I
1 ex:
atoi ( const char *string) ;
atof("30) + 30
dsdifiu~8~6;1"~~avBiuauai~~
int I
TRU 1
I
nismnaes$5-2 nis~~siod~s&'pd"bpd
Library cstdlib
#include <iostream>
#include <string>
using namespace std;
int main ( )
{
int IntValue;
long Longvalue;
float Floatvalue;
string StrValue;
Page 3 of 28
cin >> StrValue;
/ / convert string to numeric
IntValue = atoi(StrVa1ue.c str());
LongValue = at01 (StrVa1ue.c str ( ) ) ;
Floatvalue = atof (str~a1ue.c -str ( ) ) ;
tout << endl;
cout << "Convert String to Numeric.\nW;
COUt << ll**************************\n~l.
662304 - 05 Page 4 of 28
Math Library (math-hor cmath)
double ceil (double x) ~3uYs6~unisniii6a~av~wmiu~1uu~sii
x %mu
ex: ceil (54.3) +
55.0
dsi15uaduKa~avwaGuu
double floor (double x) ~ ~ u ~ s 6 ~ u n i s ~ i i i 6 a 1 a u t w m i u ~xi s%mu
vosii
ex: floor (54.3) +
54.0
dsdi~ut~u6alavwaGuu
double sqrt (double x) t 3 u ~ s 6 ~ u n i s w i ~ i s 1 n ~ f l o a vxo Fmodsii
s~i
ex: sqrt ( 4 ) 2.0+ ~ul8uKalauwaGuuFW$I x >= o
double exp (double x) exponential v o s h x &?(I
~3uYa6iumrnii-1
ex: exp (2) +
7.389056
giu e, Fnud e 8 i i 2.718282 Fnudsiin"ul$u
6a~avwaGuu
double fabs (double x) ~~uYs6~unisrniidu~sdvo~ii
x Imudsii8u
ex: fabs (78.5) or fabs (-78.5) -3 78.5
lau63tavwffGuu
double log (double x) 1guls6iunismnatural logarithm uosdi x
ex: log (10) -3 2.302585
FmudsiiGulduKalauwaGuu
double log10 (double x) i?!u~s6~urnmiiilogarithm giuzuuosdi x
ex: log10 (10) -3 1.0
FmudsiiZul3uKalauwaGuu
double pow (double x, double y) 4 s x i?Juiigiuaa::
1?Ju4.r6~umswiiiun~ia's
ex: pow ( 5 , 3 ) 125.0+ y i?Ju6a$fiia'sFmisidutauhlaunaGuu
double sin (double x) sin uosii x , ?RUG x d u
13u~t6iumsmdi
ex: sin (0) 1+ 41 radians FmuaisiiGu~~laKa~auwaGu~~
double cos (double x) ~3uls6~unirniii
cosine uosn'i x , TRU$ x
ex: cos (0) -3 1
L ~ U x~ I 131.4 radians '6mu~s6i~udu~alasl
waGuu'
double tan (double x) i$u4s6$unis~iditangent uotdi x ,1muwA x
ex: tan (0) 0+ L ~ U x~ L
I ~ radians
U *smudsii~ulauKalav
waGuu
m?jwneesi5-3 n i ? j ~ ~ s i ~ f i s d ~ Library
o d ~ o d cmath
#include <iostream>
#include <cmath>
I,
using namespace std;
662304 - 05 Page 5 of 28
double Value;
662304 - 05 Page 6 o f 28
String Library (string.h or cstring)
char *strcat ( char *Stringl, const char "String2);
ex: strcat("Funct", "ions") + "Functions"
662304 - 05 Page 7 of 28
msnnns-ri5-4 rn~l~~ub&ul:ulu
Library cshing
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
I
int main ( )
I
char Str1[30], Str2 [30];
Page 8 of 28
IOMANIP Library (iomanip)
[setw(int w) I 1Sunirfi1numnaiun~1s~~s3onai%~~0:ii~msm1%~~1u~s
w
left d 9-
Cmwaa'wsIn~m4iu%4iaun's~4sn"&u
setw
right Cmwaaa'wti%n~muai
d 9- I~iauEu4sn"~ u
setw
I.
scientific
1 showpoint
1 noshowpoint I uniFinniriam show~oint I
1 noshowbase I uni~nnirianshowbase I
1 boolalpha I tinsn'iyalu Kiaf.n$u true n?on'iii;oillu false
I no boolalpha I uniiinmrian boolalpha
662304 - 05 Page 9 of 28
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ( )
I
float Raduis, Angle;
const int WIDTH = 9;
cout << "t" << setfill('=') << setw(44) << "t" << endl;
cout << " : Angle : Sine : Cosine : Tangent : " << endl;
cout << "t" << setfill('=') << setw(44) << "+" << endl;
cout << setfill ( ' ' ) ;
for (Angle = 0.0 ; Angle <= 360.0 ; Angle += 20) (
Raduis = (Angle > 0.0) ? (180.0f*3.14f)/Angle : 0.0;
cout << " : " << setw(W1DTH) << fixed << setprecision(2)
cout << Angle;
cout << " : " << setw(W1DTH) << setprecision(4) << sin(Raduis);
cout << " : " << setw(W1DTH) << cos(Raduis);
cout << " : " << setw(W1DTH) << tan(Raduis);
cout << " : " << endl;
1
cout << "+" << setfill('=') << setw(44) << "t" << endl;
return (0);
1
#include <iostream>
#include <iomanip>
using namespace std;
int main ( )
{
tout << !' Decimal : Octal : Hexa " << endl;
cout << setfill << setw(30) << "-" << endl;
( I - ' )
Page 10 of 28
2. fi~nd%d~%il~foan
(User-Define Function)
d w 2 d w d
lumw C++ mlnsoahs4snauuua1l~s1u~~s~~a~4snaun!6~iana1a6osn1s
T~uQy.Juuu
Y
V
$¶JLI'LIPI
Function header + data-type Function Name (type var1,type var2,. ..)
I
Function body + variable declaration;
statements;
return(va1ue);
Cofii~umnisl~Ysn"$u
dw '4
- Ysnclru~arfiso:d3:nou&au 2 daun"oFunction header ua: Function body
- Tau Function header dsznau&?odata-type, a function name, a parameter
declaration 6@110%16( )
- IRU
Function body dr:nouhu~iksmuiC++ odmulu { )
- l u Function body bsjimsl~itf'sreturn duondspnoun'iot~nni~~isiu
i~arl4iiks
return 1~saisdiw6sna'~ro1nnisdunl44sn"~u
I dv
- div?u Function header o:~~u6aunisfiinumdsz~n~uosniwPilosnis~sn~ua1~os~sn"~u
n1n~nifiiwumdsz~nw!?~~~o~14sn"~udsd1nGut~u~a~au~1uawt~u(int)
la(m L L ~ ' & I ! ~
dw nY
6 ~ ~ ~ l ~ % ~ ~ ~ n " $ ~ ~ ~void
i Sfilnum!?
d ~ ~ Ll~ ~o uas n" ~~l ~~~ n~' l 6l u ~u ~~n i%~ ~~ 9~j i nli sk ~
dsdin~u(!niijrns%~iiks
return)
662304 - 0 5 Page 11 of 28
- 61th return OZI~IWGI~2 ~ 6 1 4ZB ~3uni~u0nii0uni~p;i1aiu"u0~4an"~pd
iiaz ~ ~ S n i r d a ~ i i
nriuoinmrSun'l( Tnu argument aoaii& return muirnl3u expression 1A
Kaoeiia
main() /* g~PiinSui8uinteger */
int Feet2MeterO * 4an"kud~diniiua~n integer */
void main() /* os
1lilnird.tri1ln~n ~ u i u l ~ d */
char findchar ( str, ch) /* dsdintYuigu6a6nur 1$3 */
#include <iostream>
int square( int ) ; /* function prototype */
int main ( )
t
int x;
while ( x <= 10) {
printf("%d\n" , square(x)); / * calling function * /
x = x + l ;
I
return ( 0) ;
I
int square (int n) /* Definition Function */
I
return ( n * n) ;
I
662304 - 05 Page 12 of 28
#include <iostream>
u s i n g namespace s t d ;
f l o a t CalArea ( ) ;
i n t main ( )
I
f l o a t Area;
c o u t << "Progrsm C a l c u l a t e Area o f C i r c l e . " << e n d l ;
Area = C a l A r e a O ;
c o u t << "Area o f c i r c l e : " << Area << e n d l ;
return ( 0 );
1
f l o a t CalArea ( )
I
f l o a t R a d i u s , Area;
2.2 2daauumsaisiiwd~szwi~s~sn"tw
1um~imrl~uu1ds~1ns%16a"1.d~:~~~n~sds~1~~iru"0~a1Xn"u~sn"~u~1Gsiu
2 lluu $8
1 ) Pass by Value
iiwod n4od1n.r~lfln'u~sn"%u~~ndunI4siu
19umroian'iuoaain~a11d~ I~uiiuoso~nGa
input
U U
output e int swap( int a, int b)
(
return;
1
662304 - 05 Page 13 of 28
2) Pass by Reference Y
input/ output
u Ufi
output t int swap(int&a,int&b)
{
return; '
#include <iostream>
using namespace std;
int Min (int V1, int V2) ;
int Max (int V1, int V2) ;
int main ( )
{
int Valuel, Value2;
cout << "Max value : " << Max (Valuel,Value2) << endl;
cout << "Min value : " << Min(Valuel,Value2) << endl;
return (0);
1
662304 - 05 Page 14 of 28
#include <iostream>
using namespace std;
void Sort3 (int &V1, int &V2, int &V3);
int main ( )
{
int Valuel, Value2,Value3;
662304 - 05 Page 15 of 28
#include <iostream>
#include <string>
using namespace std;
void ChangeString(string &S1, string &S2);
int main ( )
I
string Strl, Str2;
Page 16 of 28
2.3 "usu!"un$3aads(Scope of Variables)
d1n?u6~~sds%uni~111d~nisp;ii~iu 2 ?rGa so
~~Eisoonlgu
1. Global Variable ~~uhud~daiuisn~ild l~~lu~n~
tl~6Guuo.r~s~mrutu
~aomsdszniao:fii~ua~a"mda4d6Gu
main()
Y Y
ilguoulu~6a~lds
2" d s ,
I. h ~ d r o z d r z m n u u f t l friou$oddnnglu
i statement
#include <iostream>
int x, y;
char ch, name[40];
void main()
I
int a, b;
1
int funcl( int a )
I
char c;
float f;
I
char func2 ( char b )
I
int c, x;
char name;
1
msniiiunKia'9oin y = xn
floatpower(f1oat x, int n)
I
float total=l;
while ( n > 0 ) {
total *= x; n--;
1
return (total);
1
oin y = xn = (n In X)
662304 - 05 Page 18 of 28
2.4 rn=ia~iddd6$'~lsuu
Constant Parameters
#include <iostream>
#include <string>
using namespace std;
void ParseName(string &FirstName,string CLastName, const string
FullName);
int main ()
{
string Name = "Stroustrup, Bjarne";
string LastName, FirstName;
p~
Page 19 of 28
ParseName(FirstNameILastName);
Name = FirstName + " " + LastName;
cout << "Name : " << Name << endl;
return (0);
1
2.5 n1sarfilafisn~k~an.w
Default Parameters
d 9-
i ~ u n i ~ ~ i n u m ~ i ~ a s w i ~ i ~ ~ m o ~ l n n u ~ s n " ~ u ~ m i~uu?nj!,"fiiwum
o~m~up~"~lun~~~~ni
wisiG~mai't=?iw?u%m~fiinum~i
default 1~<uwi3i~~mo~~:~"iIwwi:aiauni~~szniai~'iku
I ~ U
#include <iostream>
#include <string>
using namespace std;
int Sumrnation(int StartNumber=l ,int EndNumber=lO);
int main ( )
I
int Start, End;
662304 - 05 Page 20 of 28
cout << endl;
cout << "Summation of Number " << Start << " to " << End;
cout << " = " << Summation(Start,End) << endl;
cout << "Summation of Number " << 5 << " to " << 10;
cout << " = " << Summation(5) << endl;
cout << "Summation of Number " << 1 << " to " << 10;
cout << " = " << Summation() << endl;
return (0);
1
int Sumrnation(int StartNumber, int EndNumber)
I
long Sum = 0;
for(int N = StartNumber ; N < EndNumber ; N++)
Sum += N;
return Sum;
1
2.6 ona?ldl~b6k~!1uu
Overloading
d d
dn~nisar$i94sn"~uo:&os~~o~snvuw!i~in'pd
m~ounis$i~i~duo~Ka~~ds
i~d%uni~li
C++
9 9 9, 9, A 9 4 P O
mmsolnel.aarri~4~n"$u~~~o~~n"$u~~uan'upJinninum
~mtlii~oulunonoiuau~iw~si~i~ilo~ni
aPiin"Pd ~io~trW"muos~isiG~m~~m"o~~5unua:vtrW"m
L$U
Page 21 of 28
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/ / Define prototype function
void DisplayMenu ( ) ;
float Area(const float Radius);
float Area(const float Length, const float Widht);
int main ( )
t
char Choice;
boo1 Flag = true;
40 t
DisplayMenu ( ) ;
cin >> Choice;
if (Choice == '1') {
float Radius;
cout << "\nEnter radius : ";
cin >> Radius;
cout << "Area of Circle = " << fixed;
cout << setprecision(2) << Area(Radius) << endl;
1
else if (Choice == '2') {
float Length,Widht;
cout << "Enter length and width : ";
cin >> Length >> Widht;
cout << "Area of Rectangle = " << fixed;
cout << setprecision(2) << Area(Length,Widht);
cout << endl;
1
else if (Choice == '3') Flag = false;
else {
cout << "\nYou choose out of range is ";
cout << "not process.\n";
1
) while (Flag);
cout << "\n . . . Exit Program . . .\n";
return ( 0 ) ;
1
Page 22 of 28
v o i d DisplayMenu ( )
I
cout << endl;
cout << " P r o g r a m C a l c u l a t e Area " << e n d l ;
cout << " 1. C l r c l e " << e n d l ;
cout << " 2 . R e c t a n g l e " << e n d l ;
cout << " 3 . E x i t " << e n d l ;
cout << " E n t e r y o u r c h o o s e number: ";
1
d u e
2.3 fl~n%troda%ungaa@s
(Recursion Function)
aYiulsn"~u~auRun6aaod
~ n e i i i w u m ~ f i i i s i u.lrr~luadou!udtilwumaa"
h~ awdol6istiiu~m
1mebsdo~riuil~idJi!pll4Aao
d&srnsJiwarini~lA~ouw~i~uilGsiu
msl8uu Function Recursive Lwdo13uoiru6il~wi86oulu 2 o61.r $8
1. b~~iln~n~8uuiladrnlfio~l~~d~~irrisndun~aaos
2. ~os~6ou!ud~wfu~p1nis~isiuuos~sn".ir"pd
662304 - 05 Page 23 o f 28
%$Recursion Function om n! = 1x 2 x 3 x 4 x 5 x ... x n
e:l& n! = n x (n-l)! dou'lu (1)
#include <iostream>
using namespace std;
unsigned long Factoriall(int Number);
unsigned long Factorial2 (int Number) ;
int main ( )
I
int Value;
662304 - 05 Page 24 of 28
/ / This function use loop
unsigned long Factoriall(int Number)
I
unsigned long Fac = 1;
for ( int N = 1 ; N <= Number ; N++)
Fac *= N;
return (Fac);
1
#include <iostream>
#include <cstring>
int main ( )
t
char Name[80];
int N1, N2;
662304 - 05 Page 25 of 28
cout << "\n nl = " << N1 << ", n2 = " << N2;
cout << ", string = " << Name;
C O U ~<< "\n Max value of nl & n2 = " << MaxInt(NlrN2);
cout << "\n Press key to call function...";
getchar0 ;
swap(N1, N2) ;
UpperStr (Name);
cout << "\n After call function...";
cout << "\n nl = " << N1 << ", n2 = " << N2;
cout << ", string = " << Name << endl;
return (0);
I
#include <iostream>
#include <time.h>
#include <cmath>
using namespace std;
int NumCmp (int vl, int v2) ;
int main ( )
I
int val,num,count = 0;
boo1 flag = true;
662304 - 05 Page 27 of 28
(sin x)2 x (cosx)2
3. a ~ ~ l ° l u o ~ d ~ d s a ~ n s u w s a o a r ~ u ~ a ~ a % ~ ~ ~ ~ u ~ alnonf1.~3w@s6~u
~ a % d i u a u ~ w wCheckprime
iz~~o~~
~ I H ~ ~ E U ~ % $ ~ ~ ~ ~ ~ ~ B ~ ~ B ~ $$ohh?u
Boolean ~ ~ ~ S ~ V ~ ~ J L ~ I X ~ L ~ W