两侧同时换到之前的修订记录 前一修订版 | |||
2020-2021:teams:namespace:小型代码分析系统的实现方式 [2020/08/06 12:20] great_designer [题解] |
2020-2021:teams:namespace:小型代码分析系统的实现方式 [2020/08/06 12:32] (当前版本) great_designer [代码] |
||
---|---|---|---|
行 197: | 行 197: | ||
=====代码===== | =====代码===== | ||
- | |||
- | 标程的程序用了大量的assert来控制函数是否执行下去。 | ||
<hidden> | <hidden> | ||
行 204: | 行 202: | ||
#include<stdio.h> | #include<stdio.h> | ||
- | #include<assert.h> | ||
#include<string.h> | #include<string.h> | ||
行 224: | 行 221: | ||
int PointerIndex(char ch) | int PointerIndex(char ch) | ||
{ | { | ||
- | assert('A' <= ch && ch <= 'Z'); | ||
return ch - 'A' + 26; | return ch - 'A' + 26; | ||
} | } | ||
行 230: | 行 226: | ||
int FieldIndex(char ch) | int FieldIndex(char ch) | ||
{ | { | ||
- | assert('a' <= ch && ch <= 'z'); | ||
return ch - 'a' + 1; | return ch - 'a' + 1; | ||
} | } | ||
行 241: | 行 236: | ||
int makeField(char v, char f) | int makeField(char v, char f) | ||
{ | { | ||
- | assert(('A' <= v && v <= 'Z') || ('a' <= v && v <= 'z')); | + | if ('a' <= v && v <= 'z') |
- | assert('a' <= f && f <= 'z'); | + | { |
- | if ('a' <= v && v <= 'z') return makeField(ObjectIndex(v), FieldIndex(f)); | + | return makeField(ObjectIndex(v), FieldIndex(f)); |
+ | } | ||
return makeField(PointerIndex(v), FieldIndex(f)); | return makeField(PointerIndex(v), FieldIndex(f)); | ||
} | } | ||
行 249: | 行 245: | ||
int makeField(int v, char f) | int makeField(int v, char f) | ||
{ | { | ||
- | assert(0 <= v && v <= 51); | ||
- | assert('a' <= f && f <= 'z'); | ||
return makeField(v, FieldIndex(f)); | return makeField(v, FieldIndex(f)); | ||
} | } | ||
行 257: | 行 251: | ||
{ | { | ||
int base = v % 52, field = v / 52; | int base = v % 52, field = v / 52; | ||
- | assert(0 <= base && base <= 52); | ||
- | assert(1 <= field && field <= 26); | ||
return base; | return base; | ||
} | } | ||
行 265: | 行 257: | ||
{ | { | ||
int base = v % 52, field = v / 52; | int base = v % 52, field = v / 52; | ||
- | assert(0 <= base && base <= 52); | ||
- | assert(1 <= field && field <= 26); | ||
return field; | return field; | ||
} | } | ||
行 273: | 行 263: | ||
{ | { | ||
int n = strlen(s); | int n = strlen(s); | ||
- | assert(n == 1 || n == 3); | ||
if (n == 1) | if (n == 1) | ||
{ | { | ||
return -1; | return -1; | ||
} | } | ||
- | assert(s[1] == '.'); | ||
return makeField(s[0], s[2]); | return makeField(s[0], s[2]); | ||
} | } | ||
行 312: | 行 300: | ||
int q = getBase(e->first); | int q = getBase(e->first); | ||
int f = getField(e->first); | int f = getField(e->first); | ||
- | for (int i = 0; i < 26; i++) | + | int i; |
- | if ((pt[q] >> i) & 1) | + | for(i = 0; i < 26; i++) |
+ | { | ||
+ | if ((pt[q] >> i) & 1) | ||
{ | { | ||
pt[makeField(i, f)] |= pt[p]; | pt[makeField(i, f)] |= pt[p]; | ||
} | } | ||
+ | } | ||
} | } | ||
for(e=loadEdge.begin();e!=loadEdge.end();e++) | for(e=loadEdge.begin();e!=loadEdge.end();e++) | ||
行 323: | 行 314: | ||
int f = getField(e->second); | int f = getField(e->second); | ||
int q = e->first; | int q = e->first; | ||
- | for (int i = 0; i < 26; i++) | + | int i; |
- | if ((pt[p] >> i) & 1) | + | for(i = 0; i < 26; i++) |
+ | { | ||
+ | if ((pt[p] >> i) & 1) | ||
{ | { | ||
int value = pt[makeField(i, f)]; | int value = pt[makeField(i, f)]; | ||
行 333: | 行 326: | ||
} | } | ||
} | } | ||
- | } | + | } |
- | } | + | |
- | for(e=allocEdge.begin();e!=allocEdge.end();e++) | + | |
- | { | + | |
- | assert(pt[e->second] | (e->first)); | + | |
- | } | + | |
- | for (int i = 'A'; i <= 'Z'; i++) | + | |
- | { | + | |
- | int src = PointerIndex(i); | + | |
- | vector<int>::iterator dst; | + | |
- | for(dst=assignEdge[src].begin();dst!=assignEdge[src].end();dst++) | + | |
- | { | + | |
- | assert((pt[*dst] & pt[src]) == pt[src]); | + | |
- | } | + | |
- | } | + | |
- | for(e=storeEdge.begin();e!=storeEdge.end();e++) | + | |
- | { | + | |
- | int dst = e->first; | + | |
- | int src = e->second; | + | |
- | int base = getBase(dst); | + | |
- | int f = getField(dst); | + | |
- | for (int i = 0; i < 26; i++) | + | |
- | { | + | |
- | if ((pt[base] >> i) & 1) | + | |
- | { | + | |
- | assert((pt[makeField(i, f)] & pt[src]) == pt[src]); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | for(e=loadEdge.begin();e!=loadEdge.end();e++) | + | |
- | { | + | |
- | int dst = e->first; | + | |
- | int src = e->second; | + | |
- | int base = getBase(src); | + | |
- | int f = getField(src); | + | |
- | for (int i = 0; i < 26; i++) | + | |
- | { | + | |
- | if ((pt[base] >> i) & 1) | + | |
- | { | + | |
- | assert((pt[makeField(i, f)] & pt[dst]) == pt[makeField(i, f)]); | + | |
- | } | + | |
} | } | ||
} | } | ||
行 380: | 行 333: | ||
void output(int x) | void output(int x) | ||
{ | { | ||
- | for (int i = 0; i < 26; i++) | + | int i; |
+ | for(i = 0; i < 26; i++) | ||
{ | { | ||
if ((x >> i) & 1) | if ((x >> i) & 1) | ||
行 395: | 行 349: | ||
int n; | int n; | ||
scanf("%d", &n); | scanf("%d", &n); | ||
- | for (int i = 1; i <= n; i++) | + | int ii; |
+ | for(ii= 1;ii<= n;ii++) | ||
{ | { | ||
scanf("%s = %s", receiver, sender); | scanf("%s = %s", receiver, sender); | ||
int R = checkFieldNode(receiver); | int R = checkFieldNode(receiver); | ||
int G = checkFieldNode(sender); | int G = checkFieldNode(sender); | ||
- | if (R != -1 && G != -1) | ||
- | { | ||
- | assert(0); | ||
- | } | ||
if (R != -1 && G == -1) | if (R != -1 && G == -1) | ||
{ | { | ||
行 414: | 行 365: | ||
if (R == -1 && G == -1) | if (R == -1 && G == -1) | ||
{ | { | ||
- | if (sender[0] >= 'a' && sender[0] <= 'z') | + | if (sender[0] >= 'a' && sender[0] <= 'z') |
- | allocEdge.push_back(pair<int,int>(PointerIndex(receiver[0]), ObjectIndex(sender[0]))); | + | { |
+ | allocEdge.push_back(pair<int,int>(PointerIndex(receiver[0]), ObjectIndex(sender[0]))); | ||
+ | } | ||
else | else | ||
{ | { | ||
行 423: | 行 376: | ||
} | } | ||
propagate(); | propagate(); | ||
- | for (char i = 'A'; i <= 'Z'; i++) | + | char i; |
+ | for(i = 'A'; i <= 'Z'; i++) | ||
{ | { | ||
printf("%c: ",i); | printf("%c: ",i); | ||
- | for (int j = 0; j < 26; j++) | + | int j; |
- | if ((pt[PointerIndex(i)] >> j) & 1) | + | for(j = 0; j < 26; j++) |
+ | { | ||
+ | if ((pt[PointerIndex(i)] >> j) & 1) | ||
{ | { | ||
printf("%c",(char)(j + 'a')); | printf("%c",(char)(j + 'a')); | ||
} | } | ||
+ | } | ||
printf("\n"); | printf("\n"); | ||
} | } |