c++ 链接错误 高分求解
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#define MAX 100
#define OVERFLOW -2
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int Status;
class Queue
{
public:
void InitQueue();
Status EnQueue(ElemType e);a
Status DnQueue(ElemType &e);
void YH易做图(int n);
private:
ElemType *base;
int front; //头指针
int rear; //尾指针
};
void Queue::InitQueue()
{
base = new ElemType[MAX];
if(!base)
{
exit (OVERFLOW);
}
front = rear = 0;
}
Status Queue::EnQueue(ElemType e)
{
if((rear + 1) % MAX == front)
{
cout<<"队列满"<<endl;
return ERROR; //约定队头指针在队尾指针的下位置为满
}
base[rear] = e;
rear = (rear + 1) % MAX;
return OK;
}
Status Queue::DnQueue(ElemType &e)
{
if(rear == front)
{
cout<<"队列为空"<<endl;
return ERROR;
}
e = base[front];
front = (front + 1) % MAX;
return OK;
}
//输出杨辉三角
void Queue::YH易做图(int n)
{
int i = 0, j = 0, k = 0;
ElemType pa = 0, pb = 0;
fstream out;
out.open("output.txt",ios::out);
if(!out)
{
cout<<"error on open "<<endl;
exit (0);
}
EnQueue(1);
EnQueue(0);
for(i = 1; i < n ; i++)
{
for(k = i; k < n; k++)
{
cout<<" ";
out<<" ";
}
for(j = 0 ;j <= i ; j++)
{
DnQueue(pb);
if(pb != 0)
{
cout<<pb<<" ";
out<<pb<<" ";
}
EnQueue(pa + pb);
pa=pb;
}
EnQueue(0);
cout<<endl;
out<<endl;
}
for(i = 0; i < n; ++i)
{
DnQueue(pb);
cout<<pb<<" ";
out<<pb<<" ";
}
cout<<endl;
out<<endl;
out.close();
}
int mian(void)
{
Queue Q;
int n=5;
fstream in;
in.open("input.txt", ios::in);
if(!in)
{
cout<<"error on open "<<endl;
exit (0);
}
while(!in.eof())
{
in>>n;
cout<<n;
}
in.close();
//规定输入的数的范围为0 < n < 127 的整型数,浮点型做最大整处理
if(n <= 0 || n >= 127)
{
cout<<"文件中的数不符合要求"<<endl;
}
Q.InitQueue();
Q.YH易做图(n);
return 0;
}
补充:给出答案,就送100分 ,决不食言,现在不把分放上去,是因为好多次都没有人回答,白白浪费了很多分!
答案:我做了小小改错,请见粗体字注解
程序基本上是好的,有一个问题需要你在逻辑上解决,就是n大到一定程度时,杨辉三角中的数可能太大超出定义整数的数值范围。格式也很难看清。建议把n数值减低,但要测试大于n时程序如何处理。这样更好展示杨辉三角。留给你自己慢慢做。
另外,你用文件放输入整数,究竟是打算放一个还是多个。我发现只有最后一个起作用。
现在我给出的程序时可以运行的,请你自己按要求完善。
#include的文件及名字和你用的开发环境和编译器有关,我用的是比较新的标准的C
++编译器。
#include <iostream>
#include <cstdlib> // changed from stdlib.h
#include <fstream>
#define MAX 100
#define OVERFLOW -2
#define ERROR 0
#define OK 1
using namespace std; // new
typedef int ElemType;
typedef int Status;
class Queue
{
public:
void InitQueue();
Status EnQueue(ElemType e); // remove a
Status DnQueue(ElemType &e);
void YH易做图(int n);
private:
ElemType *base;
int front; // head pointer
int rear; // tail pointer
};
void Queue::InitQueue()
{
base = new ElemType[MAX];
if(!base)
{
exit (OVERFLOW);
}
front = rear = 0;
}
Status Queue::EnQueue(ElemType e)
{
if((rear + 1) % MAX == front)
{
cout<<"Queue full"<<endl;
return ERROR; //List full when the head pointer position is below tail pointer position
}
base[rear] = e;
rear = (rear + 1) % MAX;
return OK;
}
Status Queue::DnQueue(ElemType &e)
{
if(rear == front)
{
cout<<"Queue empty"<<endl;
return ERROR;
}
e = base[front];
front = (front + 1) % MAX;
return OK;
}
//Output YANG-HUI 易做图
void Queue::YH易做图(int n)
{
int i = 0, j = 0, k = 0;
ElemType pa = 0, pb = 0;
fstream out;
out.open("output.txt",ios::out);
if(!out)
{
cout<<"error on open "<<endl;
exit (0);
}
EnQueue(1);
EnQueue(0);
for(i = 1; i < n ; i++)
{
for(k = i; k < n; k++)
{
cout<<" ";
out<<" ";
}
for(j = 0 ;j <= i ; j++)
{
DnQueue(pb);
if(pb != 0)
{
cout<<pb<<" ";
out<<pb<<" ";
}
EnQueue(pa + pb);
pa=pb;
}
EnQueue(0);
cout<<endl;
out<<endl;
}
for(i = 0; i < n; ++i)
{
DnQueue(pb);
cout<<pb<<" ";
out<<pb<<" ";
}
cout<<endl;
out<<endl;
out.close();
}
int main(void) // changed from mian()
{
Queue Q;
int n=5;
fstream in;
in.open("input.txt", ios::in);
if(!in)
{
cout<<"error on open "<<endl;
exit (0);
}
while(!in.eof())
{
in>>n;
cout<<n;
}
in.close();
//define the input number range 0 < n < 127 integer, float to be treated as the ceiling integer (round up).
if(n <= 0 || n >= 127)
{
cout<<"the number in the file is not qualified"<<endl;
}
Q.InitQueue();
Q.YH易做图(n);
return 0;
}两个地方都是小笔误啦
帮你改过来的 错的地方有注释
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#define MAX 100
#define OVERFLOW -2
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int Status;
class Queue
{
public:
void InitQueue();
Status EnQueue(ElemType e);//这里多了个a
Status DnQueue(ElemType &e);
void YH易做图(int n);
private:
ElemType *base;
int front; //头指针
int rear; //尾指针
};
void Queue::InitQueue()
{
base = new ElemType[MAX];
if(!base)
{
exit (OVERFLOW);
}
front = rear = 0;
}
Status Queue::EnQueue(ElemType e)
{
if((rear + 1) % MAX == front)
{
cout<<"队列满"<<endl;
return ERROR; //约定队头指针在队尾指针的下位置为满
}
base[rear] = e;
rear = (rear + 1) % MAX;
return OK;
}
Status Queue::DnQueue(ElemType &e)
{
if(rear == front)
{
cout<<"队列为空"<<endl;
return ERROR;
}
e = base[front];
front = (front + 1) % MAX;
return OK;
}
//输出杨辉三角
void Queue::YH易做图(int n)
{
int i = 0, j = 0, k = 0;
ElemType pa = 0, pb = 0;
fstream out;
out.open("output.txt",ios::out);
if(!out)
{
cout<<"error on open "<<endl;
exit (0);
}
EnQueue(1);
EnQueue(0);
for(i = 1; i < n ; i++)
{
for(k = i; k < n; k++)
{
cout<<" ";
out<<" ";
}
for(j = 0 ;j <= i ; j++)
{
DnQueue(pb);
if(pb != 0)
{
cout<<pb<<" ";
out<<pb<<" ";
}
EnQueue(pa + pb);
pa=pb;
}
EnQueue(0);
cout<<endl;
out<<endl;
}
for(i = 0; i < n; ++i)
{
DnQueue(pb);
cout<<pb<<" ";
out<<pb<<" ";
}
cout<<endl;
out<<endl;
out.close();
}
int main(void)//这里main写错了
{
Queue Q;
int n=5;
fstream in;
in.open("input.txt", ios::in);
if(!in)
{
cout<<"error on open "<<endl;
exit (0);
}
while(!in.eof())
{
in>>n;
cout<<n;
}
in.close();
//规定输入的数的范围为0 < n < 127 的整型数,浮点型做最大整处理
if(n <= 0 || n >= 127)
{
cout<<"文件中的数不符合要求"<<endl;
}
Q.InitQueue();
Q.YH易做图(n);
return 0;
}
上一个:C++文件和类
下一个:C++是什么意思