⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gensort.cpp

📁 labview实例
💻 CPP
字号:
// GenSort.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

extern "C" void __declspec(dllexport) generateRand(int *iArray, int ARRAYSIZE);
extern "C" void __declspec(dllexport) bubbleSort(int *iArray, int ARRAYSIZE);

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

/*
The purpose of this function is to generate an array of random numbers of the size
ARRAYSIZE.  Each number will be an integer between 0 and 100.  The numbers are seeded 
by time plus an offset.  The array is passed into this function by reference
*/
void __declspec(dllexport) generateRand(int *iArray, int ARRAYSIZE)
{
	long int i; 
	time_t sTime;
	time(&sTime);  // gets time to seed the random number
  	srand((unsigned int)sTime*100);  // seeds each random from an offset of the time
	for (i=0;i<ARRAYSIZE;i++)
  	{	
  		iArray[i]=rand()*100/RAND_MAX;  // inserts the random number into the array
  	}	
}	

/*
The purpose of this function is to sort an array of random numbers.  This function
uses the bubblesort algorithm, which has the time complexity O(n^2).  The array
is passed into this function by reference.
*/
void __declspec(dllexport) bubbleSort(int *iArray, int ARRAYSIZE)
{
	int holder, x, y;
	for(x = 0; x < ARRAYSIZE; x++)
    {
    	for(y = 0; y < ARRAYSIZE-1; y++)
      	{
      		if(iArray[y] > iArray[y+1])  
      		{			// compares neighboring elements and swaps if necessary
        		holder = iArray[y+1];
        		iArray[y+1] = iArray[y];
        		iArray[y] = holder;
      		}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -