【求助】一个朋友写的程序,求解析
c++吧
全部回复
仅看楼主
level 2
各种黑_ 楼主
1楼祭天
本人小白 完全不懂c++ 朋友写了一个程序 求各位高人告诉我程序的功能和算法 谢谢
2012年12月14日 19点12分 1
level 2
各种黑_ 楼主
第一个文件:
#include
"main.h"
void add_seg(struct line_t *l, struct rect_t *r){
update_l(l, 1, 0, l->n/2 - 1,
r->ybottom - l->ymin, r->ytop - l->ymin - 1, l->l[0], 1);
/*
int i;
printf("add_segment\n");
for (i=0; i< l->n; i++){
printf("%li
",l->l[i]);
}
printf("\n\n");
*/
}
void remove_seg(struct line_t *l, struct rect_t *r){
update_l(l, 1, 0, l->n/2 - 1,
r->ybottom - l->ymin, r->ytop - l->ymin - 1, l->l[0], -1);
/*
int i;
printf("remove_segment\n");
for (i=0; i< l->n; i++){
printf("%li
",l->l[i]);
}
printf("\n\n");
*/
}
void update_l(struct line_t *l, unsigned long int pos, unsigned long int lmin,
unsigned long int lmax, uint32_t b, uint32_t t, unsigned long int ov, int
op){
// //printf(" update_l pos: %i, lmin:
%i; lmax: %i, b: %i, t:%i, op: %i\n", pos, lmin, lmax, b, t, op);
/* is the segment included in our
segment */
if ((b <= lmin) && (t >=
lmax)){
switch (op){
case 1: /* adding element */
(l->l[pos-1])++;
//printf("setting bit pos:
%i\n", pos);
if (!ov){
if (l->l[pos-1] == 1){
l->cur += (lmax - lmin + 1);
//printf("lcur increase adding %i, pos: %i\n", lmax - lmin +1,
pos);
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, 2);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, 2);
}
}
}
break;
case 2: /* inspecting subtree after
addition */
if (!ov){ /* if there the interval
is not overflowed */
if (l->l[pos-1] > 0){ /* and the interval is marked */
l->cur -= (lmax - lmin + 1);
//printf("lcur inspection decrease removing %i, pos: %i\n",
lmax - lmin +1, pos);
}
else{
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, op);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, op);
}
}
}
break;
case -1: /* removing segment
*/
(l->l[pos-1])--;
//printf("removing bit pos: %i\n", pos);
if (!ov){
if ( l->l[pos-1] == 0){
l->cur -= (lmax - lmin + 1);
//printf("lcur decrease remove %i, pos: %i\n", lmax - lmin +1,
pos);
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, 0);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, 0);
}
}
}
break;
case 0: /* inspecting subtree after
deletion */
if (!ov){ /* if there the interval
is not overflowed */
if (l->l[pos-1] > 0){ /* and the interval is marked */
l->cur += (lmax - lmin + 1);
//printf("lcur inspection
increase add %i, pos: %i\n", lmax - lmin +1, pos);
}
else{
if ((lmax - lmin +1)/2 > 0 ){
update_l(l, pos*2, lmin , (lmin+lmax)/2 ,
b, t, ov, op);
update_l(l, pos*2 +1 , (lmin+lmax)/2 + 1
, lmax , b, t, ov, op);
}
}
}
break;
}
}
else
{
if ((l->l[pos-1] > 0) ||
ov){
//printf("overflow at position
pos: %i\n", pos);
ov = 1;
}
if (b <= (lmin+lmax)/2){
update_l(l, pos*2, lmin ,
(lmin+lmax)/2 , b, t, ov, op);
}
if (t > (lmin+lmax)/2){
update_l(l, pos*2 +1, (lmin+lmax)/2
+ 1 , lmax , b, t, ov, op);
}
}
}
2012年12月14日 19点12分 2
level 2
各种黑_ 楼主
第二个文件:
all:
prog
prog:
gcc line.c main.c -Wall -o
rectangles
#gcc line.c main.c -g -pg -Wall -O2 -o main
#make generate_rectangles line main
clean:
rm generate_rectangles rectangles
*~
run: prog
# ./generate_rectangles -n 10000 |
./main
cat sample_input.txt | ./main
2012年12月14日 19点12分 6
level 2
各种黑_ 楼主
第三个文件:
#ifndef
__KLEE__
#define __KLEE__
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX(a, b) ((uint32_t)(a) < (uint32_t)(b) ? (b) : (a))
#define MIN(a, b) ((uint32_t)(a) > (uint32_t)(b) ? (b) : (a))
/* queue */
struct queue_t {
struct rect_t **q;
int n;
};
/* rectangle structure */
struct rect_t {
uint32_t xleft;
uint32_t ybottom;
uint32_t xright;
uint32_t ytop;
};
/* Vertical line structure for the sweep algo */
struct line_t {
unsigned long int *l;
uint32_t ymin;
uint32_t ymax;
uint32_t cur;
unsigned long int n;
};
/* Quicksort comparison functions */
//static int cmp_rect_left(const void *a, const void *b);
//static int cmp_rect_right(const void *a, const void *b);
/* line_t operations */
void add_seg(struct line_t *l, struct rect_t *r);
void remove_seg(struct line_t *l, struct rect_t *r);
void update_l(struct line_t *l, unsigned long int pos, unsigned long int lmin,
unsigned long int lmax, uint32_t b, uint32_t t, unsigned long int ov, int
op);
#endif
2012年12月14日 19点12分 7
level 2
各种黑_ 楼主
第四个文件由于太和谐 贴不出来(已经被吞了很多楼了) 就用截图的方式发出来
2012年12月14日 19点12分 8
level 2
各种黑_ 楼主
以上就是这个程序所包含的四个文件 求高人解析(最主要的是算法)
2012年12月14日 19点12分 9
level 6
学习中,等高人!
2012年12月14日 23点12分 10
level 1
黑哥乃牛爆了…=口- 帮顶下不反对吧哈:D
2012年12月15日 07点12分 11
。。。我其实不懂C++
2012年12月15日 18点12分
回复 各种黑_ :啊 好吧……其实我连C++是神马都不知道[揉脸]
2012年12月16日 04点12分
level 7
目测单片机,或者是arm 之类芯片程序
2012年12月16日 04点12分 12
1