龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

程序员必看 c++笔试题汇总(1)(8)

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
37. main 主函数执行完毕后,是否可能会再执行一段代码,给出说明? 答案:可以,可以用_onexit 注册一个函数,它会在main 之后执行int fn1(void), fn2(void),

37. main 主函数执行完毕后,是否可能会再执行一段代码,给出说明?

答案:可以,可以用_onexit 注册一个函数,它会在main 之后执行int fn1(void), fn2(void), fn3(void), fn4 (void);

  1. void main( void )  
  2. {  
  3. String str("zhanglin");  
  4. _onexit( fn1 );  
  5. _onexit( fn2 );  
  6. _onexit( fn3 );  
  7. _onexit( fn4 );  
  8. printf( "This is executed first.n" );  
  9. }  
  10. int fn1()  
  11. {  
  12. printf( "next.n" );  
  13. return 0;  
  14. }  
  15. int fn2()  
  16. {  
  17. printf( "executed " );  
  18. return 0;  
  19. }  
  20. int fn3()  
  21. {  
  22. printf( "is " );  
  23. return 0;  
  24. }  
  25. int fn4()  
  26. {  
  27. printf( "This " );  
  28. return 0;  
  29. }  
  30. The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to _onexit cannot take parameters.  

38. 如何判断一段程序是由C 编译程序还是由C++编译程序编译的?

答案:

  1. #ifdef __cplusplus  
  2. cout<<"c++";  
  3. #else  
  4. cout<<"c";  
  5. #endif   

39.文件中有一组整数,要求排序后输出到另一个文件中

答案:

  1. #i nclude  
  2.  
  3. using namespace std;  
  4.  
  5.  
  6. void Order(vector<int>& data) //bubble sort  
  7. {  
  8. int count = data.size() ;  
  9. int tag = false ; // 设置是否需要继续冒泡的标志位  
  10. for ( int i = 0 ; i < count ; i++)  
  11. {  
  12. for ( int j = 0 ; j < count - i - 1 ; j++)  
  13. {  
  14. if ( data[j] > data[j+1])  
  15. {  
  16. tag = true ;  
  17. int temp = data[j] ;  
  18. data[j] = data[j+1] ;  
  19. data[j+1] = temp ;  
  20. }  
  21. }  
  22. if ( !tag )  
  23. break ;  
  24. }  
  25. }  
  26.  
  27.  
  28. void main( void )  
  29. {  
  30. vector<int>data;  
  31. ifstream in("c:data.txt");  
  32. if ( !in)  
  33. {  
  34. cout<<"file error!";  
  35. exit(1);  
  36. }  
  37. int temp;  
  38. while (!in.eof())  
  39. {  
  40. in>>temp;  
  41. data.push_back(temp);  
  42. }  
  43. in.close(); //关闭输入文件流  
  44. Order(data);  
  45. ofstream out("c:result.txt");  
  46. if ( !out)  
  47. {  
  48. cout<<"file error!";  
  49. exit(1);  
  50. }  
  51. for ( i = 0 ; i < data.size() ; i++)  
  52. out<" ";  
  53. out.close(); //关闭输出文件流  
  54. }  


精彩图集

赞助商链接