📄 editforms.c
字号:
/*editedforms.cWritten by: Peter TzannesINPUT -> OUTPUTform'i'.csv -> editedform'i'.txtCompilegcc -o <object file> editedforms.cRun<object file> <A number which corresponds to the number of inputs> The aim of this fucntion is to: - removes ambiguity of object coordinate system, that is, corrects all objects that were measured from origin. - all forms have the same objecets code - cm and mm were correced to meters.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <math.h>#define MAXLINE 150 //maximum line in form with commasint studnum(char *x) // the inital contents are x = #,1234567...{ int i,j; char temp[BUFSIZ]; if (x[2] != 'z') { for(i = 0, j = 2; i <= strlen(x); i++, j++) temp[i] = x[j]; temp[7] = '\0'; strcpy(x, temp); } else // if students put the z in their student number { for(i = 0, j = 3; i <= strlen(x); i++, j++) { temp[i] = x[j]; } temp[7] = '\0'; strcpy(x, temp); } return(0);}int postcode(char *x) // initial contents are x = ,1234...{ int i,j; char temp[BUFSIZ]; for(i = 0, j = 5; i <= strlen(x); i++, j++) { temp[i] = x[j]; } temp[4] = '\0'; strcpy(x, temp); return(0);}int removecom(char *x){ int i = 0, j = 0; char temp[BUFSIZ]; while(x[i] != ',') i++; i++; while(x[i] != ',') { temp[j] = x[i]; j++; i++; } strcpy(x, temp); return(0);}int empty(char *temp){ int j; for(j = 0 ; j<=strlen(temp); j++) temp[j] = 0; return(0);}//5 is used as it ws assumed no objects are greater than 5 metersint checkobjectmetric(float *x, float *y, float *z){ if((*x > 50) || (*y > 50) || (*z > 50)) //mm used { *x = *x/1000; *y = *y/1000; *z = *z/1000; } if((*x > 5) || (*y > 5) || (*z > 5)) //cm used { *x = *x/100; *y = *y/100; *z = *z/100; } return(0);}int checkmetric(float *x, float *y, float *height){ if(*x > 1000) //done in mm *x = *x/1000; if(*y > 1000) *y = *y/1000; if(*height > 1000) *height = *height/1000; if(*x > 100) //done in cm *x = *x/100; if(*y > 100) *y = *y/100; if(*height > 100) *height = *height/100; return(0);}int altertype(int *object){ if((*object >= 21) && (*object <= 28)) { *object = *object + 1; } if(*object >= 29) { *object = *object + 1; *object = *object + 1; } return(0);}int checkorigin(FILE *in, FILE *out, float *xlength, float *ylength, float *zlength, int *room){ char temp[BUFSIZ], line[MAXLINE];//temp buffers int a,b,c,d,e,f,g,h,i,j,k,single,length,templ,test,count; float l,m,n,o,p; rewind(in); while(!feof(in)) { fgets(line, MAXLINE, in); fscanf(in, "%s", temp); if (strcmp(temp,",,,Width") == 0) //find rooms { while(!feof(in)) //find relevent room number { sscanf(line, "%d,%d", &a,&b); // read in first 2 numbers if(a == *room) { for(single = 0, count = 0, test = 0; single <= strlen(line); single++) //skip over works { if (line[single] == ',') { count++; } if((count == 3) && (test == 0)) //third comma in line is where text starts { test = 1; for(length = single, templ = 0; length <= strlen(line); length++, templ++) { temp[templ] = line[length]; } } } //read in all other elements in a line and assign to an integer or float sscanf(temp, ",%f,%f,%f,%f,%d,%d,%d,%d,%d,%f,%d,%d,%d,%d", &l,&m,&n,&o,&c,&d,&e,&f,&g,&p,&h,&i,&j,&k); checkmetric(&l, &m, &p); //check cm or mm if(*xlength > l) *xlength = *xlength - n; //students that meausred the router from the origin of the house if(*ylength > m) *ylength = *ylength - o; return(0); } fgets(line, MAXLINE, in); } } } return(0);}int objects(FILE *in, FILE *out, int oneline, int inc_object){ int i = 0; int a,b,c,d,e; float l,m,n,o,p,q; char other[BUFSIZ], line[BUFSIZ], temp[BUFSIZ]; //other string rewind(in); while(!feof(in)) { fgets(line, MAXLINE, in); fscanf(in, "%s", temp); if (strcmp(temp,"Objects") == 0) //find rooms { fgets(line, BUFSIZ, in); while(i < oneline) { fgets(line, BUFSIZ, in); sscanf(line, "%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,%s", &a,&b,&c,&d,&e,&l,&m,&n,&o,&p,&q,other); i++; } checkorigin(in, out, &l, &m, &n, &b); //need to determine if object has been measured from the origin checkmetric(&l, &m, &n); //correct start length checkobjectmetric(&o, &p, &q); //correct object length if(inc_object == 1) altertype(&d); fprintf(out, "%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,%s\n", a,b,c,d,e,l,m,n,o,p,q,other); return(0); } } return(0);}main(int argc, char *argv[]){ FILE *fp; FILE *fw; int current, max, tempcount = 0, count; int version = 0; char vernumber[5], postc[6], studnumber[9]; const char *final = "editedform"; const char *csv = "form"; char temp[BUFSIZ], line[MAXLINE]; //temp buffers int a,b,c,d,e,f,g,h,i,j,k,single,length,templ,test; int z = 0; float l,m,n,o,p; if(argc != 2) printf("Need correct arguments in the form: ./a.out <number of csv files that are to be opened>"); else { max = atoi(argv[1]); //integer representing maximum number of csv files for(current=1; current<=max ; current++) //step through all forms which are in the arguments { version = 0; tempcount = 0; //temp counter sprintf(temp,"%s%d.csv",csv,current); fp = fopen(temp, "r"); //.csv file if ((fp) == NULL) { fprintf(stderr, "Cannot open form%d \n", current); exit(1); } sprintf(temp,"%s%d.txt",final,current); fw = fopen(temp, "w"); if ((fw) == NULL) { printf("fopen failed filename: %s\n",temp); exit(1); } else { fgets(vernumber, MAXLINE, fp); //extract fist line of the file { if (vernumber[11] != '7') // the following if statement determines what version of the form was used. { version++; } fprintf(fw,"NEW\n"); fscanf(fp, "%s %s", temp, studnumber); studnum(studnumber); // calls studnum prog, that retreives students number fprintf(fw,studnumber); fscanf(fp, "%s %s", temp, postc); // postcode postcode(postc); // calls postcode prog, that retreives students number fprintf(fw,"\n"); fprintf(fw,postc); empty(temp); while(tempcount < 21) //rest of building and experimental sheets { fgets(line, MAXLINE, fp); //extract fist line of the file fprintf(fw, line); tempcount++; } tempcount = 0; while(!feof(fp)) { fgets(line, MAXLINE, fp); fscanf(fp, "%s", temp); if (strcmp(temp,",,,Width") == 0) //find rooms { fprintf(fw, "\n\n%s \n", temp); printf(" ROOMS SECTION FOUND and REPRINTING\n"); fgets(line, MAXLINE, fp); while(tempcount < 20) //for the 20 rooms { fgets(line, 500, fp); sscanf(line, "%d,%d", &a,&b); // read in first 2 numbers fprintf(fw, "%d,%d,",a,b); tempcount++; for(single = 0, count = 0, test = 0; single <= strlen(line); single++) //skip over works { if (line[single] == ',') { count++; } if((count == 3) && (test == 0)) //third comma in line is where text starts { test = 1; for(length = single, templ = 0; length <= strlen(line); length++, templ++) { temp[templ] = line[length]; } } } //read in all other elements in a line and assign to an integer or float sscanf(temp, ",%f,%f,%f,%f,%d,%d,%d,%d,%d,%f,%d,%d,%d,%d", &l,&m,&n,&o,&c,&d,&e,&f,&g,&p,&h,&i,&j,&k); checkmetric(&l, &m, &p); fprintf(fw, "%f,%f,%f,%f,%d,%d,%d,%d,%d,%f,%d,%d,%d,%d\n",l,m,n,o,c,d,e,f,g,p,h,i,j,k); } } if (strcmp(temp,"Objects") == 0) //find rooms { fprintf(fw, "\n\n%s \n", temp); printf(" OBJECTS FOUND and REPRINTING\n"); tempcount = 1; fgets(line, MAXLINE, fp); while(tempcount <= 75) //75 objects { objects(fp, fw, tempcount, version); tempcount++; } fclose(fp); fclose(fw); z++; printf("analysed form%d...\n", current); break; //finish one form, hence break from loop } } } } } printf("\tFINISH\n"); } return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -