为什么数组的值不变呢?

xinhuowei 2005-05-28 10:09:42
我的代码是如下的,我测过了,下标的确是变的,可是为什么给数组赋值时,只有前四个的值,循环后就不没有了呢?而且在输出第四项的时候还会多输出一个?也没有让它换行啊?
#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]<<" ";
// }
}







...全文
189 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
useresu 2005-05-28
  • 打赏
  • 举报
回复
不过我估计就是我上头说的问题,

q没有加,

所以只改变了[0][i]的元素,就是前四个值了

楼主大意了,呵呵
useresu 2005-05-28
  • 打赏
  • 举报
回复
for(int q=0;t<20;t++)//这里应该是q++吧.
// {
// for(int p=0;p<4;p++)
// cout<<table[q][p]<<" ";
// }


呵呵,程序太长了,看不懂,
mostideal 2005-05-28
  • 打赏
  • 举报
回复
先帮你顶。有时间再来慢慢看了。。
xinhuowei 2005-05-28
  • 打赏
  • 举报
回复
恩,知道了,我该结帖了:(
useresu 2005-05-28
  • 打赏
  • 举报
回复
运行for循环之前你就可以看通过watch看tablg中的值了,

看是不是从文件读出来到数组时出错了.

不难跟踪到错误吧
useresu 2005-05-28
  • 打赏
  • 举报
回复
单步跟踪

想帮你调试,可是程序太长了,
看就需要时间,
楼主还是自己单步调试吧
xinhuowei 2005-05-28
  • 打赏
  • 举报
回复
555555555555555555脑袋爆炸了,改过了,还是出错啊,每次输出tablg[0][3]的时候都会换行又输出个"-"来,是为什么为什么啊?

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧