编一个c程序,按照字母表的顺序分组打印变量名字。字符串和注释中的单词不考虑...十分感谢 20
#include "string.h"
int tp(char m)
{
if (m >= 'a' && m <= 'z')
{
return 1;
}
if ((m >= 'A' && m <= 'Z') ||
(m >= '0' && m <= '9') ||
m == '_')
{
return 2;
}
return 3;
}
char* getwd(char *c)
{
while (tp(*c) <= 2)
{
c++;
}
return c;
}
void sort(char**m, int n)
{
int i, j;
for (i = 0;i < n - 1;i++)
{
for (j = i + 1;j < n;j++)
{
if (strcmp(m[i], m[j]) > 0)
{
char *c = m[i];
m[i] = m[j];
m[j] = c;
}
}
}
}
void ptsym(char *str)
{
char *name[] = {"int", "double", "char"};
int n = sizeof(name) / sizeof(char *);
int innot = 0;
int nott = 0;
char *symbols[1000];
int fn = 0;
char *nxt;
int st;
int i;
int f;
while (*str)
{
if (innot)
{
if (innot == 2)
{
if (*str == '"' && *(str - 1) != '\\')innot = 0;
}
else if (nott)
{
if (*str == '*' && *(str + 1) == '/')
{
innot = 0;
str++;
}
}
else if (*str == '\n')
{
innot = 0;
}
str++;
continue;
}
while (*str == ' ' || *str == '\t')
{
str++;
}
st = tp(*str);
f = 0;
if (st <= 2)
{
nxt = getwd(str + 1);
if (st == 1)
{
char tmp = *nxt;
*nxt = 0;
for (i = 0;i < n;i++)
{
if (strcmp(name[i], str) == 0)
{
f = 1;
break;
}
}
*nxt = tmp;
}
str = nxt;
}
if (f)
{
char *cur;
while (*str == ' ' || *str == '\t' || *str == '*')
{
str++;
}
cur = str;
st = tp(*str);
f = 0;
if (st <= 2)
{
nxt = getwd(str + 1);
str = nxt;
while (*str == ' ' || *str == '\t')
{
str++;
}
if (*str == '(')
{
continue;
}
symbols[fn] = (char*)malloc(nxt - cur + 1);
strncpy(symbols[fn], cur, nxt - cur);
symbols[fn][nxt-cur] = 0;
printf("find %s\n", symbols[fn]);
fn++;
if (fn >= 1000)break;
continue;
}
}
else
{
if (*str == '/' && *(str + 1) == '/')
{
innot = 1;
nott = 0;
str += 2;
continue;
}
if (*str == '/' && *(str + 1) == '*')
{
innot = 1;
nott = 1;
str += 2;
continue;
}
if (*str == '"') innot = 2;
str++;
}
}
sort(symbols, fn);
for (i = 0; i < fn;i++)
{
printf("%s\n", symbols[i]);
free(symbols[i]);
}
}
int main()
{
char data[] = " int a;\
int love (){ \
int m; \
int n; \
double d; int *jcm;\
if () {} \n\
else {} int mm;} \
//int ddc; double jj; \n\
int kod; \n\
int jjm; \
/*ccccc int oop;\
int jjkk; \
int ooobj;\
*/\
int ccj;\
char str[100] = \"int i;int j;int lo;double 123;\";\
";
ptsym(data);
return 0;
}