c和c++代碼精粹 學習筆記

看了c和c++代碼精粹 收益頗厚

 

//1.0.cpp >
#include<stdio.h>
class A
{
      
public :
       
double x;
       A(
double d)//若explicit聲明 則顯示轉化 f(2)改爲f(A(2)) 
       {
                x
=d;
                printf(
"A::A ");
       }

}
;
void f(const A& a)
{
     printf(
"%f ",a.x);
 }

main()
{
      A a(
1);
      f(a);
      f(
2);
      getchar();
}

//1.1.CPP   在任意一個轉化序列裏只允許一個用戶定義的轉換 
#include<stdio.h>
struct B;
struct A
{
      
public :
      
double x;
      A(
const B& b);
      
}
;
void f(const A& a)
{
     printf(
"%f ",a.x);
 }


 
struct B
 
{
        
double y;
        B(
double d):y(d)
        
{
                 
        }

 }
;
 A::A(
const B& b):x(b.y)
 
{
 }

int   main()
{
      A a(
1);
      f(a);
      B b(
2);
     
// f(3);
      f(B(3));
      f(A(
4));
      getchar();
     
}
Z6

 

 

 

#include<iostream>
#include
<iomanip>
#include
<stdlib.h> 
using namespace std;

main()
{
      
//將日期儲存在一個16bit中 
      
//法一 
      unsigned short date1,date,year=92,mon=8,day=2;
      date
=(year<<9)|(mon<<5)|day;
      cout
<<hex<<date<<endl;
      
//法二  通過位域結構實現 注意爲LITTLE ENDIAN  
      struct Date{
             unsigned day:
5;
             unsigned mon:
4;
             unsigned year:
7;
      }
;
      Date 
*dp=(Date *)&date1;
      dp
->mon=mon;
      dp
->day=day;
      dp
->year=year;
      cout
<<hex<<date1<<endl;
              
       
       getchar();
}

 

 

 

//1.5.CPP 預處理 
#include<iostream>
#include
<iomanip>
#include
<string> 
#include
<stdlib.h> 
using namespace std;
#if defined(__MSVER)
<Put statement here supported by microsoft > 
#elif defined (__BCPLUSPLUS__)
<Put statement here supported by borland > 
#else 
main()
{     //RAGGED 數組 
      char * str[]={"now" ,"is","the","time"};
      size_t n
=sizeof str / sizeof str[0];
      
for(int i=0;i<n;i++)
      
{///還如下訪問 char *p=str[0];cout<<p;  char **p =str;cout<<*(p+i) 
              cout<<"str"<<i<<"=="<<str[i]<<",size="<<
              sizeof str[i]<<",length="<<strlen(str[i])<<endl;
       }

       getchar();
}

//#error Compiler not supported
#endif

 

 

 

//1.6.CPP 十六進制轉ASCII 
#include<iostream>
#include
<iomanip>
#include
<string.h> 
#include
<stdlib.h> 
#include
<stdio.h> 
using namespace std;
long atox_ascii(char *s)
{
    
long sum;
    
while(isspace(*s)) s++;
    
for(sum=0L;isxdigit(*s);++s)
    
{
         
int digit;
         
if(isdigit(*s))
         digit
=*s-'0';
         
else digit=toupper(*s)-'A'+10;
         sum
=sum*16L+digit;
    }

    
return sum;
}
    
long atox_allplatform(char *s)
{
     
char xdigs[]="0123456789ABCDEF";
     
     
long sum;
     
while(isspace(*s)) s++;
     
for(sum=0L;isxdigit(*s);++s)
     
{
         
int digit = strchr(xdigs,toupper(*s))-xdigs;
         sum
=sum*16L+digit;
     }
    
     
return sum;
}

long atox_sscanf(char *s)
{
     
long n=0L;
     sscanf(s,
"%x",&n);
     
return n;
 }

 
 
long atox_great(char *s)
 
{
      
return strtol(s,NULL,16);
 }

main()
{    cout<<atox_ascii("2a")<<endl;
     cout
<<atox_allplatform("2a")<<endl;
     cout
<<atox_sscanf("2a")<<endl;
      cout
<<atox_great("2a")<<endl;
     getchar();
}



 

 

//1.7.CPP stdlib qsort
#include<iostream>
#include
<iomanip>
#include
<string.h> 
#include
<stdlib.h> 
#include
<stdio.h> 
using namespace std;
int comp(const void *p1,const void *p2)
{
    
const int * p11= (const int *) p1;
    
const int * p21= (const int *) p2;
    
return *p11-*p21;
}


main()
{   
    
    
int a[]={34,156,23};
    qsort(a,
sizeof a /sizeof a[0],sizeof a[0],comp);
    
for(int i=0;i<sizeof a /sizeof a[0];i++)
    cout
<<a[i]<<" ";
     getchar();
}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章