6. 如何打印出当前源文件的文件名以及源文件的当前行号?
答案:
cout << __FILE__ ;
cout<<__LINE__ ;
__FILE__和__LINE__是系统预定义宏,这种宏并不是在某个文件中定义的,而是由编译器定义的。
7. main主函数执行完毕后,是否可能会再执行一段代码,给出说明?
答案:可以,可以用_onexit 注册一个函数,它会在main 之后执行intfn1(void), fn2(void), fn3(void), fn4 (void);
void main( void )
{
String str("zhanglin");
_onexit( fn1 );
_onexit( fn2 );
_onexit(fn3 );
_onexit( fn4 );
printf( "This is executed first./n" );
}
int fn1()
{
printf( "next./n" );
return 0;
}
int fn2()
{
printf( "executed " );
return 0;
}
int fn3()
{
printf( "is " );
return 0;
}
int fn4()
{
printf( "This ");
return 0;
}
The _onexit function is passed the address of afunction (func) to be called when the program terminates normally. Successivecalls to _onexit create a register of functions that are executed in LIFO(last-in-first-out) order. The functions passed to _onexit cannot takeparameters.
8. 如何判断一段程序是由C 编译程序还是由C++编译程序编译的?
答案:
#ifdef __cplusplus
cout<<"c++";
#else
cout<<"c";
#endif
9.文件中有一组整数,要求排序后输出到另一个文件中
答案:
#i nclude
#i nclude
using namespace std;
void Order(vector& data)//bubble sort
{
int count = data.size() ;
int tag = false ; //设置是否需要继续冒泡的标志位
for ( int i = 0 ; i < count ; i++)
{
for ( int j =0 ; j < count - i - 1 ; j++)
{
if ( data[j] > data[j+1])
{
tag = true ;
int temp = data[j] ;
data[j] = data[j+1] ;
data[j+1] = temp ;
}
}
if ( !tag )
break ;
}
}
void main( void )
{
vectordata;
ifstreamin("c://data.txt");
if ( !in)
{
cout<<"file error!";
exit(1);
}
int temp;
while (!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close(); //关闭输入文件流
Order(data);
ofstream out("c://result.txt");
if ( !out)
{
cout<<"fileerror!";
exit(1);
}
for ( i = 0 ; i < data.size() ; i++)
out<
10. 链表题:一个链表的结点结构
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
(1)已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)
Node * ReverseList(Node*head) //链表逆序
{
if ( head == NULL || head->next == NULL )
returnhead;
Node *p1 = head ;
Node *p2 = p1->next ;
Node *p3 =p2->next ;
p1->next = NULL ;
while ( p3 != NULL )
{
p2->next = p1 ;
p1 = p2 ;
p2 = p3 ;
p3 = p3->next ;
}
p2->next = p1 ;
head = p2 ;
return head ;
}