Presentation is loading. Please wait.

Presentation is loading. Please wait.

尋隱者不遇 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島 1. C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time.

Similar presentations


Presentation on theme: "尋隱者不遇 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島 1. C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time."— Presentation transcript:

1 尋隱者不遇 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島 1

2 C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time type time_t struct tm - Time structure (See Chapter 7) struct tm  Time manipulation clock - Ticks since the program was launched clock time - Get current time time mktime - Convert tm structure to time_t mktime  Macro CLOCKS_PER_SEC - Clock ticks per second CLOCKS_PER_SEC  Conversion asctime - Convert tm structure to string asctime ctime - Convert time_t value to string ctime gmtime - Convert time_t to tm as UTC time gmtime localtime - Convert time_t to tm as local time localtime strftime - Format time as string strftime 2

3 time() /* time example */ #include int main () { time_t seconds; seconds = time(NULL); std::cout << seconds/3600 << " hours since" << " January 1, 1970\n"; return 0; } /* time example */ #include int main () { time_t seconds; time(&seconds); std::cout << seconds/3600 << " hours since" << " January 1, 1970\n"; return 0; } 3 #include time_t time ( time_t * timer );

4 ctime()  Converts the time_t object (seconds since 1970.1.1) to a C string containing a human-readable version of the corresponding local time and date. /* ctime example */ #include int main () { time_t rawtime; time ( &rawtime ); std::cout << "The current local time is: " << ctime (&rawtime) << std::endl; return 0; } 4 #include char * ctime ( const time_t * timer ); The current local time is: Sat Nov 10 11:57:33 2012 output

5 5 String Manipulation

6 6 Standard Header                   Q: What’s the difference between and ? A: In C++, we include ; in C, we include. Actually, including the standard header will effectively include the standard C library header within the std namespace.std namespace

7 7 strlen()  Find length of string. #include using std::cout; using std::endl; int main() { char str[10]="Hello"; cout << sizeof str; cout << strlen(str); } #include size_t strlen( char *str ); 10 5 output

8 8 strchr()  Find first occurrence of specified character in string. #include char *strchr( const char *str, int ch ); char str1[] = "Hello"; char* r= strchr(str1, 108); cout << r << endl; llo output

9 9 strstr()  Find first occurrence of specified string in another string. char str1[] = "Hello Hello"; char str2[] = "el"; char* r = strstr(str1, str2); cout << r << endl; #include char *strstr( const char *str1, const char *str2 ); ello Hello output

10 10 strcmp()  Compare two strings. #include int strcmp( const char *str1, const char *str2 ); char str1[] = "Hello"; char str2[] = "He"; int r= strcmp(str1, str2); cout << r << endl; output 1 char str1[] = "Hello"; char str2[] = "Hello"; int r= strcmp(str1, str2); cout << r << endl; output 0 char str1[] = "Hello"; char str2[] = "Hello!!"; int r= strcmp(str1, str2); cout << r << endl; output a positive number a negative number

11 11 strcpy()  Copy str2 to str1, including its terminating null character. #include char *strcpy(char *str1, const char *str2); char str1[] = "Hello"; char str2[] = "abc"; strcpy(str1, str2); cout << str1 << endl; output abc

12 12 strncpy()  Copy at most n characters of str2 to str1, not including its terminating null character. #include char *strncpy( char *str1, const char *str2, size_t n); char str1[] = "Hello"; char str2[] = "abc"; strncpy(str1, str2, 2); cout << str1 << endl; output abllo

13 13 strcat()  Append one string to another. #include char *strcat( char *str1, const char *str2 ); char str1[10] = "Hello"; char str2[] = "!!"; strcat(str1, str2); cout << str1 << endl; output Hello!!

14 14 Reference  Standard C Library http://www.mers.byu.edu/docs/standardC/ind ex.html http://www.mers.byu.edu/docs/standardC/ind ex.html http://www.elook.org/programming/c/ MSDN – String ManipulationString Manipulation

15 Exercise  Design a function int lower_string(char* str) to convert every lowercase letters in a string to uppercase. For example, char str[] = "WoW"; lower_string(str); cout << str << endl;  The result will be "wow"  You may also utilize the tolower() function defined in C library ctype. 15

16 Exercise  Write a function int find(char* text, char* pattern), which can count the occurrence of pattern in text. For example, find("This is an island.", "is") == 3 find("Hello", "g") == 0  Test your function with this main program.main program  Hint: You may utilize the strstr() function, or define a new function by yourself. 16


Download ppt "尋隱者不遇 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島 1. C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time."

Similar presentations


Ads by Google