【c++思维导图与代码示例】05 数组、指针、字符串
思维导图:
代码示例:
# include <iostream>
using namespace std;
int main ( ) {
int a[ 10 ] , b[ 10 ] ;
for ( int i= 0 ; i< 10 ; i++ ) {
a[ i] = i* 2 - 1 ;
b[ 10 - i- 1 ] = a[ i] ;
}
for ( int i = 0 ; i< 10 ; i++ ) {
cout << "a[" << i << "] = " << a[ i] << " " ;
cout << "b[" << i << "] = " << b[ i] << endl;
}
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
int f[ 50 ] = { 1 , 1 } ;
for ( int i= 2 ; i< 50 ; i++ ) {
f[ i] = f[ i- 2 ] + f[ i- 1 ] ;
}
for ( int i = 0 ; i< 50 ; i++ ) {
if ( i% 5 == 0 ) cout << endl;
cout. width ( 12 ) ;
cout << f[ i] ;
}
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
void rowSum ( int a[ ] [ 4 ] , int nRow) {
for ( int i = 0 ; i< nRow; i++ ) {
for ( int j= 1 ; j< 4 ; j++ )
a[ i] [ 0 ] += a[ i] [ j] ;
}
}
int main ( ) {
int table[ 3 ] [ 4 ] = { { 1 , 2 , 3 , 4 } , { 2 , 3 , 4 , 5 } , { 3 , 4 , 5 , 6 } } ;
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 4 ; j++ )
cout << table[ i] [ j] << " " ;
cout << endl;
}
rowSum ( table, 3 ) ;
for ( int i= 0 ; i< 3 ; i++ ) {
cout << "Sun of row " << i << " is " << table[ i] [ 0 ] << endl;
}
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 4 ; j++ )
cout << table[ i] [ j] << " " ;
cout << endl;
system ( "pause" ) ;
return 0 ;
}
}
# include <iostream>
using namespace std;
int main ( ) {
int array[ 3 ] = { 1 , 2 , 3 } ;
for ( int & e: array) {
e += 2 ;
cout<< e<< endl;
}
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
int i;
int * ptr = & i;
i = 100 ;
cout<< " i= " << i << endl;
cout<< " *ptr= " << * ptr << endl;
void * pv;
int a = 5 ;
pv = & a;
int * pint = static_cast < int * > ( pv) ;
cout << " *pint= " << * pint << endl;
int m;
const int * pm = & m;
int n;
pm = & n;
int b;
int * const pb = & b;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
void * pv;
int a = 5 ;
pv = & a;
int * pint = static_cast < int * > ( pv) ;
cout << " *pint= " << * pint << endl;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
int a[ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 } ;
for ( int i = 0 ; i < 10 ; i++ )
cout << a[ i] << " " ;
cout << endl;
for ( int i = 0 ; i < 10 ; i++ )
cout << * ( a+ i) << " " ;
cout << endl;
for ( int * p = a; p < ( a + 10 ) ; p++ )
cout << * p << " " ;
cout << endl;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
int line1[ ] = { 1 , 0 , 0 } ;
int line2[ ] = { 0 , 1 , 0 } ;
int line3[ ] = { 0 , 0 , 1 } ;
int * pline[ 3 ] = { line1, line2, line3} ;
cout << " Matrix test: " << endl;
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ )
cout << pline[ i] [ j] << " " ;
cout << endl;
}
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
void splitFloat ( float x, int * intPart, float * fracPart) {
* intPart = static_cast < int > ( x) ;
* fracPart = x - * intPart;
}
int main ( ) {
cout << "Enter 3 float point numbers:" << endl;
for ( int i = 0 ; i < 3 ; i++ ) {
float x, f;
int n;
cin >> x;
splitFloat ( x, & n, & f) ;
cout << "Integer Part = " << n << " Fraction Part = " << f << endl;
}
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int compute ( int a, int b, int ( * func) ( int , int ) )
{ return func ( a, b) ; }
int max ( int a, int b)
{ return ( ( a> b) ? a: b) ; }
int min ( int a, int b)
{ return ( ( a< b) ? a: b) ; }
int sum ( int a, int b)
{ return ( a+ b) ; }
int main ( ) {
int a, b, res;
cout << "请输入整数 a :" ;
cin >> a;
cout << "请输入整数 b :" ;
cin >> b;
res = compute ( a, b, & max) ;
cout << "Max of " << a << " and " << b << " is " << res << endl;
res = compute ( a, b, & min) ;
cout << "Min of " << a << " and " << b << " is " << res << endl;
res = compute ( a, b, & sum) ;
cout << "Sum of " << a << " and " << b << " is " << res << endl;
system ( "pause" ) ;
}
# include <iostream>
using namespace std;
class Point {
public :
Point ( int x= 0 , int y= 0 ) : x ( x) , y ( y) { }
int getX ( ) const { return x; }
int getY ( ) const { return y; }
private :
int x, y;
} ;
int main ( ) {
Point a ( 4 , 5 ) ;
Point * p1 = & a;
cout << " p1->getX(): " << p1-> getX ( ) << endl;
cout<< " a.getX()(): " << a. getX ( ) << endl;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
class Point {
public :
Point ( int x = 0 , int y = 0 ) : x ( x) , y ( y) {
count++ ;
}
Point ( const Point & p) : x ( p. x) , y ( p. y) {
count++ ;
}
~ Point ( ) { count-- ; }
int getX ( ) const { return x; }
int getY ( ) const { return y; }
static int count;
private :
int x, y;
} ;
int Point:: count = 0 ;
int main ( ) {
int * ptr = & Point:: count;
Point a ( 4 , 5 ) ;
cout << "Point A: " << a. getX ( ) << ", " << a. getY ( ) ;
cout << " Object count = " << * ptr << endl;
Point b ( a) ;
cout << "Point B: " << b. getX ( ) << ", " << b. getY ( ) ;
cout << " Object count = " << * ptr << endl;
return 0 ;
}
# include <iostream>
using namespace std;
class Point {
public :
Point ( int x = 0 , int y = 0 ) : x ( x) , y ( y) {
count++ ;
}
Point ( const Point & p) : x ( p. x) , y ( p. y) {
count++ ;
}
~ Point ( ) { count-- ; }
int getX ( ) const { return x; }
int getY ( ) const { return y; }
static void showCount ( ) {
cout << " Object count = " << count << endl;
}
private :
int x, y;
static int count;
} ;
int Point:: count = 0 ;
int main ( ) {
void ( * funcPtr) ( ) = Point:: showCount;
Point a ( 4 , 5 ) ;
cout << "Point A: " << a. getX ( ) << ", " << a. getY ( ) ;
funcPtr ( ) ;
Point b ( a) ;
cout << "Point B: " << b. getX ( ) << ", " << b. getY ( ) ;
funcPtr ( ) ;
return 0 ;
}
# include <iostream>
using namespace std;
class Point {
public :
Point ( ) : x ( 0 ) , y ( 0 ) {
cout<< "Default Constructor called." << endl;
}
Point ( int x, int y) : x ( x) , y ( y) {
cout << "Constructor called." << endl;
}
~ Point ( ) { cout<< "Destructor called." << endl; }
int getX ( ) const { return x; }
int getY ( ) const { return y; }
void move ( int newX, int newY) {
x = newX;
y = newY;
}
private :
int x, y;
} ;
int main ( ) {
cout << "Step one: " << endl;
Point * ptr1 = new Point;
delete ptr1;
cout << "Step two: " << endl;
ptr1 = new Point ( 1 , 2 ) ;
delete ptr1;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
class Point {
public :
Point ( ) : x ( 0 ) , y ( 0 ) {
cout<< "Default Constructor called." << endl;
}
Point ( int x, int y) : x ( x) , y ( y) {
cout << "Constructor called." << endl;
}
~ Point ( ) { cout<< "Destructor called." << endl; }
int getX ( ) const { return x; }
int getY ( ) const { return y; }
void move ( int newX, int newY) {
x = newX;
y = newY;
}
private :
int x, y;
} ;
int main ( ) {
Point * ptr = new Point[ 2 ] ;
ptr[ 0 ] . move ( 5 , 10 ) ;
ptr[ 1 ] . move ( 15 , 20 ) ;
cout << " Deleting " << endl;
delete [ ] ptr;
cout<< "ptr[0]X" << ptr[ 0 ] . getX ( ) << endl;
cout<< "ptr[0]" << ptr<< endl;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
int ( * cp) [ 9 ] [ 8 ] = new int [ 7 ] [ 9 ] [ 8 ] ;
for ( int i= 0 ; i< 7 ; i++ )
for ( int j= 0 ; j< 9 ; j++ )
for ( int k= 0 ; k< 8 ; k++ )
* ( * ( * ( cp+ i) + j) + k) = ( i* 100 + j* 10 + k) ;
for ( int i= 0 ; i< 7 ; i++ ) {
for ( int j= 0 ; j< 9 ; j++ ) {
for ( int k= 0 ; k< 8 ; k++ )
cout << cp[ i] [ j] [ k] << " " ;
cout << endl;
}
cout << endl;
}
delete [ ] cp;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
# include <cassert>
using namespace std;
class Point {
public :
Point ( ) : x ( 0 ) , y ( 0 ) {
cout<< "Default Constructor called." << endl;
}
Point ( int x, int y) : x ( x) , y ( y) {
cout << "Constructor called." << endl;
}
~ Point ( ) {
cout<< "Destructor called." << endl;
}
int getX ( ) const { return x; }
int getY ( ) const { return y; }
void move ( int newX, int newY) {
x = newX;
y = newY;
}
private :
int x, y;
} ;
class ArrayOfPoints {
public :
ArrayOfPoints ( int size) : size ( size) {
points = new Point[ size] ;
}
~ ArrayOfPoints ( ) {
cout << " Deleting...... " << endl;
delete [ ] points;
}
Point& element ( int index) {
assert ( index >= 0 && index < size) ;
return points[ index] ;
}
private :
Point * points;
int size;
} ;
int main ( ) {
int count;
cout << " 请输入创建点的个数:" ;
cin >> count;
ArrayOfPoints points ( count) ;
points. element ( 0 ) . move ( 5 , 0 ) ;
points. element ( 1 ) . move ( 13 , 29 ) ;
return 0 ;
}
# include <iostream>
using namespace std;
int main ( ) {
float ( * cp) [ 9 ] [ 8 ] = new float [ 8 ] [ 9 ] [ 8 ] ;
for ( int i = 0 ; i < 8 ; i++ )
for ( int j = 0 ; j < 9 ; j++ )
for ( int k = 0 ; k < 8 ; k++ )
* ( * ( * ( cp + i) + j) + k) = static_cast < float > ( i * 100 + j * 10 + k) ;
for ( int i = 0 ; i < 8 ; i++ ) {
for ( int j = 0 ; j < 9 ; j++ ) {
for ( int k = 0 ; k < 8 ; k++ )
cout << cp[ i] [ j] [ k] << " " ;
cout << endl;
}
cout << endl;
}
delete [ ] cp;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
# include <vector>
using namespace std;
double average ( const vector< double > & arr) {
double sum = 0 ;
for ( unsigned i= 0 ; i< arr. size ( ) ; i++ )
sum += arr[ i] ;
return sum/ arr. size ( ) ;
}
int main ( ) {
unsigned n;
cout << " Pelease input the number of array: " << endl;
cout << " n= " ;
cin >> n;
vector< double > arr ( n) ;
cout << " Pelease input " << n << " real numbers: " << endl;
for ( unsigned i= 0 ; i< n; i++ )
cin >> arr[ i] ;
cout << " 遍历数组元素,方法1: " < endl;
for ( auto i= arr. begin ( ) ; i!= arr. end ( ) ; i++ )
cout << * i << endl;
cout << " 遍历数组元素,方法2: " << endl;
for ( auto e: arr)
cout << e << endl;
cout << "Average = " << average ( arr) << endl;
system ( "pause" ) ;
return 0 ;
}
v
# include <iostream>
# include <cassert>
using namespace std;
class Point {
public :
Point ( ) : x ( 0 ) , y ( 0 ) {
cout<< "Default Constructor called." << endl;
}
Point ( int x, int y) : x ( x) , y ( y) {
cout << "Constructor called." << endl;
}
~ Point ( ) { cout<< "Destructor called." << endl; }
int getX ( ) const { return x; }
int getY ( ) const { return y; }
void move ( int newX, int newY) {
x = newX;
y = newY;
}
private :
int x, y;
} ;
class ArrayOfPoints {
public :
ArrayOfPoints ( int size) : size ( size) {
points = new Point[ size] ;
}
~ ArrayOfPoints ( ) {
cout << " Deleting...... " << endl;
delete [ ] points;
}
Point& element ( int index) {
assert ( index >= 0 && index < size) ;
return points[ index] ;
}
private :
Point * points;
int size;
} ;
int main ( ) {
int count;
cout << " please enter the count of points: " ;
cin >> count;
ArrayOfPoints pointsArray1 ( count) ;
pointsArray1. element ( 0 ) . move ( 5 , 10 ) ;
pointsArray1. element ( 1 ) . move ( 15 , 20 ) ;
ArrayOfPoints pointsArray2 ( pointsArray1) ;
cout << " copy of pointsArray1 : " << endl;
cout << "Point_0 of array2: " << pointsArray2. element ( 0 ) . getX ( ) << ","
<< pointsArray2. element ( 0 ) . getY ( ) << endl;
cout << "Point_1 of array2: " << pointsArray2. element ( 1 ) . getX ( ) << ","
<< pointsArray2. element ( 1 ) . getY ( ) << endl;
pointsArray1. element ( 0 ) . move ( 25 , 30 ) ;
pointsArray1. element ( 1 ) . move ( 35 , 40 ) ;
cout << " After the moving of pointsArray1: " << endl;
cout << "Point_0 of array2: " << pointsArray2. element ( 0 ) . getX ( ) << ","
<< pointsArray2. element ( 0 ) . getY ( ) << endl;
cout << "Point_1 of array2: " << pointsArray2. element ( 1 ) . getX ( ) << ","
<< pointsArray2. element ( 1 ) . getY ( ) << endl;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
# include <cassert>
using namespace std;
class Point {
public :
Point ( ) : x ( 0 ) , y ( 0 ) {
cout<< "Default Constructor called." << endl;
}
Point ( int x, int y) : x ( x) , y ( y) {
cout << "Constructor called." << endl;
}
~ Point ( ) { cout<< "Destructor called." << endl; }
int getX ( ) const { return x; }
int getY ( ) const { return y; }
void move ( int newX, int newY) {
x = newX;
y = newY;
}
private :
int x, y;
} ;
class ArrayOfPoints {
public :
ArrayOfPoints ( int size) : size ( size) {
points = new Point[ size] ;
}
ArrayOfPoints ( const ArrayOfPoints& pointsArray) ;
~ ArrayOfPoints ( ) {
cout << " Deleting...... " << endl;
delete [ ] points;
}
Point& element ( int index) {
assert ( index >= 0 && index < size) ;
return points[ index] ;
}
private :
Point * points;
int size;
} ;
ArrayOfPoints :: ArrayOfPoints ( const ArrayOfPoints& v) {
size = v. size;
points = new Point[ size] ;
for ( int i= 0 ; i< size; i++ )
points[ i] = v. points[ i] ;
}
int main ( ) {
int count;
cout << " please enter the count of points: " ;
cin >> count;
ArrayOfPoints pointsArray1 ( count) ;
pointsArray1. element ( 0 ) . move ( 5 , 10 ) ;
pointsArray1. element ( 1 ) . move ( 15 , 20 ) ;
ArrayOfPoints pointsArray2 ( pointsArray1) ;
cout << " copy of pointsArray1 : " << endl;
cout << "Point_0 of array2: " << pointsArray2. element ( 0 ) . getX ( ) << ","
<< pointsArray2. element ( 0 ) . getY ( ) << endl;
cout << "Point_1 of array2: " << pointsArray2. element ( 1 ) . getX ( ) << ","
<< pointsArray2. element ( 1 ) . getY ( ) << endl;
pointsArray1. element ( 0 ) . move ( 25 , 30 ) ;
pointsArray1. element ( 1 ) . move ( 35 , 40 ) ;
cout << " After the moving of pointsArray1: " << endl;
cout << "Point_0 of array2: " << pointsArray2. element ( 0 ) . getX ( ) << ","
<< pointsArray2. element ( 0 ) . getY ( ) << endl;
cout << "Point_1 of array2: " << pointsArray2. element ( 1 ) . getX ( ) << ","
<< pointsArray2. element ( 1 ) . getY ( ) << endl;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
# include <string>
using namespace std;
inline void test ( const char * title, bool value) {
cout << title << " returns " << ( value ? "true" : "false" ) << endl;
}
int main ( ) {
string s1 = "DEF" ;
string s2;
cout << "Please enter s2: " ;
cin >> s2;
cout << "length of s2: " << s2. length ( ) << endl;
test ( "s1 <= \"ABC\"" , s1 <= "ABC" ) ;
test ( "\"DEF\" <= s1" , "DEF" <= s1) ;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
# include <string>
using namespace std;
int main ( ) {
for ( int i = 0 ; i < 2 ; i++ ) {
string city, state;
cout << " 请输入城市名称以及所属国家,用 ',' 分隔 : " << endl;
getline ( cin, city, ',' ) ;
getline ( cin, state) ;
cout << "City:" << city << " State:" << state << endl;
}
system ( "pause" ) ;
return 0 ;
}
# include "account.h"
# include <iostream>
using namespace std;
int main ( ) {
Date date ( 2008 , 11 , 1 ) ;
SavingsAccount accounts[ ] = {
SavingsAccount ( date, "S3755217" , 0.015 ) ,
SavingsAccount ( date, "02342342" , 0.015 )
} ;
const int n = sizeof ( accounts) / sizeof ( SavingsAccount) ;
accounts[ 0 ] . deposit ( Date ( 2008 , 11 , 5 ) , 5000 , "salary" ) ;
accounts[ 1 ] . deposit ( Date ( 2008 , 11 , 25 ) , 10000 , "sell stock 0323" ) ;
accounts[ 0 ] . deposit ( Date ( 2008 , 12 , 5 ) , 5500 , "salary" ) ;
accounts[ 1 ] . withdraw ( Date ( 2008 , 12 , 20 ) , 4000 , "buy a laptop" ) ;
cout << endl;
for ( int i = 0 ; i < n; i++ ) {
accounts[ i] . settle ( Date ( 2009 , 1 , 1 ) ) ;
accounts[ i] . show ( ) ;
cout << endl;
}
cout << "Total: " << SavingsAccount :: getTotal ( ) << endl;
return 0 ;
}
# include "account.h"
# include <cmath>
# include <iostream>
using namespace std;
double SavingsAccount:: total = 0 ;
SavingsAccount :: SavingsAccount ( const Date & date, const string & id, double rate)
: id ( id) , balance ( 0 ) , rate ( rate) , lastDate ( date) , accumulation ( 0 ) {
date. show ( ) ;
cout << "\t#" << id << " created" << endl;
}
void SavingsAccount :: record ( const Date & date, double amount, const string & desc) {
accumulation = accumulate ( date) ;
lastDate = date;
amount = floor ( amount * 100 + 0.5 ) / 100 ;
balance += amount;
total += amount;
date. show ( ) ;
cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}
void SavingsAccount :: error ( const string & msg) const {
cout << "Error(#" << id << "): " << msg << endl;
}
void SavingsAccount :: deposit ( const Date & date, double amount, const string & desc) {
record ( date, amount, desc) ;
}
void SavingsAccount :: withdraw ( const Date & date, double amount, const string & desc) {
if ( amount > getBalance ( ) )
error ( "not enough money" ) ;
else
record ( date, - amount, desc) ;
}
void SavingsAccount :: settle ( const Date & date) {
double interest = accumulate ( date) * rate
/ date. distance ( Date ( date. getYear ( ) - 1 , 1 , 1 ) ) ;
if ( interest != 0 )
record ( date, interest, "interest" ) ;
accumulation = 0 ;
}
void SavingsAccount :: show ( ) const {
cout << id << "\tBalance: " << balance;
}
# ifndef __ACCOUNT_H__
# define __ACCOUNT_H__
# include "date.h"
# include <string>
class SavingsAccount {
private :
std:: string id;
double balance;
double rate;
Date lastDate;
double accumulation;
static double total;
void record ( const Date & date, double amount, const std:: string & desc) ;
void error ( const std:: string & msg) const ;
double accumulate ( const Date& date) const {
return accumulation + balance * date. distance ( lastDate) ;
}
public :
SavingsAccount ( const Date & date, const std:: string & id, double rate) ;
const std:: string & getId ( ) const { return id; }
double getBalance ( ) const { return balance; }
double getRate ( ) const { return rate; }
static double getTotal ( ) { return total; }
void deposit ( const Date & date, double amount, const std:: string & desc) ;
void withdraw ( const Date & date, double amount, const std:: string & desc) ;
void settle ( const Date & date) ;
void show ( ) const ;
} ;
# endif
# include "date.h"
# include <iostream>
# include <cstdlib>
using namespace std;
namespace {
const int DAYS_BEFORE_MONTH[ ] = { 0 , 31 , 59 , 90 , 120 , 151 , 181 , 212 , 243 , 273 , 304 , 334 , 365 } ;
}
Date :: Date ( int year, int month, int day) : year ( year) , month ( month) , day ( day) {
if ( day <= 0 || day > getMaxDay ( ) ) {
cout << "Invalid date: " ;
show ( ) ;
cout << endl;
exit ( 1 ) ;
}
int years = year - 1 ;
totalDays = years * 365 + years / 4 - years / 100 + years / 400
+ DAYS_BEFORE_MONTH[ month - 1 ] + day;
if ( isLeapYear ( ) && month > 2 ) totalDays++ ;
}
int Date :: getMaxDay ( ) const {
if ( isLeapYear ( ) && month == 2 )
return 29 ;
else
return DAYS_BEFORE_MONTH[ month] - DAYS_BEFORE_MONTH[ month - 1 ] ;
}
void Date :: show ( ) const {
cout << getYear ( ) << "-" << getMonth ( ) << "-" << getDay ( ) ;
}
# ifndef __DATE_H__
# define __DATE_H__
class Date {
private :
int year;
int month;
int day;
int totalDays;
public :
Date ( int year, int month, int day) ;
int getYear ( ) const { return year; }
int getMonth ( ) const { return month; }
int getDay ( ) const { return day; }
int getMaxDay ( ) const ;
bool isLeapYear ( ) const {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ;
}
void show ( ) const ;
int distance ( const Date& date) const {
return totalDays - date. totalDays;
}
} ;
# endif