[英]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
,因此qf
和qr
都不為-1
。 在您的情況下,它們是0
,因此系統認為a[0]
已經存在某些內容並將其打印出來。 它不可打印,因此您只能在它后面看到\\t
。 因此,初始化應該在您不能忘記調用的構造函數中完成。
從C ++ 11開始,您可以直接使用來初始化f
和r
struct queue
{
char a[max];
int f=-1, r=-1;
} q;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.