由于本人编程能力较弱,对c++新的标准理解也不是很深入,所以代码虽然调试通过,对作者测试的样例也能运行成功,但也就只能勉强写成这个样子了。难免有错误与疏漏,望匡正。
1. 理解C++变量的作用域和生命周期
a) 用少于10行代码演示你对局部变量的生命周期的理解
My code:
int main()
{
static int i = 0,j = 0;
for(i=0; i<=5; i++)
{
int j=2; //局部变量j的生命周期仅限于循环体中
printf("%d\n",j);//output is 2
}
printf("%d\n",j);//output is 0
return 0;
}
2. 理解堆和栈,两种内存的申请和释放的方式
a) 用少于30行代码演示你对堆和栈两种内存申请方式的理解
My code:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int a; //栈区存储声明
char s[]="\"abc\""; //栈区存储声明
char *ptr1; //栈区存储声明
char *ptr2 = "123456"; //ptr2 在栈区, 123456 在常量区存储
ptr1 = new char [10]; //分配得来的字节区域在堆区
strcpy(ptr1,"123456");
printf("%s\n",ptr1); //输出123456
delete ptr1; //释放堆区数据
printf("%s\n",ptr1); //输出“奇怪的字符串”表明指针ptr1 指向的空间已经被释放
return 0; //栈区的存储空间由系统自动释放
}
3. 理解unique_ptr和shared_ptr
a) http://msdn.microsoft.com/en-us/library/vstudio/hh279676.aspx
b) http://msdn.microsoft.com/en-us/library/vstudio/hh279669.aspx
4. 请尝试用“C++0x”,“C++11 & STL”两种不同的代码风格分割一个url,并上传代码到博客上。
For example:
Input: http://msdn.microsoft.com/en-us/library/vstudio/hh279674.aspx
Output: http, msdn, Microsoft, com, en-us, library, vstudio, hh279674, aspx
考察重点:
1. 类的定义和使用,基本成员是否完整
2. 输入参数的检查及其他鲁棒性的考虑
3. STL和C++11元素的使用
4. 除http://之外, 是否有考虑ftp:// site:// 等情况
5. 是否考虑url中的中文
6. 算法是否简洁高效
7. 代码风格
My code:
#include <iostream>
#include <string.h>
using namespace std;
class Parse
{
public:
void func();
void output();
char input[100];
private:
char words[101][20];
int counter;
};
void Parse::func()
{
int l = strlen(input),i,j;
int stage =0, mark = 0;
counter = 0;
for(i=0;i<l;i++)
{
if(stage==0)
{
if(input[i]==':' && input[i+1]=='/' && input[i+2]=='/')
{
for(j=mark;j<i;j++)words[counter][j-mark]=input[j];
words[counter][j-mark]='\0';
mark=i+3;
i+=2;
counter++;
stage++;
}
}
else if(stage == 1)
{
if(input[i]=='.' || input[i]=='/')
{
for(j=mark;j<i;j++)words[counter][j-mark]=input[j];
words[counter][j-mark]='\0';
mark=i+1;
counter++;
if(input[i]=='/')stage++;
}
}
else if(stage == 2)
{
if(input[i]=='.' || input[i]=='/')
{
for(j=mark;j<i;j++)words[counter][j-mark]=input[j];
words[counter][j-mark]='\0';
mark=i+1;
counter++;
if(input[i]=='.')stage++;
}
}
else if(stage == 3)
{
for(j=mark;j<l;j++)words[counter][j-mark]=input[j];
words[counter][j-mark]='\0';
}
}
}
void Parse::output()
{
int i;
for(i=0;i<counter;i++)printf("%s, ",words[i]);
printf("%s\n",words[i]);
}
int main()
{
Parse parse;
char a[100];
cin>>a;
strcpy(parse.input,a);
parse.func();
parse.output();
system("pause");
return 0;
}