首页 分类C/C++

用递归的方法计算指数次幂

#include <iostream> using namespace std; int myFunc(int,int); //用递归的方式计算指数次幂 int main() {  int x,y,z,a,b;  cout<<“请输入两个数,例如x和y,程序计算他们的指数次幂。”<<“n”<<“第一个数:”;  cin>>x;  cout<<“第二个数:”;  cin>>y;  a=x;  b=y;  z=myFunc(x,y);  cout<<a<<“的”<<b<<“次方是”<<z<<endl;  return 0; } int myFunc(int x,int y) {  if (y=...

函数的递归

//函数的递归 #include <iostream> using namespace std; int fib(int n); int main() {  int n,answer;  cout<<“Enter Number to find:”;  cin>>n;  cout<<“nn”;  answer=fib(n);  cout<<answer<<” is the “<<n;  cout<<“th Fibonacci numbern”;  return 0; } int fib(int n) {  cout<<“Processing fib(“<<n<<“)…”;  if(n<3)  {   cout<<“Return 1!n”;   re...

函数重载

#include <iostream> using namespace std; int Double(int); long Double(long); float Double(float); double Double(double); int main() {  int myInt = 6500;  long myLong = 65000;  float myFloat = 6.5f;  double myDouble = 6.5e20;    int doubledInt;  long doubledLong;  float doubledFloat;  double doubledDouble;  cout<<“myInt:”<<myInt<<endl;  cout<<“myLong:”<<myLong<<endl;  cout<<“myFloat:”<<myFloat<<endl...

使用枚举型常量与整型常量

#include <iostream> void main() {  typedef unsigned short int USI;  const USI Sunday = 0;  const USI Monday = 1;  const USI Tuesday = 2;  const USI Wednesday = 3;  const USI Thursday = 4;  const USI Friday = 5;  const USI Saturday = 6;  USI Today = Sunday;  if (Today == Sunday || Today == Saturday)   std::cout << “nGotta Weekends!n”;  else   std::cout << “nGo to work!n”; } #include <iostream> int main() {  enum Days {Sunday,Monday,Tuesday,Wednes...

使用数字来打印字符2

#include <iostream> int main() {  for (unsigned char i = 32;i<128;i++)   std::cout << i;  std::cout << std::endl;  return 0; }

使用数字来打印字符

#include <iostream> int main() {  for (int i = 32;i < 128;i++)   std::cout << (char) i;  std::cout << std::endl;  return 0; }

将过大的值赋给signed short变量

#include <iostream> int main() {  using std::cout;  using std::endl;  short int smallNumber;  smallNumber = 32767;  cout << “smallNumber :  ” << smallNumber << endl;  smallNumber++;  cout << “smallNumber :  ” << smallNumber << endl;  smallNumber++;  cout << “smallNumber :  ” << smallNumber << endl;  smallNumber++;  cout << “smallNumber :  ” << smallNumber << endl;  sma...

演示unsigned类型变量的回绕

#include <iostream> int main() {  using std::cout;  using std::endl;  unsigned short int smallNumber;  smallNumber = 65535;  cout << “smallNumber :  ” << smallNumber << endl;  smallNumber++;  cout << “smallNumber :  ” << smallNumber << endl;  smallNumber++;  cout << “smallNumber :  ” << smallNumber << endl;  smallNumber++;  cout << “smallNumber :  ” << smallNumber << e...

演示typedef创建别名

#include <iostream> //Demonstrates typedef keyword using std::cout; using std::endl; typedef unsigned short USHORT;  //typedef defined int main() {  USHORT Width = 5,Length;  Length = 10;  USHORT Area = (Width * Length);  cout << “Width:” << Width << endl;  cout << “Length:” << Length <<endl;  cout << “Area:” << Area << endl;  return 0; }

演示变量的用法

#include <iostream> //Demostration of variables using std::cout; using std::endl; int main() {  unsigned short int width = 5 , length;  length = 10; //Create an unsigned short and initialize with result //of multiplying width by length  unsigned short Area = (width * length);  cout << “width:” << width << endl;  cout << “length:” << length <<endl;  cout << “Area:” << Area << endl;  return 0; }