试题4:为什么标准头文件都有类似以下的结构?
试题4:为什么标准头文件都有类似以下的结构?
| #ifndef __INCvxWorksh #define __INCvxWorksh #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __cplusplus } #endif #endif /* __INCvxWorksh */ |
| #ifndef __INCvxWorksh #define __INCvxWorksh #endif |
| void foo(int x, int y); |
| //pStr是指向以'\0'结尾的字符串的指针 //steps是要求移动的n void LoopMove ( char * pStr, int steps ) { //请填充... } |
| 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 ); } |
| 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 ); } |
|
|
偏移地址 | 字节数 | 数据类型 | 内 容 |
| 文件头
|
00H | 4 | Char | "RIFF"标志 |
| 04H | 4 | int32 | 文件长度 | |
| 08H | 4 | Char | "WAVE"标志 | |
| 0CH | 4 | Char | "fmt"标志 | |
| 10H | 4 | 过渡字节(不定) | ||
| 14H | 2 | int16 | 格式类别 | |
| 16H | 2 | int16 | 通道数 | |
| 18H | 2 | int16 | 采样率(每秒样本数),表示每个通道的播放速度 | |
| 1CH | 4 | int32 | 波形音频数据传送速率 | |
| 20H | 2 | int16 | 数据块的调整数(按字节算的) | |
| 22H | 2 | 每样本的数据位数 | ||
| 24H | 4 | Char | 数据标记符"data" | |
| 28H | 4 | int32 | 语音数据的长度 |
| 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; |
| WAVEFORMAT waveFormat; memcpy( &waveFormat, buffer,sizeof( WAVEFORMAT ) ); |
| class String { public: String(const char *str = NULL); // 普通构造函数 String(const String &other); // 拷贝构造函数 ~ String(void); // 析构函数 String & operate =(const String &other); // 赋值函数 private: char *m_data; // 用于保存字符串 }; |
| //普通构造函数 String::String(const char *str) { if(str==NULL) { m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志'\0'的空 //加分点:对m_data加NULL 判断 *m_data = '\0'; } else { int length = strlen(str); m_data = new char[length+1]; // 若能加 NULL 判断则更好 strcpy(m_data, str); } } // String的析构函数 String::~String(void) { delete [] m_data; // 或delete m_data; } //拷贝构造函数 String::String(const String &other) // 得分点:输入参数为const型 { int length = strlen(other.m_data); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy(m_data, other.m_data); } //赋值函数 String & String::operate =(const String &other) // 得分点:输入参数为const型 { if(this == &other) //得分点:检查自赋值 return *this; delete [] m_data; //得分点:释放原有的内存资源 int length = strlen( other.m_data ); m_data = new char[length+1]; //加分点:对m_data加NULL 判断 strcpy( m_data, other.m_data ); return *this; //得分点:返回本对象的引用 } |
| const classA operator*(const classA& a1,const classA& a2); |
| classA a, b, c; (a * b) = c; // 对a*b的结果赋值 |
| int checkCPU() { { union w { int a; char b; } c; c.a = 1; return (c.b == 1); } } |
| 内存地址 | 存放内容 |
| 0x4000 | 0x34 |
| 0x4001 | 0x12 |
| 内存地址 | 存放内容 |
| 0x4000 | 0x12 |
| 0x4001 | 0x34 |
| 内存地址 | 存放内容 |
| 0x4000 | 0x78 |
| 0x4001 | 0x56 |
| 0x4002 | 0x34 |
| 0x4003 | 0x12 |
| 内存地址 | 存放内容 |
| 0x4000 | 0x12 |
| 0x4001 | 0x34 |
| 0x4002 | 0x56 |
| 0x4003 | 0x78 |
| int Sum( int n ) { return ( (long)1 + n) * n / 2; //或return (1l + n) * n / 2; } |
| int Sum( int n ) { long sum = 0; for( int i=1; i<=n; i++ ) { sum += i; } return sum; } |
[1] [2]
编辑 webmaster