如何交换对象的数组? [英] How do I swap array of the object?
问题描述
#include< iostream>
#include< cstdlib>
#include< fstream>
using namespace std;
int Qclothes,Qshoes,Qtoiletries,Qgadget,temp;
class MySuitcase
{
public:
int quantity; //数据成员声明
char颜色;
双倍重量;
char sizeA; //衣服的大小s,m,l,xl
int size2; //鞋子大小(4-10)
int i,j;
MySuitcase()//没有参数的构造函数
{
char white,s;
quantity = 0;
weight = 0;
color = white;
sizeA = s;
size2 = 4;
}
double total_weight(双v,双w,双x,双y,双z)
{
double total_weight;
total_weight = v + w + x + y + z; //用于计算物品总重量的功能
返回total_weight;
}
};
void printArray(int arr [],int size){
for(int p = 0; p< size; p ++){
cout<< arr [p]<< '';
}
cout<<结束;
}
void swap_item(int MySuitcase [],int& i,int& j)
{int temp = 0;
temp = MySuitcase [i];
MySuitcase [i] = MySuitcase [j];
MySuitcase [j] = temp;
}
int main()//主要功能
{
MySuitcase [4] = {Qtoiletries,Qclothes,Qshoes,Qgadget};
int a,b,c,d,press; //数量
双v,w,x,y,z,t;
int printArray; //重量
cout<<请输入您要带入行李箱的物品:<< endl;
cout<<\ n;
cout<<=========盥洗用品=========<< endl; //对象1
cout<<洗漱用品的数量:<<\t; //属性
cin>> a;
cout<<您的数量:<<\t<< a<< ; endl;
cout<<\ n;
cout<<你的洗漱用品的重量是多少(公斤):<< ;\t;
cin>> v;
cout<<你的体重:<<\t<< ; v<<kg<< endl;
cout<<\ n;
cout< <=======礼服=======:<< endl; //对象2
cout<<正式服装的数量:<<\t; //属性
cin>> b;
cout<<\ n;
cout<<什么是正式服装的重量(公斤):<<\t;
cin>> w;
cout<<你的重量:<<\t<<<<<kg<< endl;
cout<< \ n;
cout<<=======小工具=======<< endl; //对象3
cout<<你带多少小工具:<<\t; //属性
cin>> c;
cout<<您的小工具数量为:<<\t<< c< ;< endl;
cout<<\ n;
cout<<你的小工具重量是多少(公斤):<< ;\t;
cin>> x;
cout<<你的体重:<<\ t<< ; x<<kg<< endl;
cout<<\ n;
cout<<========鞋子=========<< endl; //对象4
cout<<鞋子的数量:<<\t; //属性
cin>> d;
cout<<您的数量:<<\t<< d<< ; endl;
cout<<\ n;
cout<<鞋子的重量是多少(公斤):<< ;\t;
cin>> y;
cout<<你的体重:<<\t<< ; y<<kg<< endl;
cout<<\ n;
cout<<行李箱中的总重量为:<<\t<< MySuitcase()。total_weight(v,w,x,y,z)< ;<kg<< endl;
//所有商品的总重量
cout<<\ n;
if(MySuitcase()。total_weight(v,w,x,y,z)< = 30)// if-else语句(选择结构)
{//说明是否用户超过30公斤
cout<<你没有超过行李箱的最高限额<< endl;
}
其他
{
cout<<你的行李箱超过了最大重量!<< endl;
}
cout<< 请输入你要交换的东西:;
cout<< 原始数组是:\t;
printArray(arr,4);
swap_item(arr,0,1) ;
cout<< 交换的数组是:\ t;
printArray(arr,4);
system(PAUSE);
返回0;
}
#include <iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
int Qclothes,Qshoes,Qtoiletries,Qgadget,temp;
class MySuitcase
{
public:
int quantity; //declaration of data members
char colour;
double weight;
char sizeA; //size for clothes s,m,l,xl
int size2; //size for shoes (4-10)
int i,j;
MySuitcase() //constructor with no argument
{
char white,s;
quantity=0;
weight = 0;
colour=white;
sizeA=s;
size2=4;
}
double total_weight(double v,double w,double x,double y,double z)
{
double total_weight;
total_weight=v+w+x+y+z; //function to calculate the total weight of items
return total_weight;
}
};
void printArray(int arr[], int size) {
for ( int p = 0; p < size; p++ ) {
cout << arr[p] << ' ';
}
cout << endl;
}
void swap_item(int MySuitcase[],int &i, int &j)
{ int temp = 0;
temp = MySuitcase[i] ;
MySuitcase[i] = MySuitcase[j];
MySuitcase[j] = temp;
}
int main() //main function
{
MySuitcase[4]= {Qtoiletries,Qclothes,Qshoes,Qgadget};
int a,b,c,d,press; //quantity
double v,w,x,y,z,t;
int printArray;//weight
cout<<"Please enter the things you want to bring in suitcase:" <<endl;
cout<<"\n";
cout<<"=========Toiletries========="<<endl; //object 1
cout<<"Quantity of your toiletries: "<<"\t"; //properties
cin>>a;
cout<<"Your quantity:"<<"\t"<<a<<endl;
cout<<"\n";
cout<<"What is the weight of your toiletries(in kg):"<<"\t";
cin>>v;
cout<<"Your weight:"<<"\t"<<v<<"kg"<<endl;
cout<<"\n";
cout<<"=======Formal clothes=======:"<<endl; //object 2
cout<<"Quantity of your formal clothes: "<<"\t"; //properties
cin>>b;
cout<<"\n";
cout<<"What is the weight of your formal clothes(in kg) :"<<"\t";
cin>>w;
cout<<"Your weight:"<<"\t"<<w<<"kg"<<endl;
cout<<"\n";
cout<<"=======Gadgets======="<<endl; //object 3
cout<<"How many gadget you bring: "<<"\t"; //properties
cin>>c;
cout<<"Your gadget quantity is:"<<"\t"<<c<<endl;
cout<<"\n";
cout<<"What is your gadget weight(in kg):"<<"\t";
cin>>x;
cout<<"Your weight:"<<"\t"<<x<<"kg"<<endl;
cout<<"\n";
cout<<"========Shoes========="<<endl; //object 4
cout<<"Quantity of your shoes: "<<"\t"; //properties
cin>>d;
cout<<"Your quantity:"<<"\t"<<d<<endl;
cout<<"\n";
cout<<"What is the weight of your shoes (in kg):"<<"\t";
cin>>y;
cout<<"Your weight:"<<"\t"<<y<<"kg"<<endl;
cout<<"\n";
cout<<"Your total weight in your suitcase is:"<<"\t"<<MySuitcase().total_weight(v, w, x, y, z)<<"kg"<<endl;
//total weight of all items
cout<<"\n";
if(MySuitcase().total_weight(v, w, x, y, z)<=30) //if-else statement(selection structure)
{ //to state whether user have exceed 30kg or not
cout<<"You have not exceed the maximum limit of your suitcase"<<endl;
}
else
{
cout<<"Your suitcase have exceeded the maximum weight!"<<endl;
}
cout << " Please input the things that you want to swap: ";
cout << "The original array is:\t";
printArray(arr, 4);
swap_item(arr, 0, 1);
cout << "The swapped array is:\t";
printArray(arr, 4);
system("PAUSE");
return 0;
}
推荐答案
这篇关于如何交换对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!