当前位置: 代码迷 >> C语言 >> 关于简易语法高亮(已有第三版,免费)
  详细解决方案

关于简易语法高亮(已有第三版,免费)

热度:625   发布时间:2008-04-24 16:33:53.0
加油!
----------------解决方案--------------------------------------------------------
以下是引用 雨中秣燕 在 2008-4-24 16:04 的发言:
自动缩进貌似在这代码的基础上做不难。。。偶今晚试试看。。。。

那就太好了。
不过希望能把代码能写的易懂些,
说实话,我这个建议是从自己的角度出发的,因为我对我的那个夭折的缩排程序仍然耿耿于怀,所以想从LZ这里得到些方法。希望我这样做不算自私。
----------------解决方案--------------------------------------------------------
对了,为什么LZ写的那些小程序都是用C++写的呢??
----------------解决方案--------------------------------------------------------
因为纯C的程序很恶心,纯C++的话,有的东西自己控制起来又不方便
算法部分是C/C++的混合体(能轻易移植),
界面是用的MFC,MFC就不管它移不移植了,
反正界面代码量很少,没什么价值

[color=white]
----------------解决方案--------------------------------------------------------
昨晚和今天早上都想了一下,要考虑的东西还不少,
麻烦在于本身不规范的,喜欢省略大括号的代码,
还有一些特殊约定的问题

假如每个代码都不省略大括号地写,那倒真的简单很多
现在偶还没开始动手,偶还是想先考虑清楚

[color=white]
----------------解决方案--------------------------------------------------------
[bo]以下是引用 [un]雨中秣燕[/un] 在 24/4/2008 18:29 的发言:[/bo]

因为纯C的程序很恶心
http://blog.programfan.com/upfile/200804/200 ...



这是什么意思``为什么这样说呢?
----------------解决方案--------------------------------------------------------
翻译下开头那段代码


#include <stdio.h>

int main()
{
    int i;
    const char *s="计铡诣窒瞪修势裸绸解\10\0 晾默?|";
   
   
    for( i=0;  putchar(s[i]+i%3) ; i++)
               ;
               
               
    for(i=0   ; putchar(s[i]+i%3) ;  i==13?i+=10:i++ )
                ;
               
               
    getchar();
    return 0;
}
----------------解决方案--------------------------------------------------------
试试
#include<stdio.h>
#include<string.h>

struct
{
        int x, y;
        int p;
} line[20000];
int map[52][52];
int yes[4][52][52];

int main(void)
{
    int i, j, k;
    char tmpchar[6];
   
    int x, y, x1, y1, x2, y2;
   
    int to[4][2]={{1,3},{0,2},{1,3},{0,2}};
    char word[4][6]={"east", "south", "west", "north"};
   
   
   
    while(scanf("%d%d", &x, &y),(x!=0 && y!=0))
    {
        int point=0, now=1;
        int flag=0;
        memset(map, 0, sizeof(map));
        memset(yes, 0, sizeof(yes));
        for(i=1; i<=x; i++)
        {
            for(j=1; j<=y; j++)
            {
                scanf("%d", &map[i][j]);
                for(k=0; k<4; k++)
                {
                    yes[k][i][j]=map[i][j];
                }
            }
        }
        scanf("%d%d%d%d%s", &x1, &y1, &x2, &y2, tmpchar);
        if(x1==x2 && y1==y2)
        {
            printf("0\n");
            continue;
        }
        line[point].x = x1;
        line[point].y = y1;
        for(i=0; i<4; i++)
        {
            if(strcmp(word[i], tmpchar) == 0)
            {
                line[point].p = i;
                break;
            }
        }
        yes[line[point].p][x1][y1]=1;
        while(point<now && now<19000)
        {
            for(i=0; i<2; i++)
            {
                if(!yes[to[line[point].p][i]][line[point].x][line[point].y])
                {
                    line[now] = line[point];
                    line[now].p = to[line[point].p][i];
                    yes[line[now].p][line[point].x][line[point].y] = yes[line[point].p][line[point].x][line[point].y]+1;
                    now++;
                }
            }
            switch(line[point].p)
            {
                case 0:
                    for(i=1+line[point].y, k=0; k<4 && i<y && (!map[line[point].x][i+1]) && (!map[line[point].x+1][i+1]); i++,k++)
                    {
                        if(!yes[line[point].p][line[point].x][i])
                        {
                            line[now] = line[point];
                            line[now].y = i;
                            yes[line[point].p][line[point].x][i] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
                case 1:
                    for(i=1+line[point].x, k=0; k<4 &&  i<x && (!map[i+1][line[point].y]) && (!map[i+1][line[point].y+1]); i++,k++)
                    {
                        if(!yes[line[point].p][i][line[point].y])
                        {
                            line[now] = line[point];
                            line[now].x = i;
                            yes[line[point].p][i][line[point].y] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
                case 2:
                    for(i=line[point].y-1, k=0; k<4 && i>0 && (!map[line[point].x][i]) && (!map[line[point].x+1][i]); i--,k++)
                    {
                        if(!yes[line[point].p][line[point].x][i])
                        {
                            line[now] = line[point];
                            line[now].y = i;
                            yes[line[point].p][line[point].x][i] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
                case 3:
                    for(i=line[point].x-1, k=0; k<4 && i>0 && (!map[i][line[point].y]) && (!map[i][line[point].y+1]); i--,k++)
                    {
                        if(!yes[line[point].p][i][line[point].y])
                        {
                            line[now] = line[point];
                            line[now].x = i;
                            yes[line[point].p][i][line[point].y] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
            } /* End of switch*/
            
point++;
        }
        finish:
                if(flag)
                {
                    printf("%d\n", yes[line[now].p][line[now].x][line[now].y]-1);
                }
                else
               
{
                    printf("-1\n");
                }
    }
    return 0;
}

----------------解决方案--------------------------------------------------------
有一些问题,例如string.h和stdio.h颜色不一样,还有map变量
#include<stdio.h>
#include<string.h>
struct
{
        int x, y;
        int p;
} line[20000];
int map[52][52];
int yes[4][52][52];
int main(void)
{
    int i, j, k;
    char tmpchar[6];
   
    int x, y, x1, y1, x2, y2;
   
    int to[4][2]={{1,3},{0,2},{1,3},{0,2}};
    char word[4][6]={"east", "south", "west", "north"};
   
   
   
    while(scanf("%d%d", &x, &y),(x!=0 && y!=0))
    {
        int point=0, now=1;
        int flag=0;
        memset(map, 0, sizeof(map));
        memset(yes, 0, sizeof(yes));
        for(i=1; i<=x; i++)
        {
            for(j=1; j<=y; j++)
            {
                scanf("%d", &map[i][j]);
                for(k=0; k<4; k++)
                {
                    yes[k][i][j]=map[i][j];
                }
            }
        }
        scanf("%d%d%d%d%s", &x1, &y1, &x2, &y2, tmpchar);
        if(x1==x2 && y1==y2)
        {
            printf("0\n");
            continue;
        }
        line[point].x = x1;
        line[point].y = y1;
        for(i=0; i<4; i++)
        {
            if(strcmp(word[i], tmpchar) == 0)
            {
                line[point].p = i;
                break;
            }
        }
        yes[line[point].p][x1][y1]=1;
        while(point<now && now<19000)
        {
            for(i=0; i<2; i++)
            {
                if(!yes[to[line[point].p][i]][line[point].x][line[point].y])
                {
                    line[now] = line[point];
                    line[now].p = to[line[point].p][i];
                    yes[line[now].p][line[point].x][line[point].y] = yes[line[point].p][line[point].x][line[point].y]+1;
                    now++;
                }
            }
            switch(line[point].p)
            {
                case 0:
                    for(i=1+line[point].y, k=0; k<4 && i<y && (!map[line[point].x][i+1]) && (!map[line[point].x+1][i+1]); i++,k++)
                    {
                        if(!yes[line[point].p][line[point].x][i])
                        {
                            line[now] = line[point];
                            line[now].y = i;
                            yes[line[point].p][line[point].x][i] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
                case 1:
                    for(i=1+line[point].x, k=0; k<4 &&  i<x && (!map[i+1][line[point].y]) && (!map[i+1][line[point].y+1]); i++,k++)
                    {
                        if(!yes[line[point].p][i][line[point].y])
                        {
                            line[now] = line[point];
                            line[now].x = i;
                            yes[line[point].p][i][line[point].y] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
                case 2:
                    for(i=line[point].y-1, k=0; k<4 && i>0 && (!map[line[point].x][i]) && (!map[line[point].x+1][i]); i--,k++)
                    {
                        if(!yes[line[point].p][line[point].x][i])
                        {
                            line[now] = line[point];
                            line[now].y = i;
                            yes[line[point].p][line[point].x][i] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
                case 3:
                    for(i=line[point].x-1, k=0; k<4 && i>0 && (!map[i][line[point].y]) && (!map[i][line[point].y+1]); i--,k++)
                    {
                        if(!yes[line[point].p][i][line[point].y])
                        {
                            line[now] = line[point];
                            line[now].x = i;
                            yes[line[point].p][i][line[point].y] = yes[line[point].p][line[point].x][line[point].y]+1;
                            if(line[now].x == x2 && line[now].y == y2)
                            {
                                flag=1;
                                goto finish;
                            }
                            now++;
                        }
                    }
                    break;
            } /* End of switch*/
            
point++;
        }
        finish:
                if(flag)
                {
                    printf("%d\n", yes[line[now].p][line[now].x][line[now].y]-1);
                }
                else
               
{
                    printf("-1\n");
                }
    }
    return 0;
}

----------------解决方案--------------------------------------------------------
写的小软件肯定会有问题了..一般认为软件是改出来得...呵呵..stl和泛型..加全了应该就差不多了..哦也写过几个小程序都不是很满意 看来还是要多改改...
----------------解决方案--------------------------------------------------------
  相关解决方案