int main(int argc,char *argv[]){ int num,i,iter,j,count=1,tmp; double x,y; double area; scanf("%d",&num); while(num){ iter=0; area=0.0; printf("Figure %d: ",count++); if(num<3){ printf("Impossible\n"); for(j=0;j<num;j++)scanf("%lf %lf",&x,&y); } else{ for(i=0;i<num;i++){ scanf("%lf %lf",&(list[iter].x),&(list[iter].y)); if(area!=-1){ if(iter!=0){ area+=getarea(list[iter].x,list[iter].y,list[iter-1].x,list[iter-1].y); } if(iter>=3){ if(getarea(list[iter].x-list[iter-1].x,list[iter].y-list[iter-1].y,list[iter-1].x-list[iter-2].x,list[iter-1].y-list[iter-2].y)==0){ if(dot(list[iter].x-list[iter-1].x,list[iter].y-list[iter-1].y,list[iter-1].x-list[iter-2].x,list[iter-1].y-list[iter-2].y)<0) area=-1; } for(j=0;j<iter-2;j++){ if(!check(list[j].x,list[j].y,list[j+1].x,list[j+1].y,list[iter-1].x,list[iter-1].y,list[iter].x,list[iter].y)){ area=-1; } } } } iter++; } if(area==-1)printf("Impossible\n"); else{ iter--; if(getarea(list[0].x-list[iter].x,list[0].y-list[iter].y,list[iter].x-list[iter-1].x,list[iter].y-list[iter-1].y)==0){ if(dot(list[0].x-list[iter].x,list[0].y-list[iter].y,list[iter].x-list[iter-1].x,list[iter].y-list[iter-1].y)<0) area=-1; } for(j=2;j<iter;j++){ if(!check(list[0].x,list[0].y,list[iter].x,list[iter].y,list[j].x,list[j].y,list[j-1].x,list[j-1].y)){ area=-1; break; } } if(area!=-1){ area+=getarea(list[0].x,list[0].y,list[i-1].x,list[i-1].y); printf("%.2f\n",area<0?-area:area); } else printf("Impossible\n"); } } scanf("%d",&num); } return 0; }
int main() { char str[5]; while(scanf("%s", str)==1) { steps = 0; while(scanf("%s", str)==1 && str[0]!='K') { switch(str[0]) { case 'N': step[steps] = 0; break; case 'W': step[steps] = 1; break; case 'S': step[steps] = 2; break; case 'E': step[steps] = 3; break; } steps++; } if(!steps) printf("1\n"); else { process(); printf("%d\n", getarea()); } } return 0; }
/* Read in the section header table, if it is present. If a section * header string table is given in the ELF header, then also load the * string table contents. */ static int readsecthdrs(void) { int n; if (!secthdrs) return TRUE; n = elffhdr.e_shnum; if (!(secthdr = malloc(n * sizeof *secthdr))) nomem(); if (fseek(thefile, elffhdr.e_shoff, SEEK_SET) || elfrw_read_Shdrs(thefile, secthdr, n) != n) { err("%s: warning: invalid section header table offset.", thefilename); secthdrs = FALSE; return TRUE; } if (elffhdr.e_shstrndx != SHN_UNDEF) { shshstr = secthdr + elffhdr.e_shstrndx; if (!(sectstr = getarea(shshstr->sh_offset, shshstr->sh_size))) { err("%s: warning: invalid string table location.", thefilename); free(sectstr); sectstr = NULL; } } return TRUE; }
void GetContourAreas(const contour *ci){ int nlevels, n; float *areas; nlevels=ci->nlevels; areas = ci->areas; for(n=0;n<nlevels;n++){ float *xnode, *ynode; int ipoly,npolys; int *polysize; areas[n]=0.0; xnode=ci->xnode[n]; ynode=ci->ynode[n]; polysize=ci->polysize[n]; npolys=ci->npolys[n]; for(ipoly=0;ipoly<npolys;ipoly++){ int j; for(j=1;j<polysize[ipoly]-1;j++){ float area; area=getarea(xnode,ynode,j); areas[n]+=area; } xnode+=polysize[ipoly]; ynode+=polysize[ipoly]; } } }
/* rebind() does most of the grunt work. After checking over the ELF * headers, the function iterates through the sections, looking for * symbol tables containing non-local symbol. When it finds one, it * loads the non-local part of the table and the associated string * table into memory, and calls changesymbols(). If changesymbols() * actually changes anything, the altered symbol table is written back * out to the object file. */ static int rebind(void) { Elf32_Shdr *shdrs; Elf32_Sym *symtab; char *strtab; unsigned offset, count; int i; if (!readheader()) return FALSE; if (!(shdrs = getarea(ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr)))) return err("invalid section header table."); for (i = 0 ; i < ehdr.e_shnum ; ++i) { if (shdrs[i].sh_type != SHT_SYMTAB && shdrs[i].sh_type != SHT_DYNSYM) continue; if (shdrs[i].sh_entsize != sizeof *symtab) { err("symbol table of unrecognized structure ignored."); continue; } offset = shdrs[i].sh_offset + shdrs[i].sh_info * sizeof *symtab; count = shdrs[i].sh_size / sizeof *symtab - shdrs[i].sh_info; if (!count) continue; if (!(symtab = getarea(offset, count * sizeof *symtab))) return err("invalid symbol table"); if (!(strtab = getarea(shdrs[shdrs[i].sh_link].sh_offset, shdrs[shdrs[i].sh_link].sh_size))) return err("invalid associated string table"); if (changesymbols(symtab, strtab, count)) { if (fseek(thefile, offset, SEEK_SET) || fwrite(symtab, sizeof *symtab, count, thefile) != count) return err("unable to write to the object file"); } free(symtab); free(strtab); } free(shdrs); return TRUE; }
/* Extract the list of source files, if present. The source files are * determined by loading the symbol table section (and its associated * string table) and looking up symbols of type STT_FILE. */ static int getsrcfiles(textline **plines) { Elf64_Sym *syms; char *nmstr; textline *lines; char *str; unsigned strtab, count, i; int j, n; if (!secthdrs) return 0; for (i = 0 ; i < elffhdr.e_shnum ; ++i) if (secthdr[i].sh_type == SHT_SYMTAB) break; if (i == elffhdr.e_shnum) return 0; count = secthdr[i].sh_size / secthdr[i].sh_entsize; strtab = secthdr[i].sh_link; if (!(syms = malloc(count * sizeof *syms))) nomem(); if (fseek(thefile, secthdr[i].sh_offset, SEEK_SET)) return err("%s: invalid symbol table offset.", thefilename); count = elfrw_read_Syms(thefile, syms, count); if (!count) return 0; nmstr = getarea(secthdr[strtab].sh_offset, secthdr[strtab].sh_size); if (!nmstr) { free(syms); return 0; } lines = gettextlines(count); n = 0; for (i = 0 ; i < count ; ++i) { if (ELF64_ST_TYPE(syms[i].st_info) != STT_FILE) continue; str = nmstr + syms[i].st_name; for (j = 0 ; j < n ; ++j) if (!strcmp(lines[j].str, str)) break; if (j == n) append(lines + n++, "%s", str); } free(syms); free(nmstr); if (n) *plines = lines; else free(lines); return n; }
void getipaddr(char *start, char **countrybuf, char **areabuf) { char *pos, *p_country = NULL, *p_area = NULL, buf[2][128]; memset(buf, 0, sizeof(buf)); unsigned int offset; int len[2]; offset = getlong3(start); pos = p_share + offset + 4; char *byte = pos; // 标志字节 pos++; switch (*byte) { case 1:{ // 标志字节为1,表示国家和区域信息都被同时重定向 int countryOffset = getlong3(pos); // 重定向地址 pos+=3; pos=p_share+countryOffset; byte = pos; // 标志字节 pos++; switch (*byte) { case 2: // 标志字节为2,表示国家信息又被重定向 { p_country=p_share+getlong3(pos); pos=p_share+countryOffset+4; p_area = getarea(pos); } break; default: // 否则,表示国家信息没有被重定向 { p_country=byte; p_area = getarea(p_country+strlen(p_country)+1); } break; } } break; case 2: // 标志字节为2,表示国家信息被重定向 { p_country=p_share+getlong3(pos);// 重定向地址 pos = p_share + offset + 8; p_area = getarea(pos); } break; default:{ // 否则,表示国家信息没有被重定向 p_country=byte; p_area=getarea(p_country+strlen(p_country)+1); } break; } if (codetype == GBK_t){ my_iconv("utf8", "gbk", buf[0], sizeof(buf[0]), p_country, strlen(p_country)); my_iconv("utf8", "gbk", buf[1], sizeof(buf[1]), p_area, strlen(p_area)); //printf("buf:%s %s, %d %d\n", buf[0], buf[1], strlen(buf[0]), strlen(buf[1])); len[0] = strlen(buf[0]), len[1] = strlen(buf[1]); if (len[0]){ *countrybuf = (char *)malloc(len[0] + 1); if (!(*countrybuf)){ perror("malloc"); exit(1); } memcpy(*countrybuf, buf[0], len[0]+1); }else { *countrybuf = NULL; } if (len[1]){ *areabuf = (char *)malloc(len[1] + 1); if (!(*areabuf)){ perror("malloc"); exit(1); } memcpy(*areabuf, buf[1], len[1]+1); }else { *areabuf = NULL; } }else if (codetype == UTF_t){ len[0] = strlen(p_country), len[1] = strlen(p_area); if (len[0]){ *countrybuf = (char *)malloc(len[0] + 1); if (!(*countrybuf)){ perror("malloc"); exit(1); } memcpy(*countrybuf, p_country, len[0]+1); }else { *countrybuf = NULL; } if (len[1]){ *areabuf = (char *)malloc(len[1] + 1); if (!(*areabuf)){ perror("malloc"); exit(1); } memcpy(*areabuf, p_area, len[1]+1); }else { *areabuf = NULL; } } //printf("areabuf:%s %s\n", *countrybuf, *areabuf); }
/* Extract the list of dependencies, if present. The program header * table entry of type PT_DYNAMIC contains the dynamic info, which * includes the dependent files, and the string table that contains * the file names. However, the string table is referenced by its * location in memory, as opposed to its location in the file, so * the program header table needs to be searched again to find the * part that will be loaded at that location. */ static int getlibraries(textline **plines) { Elf64_Dyn *dyns; char *nmstr; textline *lines; char *str; unsigned long strtab = 0, strsz = 0; unsigned count, i, j, n; if (!proghdrs) return 0; for (i = 0 ; i < elffhdr.e_phnum ; ++i) if (proghdr[i].p_type == PT_DYNAMIC) break; if (i == elffhdr.e_phnum) return 0; count = proghdr[i].p_filesz / sizeof *dyns; if (!(dyns = malloc(count * sizeof *dyns))) nomem(); if (fseek(thefile, proghdr[i].p_offset, SEEK_SET)) return err("%s: invalid dynamic table offset.", thefilename); count = elfrw_read_Dyns(thefile, dyns, count); if (!count) return 0; n = 0; for (i = 0 ; i < count ; ++i) { if (dyns[i].d_tag == DT_STRTAB) strtab = dyns[i].d_un.d_ptr; else if (dyns[i].d_tag == DT_STRSZ) strsz = dyns[i].d_un.d_val; else if (dyns[i].d_tag == DT_NEEDED) ++n; } if (!strtab || !strsz) return 0; for (i = 0 ; i < elffhdr.e_phnum ; ++i) if (strtab >= proghdr[i].p_vaddr && strtab < proghdr[i].p_vaddr + proghdr[i].p_filesz) break; if (i == elffhdr.e_phnum) return 0; if (!(nmstr = getarea(proghdr[i].p_offset + (strtab - proghdr[i].p_vaddr), strsz))) return 0; lines = gettextlines(n); n = 0; for (i = 0 ; i < count ; ++i) { if (dyns[i].d_tag != DT_NEEDED) continue; str = nmstr + dyns[i].d_un.d_val; for (j = 0 ; j < n ; ++j) if (!strcmp(lines[j].str, str)) break; if (j == n) append(lines + n++, "%s", str); } free(dyns); free(nmstr); if (n) *plines = lines; else free(lines); return n; }
//获得ip所属地理信息,isp int getipinfo(char *ipstr,localtmp *p_loc) { if (p_share == NULL){ perror("openshare..."); return -1; } char *pos = p_share; int record_len=10; char *firstip=0; //把ip转为整数 unsigned int ip=htonl(inet_addr(ipstr)); firstip=p_begin; // first record position int l=0; int u=total_record; int i=0; char* findip=firstip; unsigned int beginip=0; unsigned int endip=0; //二分法查找 while(l <= u) { i=(l+u)/2; pos=firstip+i*RECORD_LEN; beginip = getlong4(pos); pos+=4; if(ip<beginip) { u=i-1; } else { endip=getlong4(p_share+getlong3(pos)); if(ip>endip) { l=i+1; } else { findip=firstip+i*RECORD_LEN; break; } } } int offset = getlong3(findip+4); pos=p_share+offset; endip= getlong4(pos); // 用户IP所在范围的结束地址 pos+=4; unsigned int j=ntohl(beginip); inet_ntop(AF_INET,&j,p_loc->beginip, INET6_ADDRSTRLEN);// 获得开始地址的IP字符串类型 j=ntohl(endip); inet_ntop(AF_INET,&j,p_loc->endip, INET6_ADDRSTRLEN);// 获得结束地址的IP字符串类型 char *byte = pos; // 标志字节 pos++; switch (*byte) { case 1:{ // 标志字节为1,表示国家和区域信息都被同时重定向 int countryOffset = getlong3(pos); // 重定向地址 pos+=3; pos=p_share+countryOffset; byte = pos; // 标志字节 pos++; switch (*byte) { case 2: // 标志字节为2,表示国家信息又被重定向 { p_loc->p_country=p_share+getlong3(pos); pos=p_share+countryOffset+4; p_loc->p_area = getarea(pos); } break; default: // 否则,表示国家信息没有被重定向 { p_loc->p_country=byte; p_loc->p_area = getarea(p_loc->p_country+strlen(p_loc->p_country)+1); } break; } } break; case 2: // 标志字节为2,表示国家信息被重定向 { p_loc->p_country=p_share+getlong3(pos);// 重定向地址 pos = p_share + offset + 8; p_loc->p_area = getarea(pos); } break; default:{ // 否则,表示国家信息没有被重定向 p_loc->p_country=byte; p_loc->p_area=getarea(p_loc->p_country+strlen(p_loc->p_country)+1); } break; } //printf("%s %s\n", p_loc->p_country, p_loc->p_area); return 0; }