| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 809 人关注过本帖
标题:一个C++队列程序,麻烦帮看下
只看楼主 加入收藏
lly10120303
Rank: 2
等 级:论坛游民
帖 子:11
专家分:12
注 册:2011-3-30
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
一个C++队列程序,麻烦帮看下
这是个queue.h文件
const int maxsize=50;
class queue
{
private:
    int front,rear,count;
    DataType QList[maxsize];
public:
    queue();
    int GetLength()const;
    int QueueEmpty()const;
    int QueueFull()const;
    DataType QueueFront()const;
    void QueueInsert(const DataType &a);
    DataType QueueDelete();
    void Queueprint();
};
queue::queue():front(0),rear(0),count(0)
{}
int queue::GetLength()const//返回队列当前的长度
{
    return count;
}
int queue::QueueEmpty()const//判断队列是否为空,为空返回1
{
    if(count==0) return 1;
    else return 0;
}
int queue::QueueFull()const//判断队列是否满了,满了为1
{
    if(count==maxsize) return 1;
    else return 0;
}
DataType queue::QueueFront()const//返回队列最前面得数据,不改变队列
{
    if(QueueEmpty())
    {
        cerr<<"queue is empty"<<endl;
        exit(1);
    }
    return QList[front];
}
void queue::QueueInsert(const DataType &a)//在队尾插入一个数据a
{
    if(QueueFull())
    {
        cerr<<"queue is full"<<endl;
        exit(1);
    }
    QList[rear]=a;
    rear=(rear+1)%maxsize;
    count++;
}
DataType queue::QueueDelete()//删除队首的数据,并且返回
{
    if(QueueEmpty())
    {
        cerr<<"queue is empty"<<endl;
        exit(1);
    }
    DataType temp;
    temp=QList[front];
    front=(front+1)%maxsize;
    count--;
    return temp;
}
void queue::Queueprint()//打印队列的数据每行8个
{
    int i=0,j=count;
    while(i<j)
    {
        cout<<QueueDelete()<<" ";
        i++;
        if(i%8==0) cout<<endl;
    }
}
下面是主文件
#include<iostream.h>
#include<fstream>
#include<cstdlib>
#include<string>
typedef double DataType;
#include"queue.h"
using namespace std;
void main()
{
    queue q;
    DataType x;
    int i=20,j;
    ofstream ofile;//创建一个输出文件流对象
    ofile.open("pq.txt");//打开文件
    if(!ofile)
    {
        cerr<<"文件打不开"<<endl;
        exit(1);
    }
    cout<<"请输入双精度数据20个"<<endl;
    for(j=0;j<20;j++)//向文件输入数据
    {
        cin>>x;
        ofile<<x;
    }
    ofile.close();//关闭文件
    ofile.clear();//清楚文件状态值
    ifstream ifile;//创建一个输入文件流对象
    ifile.open("pq.txt");
    if(!ifile)
    {
        cerr<<"文件打不开"<<endl;
        exit(1);
    }
    while(ifile>>x)//输出数据并且放入队列中
    {
        q.QueueInsert(x);
    }
    q.Queueprint();//打印队列中数据每行8个数据
}
当我输入:1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 时结果不对,为什么啊?
搜索更多相关主题的帖子: return 
2011-05-09 23:03
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:20 
结果要什么样子的  就 你输入的那些
2011-05-09 23:09
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
程序代码:
//main.cpp
    for(j=0;j<20;j++)//向文件输入数据
    {
        cin>>x;
        ofile<<x << ' ';
    }
记得在后面加上空格
不然 读文件的时候 就看做一个整数给你 读进来啦  

图片附件: 游客没有浏览图片的权限,请 登录注册
2011-05-09 23:35
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
没加空格时的

2011-05-09 23:37
lly10120303
Rank: 2
等 级:论坛游民
帖 子:11
专家分:12
注 册:2011-3-30
收藏
得分:0 
谢谢你!!!
2011-05-10 22:29
快速回复:一个C++队列程序,麻烦帮看下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016009 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved