Home > Back-end >  C shortcut tutorial (third edition) chapter11.1 class queue definitions and use the output of the pr
C shortcut tutorial (third edition) chapter11.1 class queue definitions and use the output of the pr

Time:10-07

#include
using namespace std;


The class queue {
Int q [100];
Int sloc rloc;
Public:
Void init ();
Void qput (int I);
Int qget ();
};
//initialize the queue class
Void queue: : init ()
{
Rloc=sloc=0;
}


//put an integer in the queue,
Void queue: : qput (int I)
{
If (sloc==100) {
Cout & lt; & lt; "The Queue is full. \ n";
return ;
}
Sloc++;
Q [sloc]=I;
}


//get an integer from the queue
Int the queue: : qget ()
{
If (rloc==sloc) {
Cout & lt; & lt; "The Queue underflow. \ n";
return 0;
}
Rloc++;
The return of q [rloc];
}


Int main ()
{
The queue a, b;
Anderson, nit ();
B.i nit ();
A.q put (10);
B.q put (19);
A.q put (20);
B.q put (1);
//a: output for 10, 20; 19, 1
Cout & lt; & lt; A.q the get () & lt; & lt; "";

Cout & lt; & lt; A.q the get () & lt; & lt; Endl;
Cout & lt; & lt; B.q the get () & lt; & lt; "";

Cout & lt; & lt; B.q the get () & lt; & lt; Endl;

//case 2: output for: 20, 10; 1
3
//cout & lt; & lt; A.q the get () & lt; & lt; "" & lt; & lt; A.q the get () & lt; & lt; Endl;

//cout & lt; & lt; B.q the get () & lt; & lt; "" & lt; & lt; B.q the get () & lt; & lt; Endl;


return 0;
}

Anyone can help me explain in detail the situation and a second output different reasons?

CodePudding user response:

This is more easy to understand, in this expression, you have two qget () call, but first calculation which qget () is determined by the compiler, is not necessarily written in the left of the first, in the actual project to avoid writing this code: you cannot control the
Cout & lt; & lt; A.q the get () & lt; & lt; "" & lt; & lt; A.q the get () & lt; & lt; Endl;

For another similar code:
C=a [+ + I] - b (+ + I),
The same expression has 2 + + I, participate in the calculation of a, b exactly which elements, by the compiler to control, the results may not meet your expectations,

Different expression of the C or C + + written according to your order execution,
The same expression, similar to fun (f1 (x), f2 (x)) such expression, f1, f2 evaluation sequence is determined by the compiler, formula one is not necessarily first evaluated
  • Related