Najčešće standardne funkcije koje se najviše koriste u C++ programskom jeziku su C-String funkcije, funkcije za pojedinačne znakove, matematičke funkcije, funkcije za slučajno biranje brojeva i funkcije za datum i vreme. Rad sa funkcijama iz standardne biblioteke je zaista velika pomoć prilikom programiranja i pored toga što se na primer u C# programskom jeziku, ništa ne može porediti sa .NET Framework klasama, tako se ništa ne može porediti sa konstantnom upotrebom biblioteke standardni funkcija u C programskom jeziku, ali za C++ programski jezik, standardna biblioteka nije baš bezbedna dok postoje i bolja rešenja. Ono što izdvaja standardne funkcije je njihova jednosatvna upotreba i navika C programera. Upotreba standardne biblioteke zahteva da uvezete određeni header fajl u vaš program. Najbolje će te shvatati standardne funkcije njihovom upotrebom kodiranjem.
C-String funkcije
C-String funkcije se primenjuju na klasične char* stringove, C programskog jezika <cstring>. Nemojte to da mešate sa funkcijama klase string <string> o kojoj je bilo više reči u postu „Rad sa stringovima u C++ programskom jeziku“. Ja lično ne preporučujem da koristite <cstring> ili objekte Cstring u vašim C++ programima. Uvek koristite <string>. Ali pošte će te često nailaziti na ovakav kod u nekim primerima i starijim projektima, čisto da prepoznate o kakvim funkcijama se radi. Upotreba <cstring> funkcija nije bezbedno koristiti i zato će Microsoft Visual Studio prijavljivati vam grešku kad pokušate da koristite <cstring> osim ukoliko ne naznačite da ne prijavljuje; kliknite desnim tasterom miša na Project u Solution Explorer.
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
Unesite:
_CRT_SECURE_NO_WARNINGS
Pogledajte primer i analizirajte kod programa kako se upotrebljavaju <cstring> funkcije:
Kad pokrenete program, rezultati će izgledati slično:
Please enter your name: Manuel
Please, enter your last name: Radovanovic
Your name is: Manuel Radovanovic
Your first name have 6 letters.
Your last name have 11 letters.
Manuel, that's my first name too.
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
Funkcije za pojedinačne znakove
Da bi ste koristili funkcije za pojedinačne znakove, uključite <cctype> biblioteku zaglavlja u vaš projekat. Većina funkcija u ovoj standardnoj biblioteci ispituje pojedinačan znak i vraća true ili false. Takođe vrši i konverziju velikog slova u malo i obratno. Ukoliko unesete neki karakter koji nije slovo, konverzija se neće izvršiti, već će funkcija vratiti isti karakter bez prijavljivanja greške. Pogledajte kod:
Kad pokrenete program, rezultati će izgledati slično:
Enter a character: M
Does your character is alphanumeric ( letter or number )? True
Does your character is a letter? True
Does your character is a control character? False
Does your character is a digit number? False
Does your character is visible? True
Does your character is a little letter? False
Does your character is a big letter? True
Does your character can print? True
Does your character is a punctuation of character? False
Does your character is empty space? False
Make a big letter! M
Make a little letter! m
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
Matematičke funkcije
Da bi ste koristili matematičke funkcije , uključite <cmath> biblioteku zaglavlja u vaš projekat. Matematičke funkcije prihvataju i vraćaju tip podataka double iako možete uneti ceo broj jer taj ceo broj se unapređuje i kompajler vam neće prijaviti grešku. Ali ako dodeljujete rezultat double promenjivoj integer bez konverzije, dobićete grešku. Većina C++ programera koristi static_cast za konvertovanje kad imaju potrebu za tim. Rad sa matematičkim funkcijama je jednostavan i ne razlikuje se mnogo od matematike koju učite u školi. Pogledajte primer i analizirajte kod programa:
Kad pokrenete program, rezultati će izgledati slično:
Enter a number: 0.5
abs - returns the absolute value: 0.5
acos - returns the angle whose cosine is the specified number: 60 degrees.
asin - returns the angle whose sine is the specified number: 30 degrees.
atan - returns the angle whose tanges is the specified number: 26.5651 degrees.
atan2 - returns the angle whose tangent is the quotient of two numbers.
In this case, other number is -5.0: 135 degrees.
cos - returns the cosine of the specified angle: 0.877583
sin - returns the sine of the specified angle: 0.479426
tan - returns the tangent of the specified angle: 0.546302
cosh - returns the hyperbolic cosine of the specified angle: 1.12763
sinh - returns the hyperbolic sine of the specified angle: 0.521095
tanh - returns the hyperbolic tangent of the specified angle: 0.462117
exp - returns e raised to the specified power: 1.64872
floor - Returns the largest integer less than or
equal to the specified double-precision floating-point number: 0
log - returns the natural (base e) logarithm of a specified number: -0.693147
sqrt - returns the square root of a specified number: -0.693147
pow - returns a specified number raised to the specified power.
In this case, specifed power is 5 : 0.03125
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
Funkcije za slučajno biranje
Da bi ste koristili biranje slučajnih brojeva neophodno je da uključite i <cstdlib> i <ctime> biblioteke zaglavlja u vaš projekat. Slučajno izabrani celi broj se vraća preko funkcije rand. Međutim prvo mora da se pozove funkcija srand koji vraća raspon od 0 do RAND_MAX broja, tako što uzima broj tipa unsigned int koji se poziva u funkciji rand. Pozivanjem funkcije time(NULL) prosleđuje se sistemsko vreme kao početna vrednost. Sve ovo vam verovatno zvuči komplikovano i nejasno, ali ako pogledate i analizirate kod sledećeg programa, možete uočiti da biranje slučajnih brojeva i nije toliko komplikovano kao što zvuči:
Kad pokrenete program, rezultati će izgledati slično:
LOTTO
1. Random numbers
0. Exit
Enter 0 for exit or 1 for random lotto numbers: 1
24 38 29 27 39 22 13
Kako program funkcioniše možete pogledati i na video-u:
Funkcije za vreme i datum
U C++ programskom jeziku rad sa funkcijama standardni biblioteka po pitanju vremena i datuma nije regulisano jednostavno kao što omogućavaju klase u C# programskom jeziku. Prvo treba da uključite datoteku zaglavlja <cstring> kako bi se dobila vrednost trenutnog vremena kao broj tipa unsigned long iz funkcije time. Samo u takvom obliku se vrednost prosleđuje funkcijama gmtime koji prihvata pokazivač i vraća Greenwich Mean Time – vreme po Grinču i localtime koji vraća pokazivač na lokalno vreme. Možete koristiti i funkcije asctime i ctime za dobijanje određenih formata. Microsoft Visual Studio smatra i neke funkcije <ctime> kao nebezbedne, pa će te morati uneti:
_CRT_SECURE_NO_WARNINGS
u
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
Inače program se neće kompajlirati. Programeri danas umesto <ctime> koriste < ctime_s> i druge alternative. Najbolje, pogledajte sledeći program i analizirajte sledeći kod:
Kad pokrenete program, rezultati će izgledati slično u zavisnosti od trenutnog vremena:
DATE AND TIME
Current date and time based on current system
in unsigned longer number is: 1446234184
The local date and time is: Fri Oct 30 20:43:04 2015
The UTC date and time is: Fri Oct 30 19:43:04 2015
Year: 2015
Month: 10
Day: 30
Time: 20:43:4
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
( The C++ Standard Library )
C-String funkcije
C-String funkcije se primenjuju na klasične char* stringove, C programskog jezika <cstring>. Nemojte to da mešate sa funkcijama klase string <string> o kojoj je bilo više reči u postu „Rad sa stringovima u C++ programskom jeziku“. Ja lično ne preporučujem da koristite <cstring> ili objekte Cstring u vašim C++ programima. Uvek koristite <string>. Ali pošte će te često nailaziti na ovakav kod u nekim primerima i starijim projektima, čisto da prepoznate o kakvim funkcijama se radi. Upotreba <cstring> funkcija nije bezbedno koristiti i zato će Microsoft Visual Studio prijavljivati vam grešku kad pokušate da koristite <cstring> osim ukoliko ne naznačite da ne prijavljuje; kliknite desnim tasterom miša na Project u Solution Explorer.
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
Unesite:
_CRT_SECURE_NO_WARNINGS
Pogledajte primer i analizirajte kod programa kako se upotrebljavaju <cstring> funkcije:
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
/* I do
not recommend that you use <cstring> or Cstring objects in C++!
To use <cstring> is not safe, so if you just want to see how it
works in vs then
you must add _CRT_SECURE_NO_WARNINGS in
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor
Definition */
char firstname[50];
char lastname[50];
char fullname[100];
cout << "Please enter your
name: ";
cin.getline(firstname, 50);
cout << "Please, enter your
last name: ";
cin.getline(lastname, 50);
fullname[0] = '\0';
strcat(fullname, firstname);
strcat(fullname, " ");
strcat(fullname, lastname);
cout << endl << "Your name is: " << fullname << endl << endl;
cout << "Your first name have
" << strlen(firstname) << " letters." << endl;
cout << "Your last name have
" << strlen(lastname) << " letters." << endl;
if (strcmp(firstname, "Manuel") == 0)
{
cout << "Manuel, that's my
first name too." << endl;
}
else
{
cout << firstname << ", that's not my first
name." << endl;
}
cout << endl;
system("PAUSE");
return 0;
}
Please enter your name: Manuel
Please, enter your last name: Radovanovic
Your name is: Manuel Radovanovic
Your first name have 6 letters.
Your last name have 11 letters.
Manuel, that's my first name too.
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
( C++ Tutorial - 16. Standard Library - Cstring )
Funkcije za pojedinačne znakove
Da bi ste koristili funkcije za pojedinačne znakove, uključite <cctype> biblioteku zaglavlja u vaš projekat. Većina funkcija u ovoj standardnoj biblioteci ispituje pojedinačan znak i vraća true ili false. Takođe vrši i konverziju velikog slova u malo i obratno. Ukoliko unesete neki karakter koji nije slovo, konverzija se neće izvršiti, već će funkcija vratiti isti karakter bez prijavljivanja greške. Pogledajte kod:
#include "stdafx.h"
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch = '\0';
cout << "Enter a character:
";
cin >> ch;
cout << endl << "Does your character
is alphanumeric ( letter or number )? ";
if (isalnum(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is a letter? ";
if (isalpha(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is a control character? ";
if (iscntrl(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is a digit number? ";
if (isdigit(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is visible? ";
if (isgraph(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is a little letter? ";
if (islower(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is a big letter? ";
if (isupper(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
can print? ";
if (isprint(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is a punctuation of character? ";
if (ispunct(ch)) cout << "True"; else cout << "False";
cout << endl << "Does your character
is empty space? ";
if (isspace(ch)) cout << "True" << endl; else cout << "False" << endl;
ch = toupper(ch);
cout << endl << "Make a big letter!
" << ch << endl;
ch = tolower(ch);
cout << endl << "Make a little letter!
" << ch << endl << endl;
system("PAUSE");
return 0;
}
Kad pokrenete program, rezultati će izgledati slično:
Enter a character: M
Does your character is alphanumeric ( letter or number )? True
Does your character is a letter? True
Does your character is a control character? False
Does your character is a digit number? False
Does your character is visible? True
Does your character is a little letter? False
Does your character is a big letter? True
Does your character can print? True
Does your character is a punctuation of character? False
Does your character is empty space? False
Make a big letter! M
Make a little letter! m
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
( C++ Tutorial - 17. Standard Library - Cctype )
Da bi ste koristili matematičke funkcije , uključite <cmath> biblioteku zaglavlja u vaš projekat. Matematičke funkcije prihvataju i vraćaju tip podataka double iako možete uneti ceo broj jer taj ceo broj se unapređuje i kompajler vam neće prijaviti grešku. Ali ako dodeljujete rezultat double promenjivoj integer bez konverzije, dobićete grešku. Većina C++ programera koristi static_cast za konvertovanje kad imaju potrebu za tim. Rad sa matematičkim funkcijama je jednostavan i ne razlikuje se mnogo od matematike koju učite u školi. Pogledajte primer i analizirajte kod programa:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265
int main()
{
double n = 0.0;
cout << "Enter a number:
";
cin >> n;
cout << endl << "abs - returns the
absolute value: " << abs(n) << endl;
cout << "acos - returns the
angle whose cosine is the specified number: " << acos(n) * 180 / PI << " degrees." << endl;
cout << "asin - returns the
angle whose sine is the specified number: " << asin(n) * 180 / PI << " degrees." << endl;
cout << "atan - returns the
angle whose tanges is the specified number: " << atan(n) * 180 / PI << " degrees." << endl;
cout << "atan2 - returns the
angle whose tangent is the quotient of two numbers. "
<< endl << "\tIn this case, other
number is -5.0: " << atan2(n,-0.5) * 180
/ PI << " degrees." << endl;
cout << "cos - returns the
cosine of the specified angle: " << cos(n) << endl;
cout << "sin - returns the
sine of the specified angle: " << sin(n) << endl;
cout << "tan - returns the
tangent of the specified angle: " << tan(n) << endl;
cout << "cosh - returns the
hyperbolic cosine of the specified angle: " << cosh(n) << endl;
cout << "sinh - returns the
hyperbolic sine of the specified angle: " << sinh(n) << endl;
cout << "tanh - returns the
hyperbolic tangent of the specified angle: " << tanh(n) << endl;
cout << "exp - returns e
raised to the specified power: " << exp(n) << endl;
cout << "floor - returns the
largest integer less than or "
<< endl << "equal to the
specified double-precision floating-point number: " << floor(n) << endl;
cout << "log - returns the
natural (base e) logarithm of a specified number: " << log(n) << endl;
cout << "sqrt - returns the
square root of a specified number: " << log(n) << endl;
cout << "pow - returns a
specified number raised to the specified power." << endl
<< "\tIn this case,
specifed power is 5 : " << pow(n,5) << endl << endl;
system("PAUSE");
return 0;
}
Enter a number: 0.5
abs - returns the absolute value: 0.5
acos - returns the angle whose cosine is the specified number: 60 degrees.
asin - returns the angle whose sine is the specified number: 30 degrees.
atan - returns the angle whose tanges is the specified number: 26.5651 degrees.
atan2 - returns the angle whose tangent is the quotient of two numbers.
In this case, other number is -5.0: 135 degrees.
cos - returns the cosine of the specified angle: 0.877583
sin - returns the sine of the specified angle: 0.479426
tan - returns the tangent of the specified angle: 0.546302
cosh - returns the hyperbolic cosine of the specified angle: 1.12763
sinh - returns the hyperbolic sine of the specified angle: 0.521095
tanh - returns the hyperbolic tangent of the specified angle: 0.462117
exp - returns e raised to the specified power: 1.64872
floor - Returns the largest integer less than or
equal to the specified double-precision floating-point number: 0
log - returns the natural (base e) logarithm of a specified number: -0.693147
sqrt - returns the square root of a specified number: -0.693147
pow - returns a specified number raised to the specified power.
In this case, specifed power is 5 : 0.03125
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
( C++ Tutorial - 18. Standard Library - Cmath )
Funkcije za slučajno biranje
Da bi ste koristili biranje slučajnih brojeva neophodno je da uključite i <cstdlib> i <ctime> biblioteke zaglavlja u vaš projekat. Slučajno izabrani celi broj se vraća preko funkcije rand. Međutim prvo mora da se pozove funkcija srand koji vraća raspon od 0 do RAND_MAX broja, tako što uzima broj tipa unsigned int koji se poziva u funkciji rand. Pozivanjem funkcije time(NULL) prosleđuje se sistemsko vreme kao početna vrednost. Sve ovo vam verovatno zvuči komplikovano i nejasno, ali ako pogledate i analizirate kod sledećeg programa, možete uočiti da biranje slučajnih brojeva i nije toliko komplikovano kao što zvuči:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
short menu, random;
do
{
cout << endl << "\tLOTTO" << endl << endl;
cout << endl << "\t\t1. Random numbers" << endl;
cout
<< "\t\t0. Exit" << endl << endl;
cout << "Enter 0 for exit or 1
for random lotto numbers: ";
cin >> menu;
cout << endl;
srand((unsigned)time(0));
for (int index = 0; index < 7; index++)
{
random = (rand() % 39) + 1;
cout << random << "\t";
}
cout << endl << endl;
} while (menu != 0);
return 0;
}
LOTTO
1. Random numbers
0. Exit
Enter 0 for exit or 1 for random lotto numbers: 1
24 38 29 27 39 22 13
Kako program funkcioniše možete pogledati i na video-u:
( C++ Tutorial - 19. Standard Library - Ctime and Cstdlib - Random )
Funkcije za vreme i datum
U C++ programskom jeziku rad sa funkcijama standardni biblioteka po pitanju vremena i datuma nije regulisano jednostavno kao što omogućavaju klase u C# programskom jeziku. Prvo treba da uključite datoteku zaglavlja <cstring> kako bi se dobila vrednost trenutnog vremena kao broj tipa unsigned long iz funkcije time. Samo u takvom obliku se vrednost prosleđuje funkcijama gmtime koji prihvata pokazivač i vraća Greenwich Mean Time – vreme po Grinču i localtime koji vraća pokazivač na lokalno vreme. Možete koristiti i funkcije asctime i ctime za dobijanje određenih formata. Microsoft Visual Studio smatra i neke funkcije <ctime> kao nebezbedne, pa će te morati uneti:
_CRT_SECURE_NO_WARNINGS
u
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
Inače program se neće kompajlirati. Programeri danas umesto <ctime> koriste < ctime_s> i druge alternative. Najbolje, pogledajte sledeći program i analizirajte sledeći kod:
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
/* I
recommend that you use <ctime_s> instead of <ctime> and other
alternatives in C++
To use some functions in <ctime> is
not safe, so if you just want to see how it works in vs than
you must add _CRT_SECURE_NO_WARNINGS in
Project -> Properties -> C/C++
-> Preprocessor -> Preprocesor Definition */
time_t timeNow = time(0);
char* dateTimeNow = ctime(&timeNow);
cout << "DATE AND TIME" << endl << endl;
cout << "Current date and time
based on current system " << endl
<< "in unsigned long
number is: " << timeNow << endl << endl;
cout << "The local date and
time is: " << dateTimeNow << endl;
tm *dateTimeUTC = gmtime(&timeNow);
dateTimeNow = asctime(dateTimeUTC);
cout << "The UTC date and time
is: " << dateTimeNow << endl << endl;
tm *localTime = localtime(&timeNow);
// use
tm structure in <ctime>
cout << "Year: " << 1900 + localTime->tm_year << endl;
cout << "Month: " << 1 + localTime->tm_mon << endl;
cout << "Day: " << localTime->tm_mday << endl;
cout << "Time: " << localTime->tm_hour << ":";
cout << localTime->tm_min << ":";
cout << localTime->tm_sec << endl;
cout << endl;
system("PAUSE");
return 0;
}
Kad pokrenete program, rezultati će izgledati slično u zavisnosti od trenutnog vremena:
DATE AND TIME
Current date and time based on current system
in unsigned longer number is: 1446234184
The local date and time is: Fri Oct 30 20:43:04 2015
The UTC date and time is: Fri Oct 30 19:43:04 2015
Year: 2015
Month: 10
Day: 30
Time: 20:43:4
Press any key to continue . . .
Kako program funkcioniše možete pogledati i na video-u:
( C++ Tutorial - 20. Standard Library - Ctime - Date and Time )
No comments:
Post a Comment