繁体   English   中英

为什么程序的输出中有空格?

[英]Why is there a whitespace in my program's output?

刻录DVD时,至关重要的是要不断向表面的激光束烧录坑馈送数据,否则DVD会失败。 大多数领先的DVD刻录应用程序都使用循环缓冲区将数据从硬盘流式传输到DVD上。 第一部分,“写入过程”用数据填充圆形缓冲区,然后当激光束将凹坑烧到DVD表面上时,开始从缓冲区读取“刻录过程”。 如果缓冲区开始变空,则应用程序应继续使用磁盘中的新数据填充缓冲区中的空白空间。 使用循环队列实施此方案。

对于上述问题,我将代码编写如下

#include<iostream>
#include<string.h>
using namespace std;
#define max 5
struct queue
{
    char a[max];
    int f,r;
}q;
void initialize()
{
    q.f=q.r=-1;
}
int enqueue(char c)
{
    if(((q.f==0)&&(q.r==max-1)) || (q.r+1==q.f))
        return 1;
    else{
    if(q.r==-1)
    {
        q.r=0;
        q.f=0;
    }
    else if(q.r==max-1)
        q.r=0;
    else
        q.r++;
    q.a[q.r]=c;
    }return 0;
}
char dequeue()
{
    if(q.f==-1)
    {
        cout<<"Empty queue";
        return '\0';
    }
    else
    {
        char c = q.a[q.f];
        if(q.r==q.f)
            q.r=q.f=-1;
        else if(q.f==max-1)
            q.f=0;
        else
            q.f++;
        return c;
    }
}
void display()
{
    int i;
    for(i=0;i<max-1;i++)
        cout<<q.a[i]<<"\t";
    cout<<"\nfront: "<<q.f<<"\trear: "<<q.r<<endl;
}
int main()
{
    string str,str1;
    cout<<"Enter a String to write data in DVD\n";
    getline(cin,str,'#');
    int i,f,choice;

    for(i=0;str[i]!='\0';i++)
    {
        f=enqueue(str[i]);
        if(f==1)
        {
            do{
            cout<<"Buffer is:\n";
            display();
            cout<<"Enter 1 to read and 2 to exit\n";
            cin>>choice;
            if(choice==1)
            {
                str1=str1+dequeue();
                cout<<"output: "<<str1<<endl;
            }
            f=enqueue(str[i]);
            i++;
            }while(choice!=2);
        }
        if(choice==2)
            break;
        f=0;
    }
}

我不知道为什么在代码运行时出现白点

在此处输入图片说明

谁能指出我在哪里犯错?

您忘记了调用initialize ,因此qfqr都不为-1 在您的情况下,它们是0 ,因此系统认为a[0]已经存在某些内容并将其打印出来。 它不可打印,因此您只能在它后面看到\\t 因此,初始化应该在您不能忘记调用的构造函数中完成。

从C ++ 11开始,您可以直接使用来初始化fr

struct queue
{
    char a[max];
    int f=-1, r=-1;
} q;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM