前往
大廳
主題

C/C++紀錄extra.6<<合併總複習:氣泡+字串陣列+指標+迴圈+switch>> 2018/9/26

艾倫D索妮雅 | 2021-06-08 19:52:18 | 巴幣 0 | 人氣 122

#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
char *name[5]={"老李","王董","發叔","龍哥","小陳"};
char *cpy[5];
int age[5]={56,45,51,48,35};
int i,j,temp,key;
cout<<"1.由小到大排序     2.由大到小排序"<<endl;
while(1)
{
cout<<"請輸入選項:";
cin>>key;
switch(key)
{
case 1:
cout<<"由小排到大排序後:"<<endl;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(age[j]>age[i])
{
temp=age[i];
age[i]=age[j];
age[j]=temp;
cpy[j]=*(name+i);
name[i]=*(name+j);
name[j]=*(cpy+j);
}
}
}
for(i=0;i<5;i++)
{
cout<<name[i]<<":"<<age[i]<<"   ";
}
cout<<endl<<endl;
break;
case 2:
cout<<"由大排到小排序後:"<<endl;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(age[j]<age[i])
{
temp=age[i];
age[i]=age[j];
age[j]=temp;
cpy[j]=*(name+i);
name[i]=*(name+j);
name[j]=*(cpy+j);
}
}
}
for(i=0;i<5;i++)
{
cout<<name[i]<<":"<<age[i]<<"   ";
}
cout<<endl<<endl;
break;
default:
cout<<"不存在這個指令:error!!"<<endl<<endl;
}
}
return 0;
}

創作回應

更多創作