"PubMed machine test" (a) C / C ++ based

1.setfill / setw use

2. The definition of the structure

3. With regard to the string read

4. Scheduling: re-examination is not required for general use: Bubble Sort

5. convert between numbers and characters

6. binary conversion: 10 hex turn octal

7. Analyzing prime number

8. string copy function strcpy

9. The string concatenation function strcat

10. The string comparison function strcmp

11. Calculate the string length of strlen

#include <iostream>
#include <iomanip> 
#include <math.h>
#include <string.h>
using namespace std;

int main(void){
// 1.setfill / setw use 
//	float f1 = 2.99;
//	float f2 = 8.9099;
//	int i = 10;
//	cout << setfill('*');
// // setw is to set the field width, the output data is occupied by several columns
// // If in case of default, data output can not be achieved under a wide field padded with spaces to
// // setfill filler is provided 
// // When using the manipulator, the need to include the header iomanip 
//	cout << setw(10) << f1 << endl;
//	cout << setw(10) << f2 << endl;
//	cout << setw(10) << i << endl;

// 2. The definition of the structure
//	struct Student{
//		int id;
//		char name[20];
//	}; 
You can use typedef // add aliases
//	typedef struct Student{
//		int id;
//		char name[20];
//	}Student;
//	
// Use: Student s;  

// 3. read about string 
//	string str = "hello gy";
//	int i = 0;
//	while(str[i] != '\0'){
//		cout << str[i] << endl;
//		i++;
//	}

// 4. Scheduling
// re-examination is not required for general use: Bubble Sort
// int len ​​= 6; 
// int num [] = {5,2,77,1,99,66};
//	for(int i=0; i<len-1; i++){
//		for(int j=0; j<len-1-i; j++){
//			if( num[j]>num[j+1] ){
//				int temp = num[j];
// num [h] = num [H + 1];
//				num[j+1] = temp;
//			}
//		}
//	}
//	for(int i=0; i<len; i++){
//		cout << num[i] << " ";
//	} 

5. // convert between numbers and characters
// For example: the character '7' into the number 7 
//		char a = '7';
//		int a_num = a - '0';
// For example: 5 to convert the digital character '5' 
B_num // int = 5;
// char b = b_num + '0';

// hexadecimal 6. Conversion: 10 hex turn octal
// int num = 666;
//	int result[100];
// int len ​​= 0;
//	while(num/8 != 0){
// Result [len] = num% 8;
// as ++;
// a / = 8;
//	}
// Result [len] = num;
//	for(int i=len; i>=0; i--){
//		cout << result[i] <<endl;
//	}

7. Analyzing // prime number
// int num = 10;
//	int temp = sqrt(num);
// bool isZhiShu = true; // default is a prime number 
//	for(int i=2; i<=temp; i++){
//		if( num%i==0 ){
// isZhiShu = false; // not a prime number 
//			break; 
//		}	
//	}
//	if(isZhiShu){
// cout << "is a prime number" << endl;	
//	}else{
// cout << "not prime" << endl;
//	}

8. // string copy function strcpy
// char * strcpy (char * dest, const char * src);
// the parameter string src is copied to the address indicated in the parameter dest
//	char string[10];
//	char *str1 = "abcdefgh";
// // copy the contents of str1 to the string array 
//	strcpy(string, str1); 
//	printf("%s\n", string);

9. // string concatenation function strcat
//	char *strcat(char *dest, const char *src);
@ Action: Return the string starting address dest and dest = dest + src 
//	char str[20];
//	char* str1 = "gyy";
//	char* str2 = "wgg";
//	strcat(str, str1);
//	strcat(str, str2);
//	printf("%s\n", str);

10. // string comparison function strcmp
//	int strcmp(const char *s1, const char *s2);
// If s1 == s2 return 0; s1> s2 returns greater than 0; s1 <s2 returns less than 0
//	char *a = "aBcDeF";
//	char *b = "AbCdEf";
//	char *c = "aacdef";
//	char *d = "aBcDeF"; 
//	printf("%d\n",strcmp(a,b)); 
//	printf("%d",strcmp(a,d));
	
11. Calculate // string length of strlen
// Calculates string length, not including the end character '\ 0' a
// 	size_t strlen(const char *s);
// returns the number of characters in the string s
// But sizeof returns the amount of memory occupied by the variable declaration, not the actual length
// 	char str[5] = "abcd";
// 	cout << strlen(str) << endl;//4
// 	cout << sizeof(str) << endl;//5
		
	return 0;
}
 

Guess you like

Origin www.cnblogs.com/Whgy/p/12302284.html