Write the extension stored procedure with vs.net (three, finish)

xiaoxiao2021-03-06  94

Yesterday, I was busy writing this. I didn't watch the Olympics. I just knew that Lao Guo and Xiao Wu let the foreigners have depressed. I don't know if the Olympic Committee is going to change the rules for diving.

In the previous section, we read all the train IDs and routes information containing two stations in the int * trainid and char ** ppcStations, completed the process of last reading data, and below to deal with these data to get results ... ... is over, the future is bright, the road is flat ... The wind is blowing behind ... There are MMs to accompany you ... Dizziness, forget to tell you the brothers, the younger brother does not eat tomatoes now, change Eat eggs, please take care of your active cooperation, thank you ~~~

Step 3, find the shortest route

Obviously, first, you need to make a word processing on the string of the route information, know how long it takes from one stop to the next station to calculate the shortest route. This string will be split in the regular class class provided in the .NET class library ... Of course, it can be processed using a common method.

To use the regular class, you should use the hosting code, I still only use the application, don't speak. For friends who are interested in hosted code work, see the hosted programming section in MSDN.

First select the properties of the XstrainQuery project, change "Using Host" in "Configuration Properties" to "Yes" under "Configuration Properties", and then add the following precompiled header in proc.cpp:

#uing

#uing

Using Namespace System;

Using Namespace System :: Text :: Reg TaRexpressions

for

CHAR * string must be converted to a trust string type, that is, system :: string * pointer can be processed by regular type. I use this way

CHAR * is converted to System :: string *:

System :: string * newstr = system :: text :: Encoding :: unicode-> getString (system :: text :: eNCoding :: unicode-> getBytes (oldstr));

Now that the focus of this step - regular word. I don't know if you have a regular style of .NET, if you are like me is a rookie, or let MSDN stand at any time ~

{

// Translate the starting point and the end point from Ptrib :: String * System :: string * strstart = system :: text :: Encoding :: unicode-> getString (system :: text :: encoding :: unicode-> getBytes (PCHAR) PBSTART));

System :: string * strend = system :: text :: Encoding :: unicode-> getString (system :: text :: Encoding :: unicode-> getBytes (pchar) pband);

// Define the regular pattern // use two // because | There is a specific meaning in the regular formation, plus / escape, and // represents / regex * r = new regex (s " (?: // | (. ?), (. ?) Hour) // | ");

/ / Define the variable IOLDTIME = INT_MAX that will be used in the loop;

INT nfastest = 0;

/ / Everything, start processing PPCStations pointer array for (int i = 0; i

{

// Use r to match PPCStations [i] match * m = r-> match (PPCStations [i]);

// All sites Match Group System :: Text :: Regular = m-> Groups-> Get_Item (1);

// All time matching group, in order to match the site matching group Press the SYSTEM :: Text :: Reg TaRexPressions :: Group * gtime = m-> Groups-> Get_Item (2);

/ / Define the time required by the route represented by PPCStations [i]

INT INEWTIME = 0;

/ / Define if the Bool variable of the starting point has been found BOOL BSTARTFOUND = FALSE;

// Number captured from 0 to gStation, that is, how many sites are captured

FOR

INT J = 0; J

Captures-> count; j)

{

IF (! bstartfound)

{

// If the starting point has not been found, it is determined whether the current capture site is the starting point.

IF (gStation-> Captures-> GET_ITEM (i) -> value-> Trim () -> Equals (strstart-> Trim ())))))

Bstartfound = true;

// Even if the starting point is found, it is directly continue because the time corresponding to the starting point is meaningless.

CONTINUE;

}

Else

{

// I have found the starting point, starting to accumulate the route time inewtime = int32 :: parse (gtime-> captures-> get_item (i) -> value-> Trim ());

// See if the current capture site is end, if you exit the for loop

IF (gstation-> captures-> get_item (i) -> value-> Trim () -> equals (strend-> Trim ()))))

Break;

}

}

// for loop has exited, compare inewtime and ioldtime, and record smaller paths

INEWTIME

{

IOLDTIME = ineewtime;

nfastest = i;

}

}

// Now, NfampTest represents the index of the shortest route. IOLDTIME is the shortest time required to represent the required}

Finally found the shortest route and the shortest time, the work is completed! Running ............ I look forward to ............ Anxiety ............ Doubty ...

Oh! Forgot the last step, you still need to pass the results ...

The fourth step is the last step, the result is out.

The result is defined as such a form - 'Car ID, needs', the result is very simple: {// Define a 255-byte array, easily handle char pcResult [255]; Sprintf (PCRESULT, "Train ID % D, required% D hours ", pitrainid [nfastest], iOLDTIME);

How to send PCRESULT? This uses the SRV_PARAMSETOUTPUT method defined in srv.h, and its format is as follows:

INT SRV_PARAMSETOUTPUT (SRV_PROC * SRVPROC, INT N, BYTE * PBDATA, ULONG CBLEN, BOOL FNULL);

Where: N represents the first number, PBDATA represents parameter data, CBLEN represents parameter length (bytes), and if fnull is set to True, this export will be enforced to null. Everyone will notice that PBData is a Byte The pointer, then we have to convert PCRESULT to BYTE *, which is very simple, direct explicit conversion.

The code sets the code is as follows: {// Get the actual length (bytes) INT NRESULTLEN = Strlen (PCRESULT); // Outline parameter SRV_PARAMSETOUTPUT (SRVPROC, 3, (Pbyte) PCRESULT, NRESULTLEN, FALSE; / After all the work is complete, send Senddone information SRV_SENDDOENE (psrvproc, (srv_done_count | srv_done_more), 0, 1);

Now work seems to have been completed, take it to run, hey, run a few times ... I have run a few times ... Intoxicated ... I run a few times ... Then ... Restart the computer, continue to look down.

In the above code, we did not completely release allocated resources, so each run will cause memory leakage. We must add the statement of the memory to release memory spaces, such as PPCStations, PitrainID, etc.

Dachel, the first question has been resolved.

Originally wanted to write the second question, the difference between the second question and the first question is only the third step, that is, how to calculate the shortest route algorithm, this, I believe that the method I use now is not the best. If you don't take it out.

It turns out that writing articles is so tired ...

转载请注明原文地址:https://www.9cbs.com/read-122733.html

New Post(0)