📄 pointer.cpp
字号:
int choice;
fp fun[]={menu1,menu2,menu3};
do{
cout<<"1——display good\n"
<<"2——display better\n"
<<"3——display best\n"
<<"0——exit\n"
<<"Enter your choice:";
cin>>choice;
switch(choice){
case 1:fun[0]();break;
case 2:fun[1]();break;
case 3:fun[2]();break;
case 0:break;
default:cout<<"You entered a wrong key.\n\n";
}
}while(choice);
}
//【例6.28】设计一个程序,计算在某个范围内,步长为0.1的所有点的三角函数值的和。
#include <math.h>
double sigma(double (*fp)(double),double start,double end)
{
double temp=0.0;
for(double d=start;d<end;d+=0.1)
temp+=fp(d);
return temp;
}
void main6_28()
{
double sum;
sum=sigma(sin,0.1,1.0);
cout<<"从0.1到1.0之间(步长为0.1)所有点的正弦值之和为:"<<sum<<'\n';
sum=sigma(cos,1.0,3.0);
cout<<"从0.1到1.0之间(步长为0.1)所有点的余弦值之和为:"<<sum<<'\n';
}
//【例6.29】用梯形法求定积分:
#include <math.h>
float fun1(float x)
{
return x+sin(x);
}
float fun2(float x)
{
return 2*x/(1+x*x);
}
float fun3(float x)
{
return (2*x+x*x)/(1+x+cos(x));
}
float integ(float (*fp)(float),float x1,float x2,float n)
{
float sum,h;
sum=(fp(x1)+fp(x2))/2;
h=(x2-x1)/n;
for(int i=1;i<n;i++) sum+=fp(x1+i*h);
return sum*h;
}
void main6_29()
{
cout<<"积分integal1="<<integ(fun1,1,4,1000)<<'\n';
cout<<"积分integal2="<<integ(fun2,-1,4,1000)<<'\n';
cout<<"积分integal3="<<integ(fun3,2,5,1000)<<'\n';
}
//【例6.30】内存的动态分配和撤消
void main6_30()
{
int *p1,(*p2)[8];
float *p3;
char *p4;
float *p5=new float(2.5);
int *p6;
p1=new int(2);
cout<<*p1<<'\n';
p2=(int (*)[8])new int[8];
p2=new int[1][8];
for(int i=0;i<8;i++)
(*p2)[i]=i+1;
for(i=0;i<8;i++)
cout<<(*p2)[i]<<'\t';
cout<<'\n';
p3=new float;
*p3=5.2;
cout<<*p3<<'\n';
p4=new char[10];
strcpy(p4,"abcdefg");
cout<<p4<<'\n';
cout<<*p5<<'\n';
int c=10;
p6=new int[c];
for(i=0;i<c;i++)
p6[i]=2*i;
for(i=0;i<c;i++)
cout<<p6[i]<<'\t';
cout<<'\n';
delete p1;
delete p2;
delete p3;
delete [10]p4;
delete p5;
delete [c]p6;
p1=new int(6);
cout<<*p1<<'\n';
delete p1;
int (*p)[10]=new int[3][10];
delete p;
}
//【例6.31】为结构类型的指针动态分配内存空间。
struct node
{
int x;
float y;
char z[10];
};
void main6_31()
{
node *pn;
pn=new node;
pn->x=2;
pn->y=4.3;
strcpy(pn->z,"China!");
cout<<pn->x<<'\t'<<pn->y<<'\t'<<pn->z<<'\n';
delete pn;
}
//【例6.32】引用类型变量的定义及使用
void fun()
{
int i=10;
static int &j=i;
j++;
cout<<j<<'\n';
}
void main6_32()
{
float k,&ref=k;
k=100.0;
cout<<"&k:"<<&k<<"\t&ref:"<<&ref<<'\n';
cout<<"k="<<k<<"\tref="<<ref<<'\n';
ref+=200;
cout<<"k="<<k<<"\tref="<<ref<<'\n';
float l=500.0;
ref=l;
cout<<"k="<<k<<"\tref="<<ref<<"\tl="<<l<<'\n';
cout<<"&k:"<<&k<<"\t&ref:"<<&ref<<"\t&l:"<<&l<<'\n';
fun();
fun();
}
//【例6.33】用引用作为函数参数的方法实现例6.18中函数swap()的功能。
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
void main6_33()
{
int a=2,b=4;
cout<<"a="<<a<<"\tb="<<b<<'\n';
swap(a,b);
cout<<"a="<<a<<"\tb="<<b<<'\n';
}
//【例6.34】结构类型的引用作为函数的形参。
struct student
{
char name[10];
int age;
long id;
char address[50];
};
void input(student &stu)
{
cout<<"请输入姓名、年龄、学号和住址:\n";
cin>>stu.name>>stu.age>>stu.id>>stu.address;
}
void output(student stu)
{
cout<<"name="<<stu.name<<"\tage="<<stu.age
<<"\tid="<<stu.id<<"\taddress="<<stu.address<<'\n';
}
void main6_34()
{
student stu;
input(stu);
output(stu);
}
//【例6.35】函数返回值为引用类型应用举例
int number1;
int &set1()
{
return number1;
}
int &set2()
{
static int number2;
return number2;
}
int &max(int &m,int &n)
{
return (m>n?m:n);
}
void main6_35()
{
set1()=5;
cout<<set1()<<'\t'<<number1<<'\n';
number1=7;
cout<<set1()<<'\t'<<number1<<'\n';
set2()=6;
int a=set2();
cout<<a<<'\n';
int b=3,c=4;
cout<<max(b,c)<<'\n';
max(b,c)=2;
cout<<"b="<<b<<"\tc="<<c<<"\tmax(b,c)="<<max(b,c)<<'\n';
max(b,c)=10;
cout<<"b="<<b<<"\tc="<<c<<"\tmax(b,c)="<<max(b,c)<<'\n';
}
//【例6.36】void指针与其它类型指针的关联。
void main6_36()
{
void *pv1,*pv2;
float *pf;
int *pi=new int;
pv1=pi;
*pi=2;
cout<<*(int *)pv1<<'\n';
*(int *)pv1=5;
cout<<*pi<<'\n';
pf=(float *)pv2;
}
//【例6.37】void指针作为函数的参数。
int cmpInt(void *a,void *b)
{
return *(int *)a-*(int *)b;
}
int cmpFloat(void *a,void *b)
{
return *(float *)a-*(float *)b>0?1:-1;
}
int cmpChar(void *a,void *b)
{
return strcmp((char *)a,(char *)b);
}
void Sort(void *v,int n,int size,int(*com)(void *,void *))
{
int i,j,k;
char *p,*q,t;
for(i=0;i<n-1;i++){
p=(char *)v+i*size;
for(j=i+1;j<n;j++){
q=(char *)v+j*size;
if(com(p,q)>0)
for(k=0;k<size;k++){
t=p[k];p[k]=q[k];q[k]=t;
}
}
}
}
void main6_37()
{
int vi[]={34,12,25,43,8,16};
float vf[]={34.2,23.1, 67.4, 8.9,14.8};
char vc[][10]={"NanJing","BeiJing","WuHan","ShengYang"};
int i;
Sort(vi,sizeof(vi)/sizeof(int),sizeof(int),cmpInt);
for(i=0;i<sizeof(vi)/sizeof(int);i++)
cout<<vi[i]<<'\t';
cout<<'\n';
Sort(vf,sizeof(vf)/sizeof(float),sizeof(float),cmpFloat);
for(i=0;i<sizeof(vf)/sizeof(float);i++)
cout<<vf[i]<<'\t';
cout<<'\n';
Sort(vc,4,10,cmpChar);
for(i=0;i<4;i++)
cout<<vc[i]<<'\t';
cout<<'\n';
}
//【例6.38】指向常量的指针作为函数的参数。
void strcopy(char *to,const char * from)
{
while(*to++=*from++);
}
void main6_38()
{
char a[20]="Hello,China!";
char *b=new char[20];
strcopy(b,a);
cout<<b<<'\n';
delete b;
}
//【例6.39】链表的创建与操作
typedef struct node1{
int data1;
char data2[10];
node1 *next;
}*ptrN;
ptrN createLinked()
{
ptrN p1,p2,head;
int a;
char *ch=new char[20];
head=0;
cout<<"产生一条无序链表,请输入数据,以-1结束:\n";
while(1){
cin>>a;
if(a==-1)break;
cin>>ch;
p1=new node1;
p1->data1=a;
strcpy(p1->data2,ch);
if(head==0){
head=p1;p2=p1;
}
else{
p2->next=p1;p2=p1;
}
}
if(head)p2->next=0;
delete ch;
return head;
}
ptrN accessNode(const ptrN head,int n)
{
ptrN tmpNode;
if(head==0)
return 0;
else
tmpNode=head;
for(int i=0;i<n-1;i++)
{
if(tmpNode->next==0)
return 0;
tmpNode=tmpNode->next;
}
return tmpNode;
}
ptrN accessTrail(const ptrN head)
{
ptrN tmpNode;
if(head==0)
return 0;
else
tmpNode=head;
while(tmpNode->next)
tmpNode=tmpNode->next;
return tmpNode;
}
ptrN insertNode(ptrN head,ptrN newNode,int n)
{
ptrN pPre,pBac;
if(head==0)
{
head=newNode;
head->next=0;
}
else if(n==1)
{
pBac=head;
head=newNode;
head->next=pBac;
}
else{
pPre=accessNode(head,n-1);
if(pPre&&pPre->next)
{
pBac=pPre->next;
pPre->next=newNode;
newNode->next=pBac;
}
else
{
pPre=accessTrail(head);
pPre->next=newNode;
newNode->next=0;
}
}
return head;
}
ptrN deleteNode(ptrN head,int n)
{
ptrN pCur,pPre,pBac;
if(head==0)
return 0;
else if(n==1)
{
pCur=head;
head=head->next;
delete pCur;
}
else
{
pCur=accessNode(head,n);
if(pCur&&pCur->next)
{
pPre=accessNode(head,n-1);
pBac=(pPre->next)->next;
pPre->next=pBac;
delete pCur;
}
else if(pCur&&pCur->next==0)
{
pPre=accessNode(head,n-1);
pPre->next=0;
delete pCur;
}
else if(pCur==0)
{
cout<<"链表的结点数小于"<<n<<",没有要删除的结点!\n";
}
}
return head;
}
void deleteLink(ptrN head)
{
ptrN tmpNode;
while(head)
{
tmpNode=head;
head=head->next;
delete tmpNode;
}
}
ptrN insert_sort_node(ptrN head,ptrN newNode)
{
ptrN pPre,pBac;
if(head==0)
{
head=newNode;
head->next=0;
return head;
}
else if(head->data1>newNode->data1)
{
newNode->next=head;
head=newNode;
return head;
}
pPre=head;
while(pPre->next)
{
pBac=pPre->next;
if(pPre->data1<=newNode->data1&&pBac->data1>newNode->data1)
{
pPre->next=newNode;
newNode->next=pBac;
return head;
}
pPre=pPre->next;
}
pPre->next=newNode;
newNode->next=0;
return head;
}
ptrN create_sort_linked()
{
ptrN head=0,p1;
int a;
char *ch=new char[20];
cout<<"产生一条有序链表,请输入数据,以-1结束:\n";
while(1){
cin>>a;
if(a==-1)break;
cin>>ch;
p1=new node1;
p1->data1=a;
strcpy(p1->data2,ch);
head=insert_sort_node(head,p1);
}
return head;
}
void outputNodes(const ptrN head)
{
int i=1;
ptrN nodeN;
while(1)
{
nodeN=accessNode(head,i);
if(nodeN)
cout<<"第"<<i<<"个结点中的数据为:"<<nodeN->data1<<'\t'<<nodeN->data2<<'\n';
else
break;
i++;
}
}
void outputNodes1(const ptrN head)
{
ptrN nodeN;
int i=1;
nodeN=head;
while(nodeN)
{
cout<<"第"<<i<<"个结点中的数据为:"<<nodeN->data1<<'\t'<<nodeN->data2<<'\n';
nodeN=nodeN->next;
i++;
}
}
void main6_39()
{
/*
cout<<"创建一个无序链表:\n";
ptrN head=createLinked();
cout<<"输出链表中的所有结点:\n";
outputNodes(head);
cin.get();
cout<<"\n在链表中插入一个结点!\n";
ptrN tmpNode=new node1;
tmpNode->data1=2;
strcpy(tmpNode->data2,"China!");
head=insertNode(head,tmpNode,3);
cout<<"\n输出链表中的所有结点:\n";
outputNodes1(head);
cin.get();
cout<<"\n删除链表中的第2个结点:\n";
head=deleteNode(head,2);
cout<<"输出链表中的所有结点:\n";
outputNodes1(head);
cout<<"\n释放链表所占用的内存空间!";
deleteLink(head);
*/
ptrN head1=create_sort_linked();
outputNodes(head1);
deleteLink(head1);
}
//习题15
double root(double (*pf)(double x), double a,double b,int n)
{
double c;
for(int i=0;i<n;i++)
{
c=(a+b)/2;
if(pf(a)*pf(c)<=0)
b=c;
else if(pf(c)*pf(b)<0)
a=c;
else
cout<<"方程没有实根!\n";
}
return c;
}
double f(double x)
{
return 2-x*x;
}
void main15()
{
int x;
cout<<root(f,1,2,1000)<<endl;
}
//实验题4
typedef struct node2
{
int data, count;
node2 *next;
}snode, *ptr;
void compress(ptr head)
{
ptr p1,p2;
p1=head;
while(p1->next)
{
p2=p1->next;
if(p1->data==p2->data)
{
p1->count++;
p1->next=p2->next;
delete p2;
}
else(p1=p1->next) ;
}
}
//实验题3
typedef int *IPTR;
int sequence(IPTR p,int n)
{
int i=0;
while(*p==*(p+1)&&i<n-1)p++,i++;
if(i==n-1)return 3;
if(*p<*(p+1))
{
for(;i<n-1;i++,p++)
if(*p>*(p+1)) return 0;
return 1;
}
else
{
for(;i<n-1;i++,p++)
if(*p<*(p+1)) return 0;
return 2;
}
}
void mainex1()
{
int a[10]={1,1,1,1,1,1,1,1,1,1};
cout<<sequence(a,10)<<endl;
}
void mainex2()
{
int (*p)[4]=new int[3][4];
int (*p1)[4]=p;
int *p2=p[1]+2;
*p2=4;
delete p;
cout<<*p2<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -