C++语言程序设计(西电版)第6章 指针和引用 代码

是每一段放到一起的,中间用*//*分割,需要请自取,每一段都可以独立运行

倒序排列的!

*//*

//将一个整型数组降序排序
 
#include<iostream>
#include<iomanip>
using namespace std;

void sort(int *pArray, int n);
void search(int *pArray, int n, int x, int &y);//x为插入数 

int main()
{
	int n, x, y = 0;
	cout<<"请输入数值数:"<<endl; cin>>n;
	int *pArray = new int[n];
	
	cout<<"请输入数值"<<endl;
	for(int i = 0; i<n; i++)
	    cin>>pArray[i];   
	
	sort(pArray, n);
	for(int i = 0; i<n; i++)
	    cout<<setw(3)<<pArray[i];
	
	cout<<"\n请输入要插入的数"<<endl; cin>>x;
	search(pArray, n, x, y);
	cout<<"插入位置为第"<<y<<"个数和第"<<y+1<<"个数之间"<<endl;		
	for(int i = 0; i<=n; i++)
	    cout<<setw(3)<<pArray[i];
	    
    delete[] pArray;
    return 0;

}

void sort(int *pArray, int n)
{
  int bridge = 0;
  for(int i = 0; i<n; i++)
  {
	for(int j = 0; j<n; j++)
	{
		if(pArray[j]<pArray[j+1])
		{
			bridge = pArray[j];
			pArray[j] = pArray[j+1];
			pArray[j+1] = bridge;
		}
	}  
  }
}

void search(int *pArray, int n, int x, int &y)
{
	for(int i = 0; i<n; i++)
		if(x<pArray[i])y++;
	for(int i = n; i>y; i--)
		pArray[i] = pArray[i-1];
	pArray[y] = x; 
}

 
*//* 

//输入10个数据到一维数组,找出最大值和其下标 
#include<iostream> 
using namespace std;
void search(int *pa, int n, int *pmax, int *pflag);
int main()
{
	int n = 10, pmax, pflag;
	int a[n] = {0};
	cout<<"Input 10 numbers:"<<endl;
	for(int i = 0; i<10; i++)
	{
		cin>>a[i];
		cin.get();
	}
	
	search(a, n, &pmax, &pflag);
	cout<<"Max value: "<<pmax<<", Index: "<<pflag<<endl;  
	return 0;
}

void search(int *pa, int n, int *pmax, int *pflag)
{
	*pmax = *pa;
	*pflag = 0;
	for(int i = 1; i<n; i++)
	{
		if(*(pmax)<*(pa + i))
		{
		    *pmax = *(pa + i);	
		    *pflag =i;
		} 
	}
}

*//* 

//编写程序交换两个字符串变量 
#include<iostream>
using namespace std;

void exchange(char*& a, char*& b);
int main()
{
	char* ap = "hello";
	char* bp = "how are you";
	exchange(ap,bp);
	cout<<"ap: "<<ap<<"\n"
	    <<"bp: "<<bp<<endl;
	return 0;
}

void exchange(char*& a, char*& b)
{
	char* p = a;
	a = b;
	b = p;
}

*//*

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<iomanip>
using namespace std;

void produce_score(int score[5][10]);
void print_score(int score[5][10]);

int main()
{
	int score[5][10] = {0};
	produce_score(score);
	print_score(score);
} 

void produce_score(int score[5][10])
{
	srand((unsigned int)time(NULL));
	for(int i = 0; i<5; i++)
	{
		for(int j = 0; j<10; j++)
			score[i][j] = rand()%100;
	}
}

void print_score(int score[5][10])
{
	for(int i = 0; i<5; i++)
	{
		cout<<"class "<<i<<": ";
		for(int j = 0; j<10; j++)
			cout<<setw(2)<<score[i][j]<<" ";
		cout<<endl;
	}
}

*//* 

//存放学生数据,输出并存在文件里 
#include<iostream>
#include<iomanip>
#include<fstream> 
using namespace std;
const int N = 30;
struct student
{
	char name[15];
	int sex;//1 for male,2 for female
	int age;
	char id[10];
}students[N];

void input(student* students);
void output(student* students);
void saveToFile(student* students);

int main()
{
    input(students);
    output(students);
    saveToFile(students);
}

void input(student* students)
{
	cout<<"Input students' name, sex, age and id\n"<<endl;
	for(int i = 0; i<N; i++)
	{
		cout<<"    Now for student "<<i+1<<": "<<endl;
		cin>>students[i].name
		   >>students[i].sex
		   >>students[i].age
		   >>students[i].id;
	}
}

void output(student* students)
{
	for(int i = 0; i<N; i++)
	{
		cout<<setw(10)<<students[i].name<<" "
		    <<students[i].sex<<" "
		    <<students[i].age<<" "
		    <<students[i].id<<endl;
	}
}

void saveToFile(student* students) 
{  
    ofstream outFile;
    outFile.open("idata.txt");
    if (outFile.is_open()) 
	{  
        for (int i = 0; i < N; i++)   
            outFile.write((char*)&students[i], sizeof(student));  
            
        outFile.close();
        cout << "Done" << endl;  
    } 
	else  cout << "Error" << endl;  
}

*//* 

//使用memcpy()函数直接复制数组 
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char src[10] = "012345678";
	char dest[10];
	char* pc = (char*)memcpy(dest,src,10);
	cout<<pc<<endl;
	
	int s1[3] = {1,2,3};
	int d1[3];
	int *pi = (int*)memcpy(d1,s1,12);
	cout<<*pi<<"  "<<*(pi+1)<<"  "<<*(pi+2)<<endl;
	
	return 0;
} 

*//* 

//单向链表的建立
#include<iostream>
using namespace std;

struct student
{
	long num;
	char name[20];
	float score;
	student* next;
};

int main()
{
	student *head = NULL, *temp = NULL;
	head = new student;
	temp = head;
	int i = 1;
	while(temp != NULL)
	{
		temp->num = i;
		cout<<"Please input name and score for No."<<i<<endl;
		cin>>temp->name>>temp->score;
		temp->next = NULL;
		i++;
		if(i==5) break;
		else
		{
			temp->next = new student;
			temp = temp->next;
		}
	}
	
	temp = head;
	while(temp != NULL)
	{
		cout<<temp->num<<"  "<<temp->name<<"  "<<temp->score<<endl;
		temp = temp->next;
	}
}

*//*

//通过结构体指针数组完成结构体数组的排序
#include<iostream> 
using namespace std;

struct Employee
{
	char name[10];
	unsigned long id;
	float salary;
};
Employee allone[6] = 
{
	{ "A", 12345, 3390.0},
	{ "B", 12375, 4490.0},
	{ "C", 12466, 3310.0},
	{ "D", 42876, 6320.0},
	{ "E", 23987, 4000.0},
	{ "F", 12335, 5110.0},	
};

void print(Employee* pA[]);

int main()
{
	Employee* pA[6] = {0};
	for(int i = 0; i<6; i++)pA[i] = &allone[i];
	Employee* temp;
	print(pA);
	for(int i = 1; i<6; i++)
	{
		for(int j = 0; j<=5-i; j++)
		{
			if(pA[j]->salary > pA[j+1]->salary)
			{
				temp = pA[j];
				pA[j] = pA[j+1];
				pA[j+1] = temp;
			} 
		}
	}	
	print(pA);
	return 0;
}

void print(Employee* pA[])
{
	for(int i = 0; i<6; i++)
	    cout<<pA[i]->name<<"  "
	        <<pA[i]->id<<"  "
	        <<pA[i]->salary<<endl;
	cout<<endl;
}

*//*

//结构体数组的定义和使用
#include<iostream>
#include<cstring>
using namespace std;
struct Employee
{
	char name[10];
	unsigned long id;
	float salary;
};
int main()
{
	Employee* prPtr = new Employee;
	strcpy(prPtr->name,"ZhangSan");
	prPtr->id = 98001;
	prPtr->salary = 3350.0;
	cout<<prPtr->name<<"  "<<prPtr->id<<"  "<<prPtr->salary<<endl;
	delete prPtr;
	
	return 0;
}

*//*
 
//用单循环程序,求二维数组元素的平均值 
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<iomanip>
using namespace std;    
int main()
{
	const int M = 3, N = 4; 
	float average = 0;
	int array[M][N] = {0};
	srand((unsigned int)time(NULL));	
	int sum = 0, (*pt)[N];
	pt = array;
	for(int i = 0; i<M*N; i++)
	{
		*(*pt+i)=  rand()%100;
		cout<<setw(2)<<*(*pt+i)<<"  ";
		sum+=*(*pt+i);
	} 
	    
	average = (float)sum / (M*N);
    cout<<"\nThe average is: "<<average<<endl;
	return 0;
}

*//*

//指针数组及其访问 
#include<iostream>
using namespace std;
int main() 
{
	char* member_name[] = {"Merry","John","Hill"};
	cout<<"The namelist show as:\n"<<endl;
	for(int i = 0; i<sizeof(member_name)/sizeof(char*); i++)
	    cout<<member_name[i]<<"\n";
	return 0;
}

*//*

//求整型数组的平均值
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int* produce_array(int array[],const int N);    
void compute_average(int array[],const int N); 
int main() 
{
	cout<<"请输入元素个数"<<endl;
	int n;  cin>>n;
	const int N = n;
	
	int* intArray =new int[N];
	//int intArray[N] = {0}; 
	
	srand((unsigned int)time(NULL));
	produce_array(intArray,N);
	compute_average(intArray,N);
    
	delete []intArray;
    return 0;
}

int* produce_array(int array[],const int N)
{
	for(int i = 0; i<N; i++)
	    array[i] = rand()%100;
	return array;
}

void compute_average(int Array[],const int N)
{
	int *ptint = NULL; 
	long double sum = 0;
	float average = 0;
	ptint = Array;
	for(int i = 0; i<N; i++)
	    sum+=*ptint++; 
    average =sum/N;
    cout<<"\n平均值为:"<<average<<endl;
}

*//* 

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int n;
	const char* array1 = "Happy birthday to you!";
	char array2[25];
	char array3[15];
	
	strcpy(array2,array1);
	cout<<"array1: "<<array1
	  <<"\narray2: "<<array2<<endl;
	
	n = sizeof(array3);
	strncpy(array3,array1,n-1);
	array3[n-1] = '\0';
	cout<<"array3: "<<array3<<endl;
} 

*//* 

//使用函数指针调用函数
#include<iostream> 
using namespace std;

float S_of_Square(float width,float height)
{   return width * height;    }
float S_of_Triangle(float heml,float height)
{   return (heml * height)/2;    }

int main()
{
  float (*fptr)(float,float) = 0;
  float area,width,height;

  cout<<"计算图形面积"<<endl;	
  int choice = 1;
  while(choice == 1||choice == 2)
  { 
    cout<<"1 矩形  2 三角形  else 结束程序\n"<<endl;
	cin>>choice;
	switch(choice)
	{
	case 1:
		cout<<"请输入矩形的底和高: \n"<<endl;
        cin>>width>>height;
        fptr = S_of_Square;
        area = fptr(width,height);
	    cout<<"\n该矩形的面积为: "<<area<<"\n"<<endl;
        break;
        
	case 2:
	    cout<<"请输入三角形的底和高: \n"<<endl;
	    cin>>width>>height;
	    fptr = S_of_Triangle;
	    area = fptr(width,height);
	    cout<<"\n该三角形的面积为:"<<area<<"\n"<<endl;
		break;
		
	default:
	    break;
    }
  } 	
  cout<<"\n感谢使用!\n";
  return 0;	
}

*//* 

//利用指针函数完成数组倒序排列。原数组由函数参数传递,倒序数组由指针函数返回
#include<iostream>
#include<ctime>
#include<cstdlib> 
#include<iomanip>
using namespace std;

int* produce_array(int array[],const int N);        //随机生成数组 
int* reverse_array(int const* list,const int size); //指针函数 
void print_array(int const* list,const int size);   //打印数组

int main() 
{
	srand((unsigned int)time(NULL));
	int n = rand()%25;
	const int N = n;
	int list[N] = {0};
	produce_array(list,N);  
	int* pList = reverse_array(list, n);
	print_array(list,n);
	print_array(pList,n);
	delete[]pList;
	return 0;
}

int* produce_array(int array[],const int N)
{
	for(int i = 0; i<N; i++)
	    array[i] = rand()%100;
	return array;
} 

int* reverse_array(int const* list,const int size)
{
	int* result = new int[size];
	for(int i = 0; i<size; i++)
	    result[size-1-i] = list[i];
    return result;
}

void print_array(int const* list,const int size)
{
	for(int i =0; i<size; i++)
	    cout<<setw(2)<<list[i]<<" ";
	cout<<endl;
}

*//* 

//分别用指针、引用编程数据交换的函数
#include<iostream> 
#include<ctime>
#include<cstdlib> 
using namespace std;
int* Swap(int *a,int *b);
int Swap_1(int &x,int &y);
int main()
{
	srand((unsigned int)time(NULL));
	int x = rand()%100, y = rand()%100;
	cout<<" 原值 x = "<<x<<" y = "<<y<<"\n"<<endl;
	Swap(&x,&y);
	cout<<"返回值x = "<<x<<" y = "<<y<<"\n"<<endl;
	Swap_1(x,y);
	cout<<"返回值x = "<<x<<" y = "<<y<<endl;
}

int* Swap(int *a,int *b)
{
	int t = *a;
	*a = *b;
	*b = t;
	cout<<"函数中完成了交换:\n *a = "<<*a<<"  *b = "<<*b<<endl;
	return a;
} 

int Swap_1(int &x,int &y)
{
	int j = x;
	x = y;
	y = j;
	cout<<"函数中完成了交换:\n x = "<<x<<"  y = "<<y<<endl;
	return x;
}

*//*
 
//引用的使用
#include<iostream> 
using namespace std;
int main()
{
	int intA = 10;
	int& refA = intA;
	cout<<"引用的值与相关变量值相同:\nrefA = "<<refA<<endl;
	refA = 5;
	cout<<"引用变化,则相关变量也变化:\nintA = "<<intA<<endl;
	cout<<"引用的地址和相关变量地址相同:\nintA的地址:"<<&intA<<endl;
	cout<<"引用的地址和相关变量地址相同:\nrefA的地址:"<<&refA<<endl;
}

*//*

//堆内存的申请和释放
#include<iostream> 
#include<cstdlib> 
#include<ctime>
#include<iomanip>
using namespace std;
int main()
{
	const int N = 10000000;
	int *parr = new int[N]; 
	srand((unsigned int)time(NULL));  
	int *pm = parr;	
	for(int i = 0; i<N; i++)
	{
		*pm = rand()%1000;
		pm++;
	}
	for(int i = 0,k = 0; i<20;i++)
	{
		
		k += rand()%(N/20);
		cout<<"parr["<<setw(6)<<k<<"]="<<*(parr + k)<<endl;	
	}    
	delete []parr;

    return 0;
}

*//*

//访问指针 
#include<iostream>
using namespace std;
int main()
{
//访问变量	
	char ch1 = 'a',*pch = 0;
	int k1 = 100;
	pch = &ch1;
	cout<<"&pch= "<<&pch<<endl;  //间接访问 
	*pch = 'B';
	cout<<"ch1= "<<ch1<<endl;    //直接访问 
	ch1 = k1;
	cout<<"*pch= "<<*pch<<endl;  //间接访问 
	
	cout<<"\n"<<endl;
//间接访问数组	
	int arr1[10] = {11,23,45,23,34,25,35,67,85,23} , *pk = NULL;
	pk = &arr1[0];
	for(int i = 0; i<10; i+=2)
		cout<<"arr1["<<i<<"]= "<<*(pk+i)<<endl;	
}

*//*

// 观察指针
#include<iostream>
using namespace std;
int main()
{
	int a2 = 61;
	int* pa1 = NULL, *pa2 = &a2;
	char *ch1 = NULL, *ch2 = 0;
	cout<<a2<<"\t"<<pa2<<"\t"<<&pa2;
	
	return 0;
}

*/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值