【发布时间】:2012-02-28 23:01:32
【问题描述】:
谁能告诉我解决问题的最佳数据结构选择是什么@@http://acm.timus.ru/problem.aspx?space=1&num=1027。
复制到这里供参考。
问题
一个正确的 D++ 程序的文本包含一个符号部分,算术 表达式和 cmets。评论可能出现在任何地方,并且可能 包含任何符号。注释总是由一对符号打开 (* 并由一对符号 * ) 每条评论必须是 关闭。 D++ 中的算术表达式总是以 "(" 开头,是 由 ")" 封闭并且可能只包含符号 "=+-/0123456789)(" 和 “行尾”符号。算术表达式不能以 a 开头 一对符号“(”。您可能会遇到嵌入的括号 算术表达式。在这种情况下,这些括号是平衡的。 这意味着“((1)))”以及“(23))((+)”不正确 算术表达式。一个算术表达式是正确的,如果并且 仅当括号正确放置时。最后,剩下的所有 程序文本(拒绝所有 cmets 和算术的结果 来自程序初始文本的表达式)可能包含每个 符号不包括“(”和“)”。我们要特别注意的是 空格可以在程序文本中的任何地方出现,除非 出现在算术表达式中。
散列有用吗?
我的方法如下(还是有一些bug,集中在DS部分)
#include <stdio.h>
/* enable to get debug print statements */
#define DEBUG_ENABLE 0
#define INSUFFICIENT_BUFFER -1
#define BUFSIZE 20
char buf[BUFSIZE];/*buffer for ungetch*/
int bufp=0;/*next free position in buf*/
/*get a(possibly pushed-back)character*/
int mygetch(FILE *infile)
{
return(bufp>0)? (buf[--bufp]): (getc(infile));
}
/*push a character back on input*/
int ungetch(char c)
{
if(bufp >= BUFSIZE)
{
printf("ungetch:too many characters - increase the stack buffer size\n");
return INSUFFICIENT_BUFFER;
}
else
{
buf[bufp++]=c;
return 0;
}
}
enum CharType
{
isAlphabet=0,
isNumber,
isSpace,
isNewline,
isOperator,
isOpeningBrace,
isClosingBrace,
isStar,
isOther
};
enum
{
False=0,
True
};
/* return different codes for different types of input characters*/
int getCharType(char ch)
{
if((ch>='A'&&ch<='Z') || (ch>='a'&&ch<='z'))
{
return isAlphabet;
}
else if(ch=='+'||ch=='-'||ch=='/'||ch=='=')
{
return isOperator;
}
else if(ch>='0'&& ch<='9')
{
return isNumber;
}
else if(ch=='*')
{
return isStar;
}
else if(ch=='(')
{
return isOpeningBrace;
}
else if(ch==')')
{
return isClosingBrace;
}
else if(ch==' ')
{
return isSpace;
}
else
{
return isOther;
}
}
int parseInputFile(FILE *infile)
{
int ArthExpScanning = 0;
int CmmntScanning = False;
int ch,chtmp;
while((ch=mygetch(infile))!=EOF)
{
#if DEBUG_ENABLE
printf("%c",ch);
#endif /*DEBUG_ENABLE*/
switch(getCharType(ch))
{
/*Arithmetic Expression or possibly comment starts here*/
case isOpeningBrace :
if((chtmp=mygetch(infile))!=EOF)
{
if(getCharType(chtmp)== isStar)
{
CmmntScanning = True;
#if DEBUG_ENABLE
printf("\nCmmnt Scanning = True\n");
#endif /*DEBUG_ENABLE*/
}
else if (CmmntScanning == False)
{
ArthExpScanning += 1;
#if DEBUG_ENABLE
printf("\nArthExpScanning = %d\n",ArthExpScanning);
#endif /*DEBUG_ENABLE*/
if(ungetch(chtmp) == INSUFFICIENT_BUFFER)
{
return (0);
}
}
}
break;
/*Arithmetic Expression possibly closes here */
case isClosingBrace :
if(CmmntScanning == False)
{
ArthExpScanning -= 1;
#if DEBUG_ENABLE
printf("\nArthExpScanning = %d\n",ArthExpScanning);
#endif /*DEBUG_ENABLE*/
}
#if DEBUG_ENABLE
if(ArthExpScanning < 0)
{
printf("\nerror here!!\n");
}
#endif /*DEBUG_ENABLE*/
break;
case isStar :
if((chtmp=mygetch(infile))!=EOF)
{
if((getCharType(chtmp)== isClosingBrace) && (CmmntScanning == True))
{
CmmntScanning = False;
#if DEBUG_ENABLE
printf("\nCmmnt Scanning = False\n");
#endif /*DEBUG_ENABLE*/
}
else
{
if(ungetch(chtmp) == INSUFFICIENT_BUFFER)
{
return (0);
}
}
}
break;
case isSpace :
if((CmmntScanning == False) && (ArthExpScanning != 0))
{
/* Space not allowed while scanning arith exp */
#if DEBUG_ENABLE
printf("NO \n");
#endif /*DEBUG_ENABLE*/
return 0;
}
break;
case isAlphabet :
case isOperator :
case isNumber :
case isNewline :
default:
break;
}
}
if((ArthExpScanning == 0) && (CmmntScanning == False))
{
/* if there are no open braces and comments left after parsing the entire
file return success*/
return 1;
}
else
{
return 0;
}
}
int main(int argc,char *argv[])
{
FILE *infile;
if(argc != 2)
{
printf("Correct usage is : D++ExpParser inputfilename\n");
return (-1);
}
if((infile = fopen(argv[1],"r")) == NULL )
{
printf("Not able to open file : %f\n",argv[1]);
return (-2);
}
if(parseInputFile(infile))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
fclose(infile);
}
【问题讨论】:
-
不明白你的问题。在什么情况下散列?您是否尝试保留将要解析的符号的地图。请清楚地解释您的问题,以便社区中的任何人帮助您。否则你的问题可能会被否决。谢谢。
-
我不太了解哈希的概念,所以在这里吐槽
标签: c data-structures