帮我把c语言程序改为c++

2024-11-28 15:08:25
推荐回答(4个)
回答(1):

#include
#include

void main()
{
int *a,*b,*c;
int an,bn,i,j,k;
std::cout << "请输入数组a的元素个数:" << std::endl;
std::cin >> an;
a=new int[an];
std::cout << "输入数组a的各个元素:" << std::endl;
for(i=0;i std::cin >> a[i];
std::cout << "输入数组b的元素个数:" << std::endl;
std::cin >> bn;
b=new int[bn];
std::cout << "输入数组b的各个元素:" << std::endl;
for(i=0;i std::cin >> b[i];
std::sort(a,a+an);
std::sort(b,b+bn);
c=new int[an+bn];
i=j=k=0;
while(j if(a[j] else c[i++]=b[k++];
while(j while(k for(i=0;i std::cout << c[i] << " ";
std::cout << std::endl;
delete []a;
delete []b;
delete []c;
}

回答(2):

改好了:

#include
using namespace std;
void sort(int *a,int n)
{
int i,j,t;
for(i=0;i for(j=0;j if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
void main()
{
int *a,*b,*c;
int an,bn,i,j,k;
cout<<"输入数组a的元素个数:\n";
cin>>an;
a=new int[an];
cout<<"输入数组a的各个元素:\n";
for(i=0;i cin>>a[i];
cout<<"输入数组b的元素个数:\n";
cin>>bn;
b=new int[bn];
cout<<"输入数组b的各个元素:\n";
for(i=0;i cin>>b[i];
sort(a,an);
sort(b,bn);
c=new int[an+bn];
i=j=k=0;
while(j if(a[j] else c[i++]=b[k++];
while(j while(k for(i=0;i cout< cout<<"\n";
delete []a;
delete []b;
delete []c;
}

回答(3):

#include
using namespace std;
void sort(int *a,int n)
{
int i,j,t;
for(i=0;i for(j=0;j if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
void main()
{
int *a,*b,*c;
int an,bn,i,j,k;
cout<<"输入数组a的元素个数:\n";
cin>>an;
a=new int[an];
cout<<"输入数组a的各个元素:\n";
for(i=0;i cin>>a[i];
cout<<"输入数组b的元素个数:\n";
cin>>bn;
b=new int[bn];
cout<<"输入数组b的各个元素:\n";
for(i=0;i cin>>b[i];
sort(a,an);
sort(b,bn);
c=new int[an+bn];
i=j=k=0;
while(j if(a[j] else c[i++]=b[k++];
while(j while(k for(i=0;i cout< cout< delete []a;
delete []b;
delete []c;
}

回答(4):

哇,你这即有C,又有C++;你的内存分配与管理就是用的C++。
如果是改的话,你只需要改关键字scanf,printf分别用cin,cout替换,其他的不变。至于怎么改,你自己去看一下相关的介绍。