Chinaunix首页 | 论坛 | 博客
  • 博客访问: 466807
  • 博文数量: 40
  • 博客积分: 1178
  • 博客等级: 少尉
  • 技术积分: 578
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-28 21:27
文章分类

全部博文(40)

文章存档

2012年(3)

2011年(29)

2010年(7)

2009年(1)

分类: C/C++

2011-10-31 16:10:50

1、有以下C++代码:
  1. int main()
  2. {
  3.   cout<<"hello world!\n";
  4.   return 0;
  5. }
请不对main()函数做任何修改,程序运行后能输出:
"EMC"
"hello world!"
"exit"
答案1:
  1. struct B
  2. {
  3.     ostream& operator << (const char*)
  4.     {
  5.         return (cout <<"EMC\n"
  6.             "hello world!\n"
  7.             "exit\n" );
  8.     }
  9. }bbb;
  10. #define cout bbb
  11. int main()
  12. {
  13.     cout<<"hello world!\n";
  14.     return 0;
  15. }
答案2:
  1. using std::ostream;
  2. using std::cout;

  3. ostream& operator<<(ostream& io, const char (&szStr)[14])
  4. {
  5.     printf ("EMC\n");
  6.     printf (szStr);
  7.     printf ("exit\n");
  8.     return io;
  9. }

  10. int main()
  11. {
  12.     cout<<"hello world!\n";
  13.     return 0;
  14. }
以上答案是群里一位朋友做的,我算是学习了。
阅读(1571) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~