嗨,我是一个编程初学者,但有一些基本的了解。 我正在尝试编写一个python脚本,其中给出了一个cpp文件,如下所示:
///////////////////////////////////
// experimentOrientVertices.cpp
// (modifying square.cpp)
//
// Sumanta Guha.
///////////////////////////////////
#ifdef __APPLE__
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <OpenGL/glext.h>
#else
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <GL/glext.h>
#pragma comment(lib, "glew32.lib")
#endif
// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
// Draw a polygon with specified vertices.
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_FILL);
glBegin(GL_POLYGON);
glVertex3f(20.0, 20.0, 0.0);
glVertex3f(80.0, 20.0, 0.0);
glVertex3f(80.0, 80.0, 0.0);
glVertex3f(20.0, 80.0, 0.0);
glEnd();
/*
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_FILL);
glBegin(GL_POLYGON);
glVertex3f(20.0, 80.0, 0.0);
glVertex3f(20.0, 20.0, 0.0);
glVertex3f(80.0, 20.0, 0.0);
glVertex3f(80.0, 80.0, 0.0);
glEnd();
*/
/*
glPolygonMode(GL_FRONT, GL_LINE);
glPolygonMode(GL_BACK, GL_FILL);
glBegin(GL_POLYGON);
glVertex3f(80.0, 80.0, 0.0);
glVertex3f(80.0, 20.0, 0.0);
glVertex3f(20.0, 20.0, 0.0);
glVertex3f(20.0, 80.0, 0.0);
glEnd();
*/
glFlush();
}
// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
default:
break;
}
}
// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("experimentOrientVertices.cpp");
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
glewExperimental = GL_TRUE;
glewInit();
setup();
glutMainLoop();
}
应该输出这个:
^{pr2}$问题是,下面的脚本输出如下:
[['21=>59']]
[['63=>65'], ['65=>63']]
[['69=>76'], ['76=>69']]
[['80=>89'], ['82=>88'], ['88=>82'], ['89=>80']]
[['93=>113'], ['113=>93']]
还有。。我真的不知道怎么弄明白 谢谢你的时间,我希望你的问题写得好!在
这是我的剧本:
from sys import argv
token = {
'{': 0
}
level = ["" for i in range(10)]
funcStartStop = [] #list to store each function start and stop brackets value
def functionCount(filename):
inFile = open(filename, 'r')
currLine = 0
mbracketopenline = [] #list to store the currLine value if a { is found
mbracketcloseline = [] #list to store the currLine value if a } is found
first = True
for line in inFile:
currLine += 1
if "{" in line:
token["{"] += 1
if first:
first = False
mbracketopenline.append(currLine)
if "}" in line:
token["{"] -= 1
mbracketcloseline.append(currLine)
if not first and token["{"] == 0:
first = True
tmpfuncStartStop = []
for i in range(mgraffeopenline.__len__()):
tmpfuncStartStop.append([str(str(mbracketopenline[i])+"=>"+str(mbracketcloseline[-i-1]))]) #store opening and
#closing brackets in a list of list, so at the end of the cycle
#should be something like: [['1=>10'],['3=>8']]
funcStartStop.append(tmpfuncStartStop)
mbracketcloseline = mbracketopenline = []
if __name__ == '__main__':
functionCount(argv[1])
for i in funcStartStop:
print(i)
忽略您在为SO调整示例时所犯的代码错误(例如,
mgraffeopenline
而不是mbracketopenline
,functionCount()
不返回结果等),您曾经:有效地将}设置为同一个新列表,并且每当您修改它们中的每一个时,您都在修改同一个列表。这主要是导致你的问题的原因,但还有其他人需要考虑。在
mbracketcloseline
和{另外,正如我在评论中所说的,在同一行有多个大括号是完全有效的,你需要解释它们(包括它们的位置),所以你实际上需要逐字检查代码并找出每一个出现的地方,比如:
^{pr2}$为您的文件生成:
但如果要将
keyInput
函数更改为:它仍然会产生一个有效的结果:
如果您想像您的示例一样打印(我发现实际的行索引分隔开更有用),只需替换行:
有:
但是,这并不能解决^{} 中提到的问题,即注释、宏、字符串中可以有大括号。。。所有这些都将作为有效的函数开始/结束。简单的字符与代码的匹配远不止如此。在
如果您真的想深入分析C代码,有一个非常有用的模块^{} 正是为这个目的而设计的。它可能需要更长的时间来设置,但它将为您提供比这种暗箱解析更多的代码洞察力。当然,这取决于你的实际用例是什么。。。在
相关问题 更多 >
编程相关推荐