Some common methods in C Language

Source: Internet
Author: User

Due to academic reasons, this section has recently been in contact with C, and the process orientation is a little different from the object-oriented approach.

However, the method can still be written as a public one... the following functions are often used...

/* 25 **************** * ******************************** sourcestr source string, desstr judge string *************************/INT stringjudge (char sourcestr [], char desstr []) {int Results = 0, I; I = strlen (desstr);/* Get the length of the substring */while (sourcestr = strstr (sourcestr, desstr ))! = NULL)/* Find the first occurrence of the string in the string */{ Sourcestr = sourcestr + I;/* Move back */ Results ++;} return results ;} /* 26 ****************** determine whether the input parameter type is correct ************* * *************/INT judgeinputdata (char * STR, int status ){ Int I = 0; Int Len = strlen (STR ); Int boolpoint = 0; Switch (Status) { Case isnum: For (I = 0; I <Len; I ++) { If (! (STR [I]> = 48 & STR [I] <= 57 )) { Return 0; } } Return 1; Break; Case ischar: For (I = 0; I <Len; I ++) { If (! (STR [I]> = 65 & STR [I] <= 90) | (STR [I] >=97 & STR [I] <= 122 ))) { Return 0; } } Return 1; Break; Case isfloat: For (I = 0; I <Len; I ++) { If (STR [I]> = 48 & STR [I] <= 57) | (STR [I] = '.' & boolpoint = 0 )) { If (STR [I] = '.') { Boolpoint ++; } Continue; } Return 0; } Return 1; Break; } Return 1 ;} /* 27 ***************** enter a parameter and perform the judgment operation *********** * **************** // * parameters: * ****************** STR (input character), Len (input character length), * printinfo (input character prompt ), status (input parameter type), max (maximum input value Limited) * *************************/void inputdata (char * STR, int Len, char * printinfo, int status, int max ){ Char ch [1024] = {0 }; Char Buf [1024] = {0 }; Int I = 0; Memset (STR, 0, sizeof (STR )); Printf (printinfo ); // Print the prompt for the user to enter Sprintf (BUF, "incorrect input. Please try again! \ N % s ", printinfo ); Switch (Status) { Case isnum: While (1) { Scanf ("% s", CH ); Strncpy (STR, CH, Len ); If (judgeinputdata (CH, status) = 1) { If (atoi (STR)> MAX ){ Sprintf (BUF, "input exceeds the range of % d. Please enter it again! \ N % s ", Len, printinfo ); } Else Break; } Printf (BUF ); // Print the error message so that the user can enter it again } Break; Case ischar: While (1) { Scanf ("% s", CH ); If (judgeinputdata (CH, status) = 1) { Break; } Printf (BUF ); // Print the error message so that the user can enter it again } Strncpy (STR, CH, Len ); Break; Case isfloat: While (1) { Scanf ("% s", CH ); Strncpy (STR, CH, Len ); If (judgeinputdata (CH, status) = 1) { If (atoi (STR)> MAX ){ Sprintf (BUF, "input exceeds the range of % d. Please enter it again! \ N % s ", Max, printinfo ); } Else Break; } Printf (BUF ); // Print the error message so that the user can enter it again } Break; Default: Break; }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.