c语言

c语言

How to write a file, which named CMakeLists.txt,and build c-lang program with libevent

Linux网站管理员 Published the article • 0 comments • 151 views • 2019-04-02 08:57 • 来自相关话题

I want to write a libevent c-lang program on the IDE clion, so I need to write a file, which named CMakeLists.txt to build this program.

### My program source code
```c

#include
#include
#include
#include
#include

#include
#include
#include

struct event ev;
struct timeval tv;

void time_cb(int fd, short event, void *argc) {
printf("timer wakeup\n");
event_add(&ev, &tv);
}

int main(int argc, char* argv[]) {
struct event_base *base = event_base_new();
tv.tv_sec = 10;
tv.tv_usec = 0;
event_assign(&ev, base, -1, 0, time_cb, NULL);
event_add(&ev, &tv);
event_base_dispatch(base);
}
```

### The CMakeLists.txt content
```
cmake_minimum_required(VERSION 3.12)
project(test)

set(CMAKE_CXX_STANDARD 98)

include_directories(/usr/local/Cellar/libevent/2.1.8/include/)
link_directories(/usr/local/Cellar/libevent/2.1.8/lib/)
add_executable(test a.c)
target_link_libraries(test event)
```
> used the function link_directions load the libraries, and use function target_link_libraries to link levent 查看全部

I want to write a libevent c-lang program on the IDE clion, so I need to write a file, which named CMakeLists.txt to build this program.

### My program source code
```c

#include
#include
#include
#include
#include

#include
#include
#include

struct event ev;
struct timeval tv;

void time_cb(int fd, short event, void *argc) {
printf("timer wakeup\n");
event_add(&ev, &tv);
}

int main(int argc, char* argv[]) {
struct event_base *base = event_base_new();
tv.tv_sec = 10;
tv.tv_usec = 0;
event_assign(&ev, base, -1, 0, time_cb, NULL);
event_add(&ev, &tv);
event_base_dispatch(base);
}
```

### The CMakeLists.txt content
```
cmake_minimum_required(VERSION 3.12)
project(test)

set(CMAKE_CXX_STANDARD 98)

include_directories(/usr/local/Cellar/libevent/2.1.8/include/)
link_directories(/usr/local/Cellar/libevent/2.1.8/lib/)
add_executable(test a.c)
target_link_libraries(test event)
```
> used the function link_directions load the libraries, and use function target_link_libraries to link levent

c 管道读取

PHPbt0 Published the article • 0 comments • 255 views • 2018-02-13 12:49 • 来自相关话题

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

#define MAX_BUFFER 128
#define PIPE_RESULT_SIZE 2000

int main(int argc, char *argv[])
{
char result[PIPE_RESULT_SIZE] = {0};

if (execute("ls", result))
printf("%s
", result);

return 0;
}


int execute(const char *cmd, char *result)
{
FILE *pipe;
char buffer[MAX_BUFFER];
pipe = _popen(cmd, "r");

if (pipe == NULL) {
printf("open pipe error
");
return 0;
}
while (!feof(pipe)) {
if (fgets(buffer, sizeof(buffer), pipe))
strcat(result, buffer);
}

_pclose(pipe);
return 1;
} 查看全部


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

#define MAX_BUFFER 128
#define PIPE_RESULT_SIZE 2000

int main(int argc, char *argv[])
{
char result[PIPE_RESULT_SIZE] = {0};

if (execute("ls", result))
printf("%s
", result);

return 0;
}


int execute(const char *cmd, char *result)
{
FILE *pipe;
char buffer[MAX_BUFFER];
pipe = _popen(cmd, "r");

if (pipe == NULL) {
printf("open pipe error
");
return 0;
}
while (!feof(pipe)) {
if (fgets(buffer, sizeof(buffer), pipe))
strcat(result, buffer);
}

_pclose(pipe);
return 1;
}

简约至上 C Plus Plus 的list 容器的学习

PHPheishanlaoyao Published the article • 1 comments • 300 views • 2018-02-13 12:49 • 来自相关话题

这里就只是介绍一下list 容器的学习,其他的都一样,然后我们可以举一反三:
#include <iostream>
#include <list>
using namespace std;

typedef list<int> LIST;

int main(int argc, char* argv[])
{
Hello hello;
LIST list1;
LIST::iterator i; // list1.iterator is error for complier
list1.push_front(2);
list1.push_front(1);
list1.push_back(3);
list1.push_back(4);
cout<<"list1.begin()---------------list1.end()"<<endl;
for(i=list1.begin(); i!=list1.end(); i++){
cout<<*i<<" ";
}
cout<<endl;

system("pause");
return 0;
} 查看全部


这里就只是介绍一下list 容器的学习,其他的都一样,然后我们可以举一反三:


#include <iostream>
#include <list>
using namespace std;

typedef list<int> LIST;

int main(int argc, char* argv[])
{
Hello hello;
LIST list1;
LIST::iterator i; // list1.iterator is error for complier
list1.push_front(2);
list1.push_front(1);
list1.push_back(3);
list1.push_back(4);
cout<<"list1.begin()---------------list1.end()"<<endl;
for(i=list1.begin(); i!=list1.end(); i++){
cout<<*i<<" ";
}
cout<<endl;

system("pause");
return 0;
}


忘不了的冒泡排序

PHP766387hhh Published the article • 0 comments • 266 views • 2018-02-13 12:49 • 来自相关话题

#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int len;
int * arr;
int temp;
cin>>len;
arr=new int[len];
for(int i=0; i<len; i++)
cin>>arr[i];
for(int i=0; i<len; i++){
for(int j=0; j<len-i-1; j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0; i<len; i++)
cout<<arr[i]<<" ";
cout<<endl;
system("pause");
return 0;
} 查看全部


#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int len;
int * arr;
int temp;
cin>>len;
arr=new int[len];
for(int i=0; i<len; i++)
cin>>arr[i];
for(int i=0; i<len; i++){
for(int j=0; j<len-i-1; j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0; i<len; i++)
cout<<arr[i]<<" ";
cout<<endl;
system("pause");
return 0;
}

十进制转化为16进制以下

PHPmanw Published the article • 0 comments • 285 views • 2018-02-13 12:49 • 来自相关话题

#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int number, change;
const int N=1000;
char str[17]={"0123456789ABCDEF"};
char arr[N];
int i=0;
cin>>number>>change;
while(number>0){
arr[i]=str[number%change];
number=number/change;
i++;
}

for(int j=i-1; j>=0; j--)
cout<<arr[j];
cout<<endl;



system("pause");
return 0;
} 查看全部


#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int number, change;
const int N=1000;
char str[17]={"0123456789ABCDEF"};
char arr[N];
int i=0;
cin>>number>>change;
while(number>0){
arr[i]=str[number%change];
number=number/change;
i++;
}

for(int j=i-1; j>=0; j--)
cout<<arr[j];
cout<<endl;



system("pause");
return 0;
}

每次写算法程序的时候总是纠结在动态分配内存上面,这次总算是明白了

PHPtesttest Published the article • 0 comments • 279 views • 2018-02-13 12:49 • 来自相关话题

#include <iostream>

using namespace std;

int main(int argc, char ** argv){

int len=5;
int low=4;
int size=3;
// define array arr[size][low][len]; arr[3][4][5];
int ***arr;

arr=new int **[size];
for(int i=0; i<size; i++){
arr[i]=new int *[low];
}
for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
arr[i][j]=new int[len];
}

//printf("Error last!");

for(int i=0; i<size; i++){
for(int j=0; j<low; j++){
for(int m=0; m<len; m++){
arr[i][j][m]=i+j+m;
cout<<arr[i][j][m]<<" ";
}
cout<<endl;
}
cout<<endl;
}


for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
delete []arr[i][j]; // 删除 列的指针
}
for(int i=0; i<size; i++){
delete []arr[i]; // 删除 行的指针
}
delete []arr; // 删除 高的指针
/*int len=5;
int low=4;
cout<<"Error before1"<<endl;
int ** arr;
arr=new int *[len]; //这样声明的数组是arr[5][4]
cout<<"Error before0"<<endl;
for(int i=0; i<len-1; i++){
arr[i]=new int[low];
}

cout<<"Error before"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
arr[i][j]=i+j;
cout<<arr[i][j]<<" ";
}
cout<<endl;
}

cout<<"-------------------------------"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
//arr[i][j]=i+j;
printf("[%p] ", arr[i][j]);
}
cout<<endl;
}
*/

/*int group;
int *** arr;
**arr=new int **[group];
cin>>group;

for(int i=0; i<group; i++){
int size, form;
for(int i)
}*/

system("pause");
return 0;
} 查看全部


#include <iostream>

using namespace std;

int main(int argc, char ** argv){

int len=5;
int low=4;
int size=3;
// define array arr[size][low][len]; arr[3][4][5];
int ***arr;

arr=new int **[size];
for(int i=0; i<size; i++){
arr[i]=new int *[low];
}
for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
arr[i][j]=new int[len];
}

//printf("Error last!");

for(int i=0; i<size; i++){
for(int j=0; j<low; j++){
for(int m=0; m<len; m++){
arr[i][j][m]=i+j+m;
cout<<arr[i][j][m]<<" ";
}
cout<<endl;
}
cout<<endl;
}


for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
delete []arr[i][j]; // 删除 列的指针
}
for(int i=0; i<size; i++){
delete []arr[i]; // 删除 行的指针
}
delete []arr; // 删除 高的指针
/*int len=5;
int low=4;
cout<<"Error before1"<<endl;
int ** arr;
arr=new int *[len]; //这样声明的数组是arr[5][4]
cout<<"Error before0"<<endl;
for(int i=0; i<len-1; i++){
arr[i]=new int[low];
}

cout<<"Error before"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
arr[i][j]=i+j;
cout<<arr[i][j]<<" ";
}
cout<<endl;
}

cout<<"-------------------------------"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
//arr[i][j]=i+j;
printf("[%p] ", arr[i][j]);
}
cout<<endl;
}
*/

/*int group;
int *** arr;
**arr=new int **[group];
cin>>group;

for(int i=0; i<group; i++){
int size, form;
for(int i)
}*/

system("pause");
return 0;
}

windows 程序设计一

PHPmdgsy Published the article • 0 comments • 267 views • 2018-02-13 12:49 • 来自相关话题

#include <Windows.h>


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){
//MessageBox(NULL, "Hello world!", "Windows 编程", MB_OK | MB_ICONHAND);
WNDCLASSEX wc;
HWND hwnd;
MSG msg;

const char g_szClassName[] = "myWindowClass";

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} 查看全部


#include <Windows.h>


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){
//MessageBox(NULL, "Hello world!", "Windows 编程", MB_OK | MB_ICONHAND);
WNDCLASSEX wc;
HWND hwnd;
MSG msg;

const char g_szClassName[] = "myWindowClass";

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

二进制文件的存储关系

PHPJoshuaTix Published the article • 0 comments • 267 views • 2018-02-13 12:49 • 来自相关话题

今天我尝试着往文件中写入数据,写入的数据就是int 类型的数据(0-9),然后我强制转换了数据类型,将int toByte存入文件中,当我打开文件的时候出现乱码,于是我又使用了
Sublime Text 3 打开这个文件,里面显示的数据是(0001 0203 0405 0607 0809 )
毋庸置疑,这串数字很有规律,他们是(0-9)的十六进制。int 是4个字节,也就是4*8bit=32bit,而文件是使用2*16bit的存储方式存储数据的。 查看全部


今天我尝试着往文件中写入数据,写入的数据就是int 类型的数据(0-9),然后我强制转换了数据类型,将int toByte存入文件中,当我打开文件的时候出现乱码,于是我又使用了


Sublime Text 3 打开这个文件,里面显示的数据是(0001 0203 0405 0607 0809 


毋庸置疑,这串数字很有规律,他们是(0-9)的十六进制。int 是4个字节,也就是4*8bit=32bit,而文件是使用2*16bit的存储方式存储数据的。


二叉树测试,递归遍历,随后加上非递归遍历

PHPRaymondcoant Published the article • 0 comments • 296 views • 2018-02-13 12:49 • 来自相关话题

#ifndef _TREE_H
#define _TREE_H

typedef struct _TREE
{
int data;
struct _TREE * left;
struct _TREE * right;
}TREE, * PTREE;

void init(TREE * tree);
void TreePrint(TREE * tree);
void TreePrint1(TREE * tree);
void TreePrint2(TREE * tree);
TREE * add(TREE * left, int data, TREE * right);
TREE * create(TREE * tree);
void clear(TREE * tree);

#endif#include "Tree.h"
#include <iostream>
#include <stdlib.h>


using namespace std;

void init(TREE * tree)
{
tree->left=NULL;
tree->right=NULL;
}

void TreePrint(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
cout<<tree->data<<" ";
TreePrint(tree->right);
}
}

void TreePrint1(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
TreePrint(tree->right);
cout<<tree->data<<" ";
}
}

void TreePrint2(TREE * tree)
{
if(tree!=NULL)
{
cout<<tree->data<<" ";
TreePrint(tree->left);
TreePrint(tree->right);
}
}

TREE * create(TREE * tree)
{
static int count=0;
if(tree==NULL)
{
tree=(PTREE)malloc(sizeof(TREE));
tree->data=count;
tree->left=tree->right=NULL;
}
else if(count>=10)
{
tree->left=NULL;
tree->right=NULL;
return NULL;
}
else
{
tree->left=create(tree->left);
tree->right=create(tree->right);
}

count++;
return tree;
}

TREE * add(TREE * left, int data, TREE * right)
{
TREE * node;
node=(PTREE)malloc(sizeof(TREE));
node->data=data;
if(left==NULL)
node->left=NULL;
if(right==NULL)
node->right=NULL;
else
{
node->left=left;
node->right=right;
}
return node;
}

void clear(TREE * tree)
{
TREE * left;
TREE * right;
if(tree!=NULL)
{
cout<<"delete "<<tree->data<<" ";
if(tree->left!=NULL)
{
left=tree->left;
clear(left);
}
if(tree->right!=NULL)
{
right=tree->right;
clear(right);
}
free(tree);
}
}#include "Tree.h"

#include <iostream>

using namespace std;

void main(void)
{
TREE * node;
TREE * node1;
TREE * node2;
TREE * node3;
TREE * node4;
TREE * node5;
TREE * node6;

node3=add(NULL, 12, NULL);
node4=add(NULL, 13, NULL);
node5=add(NULL, 14, NULL);
node6=add(NULL, 15, NULL);
node1=add(node3, 10, node4);
node2=add(node5, 11, node6);
node=add(node1, 9, node2);
//node->data=9;
//node->left=node1;
//node->right=node2;

TreePrint(node);
cout<<endl;
TreePrint1(node);
cout<<endl;
TreePrint2(node);
cout<<endl;
clear(node);
system("pause");
}没用的函数大家可以忽略掉就ok了
测试结果 查看全部


#ifndef _TREE_H
#define _TREE_H

typedef struct _TREE
{
int data;
struct _TREE * left;
struct _TREE * right;
}TREE, * PTREE;

void init(TREE * tree);
void TreePrint(TREE * tree);
void TreePrint1(TREE * tree);
void TreePrint2(TREE * tree);
TREE * add(TREE * left, int data, TREE * right);
TREE * create(TREE * tree);
void clear(TREE * tree);

#endif

#include "Tree.h"
#include <iostream>
#include <stdlib.h>


using namespace std;

void init(TREE * tree)
{
tree->left=NULL;
tree->right=NULL;
}

void TreePrint(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
cout<<tree->data<<" ";
TreePrint(tree->right);
}
}

void TreePrint1(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
TreePrint(tree->right);
cout<<tree->data<<" ";
}
}

void TreePrint2(TREE * tree)
{
if(tree!=NULL)
{
cout<<tree->data<<" ";
TreePrint(tree->left);
TreePrint(tree->right);
}
}

TREE * create(TREE * tree)
{
static int count=0;
if(tree==NULL)
{
tree=(PTREE)malloc(sizeof(TREE));
tree->data=count;
tree->left=tree->right=NULL;
}
else if(count>=10)
{
tree->left=NULL;
tree->right=NULL;
return NULL;
}
else
{
tree->left=create(tree->left);
tree->right=create(tree->right);
}

count++;
return tree;
}

TREE * add(TREE * left, int data, TREE * right)
{
TREE * node;
node=(PTREE)malloc(sizeof(TREE));
node->data=data;
if(left==NULL)
node->left=NULL;
if(right==NULL)
node->right=NULL;
else
{
node->left=left;
node->right=right;
}
return node;
}

void clear(TREE * tree)
{
TREE * left;
TREE * right;
if(tree!=NULL)
{
cout<<"delete "<<tree->data<<" ";
if(tree->left!=NULL)
{
left=tree->left;
clear(left);
}
if(tree->right!=NULL)
{
right=tree->right;
clear(right);
}
free(tree);
}
}

#include "Tree.h"

#include <iostream>

using namespace std;

void main(void)
{
TREE * node;
TREE * node1;
TREE * node2;
TREE * node3;
TREE * node4;
TREE * node5;
TREE * node6;

node3=add(NULL, 12, NULL);
node4=add(NULL, 13, NULL);
node5=add(NULL, 14, NULL);
node6=add(NULL, 15, NULL);
node1=add(node3, 10, node4);
node2=add(node5, 11, node6);
node=add(node1, 9, node2);
//node->data=9;
//node->left=node1;
//node->right=node2;

TreePrint(node);
cout<<endl;
TreePrint1(node);
cout<<endl;
TreePrint2(node);
cout<<endl;
clear(node);
system("pause");
}


没用的函数大家可以忽略掉就ok了


测试结果




排序算法集锦之选择排序

PHPhelloworld Published the article • 0 comments • 295 views • 2018-02-13 12:49 • 来自相关话题

选择排序很常见,小学的时候排对,老师是由低到高的顺序排列的,先选出最矮的同学与第一个位置的同学换个位置,然后从第二个同学到最后一个同学中找出第二矮的同学,然后置换位置……同理直至最后一个同学的位置确定下来。
我接下来列出俩个方案,原理一样,但是这个是个人的理解了,第二方案使用的方法是分治的思维,问题小化,然后综合起来,废话不多说,直接看源代码:
第一个函数是交换函数,就不用多介绍了:
void Swap(int& a, int& b){ int temp=a; a=b; b=temp;}

方案一:
void SelectionSort1(int * arr, int n){ int index; int i, j; for(i=0; i<n-1; i++) { index=i; for(j=i+1; j<n; j++) if(arr[index]>arr[j]) index=j; Swap(arr[i], arr[index]); }}

方案二:
void FindMin(int * arr, int first, int last){ int i; int index=first; for(i=first+1; i<last; i++) if(arr[index]>arr[i]) index=i; Swap(arr[first], arr[index]);} void SelectionSort(int * arr, int n){ int i; for(i=0; i<n-1; i++)
FindMin(arr, i, n);}
这里主要说一下方案二,我们将问题细化,分别求出first 到 最后的一个位置的最小值,然后将其赋值给前一个位置(first位置)的数。
  查看全部


选择排序很常见,小学的时候排对,老师是由低到高的顺序排列的,先选出最矮的同学与第一个位置的同学换个位置,然后从第二个同学到最后一个同学中找出第二矮的同学,然后置换位置……同理直至最后一个同学的位置确定下来。


我接下来列出俩个方案,原理一样,但是这个是个人的理解了,第二方案使用的方法是分治的思维,问题小化,然后综合起来,废话不多说,直接看源代码:


第一个函数是交换函数,就不用多介绍了:


void Swap(int& a, int& b){ int temp=a; a=b; b=temp;}




方案一:


void SelectionSort1(int * arr, int n){ int index; int i, j; for(i=0; i<n-1; i++) { index=i; for(j=i+1; j<n; j++) if(arr[index]>arr[j]) index=j; Swap(arr[i], arr[index]); }}




方案二:


void FindMin(int * arr, int first, int last){ int i; int index=first; for(i=first+1; i<last; i++) if(arr[index]>arr[i]) index=i; Swap(arr[first], arr[index]);} void SelectionSort(int * arr, int n){ int i; for(i=0; i<n-1; i++)
FindMin(arr, i, n);}


这里主要说一下方案二,我们将问题细化,分别求出first 到 最后的一个位置的最小值,然后将其赋值给前一个位置(first位置)的数。


 


How to write a file, which named CMakeLists.txt,and build c-lang program with libevent

Linux网站管理员 Published the article • 0 comments • 151 views • 2019-04-02 08:57 • 来自相关话题

I want to write a libevent c-lang program on the IDE clion, so I need to write a file, which named CMakeLists.txt to build this program.

### My program source code
```c

#include
#include
#include
#include
#include

#include
#include
#include

struct event ev;
struct timeval tv;

void time_cb(int fd, short event, void *argc) {
printf("timer wakeup\n");
event_add(&ev, &tv);
}

int main(int argc, char* argv[]) {
struct event_base *base = event_base_new();
tv.tv_sec = 10;
tv.tv_usec = 0;
event_assign(&ev, base, -1, 0, time_cb, NULL);
event_add(&ev, &tv);
event_base_dispatch(base);
}
```

### The CMakeLists.txt content
```
cmake_minimum_required(VERSION 3.12)
project(test)

set(CMAKE_CXX_STANDARD 98)

include_directories(/usr/local/Cellar/libevent/2.1.8/include/)
link_directories(/usr/local/Cellar/libevent/2.1.8/lib/)
add_executable(test a.c)
target_link_libraries(test event)
```
> used the function link_directions load the libraries, and use function target_link_libraries to link levent 查看全部

I want to write a libevent c-lang program on the IDE clion, so I need to write a file, which named CMakeLists.txt to build this program.

### My program source code
```c

#include
#include
#include
#include
#include

#include
#include
#include

struct event ev;
struct timeval tv;

void time_cb(int fd, short event, void *argc) {
printf("timer wakeup\n");
event_add(&ev, &tv);
}

int main(int argc, char* argv[]) {
struct event_base *base = event_base_new();
tv.tv_sec = 10;
tv.tv_usec = 0;
event_assign(&ev, base, -1, 0, time_cb, NULL);
event_add(&ev, &tv);
event_base_dispatch(base);
}
```

### The CMakeLists.txt content
```
cmake_minimum_required(VERSION 3.12)
project(test)

set(CMAKE_CXX_STANDARD 98)

include_directories(/usr/local/Cellar/libevent/2.1.8/include/)
link_directories(/usr/local/Cellar/libevent/2.1.8/lib/)
add_executable(test a.c)
target_link_libraries(test event)
```
> used the function link_directions load the libraries, and use function target_link_libraries to link levent

c 管道读取

PHPbt0 Published the article • 0 comments • 255 views • 2018-02-13 12:49 • 来自相关话题

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

#define MAX_BUFFER 128
#define PIPE_RESULT_SIZE 2000

int main(int argc, char *argv[])
{
char result[PIPE_RESULT_SIZE] = {0};

if (execute("ls", result))
printf("%s
", result);

return 0;
}


int execute(const char *cmd, char *result)
{
FILE *pipe;
char buffer[MAX_BUFFER];
pipe = _popen(cmd, "r");

if (pipe == NULL) {
printf("open pipe error
");
return 0;
}
while (!feof(pipe)) {
if (fgets(buffer, sizeof(buffer), pipe))
strcat(result, buffer);
}

_pclose(pipe);
return 1;
} 查看全部


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

#define MAX_BUFFER 128
#define PIPE_RESULT_SIZE 2000

int main(int argc, char *argv[])
{
char result[PIPE_RESULT_SIZE] = {0};

if (execute("ls", result))
printf("%s
", result);

return 0;
}


int execute(const char *cmd, char *result)
{
FILE *pipe;
char buffer[MAX_BUFFER];
pipe = _popen(cmd, "r");

if (pipe == NULL) {
printf("open pipe error
");
return 0;
}
while (!feof(pipe)) {
if (fgets(buffer, sizeof(buffer), pipe))
strcat(result, buffer);
}

_pclose(pipe);
return 1;
}

简约至上 C Plus Plus 的list 容器的学习

PHPheishanlaoyao Published the article • 1 comments • 300 views • 2018-02-13 12:49 • 来自相关话题

这里就只是介绍一下list 容器的学习,其他的都一样,然后我们可以举一反三:
#include <iostream>
#include <list>
using namespace std;

typedef list<int> LIST;

int main(int argc, char* argv[])
{
Hello hello;
LIST list1;
LIST::iterator i; // list1.iterator is error for complier
list1.push_front(2);
list1.push_front(1);
list1.push_back(3);
list1.push_back(4);
cout<<"list1.begin()---------------list1.end()"<<endl;
for(i=list1.begin(); i!=list1.end(); i++){
cout<<*i<<" ";
}
cout<<endl;

system("pause");
return 0;
} 查看全部


这里就只是介绍一下list 容器的学习,其他的都一样,然后我们可以举一反三:


#include <iostream>
#include <list>
using namespace std;

typedef list<int> LIST;

int main(int argc, char* argv[])
{
Hello hello;
LIST list1;
LIST::iterator i; // list1.iterator is error for complier
list1.push_front(2);
list1.push_front(1);
list1.push_back(3);
list1.push_back(4);
cout<<"list1.begin()---------------list1.end()"<<endl;
for(i=list1.begin(); i!=list1.end(); i++){
cout<<*i<<" ";
}
cout<<endl;

system("pause");
return 0;
}


忘不了的冒泡排序

PHP766387hhh Published the article • 0 comments • 266 views • 2018-02-13 12:49 • 来自相关话题

#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int len;
int * arr;
int temp;
cin>>len;
arr=new int[len];
for(int i=0; i<len; i++)
cin>>arr[i];
for(int i=0; i<len; i++){
for(int j=0; j<len-i-1; j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0; i<len; i++)
cout<<arr[i]<<" ";
cout<<endl;
system("pause");
return 0;
} 查看全部


#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int len;
int * arr;
int temp;
cin>>len;
arr=new int[len];
for(int i=0; i<len; i++)
cin>>arr[i];
for(int i=0; i<len; i++){
for(int j=0; j<len-i-1; j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0; i<len; i++)
cout<<arr[i]<<" ";
cout<<endl;
system("pause");
return 0;
}

十进制转化为16进制以下

PHPmanw Published the article • 0 comments • 285 views • 2018-02-13 12:49 • 来自相关话题

#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int number, change;
const int N=1000;
char str[17]={"0123456789ABCDEF"};
char arr[N];
int i=0;
cin>>number>>change;
while(number>0){
arr[i]=str[number%change];
number=number/change;
i++;
}

for(int j=i-1; j>=0; j--)
cout<<arr[j];
cout<<endl;



system("pause");
return 0;
} 查看全部


#include <iostream>

using namespace std;

int main(int argc, char ** argv){
int number, change;
const int N=1000;
char str[17]={"0123456789ABCDEF"};
char arr[N];
int i=0;
cin>>number>>change;
while(number>0){
arr[i]=str[number%change];
number=number/change;
i++;
}

for(int j=i-1; j>=0; j--)
cout<<arr[j];
cout<<endl;



system("pause");
return 0;
}

每次写算法程序的时候总是纠结在动态分配内存上面,这次总算是明白了

PHPtesttest Published the article • 0 comments • 279 views • 2018-02-13 12:49 • 来自相关话题

#include <iostream>

using namespace std;

int main(int argc, char ** argv){

int len=5;
int low=4;
int size=3;
// define array arr[size][low][len]; arr[3][4][5];
int ***arr;

arr=new int **[size];
for(int i=0; i<size; i++){
arr[i]=new int *[low];
}
for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
arr[i][j]=new int[len];
}

//printf("Error last!");

for(int i=0; i<size; i++){
for(int j=0; j<low; j++){
for(int m=0; m<len; m++){
arr[i][j][m]=i+j+m;
cout<<arr[i][j][m]<<" ";
}
cout<<endl;
}
cout<<endl;
}


for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
delete []arr[i][j]; // 删除 列的指针
}
for(int i=0; i<size; i++){
delete []arr[i]; // 删除 行的指针
}
delete []arr; // 删除 高的指针
/*int len=5;
int low=4;
cout<<"Error before1"<<endl;
int ** arr;
arr=new int *[len]; //这样声明的数组是arr[5][4]
cout<<"Error before0"<<endl;
for(int i=0; i<len-1; i++){
arr[i]=new int[low];
}

cout<<"Error before"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
arr[i][j]=i+j;
cout<<arr[i][j]<<" ";
}
cout<<endl;
}

cout<<"-------------------------------"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
//arr[i][j]=i+j;
printf("[%p] ", arr[i][j]);
}
cout<<endl;
}
*/

/*int group;
int *** arr;
**arr=new int **[group];
cin>>group;

for(int i=0; i<group; i++){
int size, form;
for(int i)
}*/

system("pause");
return 0;
} 查看全部


#include <iostream>

using namespace std;

int main(int argc, char ** argv){

int len=5;
int low=4;
int size=3;
// define array arr[size][low][len]; arr[3][4][5];
int ***arr;

arr=new int **[size];
for(int i=0; i<size; i++){
arr[i]=new int *[low];
}
for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
arr[i][j]=new int[len];
}

//printf("Error last!");

for(int i=0; i<size; i++){
for(int j=0; j<low; j++){
for(int m=0; m<len; m++){
arr[i][j][m]=i+j+m;
cout<<arr[i][j][m]<<" ";
}
cout<<endl;
}
cout<<endl;
}


for(int i=0; i<size; i++){
for(int j=0; j<low; j++)
delete []arr[i][j]; // 删除 列的指针
}
for(int i=0; i<size; i++){
delete []arr[i]; // 删除 行的指针
}
delete []arr; // 删除 高的指针
/*int len=5;
int low=4;
cout<<"Error before1"<<endl;
int ** arr;
arr=new int *[len]; //这样声明的数组是arr[5][4]
cout<<"Error before0"<<endl;
for(int i=0; i<len-1; i++){
arr[i]=new int[low];
}

cout<<"Error before"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
arr[i][j]=i+j;
cout<<arr[i][j]<<" ";
}
cout<<endl;
}

cout<<"-------------------------------"<<endl;

for(int i=0; i<low; i++){
for(int j=0; j<len; j++){
//arr[i][j]=i+j;
printf("[%p] ", arr[i][j]);
}
cout<<endl;
}
*/

/*int group;
int *** arr;
**arr=new int **[group];
cin>>group;

for(int i=0; i<group; i++){
int size, form;
for(int i)
}*/

system("pause");
return 0;
}

windows 程序设计一

PHPmdgsy Published the article • 0 comments • 267 views • 2018-02-13 12:49 • 来自相关话题

#include <Windows.h>


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){
//MessageBox(NULL, "Hello world!", "Windows 编程", MB_OK | MB_ICONHAND);
WNDCLASSEX wc;
HWND hwnd;
MSG msg;

const char g_szClassName[] = "myWindowClass";

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} 查看全部


#include <Windows.h>


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){
//MessageBox(NULL, "Hello world!", "Windows 编程", MB_OK | MB_ICONHAND);
WNDCLASSEX wc;
HWND hwnd;
MSG msg;

const char g_szClassName[] = "myWindowClass";

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

二进制文件的存储关系

PHPJoshuaTix Published the article • 0 comments • 267 views • 2018-02-13 12:49 • 来自相关话题

今天我尝试着往文件中写入数据,写入的数据就是int 类型的数据(0-9),然后我强制转换了数据类型,将int toByte存入文件中,当我打开文件的时候出现乱码,于是我又使用了
Sublime Text 3 打开这个文件,里面显示的数据是(0001 0203 0405 0607 0809 )
毋庸置疑,这串数字很有规律,他们是(0-9)的十六进制。int 是4个字节,也就是4*8bit=32bit,而文件是使用2*16bit的存储方式存储数据的。 查看全部


今天我尝试着往文件中写入数据,写入的数据就是int 类型的数据(0-9),然后我强制转换了数据类型,将int toByte存入文件中,当我打开文件的时候出现乱码,于是我又使用了


Sublime Text 3 打开这个文件,里面显示的数据是(0001 0203 0405 0607 0809 


毋庸置疑,这串数字很有规律,他们是(0-9)的十六进制。int 是4个字节,也就是4*8bit=32bit,而文件是使用2*16bit的存储方式存储数据的。


二叉树测试,递归遍历,随后加上非递归遍历

PHPRaymondcoant Published the article • 0 comments • 296 views • 2018-02-13 12:49 • 来自相关话题

#ifndef _TREE_H
#define _TREE_H

typedef struct _TREE
{
int data;
struct _TREE * left;
struct _TREE * right;
}TREE, * PTREE;

void init(TREE * tree);
void TreePrint(TREE * tree);
void TreePrint1(TREE * tree);
void TreePrint2(TREE * tree);
TREE * add(TREE * left, int data, TREE * right);
TREE * create(TREE * tree);
void clear(TREE * tree);

#endif#include "Tree.h"
#include <iostream>
#include <stdlib.h>


using namespace std;

void init(TREE * tree)
{
tree->left=NULL;
tree->right=NULL;
}

void TreePrint(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
cout<<tree->data<<" ";
TreePrint(tree->right);
}
}

void TreePrint1(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
TreePrint(tree->right);
cout<<tree->data<<" ";
}
}

void TreePrint2(TREE * tree)
{
if(tree!=NULL)
{
cout<<tree->data<<" ";
TreePrint(tree->left);
TreePrint(tree->right);
}
}

TREE * create(TREE * tree)
{
static int count=0;
if(tree==NULL)
{
tree=(PTREE)malloc(sizeof(TREE));
tree->data=count;
tree->left=tree->right=NULL;
}
else if(count>=10)
{
tree->left=NULL;
tree->right=NULL;
return NULL;
}
else
{
tree->left=create(tree->left);
tree->right=create(tree->right);
}

count++;
return tree;
}

TREE * add(TREE * left, int data, TREE * right)
{
TREE * node;
node=(PTREE)malloc(sizeof(TREE));
node->data=data;
if(left==NULL)
node->left=NULL;
if(right==NULL)
node->right=NULL;
else
{
node->left=left;
node->right=right;
}
return node;
}

void clear(TREE * tree)
{
TREE * left;
TREE * right;
if(tree!=NULL)
{
cout<<"delete "<<tree->data<<" ";
if(tree->left!=NULL)
{
left=tree->left;
clear(left);
}
if(tree->right!=NULL)
{
right=tree->right;
clear(right);
}
free(tree);
}
}#include "Tree.h"

#include <iostream>

using namespace std;

void main(void)
{
TREE * node;
TREE * node1;
TREE * node2;
TREE * node3;
TREE * node4;
TREE * node5;
TREE * node6;

node3=add(NULL, 12, NULL);
node4=add(NULL, 13, NULL);
node5=add(NULL, 14, NULL);
node6=add(NULL, 15, NULL);
node1=add(node3, 10, node4);
node2=add(node5, 11, node6);
node=add(node1, 9, node2);
//node->data=9;
//node->left=node1;
//node->right=node2;

TreePrint(node);
cout<<endl;
TreePrint1(node);
cout<<endl;
TreePrint2(node);
cout<<endl;
clear(node);
system("pause");
}没用的函数大家可以忽略掉就ok了
测试结果 查看全部


#ifndef _TREE_H
#define _TREE_H

typedef struct _TREE
{
int data;
struct _TREE * left;
struct _TREE * right;
}TREE, * PTREE;

void init(TREE * tree);
void TreePrint(TREE * tree);
void TreePrint1(TREE * tree);
void TreePrint2(TREE * tree);
TREE * add(TREE * left, int data, TREE * right);
TREE * create(TREE * tree);
void clear(TREE * tree);

#endif

#include "Tree.h"
#include <iostream>
#include <stdlib.h>


using namespace std;

void init(TREE * tree)
{
tree->left=NULL;
tree->right=NULL;
}

void TreePrint(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
cout<<tree->data<<" ";
TreePrint(tree->right);
}
}

void TreePrint1(TREE * tree)
{
if(tree!=NULL)
{
TreePrint(tree->left);
TreePrint(tree->right);
cout<<tree->data<<" ";
}
}

void TreePrint2(TREE * tree)
{
if(tree!=NULL)
{
cout<<tree->data<<" ";
TreePrint(tree->left);
TreePrint(tree->right);
}
}

TREE * create(TREE * tree)
{
static int count=0;
if(tree==NULL)
{
tree=(PTREE)malloc(sizeof(TREE));
tree->data=count;
tree->left=tree->right=NULL;
}
else if(count>=10)
{
tree->left=NULL;
tree->right=NULL;
return NULL;
}
else
{
tree->left=create(tree->left);
tree->right=create(tree->right);
}

count++;
return tree;
}

TREE * add(TREE * left, int data, TREE * right)
{
TREE * node;
node=(PTREE)malloc(sizeof(TREE));
node->data=data;
if(left==NULL)
node->left=NULL;
if(right==NULL)
node->right=NULL;
else
{
node->left=left;
node->right=right;
}
return node;
}

void clear(TREE * tree)
{
TREE * left;
TREE * right;
if(tree!=NULL)
{
cout<<"delete "<<tree->data<<" ";
if(tree->left!=NULL)
{
left=tree->left;
clear(left);
}
if(tree->right!=NULL)
{
right=tree->right;
clear(right);
}
free(tree);
}
}

#include "Tree.h"

#include <iostream>

using namespace std;

void main(void)
{
TREE * node;
TREE * node1;
TREE * node2;
TREE * node3;
TREE * node4;
TREE * node5;
TREE * node6;

node3=add(NULL, 12, NULL);
node4=add(NULL, 13, NULL);
node5=add(NULL, 14, NULL);
node6=add(NULL, 15, NULL);
node1=add(node3, 10, node4);
node2=add(node5, 11, node6);
node=add(node1, 9, node2);
//node->data=9;
//node->left=node1;
//node->right=node2;

TreePrint(node);
cout<<endl;
TreePrint1(node);
cout<<endl;
TreePrint2(node);
cout<<endl;
clear(node);
system("pause");
}


没用的函数大家可以忽略掉就ok了


测试结果




排序算法集锦之选择排序

PHPhelloworld Published the article • 0 comments • 295 views • 2018-02-13 12:49 • 来自相关话题

选择排序很常见,小学的时候排对,老师是由低到高的顺序排列的,先选出最矮的同学与第一个位置的同学换个位置,然后从第二个同学到最后一个同学中找出第二矮的同学,然后置换位置……同理直至最后一个同学的位置确定下来。
我接下来列出俩个方案,原理一样,但是这个是个人的理解了,第二方案使用的方法是分治的思维,问题小化,然后综合起来,废话不多说,直接看源代码:
第一个函数是交换函数,就不用多介绍了:
void Swap(int& a, int& b){ int temp=a; a=b; b=temp;}

方案一:
void SelectionSort1(int * arr, int n){ int index; int i, j; for(i=0; i<n-1; i++) { index=i; for(j=i+1; j<n; j++) if(arr[index]>arr[j]) index=j; Swap(arr[i], arr[index]); }}

方案二:
void FindMin(int * arr, int first, int last){ int i; int index=first; for(i=first+1; i<last; i++) if(arr[index]>arr[i]) index=i; Swap(arr[first], arr[index]);} void SelectionSort(int * arr, int n){ int i; for(i=0; i<n-1; i++)
FindMin(arr, i, n);}
这里主要说一下方案二,我们将问题细化,分别求出first 到 最后的一个位置的最小值,然后将其赋值给前一个位置(first位置)的数。
  查看全部


选择排序很常见,小学的时候排对,老师是由低到高的顺序排列的,先选出最矮的同学与第一个位置的同学换个位置,然后从第二个同学到最后一个同学中找出第二矮的同学,然后置换位置……同理直至最后一个同学的位置确定下来。


我接下来列出俩个方案,原理一样,但是这个是个人的理解了,第二方案使用的方法是分治的思维,问题小化,然后综合起来,废话不多说,直接看源代码:


第一个函数是交换函数,就不用多介绍了:


void Swap(int& a, int& b){ int temp=a; a=b; b=temp;}




方案一:


void SelectionSort1(int * arr, int n){ int index; int i, j; for(i=0; i<n-1; i++) { index=i; for(j=i+1; j<n; j++) if(arr[index]>arr[j]) index=j; Swap(arr[i], arr[index]); }}




方案二:


void FindMin(int * arr, int first, int last){ int i; int index=first; for(i=first+1; i<last; i++) if(arr[index]>arr[i]) index=i; Swap(arr[first], arr[index]);} void SelectionSort(int * arr, int n){ int i; for(i=0; i<n-1; i++)
FindMin(arr, i, n);}


这里主要说一下方案二,我们将问题细化,分别求出first 到 最后的一个位置的最小值,然后将其赋值给前一个位置(first位置)的数。