//--《C++捷径教程》读书笔记--Chapter 3--基本数据类型
//--Chapter 3--基本数据类型
//--11/7/2005 Mon.
//--Computer Lab
//--Liwei
//--程序#1 局部变量的使用
#include <iostream>
using namespace std;
void func();
int main()
{
int x=10;
func();
cout<<"/n";
cout<<x<<endl;
return 0;
}
void func()
{
int x=-199;
cout<<x;
}
//--程序#2 全局变量的使用
#include <iostream>
using namespace std;
void func1();
void func2();
int count;//
int main()
{
int i;
for(i=0;i<10;i++)
{
count=i*2;
func1();
}
return 0;
}
void func1()
{
cout<<"count : "<<count;//全局变量
func2();
cout<<"/n";
}
void func2()
{
int count;
for(count=0;count<3;count++)
cout<<'.'<<"* ";
}
//--程序#3 有符号数和无符号数的区别
#include <iostream>
using namespace std;
int main()
{
short int i;
short unsigned int j;
j=60000;
i=j;
cout<<i<<' '<<j<<endl;
return 0;
}
//--程序#4 反向输出字母表
#include <iostream>
using namespace std;
int main()
{
char letter;
for(letter='Z'; letter>='A'; letter--)
cout<<" "<<letter;
cout<<endl;
cout<<"/n///b/a/?/t/r";
return 0;
}
//--程序#5 变量初始化的示例程序
#include <iostream>
using namespace std;
void total(int x);
int main()
{
cout<<"Computing summation of 5./n";
total(5);
cout<<"/nComputing summation of 6./n";
total(6);
return 0;
}
void total(int x)
{
int sum=0,i,count;
for(i=1;i<=x;i++)
{
sum+=i;
for(count=0;count<10;count++)
cout<<'.';
cout<<"the current sum is: "<<sum<<endl;
}
}
//--程序#6 %的使用
#include <iostream>
using namespace std;
int main()
{
int x,y;
x=10;
y=3;
cout<<x/y<<endl;
cout<<x%y<<endl;
x=1,y=2;
cout<<x/y<<" "<<x%y<<endl;
return 0;
}
//--程序#7 异或运算的使用
#include <iostream>
using namespace std;
bool xor(bool a,bool b);
int main()
{
bool p,q;
cout<<"Enter P (0 or 1): ";
cin>>p;
cout<<"Enter q (0 or 1): ";
cin>>q;
cout<<"p and q: "<<(p&&q)<<endl;
cout<<"p or q: "<<(p||q)<<endl;
cout<<"p xor q: "<<xor(p,q)<<endl;
return 0;
}
bool xor(bool a,bool b)
{
return (a||b)&&!(a&&b);
}
//--程序#8 强制转换的使用
#include <iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<100;i++)
cout<<i<<"/ 2 is :"<<(float)i/2<<endl;
return 0;
}