使用语法
1.数组嵌套
2.switch
3.for
4.函数
5.冒泡排序
6.string.h的函数功能
代码如下
#include <stdio.h>
#define max 6
#include<string.h>
int i,j,temp;
int count=5;
int choice;
int searchIndex=-1;
char emperorName[50];
char tempName[20];
char names[max][20]={"貂蝉","杨玉环","不知火舞","西施","蔡文姬"};
char levelName[5][10]={"贵人","嫔妃","贵妃","皇贵妃","皇后"};
int levels[max]={1,2,0,0,4,-1};
int loves[max] ={100,100,100,100,100,-1};
void show()
{
printf("%-12s级别\t好感度\n","姓名");
printf("------------------------------\n");
for(i=0;i<count;i++)
{
printf("%-12s%s\t%d\n",names[i],levelName[levels[i]],loves[i]);
}
printf("------------------------------\n");
}
void bubble_sort()
{
for(i=0;i<count-1;i++)
for(j=0;j<count-i-1;j++)
{
if( levels[j]<levels[j+1] )
{
temp = levels[j];
levels[j] = levels[j+1];
levels[j+1]=temp;
temp=loves[j];
loves[j] = loves[j+1];
loves[j+1]=temp;
strcpy(tempName,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],tempName);
}
}
}
void select(int )
{
switch(choice)
{
case 1:
if(count<max)
{
printf("请输入娘娘名讳:");
scanf("%s",names[count]);
levels[count]=0;
loves [count]=100;
count++;
}
else
{
printf("人满为患了!\n 添加失败");
}
break;
case 2:
printf("陛下,请输入今天翻牌的娘娘:");
scanf("%s",tempName);
for(i=0;i<count;i++)
{
if( strcmp(tempName,names[i])==0 )
{
loves[i] +=10;
levels[i]=(levels[i]>=4 ? 4: levels[i]+1 );
}
else
{
loves[i] -=10;
}
if( (i==count-1) && strcmp(tempName,names[i]) != 0 )
{
printf("后宫没有这位%s娘娘!\n",tempName);
for(i=0;i<count;i++)
loves[i] +=10;
break;
}
}
break;
case 3:
printf("请输入要打入冷宫的娘娘:");
scanf("%s",tempName);
for(i=0;i<count;i++)
{
if(strcmp(tempName,names[i])==0)
{
searchIndex = i;
break;
}
}
if(-1==searchIndex)
{
printf("查无此人!");
}
else
{
for(i=searchIndex;i<count;i++)
{
strcpy( names[i],names[i+1] );
loves[i]=loves[i+1];
levels[i]=levels[i+1];
}
count--;
}
break;
case 4:
printf("陛下,请输入单独召见的娘娘:");
scanf("%s",tempName);
for(i=0;i<count;i++)
{
if(strcmp(tempName,names[i])==0)
{
loves[i] += 20;
levels[i]=(levels[i]>=4 ? 4: levels[i]+1 );
}
if( (i==count-1) && strcmp(tempName,names[i]) != 0 )
{
printf("后宫没有这位%s娘娘!\n",tempName);
break;
}
}
break;
default:
printf("君无戏言,陛下请重新选\n");
}
}
int main ()
{
show();
printf("请输入皇帝名号:");
scanf("%s",emperorName);
printf("%s皇帝陛下,万岁万岁!\n",emperorName);
printf("1.皇帝下旨选妃:\t\t(增加功能)\n");
printf("2.翻牌宠幸:\t\t(修改状态功能)\n");
printf("3.打入冷宫!\t\t(删除功能)\n");
printf("4.单独照见爱妃\n");
printf("陛下请选择:");
scanf("%d",&choice);
select(choice);
bubble_sort();
show();
return 0;
}