为什么数组的值不变呢?
我的代码是如下的,我测过了,下标的确是变的,可是为什么给数组赋值时,只有前四个的值,循环后就不没有了呢?而且在输出第四项的时候还会多输出一个?也没有让它换行啊?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
string opp,xx,yy,zz;
string table[16][4];
int find(string tab[][2],string x,int n,int m)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(x==tab[i][j])
return i;
}
}
return -1;
}
int getreg(int m,int i)
{
string tab[4][2];
tab[0][0]="+";
tab[0][1]="ADD";
tab[1][0]="-";
tab[1][1]="SUB";
tab[2][0]="*";
tab[2][1]="MPY";
// tab[3][0]="\";
// tab[3][1]="DIV";
tab[3][0]=":=";
tab[3][1]=":=";
int search;
search=-1;
if(m>0)
{
if(yy!=zz)
{
cout<<"MOV"<<" "<<xx<<", "<<"R"<<i<<" "<<endl;
search=find(tab,opp,4,2);
if(search!=-1)
{
cout<<tab[search][2]<<" "<<yy<<", "<<"R"<<i<<" ";
cout<<"R"<<i<<"包含"<<zz<<" "<<zz<<"在R"<<i<<"中"<<endl;
}
}
else
{
search=find(tab,opp,5,2);
if(search!=-1)
{
cout<<tab[search][1]<<" "<<"R"<<i<<","<<yy<<" "<<"R_"<<i<<"包含"<<yy<<","<<xx;
cout<<" "<<yy<<"在R"<<i<<"和内存单元中,"<<yy<<"在R"<<i<<"中"<<endl;
}
}
opp="";
xx="";
yy="";
zz="";
}
m--;
return m;
}
void main(void)
{
string op,x,y,z;
int i=0;
int j=0;
int k=0;
int n,t;
n=4;
t=n;
// cout<<"请输入寄存器的个数:";
// cin>>n;
cout<<endl;
cout<<"四元式序列"<<" "<<"目标代码"<<" "<<"寄存器描述符"<<" "<<"地址描述符"<<endl;
char ch;
ifstream tfile("program.txt");
tfile.get(ch);
while(!tfile.eof())
{
//if(ch!='/n')
//{
while(ch!=' ')
{
op=op+ch;
tfile.get(ch);
if(tfile.eof())
break;
}
cout<<op<<" ";
opp=op;
table[j][k]=op;
cout<<table[j][k]<<" ";
k=k+1;
while(ch==' ')
{
tfile.get(ch);
if(tfile.eof())
break;
}
while(ch!=' ')
{
x=x+ch;
tfile.get(ch);
if(tfile.eof())
break;
}
cout<<x<<" ";
xx=x;
table[j][k]=x;
k=k+1;
while(ch==' ')
{
tfile.get(ch);
if(tfile.eof())
break;
}
while(ch!=' ')
{
y=y+ch;
tfile.get(ch);
if(tfile.eof())
break;
}
cout<<y<<" ";
yy=y;
table[j][k]=y;
k=k+1;
while(ch==' ')
{
tfile.get(ch);
if(tfile.eof())
break;
}
while(ch!=' '&&ch!='/n')
{
z=z+ch;
tfile.get(ch);
if(tfile.eof())
break;
}
cout<<z<<" ";
zz=z;
table[j][k]=z;
k=0;
j=j+1;
//if(i<t)
//{
// t=getreg(t,i);
// i++;
// }
// getreg(n,i);
op="";
x="";
y="";
z="";
//cout<<opp<<endl;
}
tfile.close();
cout<<endl;
cout<<table[0][3]<<" ";
// for(int q=0;t<20;t++)
// {
// for(int p=0;p<4;p++)
// cout<<table[q][p]<<" ";
// }
}