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

被折叠的 条评论
为什么被折叠?



