呵呵,今天刚进行代码第一次测试,相当凑合,更多功能正在完善。
程序内容是实现从一个文本文件读取桩号内容,然后自动识别判断,进行路桩设定。打印到一个文本文件里,该文本文件采用逗号分隔符的方式存储,这样就可以直接使用Excel读取和编辑了。
- #include"stdio.h"
- #include"stdlib.h"
- #include"string.h"
- #include"math.h"
- #define TAN(angle) tan(M_PI*(angle/180))
- #define SIN(angle) sin(M_PI*(angle/180))
- #define mid_number(X,H,angle) (X+(H/TAN(angle))/2)
- #define f_2_MAX(x) ((x)<0?-2+2*(int)((x)/2):2*(int)((x)/2))
- #define f_4_MAX(x) ((x)<0?-4+4*(int)((x)/4):4*(int)((x)/4))
- void get_an_han(float,float,float,float,float *,int * );
- void get_ming_han(float,float,float,float,float *,int * );
- void get_da_zhong_qiao(float,float,float,float,float *,int * );
- FILE *fp_source , *fp_result;
- float line_1[100]={0};
- char str[7],str_1[]="暗涵",str_2[]="明涵",str_3[]="大中桥";
- void main(void)
- {
- int i=0,line_num=0,j=0;
- char culvert_type=0; //涵洞类型标记符号,用于switch case分支语句
- char str[20]; //存储涵洞类型名,
- float angle,O,H,y,X;
- /////////////////////////////////////////////////////////////////////////
- // FILE *fp_source , *fp_result;
- int ch=0;
- float mubiao[5]={0.0};
- if((fp_source=fopen("source.csv","r"))==NULL)
- {
- printf("can not open file\n");
- exit(0);
- }
- if((fp_result=fopen("result.csv","w+"))==NULL)
- {
- printf("can not open file\n");
- exit(0);
- }
- while(ch!=EOF)
- {
- //////////////////////////////////////////////////////////////////////
- /*
- printf("请输入桥涵类型:");
- scanf("%s",str);
- printf("\n %s \n\n",str);
- printf("请输入中心桩号、路宽、倾角、桥长四个参数:\n");
- printf("原点O=");
- scanf("%f",&O);
- printf("路宽H=");
- scanf("%f",&H);
- printf("倾角angle=");
- scanf("%f",&angle);
- printf("桥长X=");
- scanf("%f",&X);
- */
- ////////////////////////////////////////////////////////////////////
- i=0;
- while((ch=fgetc(fp_source))!=',')
- {
- // printf("%c", ch);
- str[i++]=ch;
- }
- str[i]='\0';
-
- ///////////////////////////////////////////////////////
- for(i=0;i<4;i++)
- {
-
- fscanf(fp_source,"%f", &mubiao[i]);
- printf( "%.2f", mubiao[i]);
-
- ch=fgetc(fp_source); //读取数字后面的符号,如果是逗号打印出来并跟着两个空格
- if(ch==',') printf("%c ", ch);
- else printf("%c",ch); //如果后面不是跟的逗号而是换行符直接打印出来,显示上保持一致
- }
- O= mubiao[0];
- H= mubiao[1];
- angle=mubiao[2];
- X= mubiao[3];
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////
- if(strcmp(str,str_1)==0) culvert_type=1; //暗涵
- else if(strcmp(str,str_2)==0) culvert_type=2; //明涵
- else if(strcmp(str,str_3)==0) culvert_type=3; //大中桥
- switch(culvert_type)
- {
- case 1:printf(" 这个目标为暗涵"); culvert_type=0; get_an_han(O,H,angle,X,line_1,&line_num);
- break;
- case 2:printf(" 这个目标为明涵"); culvert_type=0; get_ming_han(O,H,angle,X,line_1,&line_num);
- break;
- case 3:printf(" 这个目标为大中桥"); culvert_type=0; get_da_zhong_qiao(O,H,angle,X,line_1,&line_num);
- break;
- default:printf("ERROR");
-
- }
- printf("\n\n");
- ///////////////////////////////////////////////////////////////////////////////////////////////////////
- }
- for(i=0;i<100;i++)
- {
- printf("line_1[%d]=%f\n",i,line_1[i]);
- if(line_1[i+1]==0) break;
- }
- printf("line_num=%d",line_num);
- fclose(fp_source);
- fclose(fp_result);
- getch();
-
-
- }
- /****************************计算子函数*********************************/
- void get_an_han(float O,float H,float angle,float X,float * line_1,int * line_num)
- {
- float an_han[2],y;
-
- printf("\n这是咋了??");
- /****涵洞2个桩号求解****/
- y=mid_number(O,H,angle); printf("\ny= %f",y);
- X=(X/SIN(angle))-1;
- an_han[0]=y-(f_4_MAX(X)/2);
- an_han[1]=y+(f_4_MAX(X)/2);
-
- /*************/
- *(line_1+(*line_num))=an_han[0]; (*line_num)++;
- *(line_1+(*line_num))=an_han[1]; (*line_num)++;
- /*下面这一段是将数组an_han[]的前四个字符以逗号分隔符的形式写入到 fp_result所打开的文件中去。写完一行后写入换行符*/
-
- {
- fprintf(fp_result,"%.2f ", *(line_1+(*line_num)-3));
- fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",an_han[0]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-A-4E",an_han[0]-*(line_1+(*line_num)-3));
- fputc('\n',fp_result);
-
- fprintf(fp_result,"%.2f",an_han[0]); fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",an_han[1]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-A-4C",an_han[1]-an_han[0]);
- fputc('\n',fp_result);
- }
-
- }
- void get_ming_han(float O,float H,float angle,float X,float * line_1,int * line_num)
- {
- float ming_han[4],y;
- int k=0;
- /****明涵洞四个桩号求解****/
- y=mid_number(O,H,angle);
- X=(X/SIN(angle))-1;
- ming_han[0]=y-24-(f_2_MAX(X)/2);
- ming_han[1]=y-(f_2_MAX(X)/2);
- ming_han[2]=y+(f_2_MAX(X)/2);
- ming_han[3]=y+24+(f_2_MAX(X)/2);
- /*************/
- *(line_1+(*line_num))=ming_han[0];(*line_num)++;
- *(line_1+(*line_num))=ming_han[1];(*line_num)++;
- *(line_1+(*line_num))=ming_han[2];(*line_num)++;
- *(line_1+(*line_num))=ming_han[3];(*line_num)++;
-
- /*下面这一段是将数组ming_han[]的前四个字符以逗号分隔符的形式写入到 fp_result所打开的文件中去。写完一行后写入换行符*/
-
- {
- fprintf(fp_result,"%.2f ", *(line_1+(*line_num)-5));
- fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",ming_han[0]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-A-4E",ming_han[0]-*(line_1+(*line_num)-5));
- fputc('\n',fp_result);
-
- fprintf(fp_result,"%.2f",ming_han[0]); fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",ming_han[1]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.0f","Gr-A-2E",24.0);
- fputc('\n',fp_result);
-
- fprintf(fp_result,"%.2f",ming_han[1]); fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",ming_han[2]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-A-2B1",ming_han[2]-ming_han[1]);
- fputc('\n',fp_result);
-
- fprintf(fp_result,"%.2f",ming_han[2]); fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",ming_han[3]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-A-2E",24.0);
- fputc('\n',fp_result);
- }
- }
- void get_da_zhong_qiao(float O,float H,float angle,float X,float * line_1,int * line_num)
- {
- float da_zhong_qiao[4],y;
- /****大中桥四个桩号求解****/
- y=mid_number(O,H,angle);
- da_zhong_qiao[0]=y-(X/2)-0.65-(X>=100?40:32);
- da_zhong_qiao[1]=y-(X/2)-0.65;
- da_zhong_qiao[2]=y+(X/2)+0.65;
- da_zhong_qiao[3]=y+(X/2)+0.65+(X>=100?40:32);
- /*************/
- *(line_1+(*line_num))=da_zhong_qiao[0];(*line_num)++;
- *(line_1+(*line_num))=da_zhong_qiao[1];(*line_num)++;
- *(line_1+(*line_num))=da_zhong_qiao[2];(*line_num)++;
- *(line_1+(*line_num))=da_zhong_qiao[3];(*line_num)++;
- /*下面这一段是将数组da_zhong_qiao[]的前四个字符以逗号分隔符的形式写入到 fp_result所打开的文件中去。写完一行后写入换行符*/
-
- {
- fprintf(fp_result,"%.2f ", *(line_1+(*line_num)-5));
- fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",da_zhong_qiao[0]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-A-4E",da_zhong_qiao[0]-*(line_1+(*line_num)-5));
- fputc('\n',fp_result);
- fprintf(fp_result,"%.2f",da_zhong_qiao[0]); fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",da_zhong_qiao[1]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-SB-2E",da_zhong_qiao[1]-da_zhong_qiao[0]+3.5);
- fputc('\n',fp_result);
-
- fprintf(fp_result,"%.2f",da_zhong_qiao[1]); fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",da_zhong_qiao[2]); fputc(',',fp_result);
- fprintf(fp_result,"%s","混凝土护栏");
- fputc('\n',fp_result);
-
- fprintf(fp_result,"%.2f",da_zhong_qiao[2]); fputc(',',fp_result);
- fprintf(fp_result,"%s","~"); fputc(',',fp_result);
- fprintf(fp_result,"%.2f",da_zhong_qiao[3]); fputc(',',fp_result);
- fprintf(fp_result,"%s,%.2f","Gr-SB-2E",da_zhong_qiao[3]-da_zhong_qiao[2]+3.5);
- fputc('\n',fp_result);
- }
- }