C++编程题

2025-02-26 02:26:46
推荐回答(2个)
回答1:

拿去交作业吧- -|||||

#include
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<<"请输入 "<for(i=0;i{
cin>>p[i];
}
cout<
for(int j=0;jfor(int k=0;kif(p[k]{int temp;
temp=p[k];p[k]=p[k+1];p[k+1]=temp;
}
cout<<"排序后的结果为:"<for(i=0;i{
cout<}
cout<
}
int main()//主函数
{char g;
int y;int z;
char h;

cout<<".............Welcome to use calculator!.........."<cout<<"....................1.int add....................."<cout<<"....................2.double add.................."<cout<<"....................3.sub ........................"<cout<<"....................4.divide ...................."<cout<<"....................5.exchange...................."<cout<<"....................6.sort ......................."<
do{
cout<<"============ Please choose the menu: ============"<cin>>h;
switch(h)
{
case '1':cout<<"请输入相加的两个整数:"<int d,f,c;
cin>>d>>f;
c=add( d, f);
cout<<"相加的和为:"<case '2':cout<<"请输入相加的两个双精度数:"<double i,j,k;
cin>>i>>j;
k=add( i, j);
cout<<"相加的和为:"<case '3':cout<<"请输入相减的两个数:(内置函数)"<int l,m,n;
cin>>m>>n;
l=sub( m,n);
cout<<"相减的差为:"<case '4':
cout<<"两个整形数据相除(345/67):"<int x;
x=divide(345,67);
cout<<"结果为:"<case '5':

cout<<"输入要交换的两个数"<cin>>y>>z;cout<cout<<"两个数交换如下:"<exchange(y,z);
cout<case '6':sort();break;
default:cout<<"输入错误,请重新输入!"<}
cout<<"是否继续操作:(是请按y---退出请按其它任意键)"<cin>>g;
}
while(g=='y'||g=='Y');
return 0;
}

回答2:

1
int add(int a,int b){return a+b;}
double add(double a,double b){return a+b;}