本人随便编了一个平方根的函数
#include <iostream>
#include <cmath>
using namespace std;
void f(float);
int main(){
float a;
cin>>a;
f(a);
return 0;
}
void f(float n){
float ans;
ans=sqrt(n);
cout<<n<<"的平方根是:"<<ans<<endl;
}
C++的ascii码有趣,输出的形式由一开始变量定义类型决定
#include <iostream>
using namespace std;
int main(){
char a;
int b;
cin>>a;
b=a+1;
a=b;
cout<<a<<"的ascii码"<<b<<endl;
return 0;
}
指针的初体验
#include <iostream>
using namespace std;
int main(){
int a=10,b=20;
int*m=&a;
cout<<m<<"的值是"<<a<<"内存"<<sizeof(m)<<endl;
return 0;
}
指针交换的方式,为了清晰过程,我把每一步交换方式打印出来
#include <iostream>
using namespace std;
void swap01(int*p1,int*p2){
cout<<"初始:"<<endl;
cout<<"p1="<<p1<<" *p1="<<*p1<<"\n";
cout<<"p2="<<p2<<" *p2="<<*p2<<"\n";
int temp=*p1;
cout<<"int temp=*p1的时候\n";
cout<<"p1="<<p1<<" *p1="<<*p1<<"\n";
cout<<"p2="<<p2<<" *p2="<<*p2<<"\n";
*p1=*p2;
cout<<"*p1=*p2的时候\n";
cout<<"p1="<<p1<<" *p1="<<*p1<<"\n";
cout<<"p2="<<p2<<" *p2="<<*p2<<"\n";
*p2=temp;
cout<<"*p2=temp的时候\n";
cout<<"p1="<<p1<<" *p1="<<*p1<<"\n";
cout<<"p2="<<p2<<" *p2="<<*p2<<"\n";
}
int main(){
int a=10,b=20;
swap01(&a,&b);
cout<<"a="<<a<<"b="<<b<<"\n";
}
数组就是一种地址?
//数组与指针
#include <iostream>
using namespace std;
int main(){
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int*p;
p=arr;
for(int i=0;i<=9;i++){
cout<<"第"<<i+1<<"个是" <<*p<<endl;
p++;
}
return 0;
}