Distance Vector routing algorithm C source code

Source: Internet
Author: User

I haven't been there for a long time. After I learned the model, I tried to plan a graphics processing software. The model I learned is really different from the model I didn't know. My ability to grasp the overall situation of the software has improved a lot. In the past two weeks, I have finished writing the computer graphics program. Two days ago, I got started with direct3d. The sophomore year did not know why the API learned DirectDraw, but I only remember how hard DirectDraw was at that time. Looking at direct3d, we don't feel it at that time. I just learned about matrix transformation in my freshman year and next semester. Now I forget that I have no idea about methods such as plane normal.
I wrote a program for someone else last night, distance vector routing algorithm. At first I understood it wrong. I thought it was a problem of dynamic planning, and I didn't understand it until I wrote it yesterday. I haven't posted any post for a long time. Today I can write something here.
/* Routine. c tc2.0 WINXP */
# Include <stdio. h>
# Include <stdlib. h>
# Include <alloc. h>
# Define routnum 7

Typedef struct
{
Int DIS;
Int from;
} Routnode;

Routnode data [routnum] [routnum];/* route table */

Void initdata (File * pfile);/* read data from the data file and initialize the route table */
Void outputroutdata ();/* output all route tables */
Void communication (INT Recv, int send);/* Send a route table to the Recv node */
Void Exchange ();/* all nodes exchange data once to update the route table */

Void main ()
{
Int start, end;
Int I;
File * pfile;
Pfile = fopen ("data.txt", "R ");
If (pfile = NULL)
{
Printf ("data file failed to open, press any key to quit./N ");
Getch ();
Return;
}
Else
Initdata (pfile );
Fclose (pfile );
Printf ("/ninitial state:/N ");
Outputroutdata ();
For (I = 0; I <routnum; I ++)
{
Exchange ();
}
Printf ("/nstate exchanged:/N ");
Outputroutdata ();

Printf ("input the Start Node (% d-% d):", 0, ROUTNUM-1 );
Scanf ("% d", & START );
Printf ("input the End Node (% d-% d):", 0, ROUTNUM-1 );
Scanf ("% d", & End );
If (START = end | start <0 | Start> 6 | End <0 | End> 6)
{
Printf ("/nwrong input, press any key to quit./N ");
Getch ();
Return;
}
Else
{
Int cur = start;
Int Total = 0;
If (data [start] [end]. Dis <0)
{
Printf ("no routine find! /N ");
Getch ();
Return;
}

Printf ("% C->", cur + 65 );
While (data [cur] [end]. From> = 0)
{
Total + = data [cur] [DATA [cur] [end]. From]. DIS;
Printf ("% C->", data [cur] [end]. From + 65 );
Cur = data [cur] [end]. From;
}
Total + = data [cur] [end]. DIS;
Printf ("% C/ntotal distance = % d", end + 65, total );
Getch ();
Return;
}
}

Void initdata (File * pfile)
{
Char num [10];
Int I = 0;
Char C;
Int M, N;
Fseek (pfile, 0, 0 );
For (m = 0 ;! Feof (pfile) & M <7; m ++)
{
For (n = 0 ;! Feof (pfile) & n <7; n ++)
{
While (! Feof (pfile ))
{
C = fgetc (pfile );
If (C = ',')/* read a number */
{
Num [I] = '/0 ';
Data [m] [N]. Dis = atoi (Num );
Data [m] [N]. From =-1;
I = 0;
Break;
}
Else if (C> = '0' & C <= '9') | C = '-')/* If you read a number or symbol */
{
Num [I ++] = C;
}
}
}
}
}

Void outputroutdata ()
{
Int I, J;
Printf ("");
For (I = 0; I <routnum; I ++)
{
Printf ("<% d>", I );
}
Printf ("/N ");
For (I = 0; I <routnum; I ++)
{
Printf ("% d", I );
For (j = 0; j <routnum; j ++)
{
If (data [I] [J]. Dis <0)/* if no path */
Printf ("-");
Else
Printf ("%-5d", data [I] [J]. Dis );
If (data [I] [J]. From <0)/* if no other node exists */
Printf ("-");
Else
Printf ("% C", data [I] [J]. From + 65 );
}
Printf ("/N ");
}
}

Void communication (INT Recv, int send)
{
Int I;
For (I = 0; I <routnum; I ++)
{
If (data [send] [I]. Dis> 0)/* If the send node has a route to node I */
{
If (data [Recv] [I]. Dis <0)/* if there is no path from Recv to node I */
{
Data [Recv] [I]. Dis = data [send] [I]. DIS + data [Recv] [send]. DIS;
Data [Recv] [I]. From = Send;
}
Else if (data [Recv] [I]. dis> data [send] [I]. DIS + data [Recv] [send]. dis)/* If the existing path is farther than the new path */
{
Data [Recv] [I]. Dis = data [send] [I]. DIS + data [Recv] [send]. DIS;
Data [Recv] [I]. From = Send;
}
}
}
}

Void Exchange ()
{
Int I, J;
For (I = 0; I <routnum; I ++)
{
For (j = 0; j <routnum; j ++)
{
If (data [I] [J]. Dis> 0)/* if there is a path between two nodes */
{
Communication (J, I);/* Send the route table of node I to node J */
}
}
}
}

/* Data.txt */
0, 2,-1,-1, 8,-1, 5,
2, 0,-1, 5,-1,-1,-1,
-1,-1, 0,-1,-1, 9,-1,
-1, 5,-1, 0,-1,-1,-1,
8,-1,-1,-1, 0,-1, 7,
-1,-1, 9,-1,-1, 0, 3,
5,-1,-1,-1, 7, 3, 0,

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.