所以这个程序应该接受一个字符串并将其转换为逆波兰表示法,然后为其生成汇编代码。
例如。如果我输入“x = y”,程序将返回
“RPN:xy=”
“代码:1 lod y 2 sto x”
然而,该程序反而返回乱码并继续这样做,直到它耗尽内存。
这是输入函数
void
getstring()
{
if(datafile) {
file.getline(str,241);
}else{
cout << "Enter a string, please \n\n";
cin.getline( str, 241);
}
nstring++;
}
这些是与它一起工作的功能。
void
internalize()
{
int i, j;
static char inter[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 6, 4, 0, 5, 0, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,
0,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
23,24,25,26,27,28,29,30,31,32,33, 0, 0, 0, 0, 0,
0,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
49,50,51,52,53,54,55,56,57,58,59, 0, 0, 0, 0, 0};
char ch, code, k, *p, *q;
cout << "internal form: \n\n";
k=0;
q=inform;
for(p=str;*p;p++){
*q++ = code = inter[(int)*p];
if(k+code == 0){
k = p-str+1;
ch = *p;
}
}
*q = 0;
for(i=j=0, p = inform;p++,j++;j<len){
cout << setw(3) << (int)*p;
if(++i == 16){
cout << '\n';
i = 0;
}
}
if (i !=0){
cout <<'\n';
}
if((err = (0<k))!=0){
cout << '\n**' << (int)k << "-th nonblank char <" <<ch<<"> is illegal. **\n";
}
}
void
makerpn()
{
static char pr[]={0,0,1,2,3,3,4,4};
char n, *p, *r, s, *t;
cout << "\nRPN:\n\n";
t = stak - 1;
r = rpn;
for (p = inform;p++ ; *p){
if(7 < (s = *p)){
*r++ = s;
//a
}else if(s == 1){
*++t = s;
} else{
while(1){
if(t<stak){
*++t = s;
break;
} else if(pr[s] <= pr[n = *t--]){
*r++ = n;
} else if(3 < n+s){
*++t = n;
*++t = s;
}
break;
}
}
while(stak <= t){
*r++ = *t--;
}
*r = '\0';
for(r=rpn;*r;r++){
cout << ext[(int)*r];
}
cout << "\n\n";
}}
void
gencode()
{
void emit(char, char);
char a,j,lop,n1,n2,*p,*t;
cout << "Generated symbolic code \n\n";
j = len = lop = 0;
t = p = rpn;
while(3 <(a = *++p)){
if(7 <a){
*++t = a;
if((++lop == 2) && (0<len)){
n2=*t--;
n1=*t--;
*++t=j+60;
*++t=n1;
*++t=n2;
emit(2,(j++)+60);
}
}else {
if(lop == 1){
emit(a,*t--);
}else{
if( 1 < lop){
n2 = *t--;
n1 = *t--;
emit(1,n1);
emit(a,n2);
}else {
if((a==4) || (a==6)){
n1 = *t--;
emit(a,n1);
}else {
n1=*t--;
emit(2,j+60);
emit(1,n1);
emit(a,j+60);
if( 59 < n1){
--j;}}}}}}
lop = 0;
}
我猜循环有问题,但我不确定是什么。我已经对它们进行了一段时间的调整,而我所能做的就是让它重复 Hp?而不是一遍又一遍的硬件。