⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module7.lst

📁 Programs for the book Advanced Engineering Mathematics using MATLAB, 2ndEd.
💻 LST
字号:
listing 1
#include <iostream> 
using namespace std; 
 
const int num_employees = 100; 
 
int main() 
{ 
  int empNums[num_employees]; 
  double salary[num_employees]; 
  char *names[num_employees]; 
 
  // ... 
}

listing 2
// Use a const pointer parameter. 
 
#include <iostream> 
using namespace std; 
 
int negate(const int *val); 
  
int main() 
{ 
  int result; 
  int v = 10; 
 
  result = negate(&v); 
 
  cout << v << " negated is " << result; 
  cout << "\n"; 
 
  return 0; 
} 
 
int negate(const int *val) 
{ 
  return - *val; 
}

listing 3
// This won't work! 
int negate(const int *val) 
{ 
  *val = - *val; // Error, can't change 
  return *val; 
}

listing 4
// This won't work either! 
int negate(const int &val) 
{ 
  val = -val; // Error, can't change 
  return val; 
}

listing 5
// Compute a running average of numbers entered by the user. 
 
#include <iostream> 
using namespace std; 
 
int running_avg(int i); 
 
int main() 
{ 
  int num; 
 
  do { 
    cout << "Enter numbers (-1 to quit): "; 
    cin >> num; 
    if(num != -1) 
      cout << "Running average is: " << running_avg(num); 
    cout << '\n'; 
  } while(num > -1); 
 
  return 0; 
} 
 
int running_avg(int i) 
{ 
  static int sum = 0, count = 0; 
 
  sum = sum + i; 
 
  count++; 
 
  return sum / count; 
}

listing 6
// ---------------------- First File ---------------------- 
 
#include <iostream> 
using namespace std; 
 
int running_avg(int i); 
void reset(); 
 
int main() 
{ 
  int num; 
 
  do { 
    cout << "Enter numbers (-1 to quit, -2 to reset): "; 
    cin >> num; 
    if(num == -2) { 
      reset(); 
      continue; 
    } 
    if(num != -1) 
      cout << "Running average is: " << running_avg(num); 
    cout << '\n'; 
  } while(num != -1); 
 
  return 0; 
} 
 
// ---------------------- Second File ---------------------- 
 
static int sum = 0, count = 0; 
 
int running_avg(int i) 
{ 
  sum = sum + i; 
 
  count++; 
 
  return sum / count; 
} 
 
void reset() 
{ 
  sum = 0; 
  count = 0; 
}

listing 7
// Demonstrate register. 
 
#include <iostream> 
using namespace std; 
 
int summation(int nums[], int n); 
 
int main() 
{ 
  int vals[] = { 1, 2, 3, 4, 5 }; 
  int result; 
 
  result = summation(vals, 5); 
 
  cout << "Summation is " << result << "\n"; 
 
  return 0; 
} 
 
// Return summation of an array of ints. 
int summation(int nums[], int n) 
{ 
  register int i; 
  register int sum = 0; 
 
  for(i = 0; i < n; i++) 
    sum = sum + nums[i]; 
 
  return sum; 
}

listing 8
// Demonstrate an enumeration. 
 
#include <iostream> 
using namespace std; 
 
enum transport { car, truck, airplane, train, boat }; 
 
char name[][20] = { 
  "Automobile", 
  "Truck", 
  "Airplane", 
  "Train", 
  "Boat" 
}; 
 
int main() 
{ 
  transport how; 
 
  how = car; 
  cout << name[how] << '\n'; 
 
  how = airplane; 
  cout << name[how] << '\n'; 
 
  how = train; 
  cout << name[how] << '\n'; 
 
  return 0; 
}

listing 9
// Uppercase letters using bitwise AND. 
 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  char ch; 
 
  for(int i = 0 ; i < 10; i++)  { 
    ch = 'a' + i; 
    cout << ch; 
 
    // This statement turns off the 6th bit.  
    ch = ch & 223; // ch is now uppercase 
 
    cout << ch << " "; 
  }  
 
  cout << "\n"; 
 
  return 0; 
}

listing 10
// Display the bits within a byte. 
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t > 0; t = t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}

listing 11
// Lowercase letters using bitwise OR. 
 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  char ch; 
 
  for(int i = 0 ; i < 10; i++)  { 
    ch = 'A' + i; 
    cout << ch; 
 
    // This statement turns on the 6th bit.  
    ch = ch | 32; // ch is now lowercase 
 
    cout << ch << " "; 
  }  
 
  cout << "\n"; 
 
  return 0; 
} 


listing 12
// Use XOR to encode and ecode a message. 
 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  char msg[] = "This is a test"; 
  char key = 88; 
 
  cout << "Original message: " << msg << "\n"; 
 
  for(int i = 0 ; i < strlen(msg); i++)  
    msg[i] = msg[i] ^ key; 
 
  cout << "Encoded message: " << msg << "\n"; 
 
  for(int i = 0 ; i < strlen(msg); i++)  
    msg[i] = msg[i] ^ key; 
 
  cout << "Decoded message: " << msg << "\n"; 
 
  return 0; 
}

listing 13
#include <iostream> 
using namespace std; 
 
void show_binary(unsigned int u); 
 
int main() 
{ 
  unsigned u; 
 
  cout << "Enter a number between 0 and 255: "; 
  cin >> u; 
 
  cout << "Here's the number in binary: "; 
  show_binary(u); 
 
  cout << "Here's the complement of the number: "; 
  show_binary(~u); 
 
  return 0; 
} 
 
// Display the bits within a byte. 
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t = t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}

listing 14
// Example of bitshifting.  
 
#include <iostream> 
using namespace std; 
 
void show_binary(unsigned int u); 
 
int main() 
{ 
  int i=1, t; 
 
  // shift left 
  for(t=0; t < 8; t++) { 
    show_binary(i); 
    i = i << 1; 
  } 
 
  cout << "\n"; 
 
  // shift right 
  for(t=0; t < 8; t++) { 
    i = i >> 1; 
    show_binary(i); 
  } 
 
  return 0; 
} 
 
// Display the bits within a byte. 
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t=t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}

listing 15
/* 
   Project 7-1 
 
   Left and right rotate functions for byte values. 
*/ 
 
#include <iostream> 
using namespace std; 
 
unsigned char rrotate(unsigned char val, int n); 
unsigned char lrotate(unsigned char val, int n); 
void show_binary(unsigned int u); 
 
int main() 
{ 
  char ch = 'T'; 
 
  cout << "Original value in binary:\n"; 
  show_binary(ch); 
 
  cout << "Rotating right 8 times:\n"; 
  for(int i=0; i < 8; i++) { 
    ch = rrotate(ch, 1); 
    show_binary(ch); 
  } 
 
  cout << "Rotating left 8 times:\n"; 
  for(int i=0; i < 8; i++) { 
    ch = lrotate(ch, 1); 
    show_binary(ch); 
  } 
 
  return 0; 
} 
 
// Left-rotate a byte n places. 
unsigned char lrotate(unsigned char val, int n) 
{ 
  unsigned int t; 
 
  t = val; 
 
  for(int i=0; i < n; i++) { 
    t = t << 1; 
 
    /* If a bit shifts out, it will be in bit 8 
       of the integer t. If this is the case, 
       put that bit on the right side. */ 
    if(t & 256) 
      t = t | 1; // put a 1 on the right end 
  } 
 
  return t; // return the lower 8 bits. 
} 
 
// Right-rotate a byte n places. 
unsigned char rrotate(unsigned char val, int n) 
{ 
  unsigned int t; 
 
  t = val; 
 
  // First, move the value 8 bits higher. 
  t = t << 8; 
 
  for(int i=0; i < n; i++) { 
    t = t >> 1; 
 
    /* If a bit shifts out, it will be in bit 7 
       of the integer t. If this is the case, 
       put that bit on the left side. */ 
    if(t & 128)  
      t = t | 32768; // put a 1 on left end 
  } 
 
  /* Finally, move the result back to the 
     lower 8 bits of t. */ 
  t = t >> 8; 
 
  return t; 
} 
 
// Display the bits within a byte. 
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t = t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}

listing 16
/* This program uses the ? operator to prevent 
   a division by zero. */ 
 
#include <iostream> 
using namespace std; 
 
int div_zero(); 
 
int main() 
{ 
  int i, j, result; 
 
  cout << "Enter dividend and divisor: "; 
  cin >> i >> j; 
 
  // This statement prevents a divide by zero error. 
  result = j ? i/j : div_zero(); 
 
  cout << "Result: " << result; 
 
  return 0; 
} 
 
int div_zero() 
{ 
  cout << "Cannot divide by zero.\n"; 
  return 0; 
}

listing 17
var = (count=19, incr=10, count+1);

listing 18
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  int i, j; 
 
  j = 10; 
 
  i = (j++, j+100, 999+j); 
 
  cout << i; 
 
  return 0; 
}

listing 19
// Demonstrate sizeof. 
 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  char ch; 
  int i; 
 
  cout << sizeof ch << ' '; // size of char 
  cout << sizeof i << ' ';  // size of int 
  cout << sizeof (float) << ' '; // size of float 
  cout << sizeof (double) << ' '; // size of double 
 
  return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -