实训C++语言设计——数组元素的排序查找
2008-02-13 14:00
225 查看
掌握了对数组元素的排序的方法,学会了对数组元素的查找的方法,还学习到了C++库实现查找和排序.
程序:
//Lab10_2.cpp
#include<iostream>
#include<algorithm>
using namespace std;
void displayArray(int a[],int n)
{ cout<<endl<<"你输入的数组是:";
for(int i=0;i<n;i++)cout<<a[i]<<" ";
cout<<endl;}
int main()
{ int n,j;
cout<<"请输入你要输入的数值的个数:";
cin>>n;
int *p;
p=new int
;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for(int i=0;i<n;i++)
{ cout<<"输入第"<<i+1<<"个数值:"<<endl;
cin>>j;
p[i]=j;}
displayArray(p,n);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int l=1;
while(l)
{ int k;
int *q;
cout<<endl<<"输入你要查找的数值:";
cin>>k;
q=find(p,p+n-1,k);
if(q!=p+n-1)
cout<<"你查找的数值"<<k<<"的位置在第"<<q-p+1<<"位!"<<endl<<endl;
else
cout<<"你查找的数值"<<k<<"不存在!"<<endl<<endl;
cout<<"是否继续查找功能?"<<endl<<"是按1/否按0"<<endl;
cin>>l;
} return 0;}
程序:
//Lab10_2.cpp
#include<iostream>
#include<algorithm>
using namespace std;
void displayArray(int a[],int n)
{ cout<<endl<<"你输入的数组是:";
for(int i=0;i<n;i++)cout<<a[i]<<" ";
cout<<endl;}
int main()
{ int n,j;
cout<<"请输入你要输入的数值的个数:";
cin>>n;
int *p;
p=new int
;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for(int i=0;i<n;i++)
{ cout<<"输入第"<<i+1<<"个数值:"<<endl;
cin>>j;
p[i]=j;}
displayArray(p,n);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int l=1;
while(l)
{ int k;
int *q;
cout<<endl<<"输入你要查找的数值:";
cin>>k;
q=find(p,p+n-1,k);
if(q!=p+n-1)
cout<<"你查找的数值"<<k<<"的位置在第"<<q-p+1<<"位!"<<endl<<endl;
else
cout<<"你查找的数值"<<k<<"不存在!"<<endl<<endl;
cout<<"是否继续查找功能?"<<endl<<"是按1/否按0"<<endl;
cin>>l;
} return 0;}
相关文章推荐
- 实训C++语言设计——BookManage图书管理系统
- 实训C++语言设计——实现复数运算
- 实训C++语言设计——Student学生类设计、继承、重写
- 实训C++语言设计——二进、八进和十六进制 表输出
- 实训C++语言设计——定义一个类
- 实训C++语言设计——BookManage图书管理系统
- 实训C++语言设计——实现复数运算
- 实训C++语言设计——Student学生类设计、继承、重写
- 实训C++语言设计——接受两个非递减的线性表,并将它们合并为非递减的Lc
- 实训C++语言设计——继承与派生
- 实训C++语言设计——Date日期系统实现
- 实训C++语言设计——多项式Polynomial运算
- 实训C++语言设计——接受两个非递减的线性表,并将它们合并为非递减的Lc
- 实训C++语言设计——继承与派生
- 实训C++语言设计——STL链表、栈类、队列
- 实训C++语言设计——Date日期系统实现
- 实训C++语言设计——多项式Polynomial运算
- Arrays 类(数组元素的查找,数组内容的填充、排序等)
- 实训C++语言设计——文件和流
- 实训C++语言设计——Clock模拟时钟系统