拿去交作业吧- -|||||
#include<iostream>
using namespace std;
int add(int a,int b)
{int c=a+b;
return c;
}
double add(double a,double b)
{double c=a+b;
return c;
}
int divide(int x=345,int y=67)
{int c;
return c=345/67;}
inline int sub(int a,int b)
{int c;
return c=a-b;
}
void exchange(int &x,int &y)
{int temp;
temp=x;x=y;y=temp;
}
void sort()
{
int size,i;
cout<<"请输入数组元素的个数:";
cin>>size;
int *p=new int[size]; //开辟一个存放数组的空间
cout<<"请输入 "<<size<<" 个元素!"<<endl; //对数组元素初始化
for(i=0;i<size;i++)
{
cin>>p[i];
}
cout<<endl;
for(int j=0;j<size-1;j++)//冒泡法进行由大到小的排列
for(int k=0;k<size-j;k++)
if(p[k]<p[k+1])
{int temp;
temp=p[k];p[k]=p[k+1];p[k+1]=temp;
}
cout<<"排序后的结果为:"<<endl;
for(i=0;i<size;i++)
{
cout<<p[i]<<" ";
}
cout<<endl;
}
int main()//主函数
{char g;
int y;int z;
char h;
cout<<".............Welcome to use calculator!.........."<<endl;
cout<<"....................1.int add....................."<<endl;
cout<<"....................2.double add.................."<<endl;
cout<<"....................3.sub ........................"<<endl;
cout<<"....................4.divide ...................."<<endl;
cout<<"....................5.exchange...................."<<endl;
cout<<"....................6.sort ......................."<<endl;
do{
cout<<"============ Please choose the menu: ============"<<endl;
cin>>h;
switch(h)
{
case '1':cout<<"请输入相加的两个整数:"<<endl;
int d,f,c;
cin>>d>>f;
c=add( d, f);
cout<<"相加的和为:"<<c<<endl;break;
case '2':cout<<"请输入相加的两个双精度数:"<<endl;
double i,j,k;
cin>>i>>j;
k=add( i, j);
cout<<"相加的和为:"<<k<<endl;break;
case '3':cout<<"请输入相减的两个数:(内置函数)"<<endl;
int l,m,n;
cin>>m>>n;
l=sub( m,n);
cout<<"相减的差为:"<<l<<endl;break;
case '4':
cout<<"两个整形数据相除(345/67):"<<endl;
int x;
x=divide(345,67);
cout<<"结果为:"<<x<<endl;break;
case '5':
cout<<"输入要交换的两个数"<<endl;
cin>>y>>z;cout<<endl;
cout<<"两个数交换如下:"<<endl;
exchange(y,z);
cout<<y<<" "<<z<<endl;break;
case '6':sort();break;
default:cout<<"输入错误,请重新输入!"<<endl;break;
}
cout<<"是否继续操作:(是请按y---退出请按其它任意键)"<<endl;
cin>>g;
}
while(g=='y'||g=='Y');
return 0;
}
温馨提示:答案为网友推荐,仅供参考