Question 5: Write a function to shift n loops of a char string to the right. For example, if "abcdefghi" is n = 2, it should be "hiabcdefgh" after the shift"
The function header is as follows:
// Pstr is a pointer to a string ending with '\ 0'
// Steps is the n that requires moving
Void loopmove (char * pstr, int steps)
{
// Fill in...
}
Answer:
Answer 1:
Void loopmove (char * pstr, int steps)
{
Int n = strlen (pstr)-steps;
Char TMP [max_len];
Strcpy (TMP, pstr + n );
Strcpy (TMP + steps, pstr );
* (TMP + strlen (pstr) = '\ 0 ';
Strcpy (pstr, TMP );
}
Correct answer 2:
Void loopmove (char * pstr, int steps)
{
Int n = strlen (pstr)-steps;
Char TMP [max_len];
Memcpy (TMP, pstr + N, steps );
Memcpy (pstr + steps, pstr, N );
Memcpy (pstr, TMP, steps );
}
Analysis:
This question mainly examines the interviewer's proficiency in the standard library functions. When necessary, referencing the library functions can greatly simplify the workload of programming.
The most frequently used library functions include:
(1) strcpy
(2) memcpy
(3) memset
Question 6: The following table lists the known WAV file formats. Open a WAV file, organize the wav file header in an appropriate data structure, and parse the information in WAV format.
Wave file format description
|
Offset address |
Bytes |
Data Type |
Internal capacity |
File Header |
00 h |
4 |
Char |
"Riff" flag |
04 H |
4 |
Int32 |
File Length |
08 h |
4 |
Char |
"Wave" flag |
0ch |
4 |
Char |
"FMT" flag |
10 h |
4 |
|
Transition bytes (not fixed) |
14 h |
2 |
Int16 |
Format category |
16 h |
2 |
Int16 |
Number of channels |
18 h |
2 |
Int16 |
Sampling rate (number of samples per second), indicating the playback speed of each channel |
1ch |
4 |
Int32 |
Waveform audio data transmission rate |
20 h |
2 |
Int16 |
Adjust the number of data blocks (in bytes) |
22 h |
2 |
|
Number of Data digits per sample |
24 h |
4 |
Char |
DATA tag "data" |
28 h |
4 |
Int32 |
Length of Speech Data |
Answer:
The WAV file format is defined as the structure waveformat:
Typedef struct tagwaveformat
{
Char criffflag [4];
Uin32 nfilelen;
Char cwaveflag [4];
Char cfmtflag [4];
Char ctransition [4];
Uin16 nformattag;
Uin16 nchannels;
Uin16 nsamplespersec;
Uin32 navgbytespersec;
Uin16 nblockalign;
Uin16 nbitnumpersample;
Char cdataflag [4];
Uin16 naudiolength;
} Waveformat;
Assume that the wav file content is read and stored in the memory unit starting with the pointer buffer, the code for analyzing the file format is very simple:
Waveformat;
Memcpy (& waveformat, buffer, sizeof (waveformat ));
You can directly access the members of The waveformat to obtain the format information of a specific WAV file.
Analysis:
Exam 6 examines the ability of the interviewer to organize data structures. experienced programmers organize a whole data member into a struct using pointer type conversion, you can use functions such as memcpy and memset to directly use the struct address for the overall operation of the struct. Through this question, we can see whether the interviewer has rich experience in programming.
Question 7: Compile the constructor, destructor, and value assignment functions of the string class. It is known that the prototype of the string class is:
Class string
{
Public:
String (const char * STR = NULL); // common Constructor
String (const string & other); // copy the constructor
~ String (void); // destructor
String & operate = (const string & other); // value assignment function
PRIVATE:
Char * m_data; // used to save strings
};
Answer:
// Common Constructor
String: string (const char * Str)
{
If (STR = NULL)
{
M_data = new char [1]; // score point: An empty string is automatically applied for storing the end sign '\ 0'.
// Extra points: determines if m_data is added with null
* M_data = '\ 0 ';
}
Else
{
Int length = strlen (STR );
M_data = new char [Length + 1]; // It is better if null can be added.
Strcpy (m_data, STR );
}
}
// String destructor
String ::~ String (void)
{
Delete [] m_data; // or delete m_data;
}
// Copy the constructor
String: string (const string & Other) // score point: the input parameter is of the const type.
{
Int length = strlen (other. m_data );
M_data = new char [Length + 1]; // points for adding null to m_data
Strcpy (m_data, other. m_data );
}
// Value assignment function
String & string: operate = (const string & Other) // score point: the input parameter is of the const type.
{
If (this = & Other) // score point: Check auto-assigned values
Return * this;
Delete [] m_data; // score point: Release the original memory resource
Int length = strlen (other. m_data );
M_data = new char [Length + 1]; // points for adding null to m_data
Strcpy (m_data, other. m_data );
Return * This; // score point: return the reference of this object
}
Analysis:
The interviewer who can accurately compile string class constructor, copy constructor, assign value function and destructor has at least 60% of the basic C ++ skills!
This class includes the pointer class member variable m_data. When the class includes pointer class member variables, you must reload the copy constructor, value assignment function, and destructor, this is not only a basic requirement for C ++ programmers, but also a special clause in Objective C ++.
Take a closer look at this class and pay special attention to the significance of adding comments to the score points and points. In this way, more than 60% of the basic C ++ skills are available!
Question 8: Tell me as many static and const keywords as possible
Answer:
The static keyword has at least N functions:
(1) The static variable in the function body applies to this function body. Unlike the auto variable, the memory of this variable is allocated only once, therefore, the value remains the value of the previous time during the next call;
(2) The static global variables in the module can be accessed by the functions used in the module, but cannot be accessed by other functions outside the module;
(3) The static function in the module can only be called by other functions in the module. The scope of use of this function is limited to the module that declares it;
(4) static member variables in the class belong to the entire class and only one copy of all objects in the class;
(5) The static member function in the class belongs to the whole class. This function does not receive the this pointer, so it can only be a static member variable of the category.
The const keyword has at least N functions:
(1) to prevent a variable from being changed, you can use the const keyword. When defining the const variable, you usually need to initialize it, because there will be no chance to change it in the future;
(2) For pointers, you can specify the pointer itself as const, or you can specify the data referred to by the pointer As const, or both of them as const;
(3) In a function declaration, const can modify the form parameter, indicating that it is an input parameter and cannot change its value within the function;
(4) If the member function of the class is specified as the const type, it indicates that it is a constant function and the member variables of the class cannot be modified;
(5) For a member function of the class, you must specify its return value as the const type so that its return value is not the "Left value ". For example:
Const classa operator * (const classa & A1, const classa & A2 );
The return result of operator * must be a const object. If not, such abnormal code will not cause compilation errors:
Classa A, B, C;
(A * B) = C; // assign a value to the result of a * B
Operation (A * B) = C obviously does not conform to the programmer's original intention, and does not make any sense.
Analysis:
Surprised? What are the functions of small static and const? If you can only answer 1 ~ 2. You have to close the door and practice well.
This question can be used to check whether the subject's knowledge about programming is elementary, intermediate, or in-depth. Without a certain knowledge breadth and depth, it is impossible to give a comprehensive answer to this question. Most people can only answer some functions of the static and const keywords.
4. Tips
Question 1: Write a c function. If the processor is big_endian, 0 is returned. If the processor is little_endian, 1 is returned.
Answer:
Int checkcpu ()
{
{
Union W
{
Int;
Char B;
} C;
C. A = 1;
Return (C. B = 1 );
}
}
Analysis:
Embedded system developers should be familiar with the little-Endian and big-Endian modes. In little-Endian mode, the number of CPUs is stored in bytes from low to high, while in big-Endian mode, the number is stored in bytes from high to low. For example, the storage method of 16-bit 0x1234 in little-Endian mode CPU memory (assuming that it starts from address 0x4000) is:
Memory Address |
Store content |
Zero X 4000 |
0x34 |
Zero X 4001 |
0x12 |
In big-Endian mode, the CPU memory is stored as follows:
Memory Address |
Store content |
Zero X 4000 |
0x12 |
Zero X 4001 |
0x34 |
32-bit-width 0x12345678 storage method in the little-Endian mode CPU memory (assuming it starts from address 0x4000:
Memory Address |
Store content |
Zero X 4000 |
0x78 |
Zero X 4001 |
0x56 |
Zero X 4002 |
0x34 |
Zero X 4003 |
0x12 |
In big-Endian mode, the CPU memory is stored as follows:
Memory Address |
Store content |
Zero X 4000 |
0x12 |
Zero X 4001 |
0x34 |
Zero X 4002 |
0x56 |
Zero X 4003 |
0x78 |
The storage order of union Union is that all members are stored from the low address. The interviewer's answer uses this feature to easily obtain the CPU reads and writes to the memory in the little-Endian or big-Endian mode. If anyone could give this answer on the spot, it would be a talented programmer.
Question 2: Write a function and return 1 + 2 + 3 +... + N value (assuming that the result does not exceed the range of long integer variables)
Answer:
Int sum (int n)
{
Return (long) 1 + n) * n/2; // or return (1l + n) * n/2;
}
Analysis:
The simplest answer is the best one. The following answers, or optimization based on the following solutions, cannot be compared with direct return (1 L + n) * n/2 no matter how hard it is!
Int sum (int n)
{
Long sum = 0;
For (INT I = 1; I <= N; I ++)
{
Sum + = I;
}
Return sum;
}
So programmers need to use mathematics and other knowledge in programming with sensitivity.