1 / 16

尋隱者不遇

尋隱者不遇. 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島. C Time Library < ctime >. Types clock_t - Clock type size_t - Unsigned integral type time_t - Time type struct tm - Time structure (See Chapter 7) Time manipulation clock - Ticks since the program was launched time - Get current time

Télécharger la présentation

尋隱者不遇

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. #include <ctime> char * ctime ( const time_t * timer ); 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 <iostream> #include <ctime> int main () { time_t rawtime; time ( &rawtime ); std::cout << "The current local time is: " << ctime (&rawtime) << std::endl; return 0; } The current local time is: Sat Nov 10 11:57:33 2012 output

  5. String Manipulation

  6. <assert.h> <ctype.h> <errno.h> <float.h> <iso646.h> <limits.h> <locale.h> <math.h> <setjmp.h> <signal.h> <stdarg.h> <stddef.h> <stdio.h> <stdlib.h> <string.h> <time.h> <wchar.h> <wctype.h> Standard Header Q: What’s the difference between <cstring> and <string.h>?A: In C++, we include <cstring>; in C, we include <string.h>. Actually, including the standard header <cstring>will effectively include the standard C library header <string.h> within the std namespace.

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

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

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

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

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

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

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

  14. Reference • Standard C Library • http://www.mers.byu.edu/docs/standardC/index.html • http://www.elook.org/programming/c/ • MSDN – String 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.

  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. • Hint: You may utilize the strstr() function, or define a new function by yourself.

More Related