《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第二部分)

该博客是《C++捷径教程》第九章更多数据类型与运算符的读书笔记,包含多个C++程序示例,如字符大小写转换、二进制输出、移位运算、条件运算、逗号运算等,还涉及sizeof、new delete、malloc()/free()的使用。

//--《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第二部分)
//--Chapter 9--更多的数据类型与运算符
//--11/17/2005 Thurs.
//--Computer Lab
//--Liwei


//--程序#9  转换为大写字符
#include <iostream>
using namespace std;

int main()
{
 char ch;

 do{
  cin>>ch;
  ch&=223;
  //ch=ch&223;//223=1101 1111
  cout<<ch;
 }while(ch!='Q');

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}


//--程序#10  转换为小写字符
#include <iostream>
using namespace std;

int main()
{
 char ch;

 do{
  cin>>ch;
  ch|=32;
  //ch=ch|32;//223=1101 1111
  cout<<ch;
 }while(ch!='q');

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}


//--程序#11  二进制输出
#include <iostream>
using namespace std;

void disp_binary(unsigned u);

int main()
{
 unsigned u;
 cout<<"Enter a number between 0--255: ";
 cin>>u;

 cout<<"Here is ther number in binary: ";
 disp_binary(u);

 cout<<"Here is the complement of the number: ";
 disp_binary(~u);

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void disp_binary(unsigned u)
{
 register int t;

 for(t=128; t>0; t/=2){
  cout<<endl<<"t= "<<t<<"===";
  if(u & t)
   cout<<"1 ";
  else
   cout<<"0 ";
 }

 cout<<endl;
}


//--程序#12  移位运算
#include <iostream>
using namespace std;

void disp_binary(unsigned int u);

int main()
{
 unsigned int i=1,t;

 for(t=0; t<32; t++){
  disp_binary(i);
  i=i<<1;
 }

 cout<<endl;

 //i=4294967295;

 for(t=0; t<32; t++){
  i=i>>1;
  disp_binary(i);
 }


 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void disp_binary(unsigned int u)
{
 unsigned int t;

 //t=128 64 32 16 8 4 2 1
 for(t=2147483648; t>0; t/=2){
  //cout<<endl<<"t= "<<t<<"===";
  if(u & t)
   cout<<"1 ";
  else
   cout<<"0 ";
 }

 cout<<endl;
}

//--程序#13  ?运算
#include <iostream>
using namespace std;

int div_zero();

int main()
{
 int i,j,result;
 cout<<"Enter dividend and divisor: ";
 cin>>i>>j;

 result=j ? i/j : div_zero();//有问题
 cout<<"Result: "<<result;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

int div_zero()
{
 cout<<"Cannot divide by zero./n";
 return 0;
}


//--程序#14  ,运算
#include <iostream>
using namespace std;

int main()
{
 int i,j;
 j=10;
 i=(j++, j+100, 999+j);
 cout<<j<<' '<<i;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}


//--程序#15  sizeof的使用
#include <iostream>
using namespace std;

int main()
{
 char ch;
 int i,nums[4];

 cout<<sizeof ch<<' ';
 cout<<sizeof i<<' ';
 cout<<sizeof(float)<<' ';
 cout<<sizeof(double)<<' ';
 cout<<sizeof nums;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}


//--程序#16  new delete的使用
#include <iostream>
using namespace std;

int main()
{
 int *p;
 p=new int;
 *p=20;

 cout<<*p;

 delete p;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程序#17  new delete的使用
#include <iostream>
using namespace std;

int main()
{
 int *p;
 p=new int(99666666);
 
 cout<<*p;

 delete p;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程序#18  new delete的使用
#include <iostream>
using namespace std;

int main()
{
 double *p;
 int i;

 p=new double[10];

 for(i=0; i<10; i++)
  p[i]=100.00+i;

 for(i=0; i<10; i++)
  cout<<p[i]<<' ';

 delete []p; 

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}


//--程序#19  malloc()/free()的使用
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
 int *i;
 double *j;

 i=(int *)malloc(sizeof(int));
 if(!i){
  cout<<"Allocation failure./n";
  return 1;
 }

 j=(double *)malloc(sizeof(double));
 if(!j){
  cout<<"Allocation failure./n";
  return 1;
 }

 *i=10;
 *j=100.123;

 cout<<*i<<' '<<*j;

 free(i);
 free(j);

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值