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

📄 指针.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 TXT
字号:
//*************************
//**      ch8_1.cpp      **
//*************************

#include <iostream.h>

void main()
{
	int *iPtr;
	int iCount=18;
	iPtr=&iCount;
	cout<<*iPtr<<endl;

	*iPtr=56;
	cout<<iCount<<endl;
}

//*************************
//**      ch8_2.cpp      **
//*************************

#include <iostream.h>

void main()
{
	int iCount=18;
	int *iPtr=&iCount;
	cout<<"Original:"
		<<*iPtr<<endl;

	*iPtr=56;

	cout<<"Changed:"<<endl
		<<"iCount:"<<iCount<<endl;
	cout<<"*iPtr:"<<*iPtr<<endl
		<<"iPtr:"<<iPtr<<endl
		<<"&iCount:"<<&iCount<<endl
		<<"&iPtr:"<<&iPtr<<endl;
}

//*************************
//**      ch8_4.cpp      **
//*************************

#include <iostream.h>

void main()
{
	int iArray[10];
	int sum=0;
	int* iPtr=iArray;

	for(int i=0;i<10;i++)
		iArray[i]=i*2;

	for(int index=0;index<10;index++)
	{
		sum+=*iPtr;
		iPtr++;
	}
	cout<<"sum is "<<sum<<endl;
}

//*************************
//**      ch8_5.cpp      **
//*************************

#include <iostream.h>

void main()
{
	int iArray[10];
	int sum=0;
	int* iPtr=iArray;

	for(int i=0;i<10;i++)
		iArray[i]=i*2;

	for(int index=0;index<10;index++)
	{
		cout<<"&iArray["<<index<<"]:"<<iPtr
			<<"=>"<<*iPtr<<endl;
		sum+=*iPtr;
		iPtr++;
	}
	cout<<"sum is "<<sum<<endl;
}

//*************************
//**      ch8_7.cpp      **
//*************************

#include <iostream.h>
#include <malloc.h>    //note:Microsoft has changed alloc.h to malloc.h in VC6.0

void main()
{
	int arraysize;
	int *array;
	cout<<"please input a number of array:"<<endl;
	cin>>arraysize;

	array=(int*)malloc(arraysize*sizeof(int));

	for(int cnt=0;cnt<arraysize;cnt++)
		array[cnt]=cnt*2;

	for(cnt=0;cnt<arraysize;cnt++)
		cout<<array[cnt]<<" , ";
	cout<<endl;
}
改进:判断堆空间是否可以分配
	if((array=(int*)malloc(arraysize*sizeof(int)))==NULL)
	{
		cout<<"can't allocate more memory, terminating."<<endl;
		exit(1);
	}
须增加头文件:stdlib.h
改进:释放堆内存
	free(array);

//*************************
//**      ch8_9.cpp      **  ch8_7改进版
//*************************
//采用new,delete
改进部分:
	if((array=new int[arraysize])==NULL)
	{
		cout<<"can't allocate more memory, terminating."<<endl;
		exit(1);
	}
释放内存语句:
	delete [] array;  //note the use of delete
new和delete更简洁;

//**************************
//**      ch8_10.cpp      **
//**************************

#include <iostream.h>

void strcpy(char* dest, const char* source)
{
	while(*dest++ = *source++);
}

void main()
{
	char a[20]="How are you!";
	char b[20];
	strcpy(b,a);
	cout<<b<<endl;
}

//指向常量的指针常量 const* const
#include <iostream.h>

void main()
{
	const int ci=7;
	int ai;
	const int* const cpc=&ci;
	const int* const cpi=&ai;
	//cpi=&ai;  //指向常量的指针常量,必须在定义时初始化
	//*cpi=29;    //error!
	ai=88;
}

//************************************//
//**            ch8_11.cpp            **
//************************************//
#include <iostream.h>

void Sum(int array[],int n)  //此处array[]本质上是一个指针,可以进行递增、递减操作,
                             //故亦可写成*array
{
	int sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=*(array++);
	}
	cout<<"sum:"<<sum<<endl;
}

void main()
{
	int a[10]={1,2,3,4,5,6,7,8,9,10};
	Sum(a,10);
}

//************************************//
//**            ch8_12.cpp            **  
//************************************//
#include <iostream.h>

void swap(int,int);

void main()
{
	int a=3,b=9;
	cout<<"a="<<a<<",b="<<b<<endl;
	swap(a,b);
	cout<<"after swapping..."<<endl;
	cout<<"a="<<a<<",b="<<b<<endl;   //actually, the value of a and b are not changed
}

void swap(int x,int y)
{
	int tmp=x;
	x=y;
	y=tmp;
}

//************************************//
//**            ch8_13.cpp            **  
//************************************//
//see the advantage of pointer, cmp with the pre program

#include <iostream.h>

void swap(int*,int*);

void main()
{
	int a=3,b=9;
	cout<<"a="<<a<<",b="<<b<<endl;
	swap(&a,&b);
	cout<<"after swapping..."<<endl;
	cout<<"a="<<a<<",b="<<b<<endl;
}

void swap(int* x,int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

//************************************//
//**            ch8_14.cpp            **
//************************************//
#include <iostream.h>

int* getInt(char* str)
{
	int value=20;
	cout<<str<<endl;
	return &value;
}

void somefn(char* str)
{
	int a=40;
	cout<<str<<endl;
}

void main()
{
	int* pr=getInt("input a value:");
	cout<<*pr<<endl;
	somefn("It is uncertain.");
	cout<<*pr<<endl;
}

//************************************//
//**            ch8_15.cpp            **
//************************************//
#include <iostream.h>
#include <memory.h>

void main()
{
	char src[10]="*********";
	char dest[10];
	char* pc=(char*)memcpy(dest,src,10);
	cout<<pc<<endl;
}
注:memcpy()原型为:void* memcpy(void* d,void* s,size_t n);

//字符串常量的类型是指向字符的指针(字符指针char*)
#include <iostream.h>
#include <memory.h>

void main()
{
	char* pc="good";
	cout<<pc<<endl;
}
//输出字符指针就是输出字符串;
//输出字符指针的间接引用,就是输出单个字符

//************************************//
//**            ch8_16.cpp            **
//************************************//
#include <iostream.h>

void main()
{
	char* a="join";
	char* b="joic";
	if(a==b)
		cout<<"equal!"<<endl;
	else 
		cout<<"not equal!"<<endl;
} //in fact, in VC6.0, the result is equal!

//************************************//
//**            ch8_17.cpp            **
//************************************//
#include <iostream.h>

void main()
{
	char buffer[10]="ABC";
	char* pc;
	pc="hello";
	cout<<pc<<endl;
	pc++;
	cout<<pc<<endl;
	cout<<*pc<<endl;
	pc=buffer;
	cout<<pc<<endl;
}

//************************************//
//**            ch8_18.cpp            **
//************************************//
#include <iostream.h>

void main()
{
	char buffer1[10]="hello";
	char buffer2[10]="hello";

	if(buffer1==buffer2)
		cout<<"equal"<<endl;
	else 
		cout<<"not equal"<<endl;
}  //the result is not equal, for the address of buffer1 and buffer2 don't equal

//************************************//
//**            ch8_19.cpp            **
//************************************//
#include <iostream.h>
#include <string.h>

void main()
{
	char buffer1[10]="hello";
	char buffer2[10]="hello";
	int v=strcmp(buffer1,buffer2);

	if(v==0)
		cout<<"a is equal to b."<<endl;
	else if(v>0)
		cout<<"a is greater than b."<<endl;
	else 
		cout<<"a is smaller than b."<<endl;
}

//字符串赋值
#include <iostream.h>
#include <string.h>

void main()
{
	char buffer1[10];
	char buffer2[10];
	strcpy(buffer1,"hello");
	strcpy(buffer2,buffer1);
	cout<<"buffer1:"<<buffer1<<endl;
	cout<<"buffer2:"<<buffer2<<endl;
}
strcpy原型:char* strcpy(char* dest,char*src); 返回值为dest值

//其他类型数组赋值,memcpy():
#include <iostream.h>
#include <memory.h>

void main()
{
	int intarray1[5]={1,3,5,7,9};
	int intarray2[5];
	memcpy(intarray2,intarray1,5*sizeof(int));
	for(int i=0;i<sizeof(intarray2)/sizeof(*intarray2);i++)
		cout<<*(intarray2+i)<<endl;
}

//**************************
//**      ch8_20.cpp      **
//**************************
#include <iostream.h>

void Print(char* [],int);

void main()
{
	char* pn[]={"Fred","Barney","Wilma","Betty"};
	int num=sizeof(pn)/sizeof(char*);
	cout<<"num="<<num<<endl;

	Print(pn,num);
}

void Print(char* arr[],int len)
{
	for(int i=0;i<len;i++)
		cout<<(int)arr[i]<<"  "
			<<arr[i]<<endl;
}

//**************************
//**      ch8_21.cpp      **
//**************************

#include <iostream.h>

void Print(char* []);

void main()
{
	char* pn[]={"Fred","Barney","Wilma","Betty",NULL};
	Print(pn);
}

void Print(char* arr[])
{
	while(*arr!=NULL)
	{
		cout<<(int)*arr<<"  "
			<<*arr<<endl;
		arr++;
	}
}

//**************************
//**      ch8_22.cpp      **
//**************************

#include <iostream.h>

void main(int argc,char* argv[])
{
	int iCount=0;
	while(iCount<argc)
	{
		cout<<"arg"<<iCount<<":"<<argv[iCount]<<endl;
		iCount++;
	}
}

//**************************
//**      ch8_24.cpp      **
//**************************

#include <iostream.h>
#include <math.h>

double sigma(double(*func)(double),double dl, double du)
{
	double dt=0.0;
	for(double d=dl;d<du;d+=0.1)
		dt+=func(d);
	return dt;
}

void main()
{
	double dsum;
	dsum=sigma(sin,0.1,1.0);
	cout<<"the sum of sin from 0.1 to 1.0 is "<<dsum<<endl;
	dsum=sigma(cos,0.5,3.0);
	cout<<"the sum of cos from 0.5 to 3.0 is "<<dsum<<endl;
}

//**************************
//**      ch8_25.cpp      **
//**************************

#include <iostream.h>
#include <stdlib.h>
#include <string.h>

int compare(const void* a, const void* b);

char* list[5]={"cat","car","cab","cap","can"};

void main()
{
	qsort((void*)list,5,sizeof(list[0]),compare);

	for(int i=0;i<5;i++)
		cout<<list[i]<<endl;
}

int compare(const void* a, const void* b)
{
	return strcmp(*(char**)a, *(char**)b);
}

//**************************
//**      ch8_26.cpp      **
//**************************

#include <iostream.h>

typedef void(*MenuFun)();
void f1() { cout<<"good!"<<endl; }
void f2() { cout<<"better!"<<endl; }
void f3() { cout<<"best!"<<endl; }

MenuFun fun[]={f1,f2,f3};

void main()
{
	int choice;
	do{	
		cout<<"1——display good"<<endl
			<<"2——display better"<<endl
			<<"3——display best"<<endl
			<<"0——exit"<<endl
			<<"Enter your choice:";
		cin>>choice;
		switch(choice)
		{
			case 1:fun[0](); break;
			case 2:fun[1](); break;
			case 3:fun[2](); break;
			case 0:return;
			default:cout<<"you enter a wrong key."<<endl;
		}
	}while(choice);
}

⌨️ 快捷键说明

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