c_cpp PathFileExists

PathFileExists

PathFileExists.cpp
#include <windows.h>
#include <iostream>
#include "Shlwapi.h"

#pragma comment(lib, "Shlwapi.lib")

using namespace std;

int main(void)
{
    // Valid file path name (file is there).
    char buffer_1[ ] = "\\\\10.88.144.198\\os\\陈平20-9井.mdb"; 
    char *lpStr1;
    lpStr1 = buffer_1;
    
    // Invalid file path name (file is not there).
    char buffer_2[ ] = "\\\\10.88.144.198\\空"; 
    char *lpStr2;
    lpStr2 = buffer_2;
    
    // Return value from "PathFileExists".
    int retval;
    
    // Search for the presence of a file with a true result.
    retval = PathFileExists(lpStr1);
    if(retval == 1)
    {
        cout << "Search for the file path of : " << lpStr1 << endl;
        cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
        cout << "The return from function is : " << retval << endl;
    }
    
    else
    {
        cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
        cout << "The return from function is : " << retval << endl;
    }
    
    // Search for the presence of a file with a false result.
    retval = PathFileExists(lpStr2);
    
    if(retval == 1)
    {
        cout << "\nThe file requested " << lpStr2 << " is a valid file" << endl;
        cout << "Search for the file path of : " << lpStr2 << endl;
        cout << "The return from function is : " << retval << endl;
    }
    else
    {
        cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
        cout << "The return from function is : " << retval << endl;
    }

    return 0;
}

c_cpp mmal.c

mmal.c

mmal.c
camera_preview_port = camera->output[MMAL_CAMERA_PREVIEW_PORT];
camera_video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
camera_still_port = camera->output[MMAL_CAMERA_CAPTURE_PORT];

{
    MMAL_PARAMETER_CAMERA_CONFIG_T cam_config = {
        { MMAL_PARAMETER_CAMERA_CONFIG, sizeof (cam_config)},
        .max_stills_w = 1280,
        .max_stills_h = 720,
        .stills_yuv422 = 0,
        .one_shot_stills = 0,
        .max_preview_video_w = 1280,
        .max_preview_video_h = 720,
        .num_preview_video_frames = 2,
        .stills_capture_circular_buffer_height = 0,
        .fast_preview_resume = 1,
        .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC
    };

    mmal_port_parameter_set(camera->control, &cam_config.hdr);
}

c_cpp 给定两个字符串S1和S2,要求判断S2能否被S1做循环移位得到的字符串包含。例如:s1 = AABCD和S2 = CDAA,返回true,s1 = ABCD和s2 = ACBD,返回false 。

给定两个字符串S1和S2,要求判断S2能否被S1做循环移位得到的字符串包含。例如:s1 = AABCD和S2 = CDAA,返回true,s1 = ABCD和s2 = ACBD,返回false 。

strcontain.c
int strcontain(const char* const str1, const char* const str2){
    if(NULL == str1 || NULL == str2)
        return 0;
        
    int len1, len2, i;
    char* tmp = NULL;
    len1 = strlen(str1);
    len2 = strlen(str2);
    
    if(len2 > len1)
        return 0;
        
    tmp = (char*)malloc(len1 * 2);
    memset(tmp, 0, len1*2);
    strcpy(tmp, str1);
    
    for(i = 0; i < len1; i++){
        if(strncmp(tmp + i, str2, len2) == 0){
            free(tmp);
            tmp = NULL;
            return 1;
        }
        tmp[len1 + i] = str1[i];
    }
    
    free(tmp);
    tmp = NULL;
    return 0;
}

c_cpp C - 档案io

C - 档案io

file_io.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	if (argc != 3){
		printf("Usage: %s dir1/fileA dir2/fileB\n", argv[0]);
		exit(1);
	}

	FILE *fp1, *fp2;

	char *file1 = argv[1];
	char *file2 = argv[2];
	char *error_str = "Open file ";

	if (NULL == (fp1 = fopen(file1, "r"))){
		sprintf(error_str, "Error open file %s\n", file1);
		perror(error_str);
		exit(1);
	}

	if (NULL == (fp2 = fopen(file2, "w"))){
		sprintf(error_str, "Error open file %s\n", file2);
		perror(error_str);
		exit(1);
	}

	int ch;
	while ((ch = fgetc(fp1)) != EOF){
		fputc(ch, fp2);
	}

	fclose(fp1);
	fclose(fp2);
	
	return 0;
}

c_cpp C - fputc&fgetc

C - fputc&fgetc

fputc_fgetc_example.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	FILE *fp;
	int ch;

	if ( (fp = fopen("file2", "w+")) == NULL) {
		perror("Open file file2\n");
		exit(1);
	}
	while ( (ch = getchar()) != EOF)
		fputc(ch, fp);
	rewind(fp);
	while ( (ch = fgetc(fp)) != EOF)
		putchar(ch);
	fclose(fp);
	return 0;
}

c_cpp C - qsort一个字符串

C - qsort一个字符串

qsort_example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int cmpstringp(const void *p1, const void *p2)
{
	/* The actual arguments to this function are "pointers to
	   pointers to char", but strcmp(3) arguments are "pointers
	   to char", hence the following cast plus dereference */
	return strcmp(* (char * const *) p1, * (char * const *) p2);
}

int main(int argc, char *argv[])
{
	int j;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <string>...\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);

	for (j = 1; j < argc; j++)
		puts(argv[j]);
	exit(EXIT_SUCCESS);
}

c_cpp C - malloc&free

C - malloc&free

malloc_example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
	int number;
	char *msg;
} unit_t;

int main(void)
{
	unit_t *p = malloc(sizeof(unit_t));

	if (p == NULL) {
		printf("out of memory\n");
		exit(1);
	}

	p->number = 3;
	p->msg = malloc(20);
	strcpy(p->msg, "Hello world!");
	printf("number: %d\nmsg: %s\n", p->number, p->msg);
	free(p->msg);
	free(p);
	p = NULL;

	return 0;
}


c_cpp C - malloc&free

C - malloc&free

malloc_example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
	int number;
	char *msg;
} unit_t;

int main(void)
{
	unit_t *p = malloc(sizeof(unit_t));

	if (p == NULL) {
		printf("out of memory\n");
		exit(1);
	}

	p->number = 3;
	p->msg = malloc(20);
	strcpy(p->msg, "Hello world!");
	printf("number: %d\nmsg: %s\n", p->number, p->msg);
	free(p->msg);
	free(p);
	p = NULL;

	return 0;
}


c_cpp 虽然其他数字出现3次,但找到uniq。

虽然其他数字出现3次,但找到uniq。

find_uniq.c
#include <stdio.h>

#define INT_LEN 32

static void
dec2bin(int num,int *buf,size_t len){
    int i = len - 1;
    while (num) {
        buf[i--] = num % 2;
        num /= 2;
    }
}

static int 
bin2dec(int *buf){
    int result = 0;
    int exp = 1;
    for(int i = INT_LEN - 1;i >= 0; i--){
        result += buf[i] * exp;
        exp *= 2;
    }
    return result;
}

int
find_uniq(const int *buf, size_t len){

    int plused[INT_LEN] = {0};
    int result[INT_LEN] = {0};

    for (int i = 0; i < len; i++) {
        int tmp[INT_LEN] = {0};
        dec2bin(buf[i],tmp,INT_LEN);
        for (int j = 0; j < INT_LEN;j++)
            plused[j] += tmp[j];
    }
    for(int i = 0; i < INT_LEN; i++){
        if(plused[i] % 3 == 0)
            result[i] = 0;
        else
            result[i] = 1;
    }
    return bin2dec(result);
}

int main(int argc, const char *argv[]) {
    int arr[] = {9,9,1,2,45,1,2,88,34,34,34,45,1,45,2,9};
    /*
     *int buf[INT_LEN] ={0};
     *dec2bin(65535,buf,INT_LEN);
     *for (int i = 0; i < INT_LEN; i++) {
     *    printf("%d",buf[i]);
     *}
     *printf("\n");
     *printf("%d\n",bin2dec(buf));
     */
    printf("%d\n",find_uniq(arr,sizeof(arr)/sizeof(int)));
    return 0;
}

c_cpp C ++ - strassen算法

C ++ - strassen算法

strassen.cpp
#include <iostream>

using namespace std;

const int N = 6;     //Define the size of the Matrix
  
template<typename T>
void Strassen(int n, T A[][N], T B[][N], T C[][N]); 

template<typename T>
void input(int n, T p[][N]);

template<typename T>
void output(int n, T C[][N]);

int main() {
    //Define three Matrices
    int A[N][N],B[N][N],C[N][N];    
    
    //对A和B矩阵赋值,随便赋值都可以,测试用
    for(int i=0; i<N; i++) {
       for(int j=0; j<N; j++) {
          A[i][j] = i * j;
          B[i][j] = i * j;   
       }        
    }
    
    //调用Strassen方法实现C=A*B
    Strassen(N, A, B, C);
    
    //输出矩阵C中值
    output(N, C);
    
    system("pause");
    return 0;
}

/**The Input Function of Matrix*/
template<typename T>
void input(int n, T p[][N]) {
     for(int i=0; i<n; i++) {
        cout<<"Please Input Line "<<i+1<<endl;
        for(int j=0; j<n; j++) {
           cin>>p[i][j];
        }        
     }
}

/**The Output Fanction of Matrix*/
template<typename T>
void output(int n, T C[][N]) {
     cout<<"The Output Matrix is :"<<endl;
     for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
           cout<<C[i][j]<<" "<<endl;        
        }        
     }     
}

/**Matrix Multiplication as the normal algorithm*/
template<typename T>
void Matrix_Multiply(T A[][N], T B[][N], T C[][N]) {  //Calculating A*B->C
     for(int i=0; i<2; i++) {
        for(int j=0; j<2; j++) {
           C[i][j] = 0;      
           for(int t=0; t<2; t++) {
              C[i][j] = C[i][j] + A[i][t]*B[t][j];        
           }  
        }        
     }
}

/**Matrix Addition*/
template <typename T>
void Matrix_Add(int n, T X[][N], T Y[][N], T Z[][N]) {
     for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
           Z[i][j] = X[i][j] + Y[i][j];        
        }        
     }     
}

/**Matrix Subtraction*/
template <typename T>
void Matrix_Sub(int n, T X[][N], T Y[][N], T Z[][N]) {
     for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
           Z[i][j] = X[i][j] - Y[i][j];        
        }        
     }     
}

/**
* 参数n指定矩阵A,B,C的阶数,因为随着递归调用Strassen函数
* 矩阵A,B,C的阶数是递减的N只是预留足够空间而已
*/
template <typename T> 
void Strassen(int n, T A[][N], T B[][N], T C[][N]) {
     T A11[N][N], A12[N][N], A21[N][N], A22[N][N];
     T B11[N][N], B12[N][N], B21[N][N], B22[N][N];     
     T C11[N][N], C12[N][N], C21[N][N], C22[N][N];
     T M1[N][N], M2[N][N], M3[N][N], M4[N][N], M5[N][N], M6[N][N], M7[N][N];
     T AA[N][N], BB[N][N];
     
     if(n == 2) {  //2-order
        Matrix_Multiply(A, B, C);     
     } else {
        //将矩阵A和B分成阶数相同的四个子矩阵,即分治思想。
        for(int i=0; i<n/2; i++) {
           for(int j=0; j<n/2; j++) {
              A11[i][j] = A[i][j];
              A12[i][j] = A[i][j+n/2];
              A21[i][j] = A[i+n/2][j];
              A22[i][j] = A[i+n/2][j+n/2];
              
              B11[i][j] = B[i][j];
              B12[i][j] = B[i][j+n/2];
              B21[i][j] = B[i+n/2][j];
              B22[i][j] = B[i+n/2][j+n/2];    
           }        
        }  
        
        //Calculate M1 = (A0 + A3) × (B0 + B3)
        Matrix_Add(n/2, A11, A22, AA);
        Matrix_Add(n/2, B11, B22, BB);
        Strassen(n/2, AA, BB, M1);
        
        //Calculate M2 = (A2 + A3) × B0
        Matrix_Add(n/2, A21, A22, AA);
        Strassen(n/2, AA, B11, M2);
        
        //Calculate M3 = A0 × (B1 - B3)
        Matrix_Sub(n/2, B12, B22, BB);
        Strassen(n/2, A11, BB, M3);
        
        //Calculate M4 = A3 × (B2 - B0)
        Matrix_Sub(n/2, B21, B11, BB);
        Strassen(n/2, A22, BB, M4);
        
        //Calculate M5 = (A0 + A1) × B3
        Matrix_Add(n/2, A11, A12, AA);
        Strassen(n/2, AA, B22, M5);
        
        //Calculate M6 = (A2 - A0) × (B0 + B1)
        Matrix_Sub(n/2, A21, A11, AA);
        Matrix_Add(n/2, B11, B12, BB);
        Strassen(n/2, AA, BB, M6);
        
        //Calculate M7 = (A1 - A3) × (B2 + B3)
        Matrix_Sub(n/2, A12, A22, AA);
        Matrix_Add(n/2, B21, B22, BB);
        Strassen(n/2, AA, BB, M7);
        
        //Calculate C0 = M1 + M4 - M5 + M7
        Matrix_Add(n/2, M1, M4, AA);
        Matrix_Sub(n/2, M7, M5, BB);
        Matrix_Add(n/2, AA, BB, C11);
        
        //Calculate C1 = M3 + M5
        Matrix_Add(n/2, M3, M5, C12);
        
        //Calculate C2 = M2 + M4
        Matrix_Add(n/2, M2, M4, C21);
        
        //Calculate C3 = M1 - M2 + M3 + M6
        Matrix_Sub(n/2, M1, M2, AA);
        Matrix_Add(n/2, M3, M6, BB);
        Matrix_Add(n/2, AA, BB, C22);
        
        //Set the result to C[][N]
        for(int i=0; i<n/2; i++) {
           for(int j=0; j<n/2; j++) {
              C[i][j] = C11[i][j];
              C[i][j+n/2] = C12[i][j];
              C[i+n/2][j] = C21[i][j];
              C[i+n/2][j+n/2] = C22[i][j];        
           }        
        }
     }
}