c++试验点

来源:互联网 发布:初级程序员考试资料 编辑:程序博客网 时间:2024/06/05 09:57

                                                         为了面试,不得不把有些东西整理一下

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////function.cpp 

#include "incl.h"
#define TRUE 1
#define FALSE 0
int find_char(char **strings,int value)
{
 
  assert(strings!=NULL);
  while(*strings!=NULL)
  {
   while(**strings!=0)
    if(*(*strings)++==value)
     return TRUE;
   strings++;
  }
  return FALSE;
}

////////////////////////////////////////////////////
void notConstRef(int &a)
{
 
 a++;
}
void constRef(const int &a)
{
 return;
}

///////////////////////////////////////////////////
//下面一个函数目的在于告诉你case语句的特别之处,如果你不用break语句。
void testCase()
{
 using namespace std;
 int a='/t';
 int c=0;
 int word=0;
 int line=0;
 switch(a)
 {
case '/n':
  line+=1;
case ' ':
case '/t':
  word+=1;
default :
  c+=1;
  
 }
 cout<<"c: "<<c<<endl;
 cout<<"word: "<<word<<endl;
 cout<<"line: "<<line<<endl;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////incl.h

int find_char(char **strings,int value);
void notConstRef(int &a);
void constRef(const int &a);
void testCase();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////主程序

#include <iostream>
#include "incl.h"
int main()
{
 using namespace std;
 //测试&变量别名的初始化和赋值的意义大不同
 int one=3;
 int next=4;
 int &oneAlian=one;
 
 cout<<one<<" "<<oneAlian<<endl;
 cout<<&one<<"    "<<&oneAlian<<endl;
 oneAlian=next;
 cout<<one<<" "<<oneAlian<<endl;
 cout<<&one<<" "<<&oneAlian<<endl;
 cout<<"**********"<<&next<<"****************"<<endl;
 //测试字符串常量在表达式里面表示首地址的事实
 printf("%c/n","0123456789ABCDEF"[3]);
 printf("%s/n","0123456789ABCDEF"+3);
 //说面下表操作就是指针加常数的事实
 printf("%c/n",3["abcdefg"]);
 cout<<"********************************************"<<endl;
 testCase();
 cout<<"*******************************"<<endl;
 char *s[]={"abcde","fghijk",0};//此处如何声明为好?
 int ifExist=find_char(s,'l');
 cout<<ifExist<<endl;
 cout<<"******************下面演示了能做引用实参时候可能的临时变量问题******************"<<endl;
 int testIfConst=1;
 notConstRef(testIfConst);
 //notConstRef(testIfConst+1);
 //notConstRef(2);
 constRef(testIfConst+1);
 constRef(2);
 cout<<"testIfConst: "<<testIfConst;
 
}