最近做了一个简单的编辑程序,想在编写C,C++文件时,让关键字高亮显示。
哪位大侠能提供些思路,小弟感激不尽

解决方案 »

  1.   

    关键在于你能不能区分你读出的字符是不是关键字
    给你一段读文件的例子,可以读出关键字,你在修改一下吧
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>struct KeyWord
    {
    char keyword[20];
    int count;
    };
    KeyWord KeyWordTable[]=
    {
    {"else",0},{"for",0},{"if",0},{"while",0}
    };
    int SeqSearch(KeyWord* tab,int n,char* word)
    {
    int i;
    for(i=0;i<n;i++,tab++)
    if(strcmp(word,tab->keyword)==0)
    return i;
    return -1;
    }
    int GetWord(ifstream& fin,char w[])
    {
    char c;
    int i=0; while(fin.get(c)&&!isalpha(c))
    ; if(fin.eof())
    return 0; w[i++]=c;
    while(fin.get(c)&&(isalpha(c)||isdigit(c)))
    w[i++]=c;
    w[i]='\0';
    return 1;
    }
    void main(void)
    {
    const int MAXWORD =50;
    const int NKEYWORDS = sizeof(KeyWordTable)/sizeof(KeyWord);
    int n;
    char word[MAXWORD],c;
    ifstream fin;
    fin.open("SearchKeyWord.cpp",ios::in|ios::nocreate);
    if(!fin)
    {
    cerr<< "Could not open file 'SearchKeyWord'"<<endl;
    exit(1);
    }
    while(GetWord(fin,word))
    if((n=SeqSearch(KeyWordTable,NKEYWORDS,word))!=-1)
    KeyWordTable[n].count++;
    for(n=0;n<NKEYWORDS;n++)
    {
    if(KeyWordTable[n].count>0)
    {
    cout<< KeyWordTable[n].count ;
    cout<<" "<< KeyWordTable[n].keyword <<endl;
    }
    }
    fin.close();
    }
      

  2.   

    我有一个VB语法高亮的源程序~ 
    如果是C++,我想把其中的关键字改了就行了~ 
    如果你需要,就发消息给我.