回复 5楼 TonyDeng
vs2013,我写的是c程序,不是cpp。在VS2013下通过了,不过shuzhuangfuzhi确实应该写成struct node *类型,我把它改过来问题依旧。
目前我通过struct node *p[]代替每次对malloc()的调用来分配内存地址,然后可以成功的运行了。
以下是我成功运行的程序:
#include<stdio.h>
#include <ctype.h>
#define MAXLEN 1000
#include <string.h>
#include <stdlib.h>
struct node {
char *p;
struct node *zuo;
struct node *you;
};//采用树状结构存取变量
int shuzhuangfuzhi(struct node *, char *word, int weizhi, struct node *pn[]);//该函数用来排序,将新得到的单词和已有结构中的单词相比较,
//单词小于已有单词,则传入结构中的*zuo指针中,之后继续比较,若大于则传入*you指针中,如相等则不进行任何操作,通过这种方式进行树状排序
int getword(char *word, int lim);//读入一个单词
int bijiao(char *p, char *q);//返回一个正负数,正负号表示大于或小于,位数表示具体在哪一位出现的不相等
int bijiao1(char *p[], char *q);//用来将所得的单词和已设变量名相比较,如单词和某一个变量相等,则返回0,否则返回-1
void printree(struct node *, int weishu);//用来打印排序好的树状结构中的字符串
int main(int argc, char *argv[])
{
struct node *budeyi[100];
int ab = 0;
for (ab = 0; ab < 20; ab++)
{
budeyi[ab] = (struct node *) malloc(sizeof(struct node));
}
char *pk[1000];
for (ab = 0; ab < 100; ab++)
{
pk[ab] = (char *)malloc(sizeof(char *));
}
struct node *p = NULL;
char *duizhao[] = { "void", "int", "char", "float", "double", "unsigned", "signed", NULL };
int weishu, a = 0, b = 0, c = 0;
if (argc == 1)weishu = 6;
else if (argc > 2)printf("wrong use \n");
else {
++argv;
while ((a = *(*argv)++))
if (a<'0'&&a>'9')
{
printf("wrong usage");
return -1;
}
else b = b * 10 + a - '0';
weishu = b;
}
char word[MAXLEN];
while (getword(word, MAXLEN) != EOF)
{
if (bijiao1(duizhao, word) == 0)
{
while ((c = getword(word, MAXLEN)) != ';'&& bijiao1(duizhao, word) != 0)
if (isalpha(c))
p = shuzhuangfuzhi(p, word, weishu, budeyi, pk);
}
}
printree(p, weishu);
printf("\n");
system("pause");
}
char last[] = { "" };
void printree(struct node *p, int weishu)
{
int a;
if (p != NULL)
{
printree(p->zuo, weishu);
if ((a = bijiao(last, p->p)) < 0)
a = -a;
if (a > weishu)
printf("%s ", p->p);
else
printf("\n%s ", p->p);
strcpy_s(last, 50, p->p);
if (p->you != NULL)
printree(p->you, weishu);
}
}
struct node *talloc(void);
char *strup(char *);
int shuzhuangfuzhi(struct node *p, char *word, int weizhi, struct node *pn[], char *pl[])
{
int a;
if (p == NULL)//将所得变量名放入结构中,若结构指针为null,则说明此节点的字符指针没有赋值,则将变量名放入字符指针p->中
{
p = talloc(pn);//若之前结构指针p为null,则调用talloc函数为其分配内存地址,###我就是在第一次调用talloc函数时,给我分配了指针地址,当以第二次调用talloc(即读入的第三个字符)时,
//没有给我分配指针地址。
p->p = strup(word, pl);
p->zuo = NULL;
p->you = NULL;
}
else if ((a = bijiao(p->p, word)) > 0)//若该结构指针不是NULL说明此节点的字符指针已赋值,则将变量名与字符指针中的字符进行比较,若大于,则将
//变量名与右节点即*you结构指针进行比较
{
p->zuo = shuzhuangfuzhi(p->zuo, word, weizhi, pn, pl);
}
else if (a < 0)//若小于,变量名与左节点即*zuo结构指针进行比较
{
p->you = shuzhuangfuzhi(p->you, word, weizhi, pn, pl);
}
return p;//将变量地址返回
}
int bijiao(char *p, char *q)
{
int a = 0;
while (*(p + a) == *(q + a) && *(p + a) != '\0')
a++;
if (*(p + a) == *(q + a))
return 0;
if (*(p + a) > *(q + a))
return a + 1;
if (*(p + a) < *(q + a))
return -(a + 1);
}
int bijiao1(char *p[], char *q)
{
while (*p != NULL)
{
if (strcmp(*p++, q) == 0)
return 0;
}
return -1;
}
int getword(char *word, int lim)
{
int c, getch(void);
void ungetch(int);
char *w = word;
while (isspace(c = getch()));
if (c != EOF)
*w++ = c;
if (!isalpha(c))
{
*w = '\0';
return c;
}
for (; --lim > 0; w++)
if (!isalnum(*w = getch()))
{
ungetch(*w);
break;
}
*w = '\0';
return word[0];
}
#define BUFSIZE 10000
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch:too many");
else
buf[bufp++] = c;
}
int m = 0;
struct node *talloc(struct node *p[])
{
return p[m++];
}
int q = 0;
char *strup(char *s, char *ph[])
{
char *p;
p = ph[q++];
if (p != NULL)
strcpy_s(p, 50, s);
return p;
}