#if 1
#include<cstdio>
#include<iostream>
using namespace std;
void copy_string(char *a,char *b)
{
int i=0;
for(a[0] = a[0],b[0] = b[0];*a!='\0';a++,b++)//for(;*a!='\0';a++,b++)
*b=*a;
*b='\0';
}
int main()
{
char a[100]="I love China";
char b[100]="China";//这里如果不把b开辟的大一点,最后的a就被淹没掉一部分,不知道为什么
//start:
printf("start:%p %p \n",a,b);
copy_string(a,b);
printf("after:%p %p \n",a,b);//%p是输出地址
printf("%s\n",a);
printf("%s",b);
return 0;
}
#endif // 0
----------------------------------------------------------正经的分隔符------------------------------------------------------------------
#if 0
#include<cstdio>
#include<iostream>
using namespace std;
void copy_string(char *a,char *b)
{
int i=0;
for(a[0] = a[0],b[0] = b[0];*a!='\0';a++,b++)//for(;*a!='\0';a++,b++)
*b=*a;
*b='\0';
}
int main()
{
char a[100]="I love China";
char b[100]="";
//start:
printf("start:%p %p \n",a,b);
copy_string(a,b);
printf("after:%p %p \n",a,b);//%p是输出地址
printf("%s\n",a);
printf("%s",b);
return 0;
}
#endif // 0
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
char a[100]="I love China";
char b[100];
//start:
printf("start:%p %p \n",a,b);
char *p1;char *p2;p1 = a;p2 = b;//*p1 = a[0],*p2 = b[0]
for(;*p1!='\0';p1++,p2++)//for(;*a!='\0';a++,b++)
*p2=*p1;
*p2='\0';
printf("after:%p %p \n",a,b);//%p是输出地址
//printf("%s\n",a);
//printf("%s",b);
p1 = a;p2 = b;//这一行不能去掉,因为在赋值过程中,p1,p2已经移动了,不再是头部了
cout<<p1<<endl;
cout<<p2<<endl;
return 0;
}
-----------------------------------------------上面两种都能实现将a拷贝到b的功能------------------------------------
通过调用函数指针实现计算不同函数的积分(主要学习使用函数指针)
#include<stdio.h>
#include<math.h>
double T(double x,double y,int z,double (*fun)(double)) ;
double integral (double a,double b,double (*fun)(double));
double f1(double t);
double f2(double t);
double f3(double t);
double f4(double t);
double f5(double t);
int n=0; //用来记录积分区间划分的间隔数,数量越大,越精确
int main()
{
double a,b,s;
printf("down a:\n");
scanf("%lf",&a);
printf("up b:\n");
scanf("%lf",&b);
printf("number of integral n :\n");
scanf("%d",&n);
/*利用辛甫生公式求解定积分*/
s=integral(a,b,f1);//用函数f1来验证
printf("函数 f(x)在区间%f到%f 的积分值为 s=%f\n",a,b,s);
return 0;
}
double f1(double t)
{
return 1+t;
}
double f2(double t)
{
return 3+2*t;
}
double f3(double t)
{
return pow(2.71828,t)+1;//自然常数e,取了一个近似值2.71828
}
double f4(double t)
{
return (1+t)*(1+t);
}
double f5(double t)
{
return t*t*t;
}
//辛普森公式:
double T(double x,double y,int z,double (*fun)(double))
{
double h,Tn;
int i;
h=(y-x)/z;
Tn=(fun(x)+fun(y))/2;
for(i=1;i<z;i++)
Tn=Tn+fun(x+i*h);
Tn=Tn*h;
return (Tn);
}
double integral(double x,double y,double(*fun)(double))
{
return (4*T(x,y,2*n,fun)-T(x,y,n,fun))/3;
}
-----------------------------通过上面的例子,我终于知道怎样使用函数指针了----------------------------------
#if 1
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
//指针函数
double max(double x,double y);double min(double x,double y);
double ave(double a,double b,double (*p1)(double x,double y),double (*p2)(double x,double y));
int main()
{
double (*p1)(double,double);//定义指向函数的指针变量
double (*p2)(double,double);//定义指向函数的指针变量
int a,b;
double m;
p1 = max;//使p指向函数max;
p2 = min;//使p指向函数max;
cout<<"please input down and up line :a,b :"<<endl;
cin>>a>>b;
m = ave(a,b,p1,p2);//一脸严肃,这里调用子函数的时候不传变量,在内部实现的时候进行参数传入
cout<<"max = "<<m<<endl;
return 0;
}
double max(double x,double y)
{
if(x<y)
x = y;
return x;
}
double min(double x,double y)
{
if(x>y)
x = y;
return x;
}
double ave(double a,double b,double (*p1)(double x,double y),double (*p2)(double x,double y))
{
double z;
z = (p1(a,b)+p2(a,b))/2;//两个数字中的最大和最小的平均数肯定是平均数,废。
return z;
}
#endif // 0
-------------------------------------------------------字符数组的使用----------------------------------------------------------------------------------
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
//实现字符串由小到大排列
void sort(char *name[],int n);
void print(char *name[],int n);//*name[]是一个数组,元素是指针,存放字符串的首地址
int main()
{
char *name[] = {"abc","bac","cab"};
int n = 3;
sort(name,n);
print(name,n);
return 0;
}
void sort(char *name[],int n)
{
int i,j,k;
char *temp;
for(i = 0;i<3;i++)
{
k = i;
for(j = i+1;j<3;j++)
if(strcmp(name[k],name[j])<0)
k = j;
if(i!=k)
{
temp = name[k];
name[k] = name[i];
name[i] = temp;
}
}
}
void print(char *name[],int n)
{
int i;
for(i=0;i<n;i++)
cout<<name[i]<<endl;
}
------------------------------------------------------------------c++中形参引用-----------------------------------------------------------------------------------
#if 1
#include<cstdio>
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;//采用形参引用实现两个值的交换
int main()
{
void swap(int &,int &);
int a = 0, b = 1;
swap(a,b);
cout<<a<<setw(6)<<b<<endl;
return 0;
}
void swap(int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
#endif // 0
------------------------------------数组中输入n个数,最小的数与第一个数交换,最大的数与最后一个数交换------------------------------------
#if 1
#include<cstdio>
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;//采用形参引用实现两个值的交换
int main()
{
void c_in(int number[],int n);void c_out(int number[],int n);
void swap_min(int number[],int n);
void swap_max(int number[],int n);
int n = 3;//number数组中元素的个数
int number[n];
c_in(number,n);
swap_min(number,n);
c_out(number,n);
return 0;
}
void swap_min(int number[],int n)//最小的数与第一个数交换,最大的数与第二个数交换
{
void swap(int &a,int &b);
int temp;
int i,j,k;
k = 0;j = n-1;
for(i = 1;i<n;i++)
{
if(number[i]<number[k])
swap(number[i],number[k]);
if(number[i]>number[j])
swap(number[i],number[j]);
}
}
void swap(int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void c_in(int number[],int n)
{
int i,b;
for(i = 0;i<n;i++)
{
scanf("%d",&b);
number[i] = b;
}
}
void c_out(int number[],int n)
{
int i;
for(i = 0;i<n;i++)
printf("%d " ,number[i]);
}
#endif // 0
------------------------------------------------------------后m个前移----------------------限制我的可能是数学上的算法, 取模 remember!-----
# if 1
#include<iostream>
#define MAXLEN 200
using namespace std;
int a[MAXLEN],b[MAXLEN];
int main()
{
int * move(int a[],int n,int m);//声明用来进行移动操作的函数
int *p;
int n=0,m=0,i=0; //i是计数器
cout<<"n:";
cin>>n;
cout<<"\nnumber\n"<<endl;
//初始化数组
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nm:";
cin>>m;
p=move(a,n,m); //执行移动操作
cout<<"\nafter move:\n"<<endl;
//输出数组
for(i=0;i<n;i++)
{
cout<<*(p+i)<<" ";
}
cout<<"\n\n";
return 0;
}
int * move(int a[],int n,int m)
{
int i=0,k;
for(i=n-1;i>=0;i--)
{
k=(i+m)%n; //执行向后移动的操作
b[k]=a[i]; //将向后移动的数据存到数组b中
}
return b; //返回数组头指针
}
# endif // 1
------------------------------------------使用指向指针的指针对字符串进行排序---------------------------------------
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "string.h"
/*使用指针的指针对字符串排序*/
/*排序是按照汉字的首字母进行*/
/*
*整体思路:1、输出排序前的数组元素
* 2、输出排序后的数组元素
*/
//自定义函数sort(),实现对字符串的排序
sort(char*strings[],int n)//参数1:字符型指针数组。参数2:整型变量
{
char *temp; //声明字符型指针变量
int i,j; //声明整型变量
for(i=0;i<n;i++) //之所以要两个循环,因为每个数都要和其他比较
{
for(j=i+1;j<n;j++)
{
/*
*strcmp如何实现两个字符的比较??
① str1小于str2,返回负值或者-1(VC返回-1);
② str1等于str2,返回0;
③ str1大于str2,返回正值或者1(VC返回1);
*/
if(strcmp(strings[i],strings[j])>0)//比较两个字符
{
temp=strings[i];//交换字符位置
strings[i]=strings[j];
strings[j]=temp;
}
}
}
}
int main()
{
int n=5;
int i;
//char *strings[];
char **p;//指向指针的指针变量
char *strings[]={"ca","aa","da","ba","ea"};//初始化字符串数组
p=strings;//指针指向数组首地址
printf("排序前的数组:\n");
for(i=0;i<n;i++)
{
printf("%s\n",strings[i]);
}
sort(p,n);//调用排序自定义过程
printf("\n排序后的数组:\n");
for(i=0;i<n;i++)//循环输出排序后的数组元素
{
printf("%s\n",strings[i]);
}
return 0;
}