Stored Procedures
Yesterday busy writing this, did not see the Olympic Games, just know old Guo and Xiao Wu again let foreigners depressed a. I wonder if the IOC is going to change the rules about diving again.
In the previous section, we read int *trainid and char** ppcstations with all train IDs and route information containing two stations, completing the last reading process, and then processing the data to get results ... It's over, the future is bright, the road is flat ... The wind is blowing in the back ... MM in the side with the ... Halo, forgot to tell you brothers, younger brother now do not eat tomatoes, change to eat eggs, please take care of the active cooperation, thank you ~ ~ ~
Step three, find the shortest route.
Obviously, first of all to the route information string to do participle processing, know from one station to the next station how long it takes to calculate the shortest route. This article will use. NET class library to split these strings ...... Of course, the normal method can also be completely handled.
To use a regular class, you need to use managed code, I still only talk about the application, do not speak the principle. For friends who are interested in the managed code work mechanism, see the Managed Programming section in MSDN.
Select the properties of the Xstrainquery project, change the use Managed Extensions in general under Configuration properties to Yes, and then add the following precompiled headers in Proc.cpp:
#using
#using
using namespace System;
using namespace System::text::regularexpressions;
For a char* string, it must be converted to a system::string* string type, that is, a pointer to a regular class. This is the way I turn char* into system::string*:
System::String *newstr = system::text::encoding::unicode->getstring (system::text::encoding::unicode-> GetBytes (OLDSTR));
Now it's the play of this step-the regular participle. I do not know if you have mastered. NET's regular style, if like me is a rookie, or let MSDN stand by!
{
Convert the starting point and terminal station from Pbyte to system::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) pbend));
Defining the regular pattern
With two \, because | There is a specific meaning in the regular formula, it has to be added \ escaped before | and \ \ represents the \
Regex *r = new Regex (S) (?: \ \| (.+?),(.+?) Hours) +\\| ");
Define the variables to be used in the loop
int ioldtime = Int_max;
int nfastest = 0;
Everything is ready to start processing the ppcstations pointer array
for (int i=0; i
{
Use R to match Ppcstations[i]
Match *m = R->match (Ppcstations[i]);
Matching groups for all sites
System::text::regularexpressions::group *gstation = M->groups->get_item (1);
Match groups of all times, corresponding to site matching groups in order one by one
System::text::regularexpressions::group *gtime = M->groups->get_item (2);
Define the time required for the route represented by Ppcstations[i]
int inewtime = 0;
Defines whether a starting point bool variable has been found
BOOL bstartfound = FALSE;
J The number of captures from 0 to Gstation, that is, how many sites are captured in total
for (int j=0; jcaptures->count; ++j)
{
if (!bstartfound)
{
If the starting point has not been found, determine 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 starting point corresponding time to the operation meaningless
Continue
}
Else
{
Starting point has been found, starting the cumulative route time
Inewtime + + Int32::P arse (Gtime->captures->get_item (i)->value->trim ());
See if the current capture site is the endpoint, or exit the For loop
if (Gstation->captures->get_item (i)->value->trim ()->equals () (Strend->trim ())
Break
}
}
The For loop has exited, comparing Inewtime and Ioldtime, and recording a smaller route index
if (Inewtime < ioldtime)
{
Ioldtime = Inewtime;
Nfastest = i;
}
}
Now, Nfastest represents the shortest route index, and Ioldtime represents the minimum time required
}
Finally found the shortest route and the shortest time, the work is done! Run and run ... ..... Expect............ Expect............ Anxiety............ Doubts............ No, how come there's no results?
Oh! Forget the last step, you need to spread the results ...
Fourth and final step, outgoing results
The result is defined as this form-' car ID, when needed ', and the resulting results are simple:
{
Defines a 255-byte array that is easier to handle
CHAR pcresult[255];
sprintf (Pcresult, "Train ID%d, takes%d hours", pitrainid[nfastest], ioldtime);
}
How to send Pcresult out? This uses the Srv_paramsetoutput method defined in Srv.h, which is in the following format:
int Srv_paramsetoutput (
Srv_proc * Srvproc,
int N,
BYTE * pbdata,
ULONG Cblen,
BOOL fnull);
where n represents the number of parameters, Pbdata represents parameter data, Cblen represents the length of the parameter (bytes),
If Fnull is set to true, this arguments is forced to null.
You must have noticed that pbdata is a byte pointer, so we also want to convert Pcresult to byte*, which is very simple, direct explicit conversion.
The code to set the arguments is as follows:
{
The actual length of the result (number of bytes)
int nresultlen = strlen (Pcresult);
Outgoing parameters
Srv_paramsetoutput (Srvproc, 3, (pbyte) Pcresult, Nresultlen, FALSE);
After all the work is done, send out senddone information
Srv_senddone (Psrvproc, Srv_done_count | Srv_done_more), 0, 1);
}
Now the work seems to have been completed, quickly take to run it, hehe, run more than a few times ... Run a few more times ... Revel in ... Run again several times ... And then...... Restart your computer and continue to look down.
In the above code, we do not completely release the allocated resources, so each run will cause a memory leak. We have to add a statement that frees up memory space at the end of the code, such as Ppcstations, Pitrainid, and so on.
The first problem was solved.
Originally wanted to write a second question, but the second question and the first problem is the difference between the third step, that is, how to calculate the shortest route algorithm, this, I believe I now use the method must not be the best, do not take out shortcoming.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.