您的位置:首页 > 编程语言 > C语言/C++

C++基础实例(2)

2016-05-02 18:11 411 查看

一维数组

#if 0
/*数组*/
#include<iostream>
using namespace std;

int  main()
{
//声明数组和变量
int i,sum=0,a[5];
//从键盘上循环为数组赋值
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}

//直接显示数组元素
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<endl;

//利用for循环显示数组各元素的值
for(i=0;i<5;i++)
cout<<a[i]<<" "<<endl;

//计算数组之和
sum=a[0]+a[1]+a[2]+a[3]+a[4];

//利用for循环计算数组之和
for(i=0;i<5;i++)
sum+=a[i];

//显示累加和及平均值
cout<<"sum="<<sum<<endl;
double avg=sum/5.0;
cout<<"avg="<<avg<<endl;

return 0;
}
#endif

数组计算

#if 0
/*数组计算*/
#include<iostream>
using namespace std;

int main()
{
int i,max,index,a[5];
//从键盘上为数组赋值
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
//利用循环遍历数组,并找出最大值与下标
max=a[0];
for(i=0;i<5;i++)
{
if(max<a[i])
{
max=a[i];
index=i;
}
}
cout<<"\nmax="<<max<<" index="<<index<<endl;
return 0;
}
#endif

数组排序、二分法查找

#if 0
/*数组排序(小到大)、二分法*/
#include<iostream>
using namespace std;
#define size 5
main()
{
//声明变量
int i,j;
float t,a[size];

//从键盘上为数组赋值
for (i=0;i<size;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}

//对数组按从小到大顺序排序
for (i=0;i<size-1;i++)
for (j=i+1;j<size;j++)
if (a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}

//显示排序结果
for (i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;

//输入要查找的数据
int value;
int found; //找到为1,否则为0
int low,high,mid;
for (i=1;i<=3;i++) {
cout<<"value=";
cin>>value;

//二分法查找数组a
found=0;
low=0;
high=size-1;
while(low<=high)
{
mid=(high+low)/2;
if (a[mid]==value)
{
found=1;
break;
}
if (a[mid]<value)
low=mid+1;
else
high=mid-1;
}
if (found)
cout<<"The valu found at:a["<<mid<<"]="<<a[mid]<<endl;
else
cout<<"The "<<value<<" is not found!"<<endl;
}
}
#endif

大-小排序

#if 0
/*数组排序*/
#include<iostream>
using namespace std;

int main()
{
int i,j;
float t,a[5];

//从键盘为数组赋值
for(i=0;i<5;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}

//对数组按从大到小顺序排序
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
//显示排序结果
for(i=0;i<5;i++)
cout<<a[i]<<" ";
}
#endif

二维数组

#if 0
/*二维数组*/
#include<iostream>
using namespace std;
main()
{
//声明二维数组及变量
int a[2][3],i,j;

//从键盘上为数组a赋值
for (i=0;i<2;i++)
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}

//显示数组a
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<a[i][j]<<"  ";
}
cout<<endl;
}
//找出该数组的最大元素及其下标
int h,l,Max=a[0][0];
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
if (Max<a[i][j]) {
Max=a[i][j];
h=i;
l=j;
}
}
}
cout<<"Max:"<<"a["<<h<<"]["<<l<<"]="<<a[h][l]<<endl;
}
#endif

字符数组

#if 0
/*字符数组*/
#include<iostream>
using namespace std;
main()
{
//声明字符数组和变量
char str[6];
int i;

//从键盘上输入字符串
cout<<"str=";
cin>>str;
cout<<str<<endl;

//按数组和下标变量两种方式显示字符数组
cout<<str<<endl;
for (i=0;i<6;i++)
cout<<str[i];
cout<<endl;

//字符串反向输出
for (i=5;i>=0;i--)
cout<<str[i];
cout<<endl;

//将字符数组变成大写字母后输出
for (i=0;i<=5;i++)
str[i]-=32;      	//小写字母转换成大写字母
cout<<str<<endl;    	//显示字符串
}
#endif

指针基础

#if 0
/*指针基础*/
#include<iostream>
using namespace std;
main()
{
//声明变量和指针变量
int a,b,c,*ip;

//指针变量ip指向变量a
a=100;
ip=&a;        //使指针变量 ip 指向变量a
cout<<"a="<<a<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;//地址

//指针变量ip指向变量b
ip=&b;        //使指针变量 ip 指向变量b
b=200;
cout<<"b="<<b<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;

//指针变量ip指向变量c
ip=&c;        //使指针变量 ip 指向变量b
*ip=a+b;
cout<<"c="<<c<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;
}
#endif

数组指针

#if 0
/*数组指针*/
#include<iostream>
using namespace std;
main()
{
//声明数组、变量和指针变量
int a[2][3],i,j;
int* ip;

//从键盘上为数组a赋值
for (i=0;i<2;i++)  //为数组a赋值
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}

//利用下标变量显示数组a
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<a[i][j]<<"  ";
}
cout<<endl;
}

//利用指针变量显示数组a
ip=&a[0][0];
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cout<<ip<<"  ";
cout<<*ip<<endl;
ip++;
}
}
}
#endif

指针运算

#if 0
/*指针运算*/
#include<iostream>
using namespace std;
main()
{
//声明数组、变量和指针变量
int a[]={1,2,3,4,5,6};
int *ip1,*ip2;

//测试指针的赋值运算
ip1=a;
ip2=ip1;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl;

//测试指针的自增自减运算和组合运算
ip1++;
ip2+=4;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl;

//测试指针变量之间的关系运算
int n=ip2>ip1;
cout<<"ip2>ip1="<<n<<endl;
cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl;

//指针变量之间的减法
n=ip2-ip1;
cout<<"ip2-ip1="<<n<<endl;
}
#endif

数组与指针

#if 0
/*数组与指针*/
#include<iostream>
using namespace std;
main()
{
//声明字符型数组和指针变量
char str[10];
char *strip=str;

//输入输出
cout<<"str=";
cin>>str;      //用字符数组输入字符串
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
cout<<"strip=";
cin>>strip;     //用字符指针变量输入字符串
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;

//利用指针变量改变其指向字符串的内容
*(strip+2)='l';
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;

//动态为字符型指针变量分配内存
strip=new char(100);
cout<<"strip=";
cin>>strip; //用字符指针变量输入字符串
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
}
#endif

数组综合

#if 0
#include<iostream>
using namespace std;
main()
{
// 声明用于存放运动员号码的数组
int h[]={1001,1002,1003,1004};
// 声明用于存放运动员成绩的数组
float x[]={12.3,13.1,11.9,12.1};
//声明用于存放运动姓名的字符型指针数组
char *p[]={"Wang","Zhang","Li","Hua"};
//i,j,it是用做循环控制变量和临时变量
int i,j,it;
//ft 用做暂存变量
float ft;
//pt为字符型指针变量用做暂存指针变量
char *pt;

//用选择法对数组x进行排序,并相应调整数组h和p中的数据
for (i=0;i<=3;i++)
for (j=i+1;j<=3;j++)
if (x[i]>=x[j]) {
ft=x[i],x[i]=x[j],x[j]=ft;
it=h[i],h[i]=h[j],h[j]=it;
pt=p[i],p[i]=p[j],p[j]=pt;
}

//以下打印排序结果
for (i=0;i<=3;i++)
cout<<h[i]<<" ,"<<p[i]<<" ,"<<x[i]<<endl;
}
#endif

指针数组

#if 0
#include<iostream>
using namespace std;
main()
{
//声明指针数组
char *colors[]={"Red","Blue","Yellow","Green"};
//指向指针的指针变量
char **pt;

//通过指向指针的变量访问其指向的内容
pt=colors;
for (int i=0;i<=3;i++) {
cout<<"pt="<<pt<<endl;
cout<<"*pt="<<*pt<<endl;
cout<<"**pt="<<**pt<<endl;
pt++;
}
}
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: