C++基础实例(2)
来源:互联网 发布:科脉软件 编辑:程序博客网 时间:2024/06/05 15:21
一维数组
#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 5main(){ //声明变量 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 intlow,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
0 0
- C语言基础实例
- C语言基础实例
- c基础实例
- 【C#】基础实例演练
- opencv基础实例(C++)
- mysql基础:mysql与C结合实例
- mysql基础:mysql与C结合实例
- C++基础实例(2)
- C语言实例*2
- 第25周-window程序设计(基础篇)-第2章-实例ScreenSize.c
- 用C开发PHP扩展 实例(基础版)
- Objective-C基础之实例变量的默认值
- 链表基础操作+实例[C语言实现]
- C语言基础—小实例:三天打渔两天晒网
- Linux Socket 基础实例 (C语言版)-转
- c基础(2)
- c基础2
- c语言基础 2
- 学生化残差
- React Native 六:使用源码构建React Native
- 拿来主义太严重
- sprignmvc 中使用zyUpload 上传图片(批量)
- 数据结构之排序:堆排序
- C++基础实例(2)
- 关于sort()的疑问?
- scrollView 实现图片轮播
- cocoa pods的安装
- 利用volley去请求扇贝API获取个人信息
- hdu 1087 Super Jumping! Jumping! Jumping!
- 使用javascript和ajax中的不细心!!!
- ActiveMQ实战(二)-代码示例
- leetcode 344