C's Practice

Source: Internet
Author: User

In order to become familiar with C, I think of a topic to try to do up, found a lot of harvest.

Although is patchwork, the whole procedure does not have the very good structure, therefore the optimization space is still very big, today summarizes this one's harvest.

The title is like this.

The reality is that aircraft routes will avoid some designated areas and then need to update their location relationships with those areas in real time.

So I abstracted a bit. Suppose I enter the coordinates of a "no-fly zone" in a coordinate system and record the range, preferably with a record of the position relationship

The structure is used here.

struct point_date{    int  coor_x;     int coor_y;     int range;     float Dis;} Point [SIZE];

The whole is a location. The line is stored in the array.

File operations

If you want your data to be retained, you need to use the file read and write, so as to be familiar with the use of documents, but also better reflect the practicality

In the operation of a file, the first thing to do is write data through a struct array and then log the array data to a file. That's a basic idea.

     for (i=0; i<size;i++)    {        scanf ("%d%d%d",&point[i].coor_x,&point[i].coor_y,&point[ I].range);        Record ();        }    

Write an array

voidrecord () {FILE*FP; inti; if((Fp=fopen ("point_date","WB"))==NULL) {printf ("cannot open file \ n"); return ; }     for(i=0; i<size;i++)        if(Fwrite (&point[i),sizeof(structPoint_date),1, fp)! =1) {printf ("file write error \ n");    } fclose (FP);} 

Write to the file here also a little bit do not understand, not very understand the meaning of this operation to mark tomorrow to solve

It's in the file, and now it needs to be read.

voidRead () {FILE*FP; inti;  for(i=0; i<size;i++) { fread ( &point[i],sizeof (struct point_date), 1,FP); intTemp,varout,varin;  for(Varout =1; varout<size;varout++) {Temp=point[varout].coor_x; Varin= Varout-1;  while(Varin >=0&& temp<point[varin].coor_x) {Point[varin+1].coor_x =point[varin].coor_x; Varin--; } Point[varin+1].coor_x =temp; } printf ("x=%d y=%d rang=%d \ n", Point[i].coor_x,point[i].coor_y,point[i].range); }}

The red of the mark knows the function but still lacks the deep understanding, here is not only to read out also realizes a sort of function, uses the Hasselblad inserts the sort, later has the time slowly

You can use some other algorithms to improve efficiency.

After finishing the sequence, you can first look at the logic of the main function

voidMain () {inti; intT_x,t_y,t_ran; printf ("Input the point and range \ n");  for(i=0; i<size;i++) //Through this loop implementation of the file write {scanf ("%d%d%d", &point[i].coor_x,&point[i].coor_y,&Point[i].range);        Record (); } read (); //read out the data you just wrote and sort by the size of the x-coordinate to printf ("input the target point x y range\n"); scanf ("%d%d%d",&t_x,&t_y,&T_ran); //Enter the location where you are now floatA[size];  for(i=0; i<size;i++) {Count_dis (t_x,t_y,point[i].coor_x,point[i].coor_y); //Calculates the value of x, Y,  and X and y of the location at which the function of the distance is entered, and the value of Point[i].dis for each point in the array=Count_dis (t_x,t_y,point[i].coor_x,point[i].coor_y); A[i]=Point[i].dis; //Because you want to sort it out so it's easy to operate printf ("The distance is%LF \ n", A[i]); } range (a); //To sort from small to large          for(i=0; i<size;i++) {printf ("\ n"); printf ("The distance is%LF \ n", A[i]); }    }

In fact, after reading the main function basically finished the following to post the sorting and calculate the distance function

floatCount_dis (intT_x,intT_y,intXinty) {        intDis,dx,dy; intpx,py; DoubleA; DX= t_x-x; Dy= T_y-y; PX=ABS (DX); PY=abs (DY); Dis= px*px + py*py; A=sqrt (DIS); returnA; }voidRange (floata[]) {    inti; Doubletemp;  for(i=0; i<size-1; i++){        if(a[i]>=a[i+1]) {temp=A[i]; A[i]= a[i+1]; A[i+1] =temp; }    }}

This involves more practical two things one is abs () This function, is to take absolute value. There is also a sqrt ()

As for the sort, it should be the bubble method is the size Exchange position, small to the front.

This is basically done although not much but their efficiency is not high, or up and down one weeks.

This is actually the prototype, there are a lot of places to optimize the following list below, I have the ability time to try

1. The name of the last three points after sorting the nearest distance, and the azimuth reference coordinate system

2. Go out and do something repetitive, like two times in a sort. Can you open a function independently?

3. The array can be changed into a linked list to operate, improving the scalability

There are, of course, some questions

1. When I read from a file, the array of arrays is reordered, and the contents of the file are changed.

2. The use of fopen fwrite fclose in file operations is still not very understanding

3. The last distance sort of a little bit bug sometimes not according to small to large Depressed in

This is another 1 points, today saw a sentence, I think it is very interesting to come out and share with you

The scientist worked late. Because the day is mediocre, and then at night with guilt to do their own things.

Well, this is the day I play, and at night I find it's not done yet and stay up late ...

It means a lot. You can have more experience.

中文版 version coming soon~~~~~

C's Practice

Related Article

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.