|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PLAYERS 100
#define MAX_NAME 50
#define MAX_IDENTITY 20
#define MAX_RANK 15
enum Option
{
EXIT,
IMP,
SAVE,
DISP,
SEARCH,
ADD,
DEL,
FIX,
COUNT,
SORT
};
struct Info
{
char id;
char name[MAX_NAME];
char identity[MAX_IDENTITY];
char rank[MAX_RANK];
float rating;
};
struct PlayerInfo
{
struct Info data[MAX_PLAYERS];
int size;
};
void welcome();
void InitPlayerInfo(struct PlayerInfo* ps);
void AddPlayerInfo(struct PlayerInfo* ps);
void DisplayPlayerInfo(struct PlayerInfo* ps);
void DeletePlayerInfo(struct PlayerInfo* ps);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main()
{
int choice = 0;
struct PlayerInfo player;//包含100个元素的数组和size
InitPlayerInfo(&player);
do
{
welcome();
printf("请选择(数字):");
scanf("%d", &choice);
switch(choice)
{
case IMP:
break;
case SAVE:
break;
case DISP:
DisplayPlayerInfo(&player);
break;
case SEARCH:
break;
case ADD:
AddPlayerInfo(&player);
break;
case DEL:
DeletePlayerInfo(&player);
break;
case FIX:
break;
case COUNT:
break;
case SORT:
break;
case EXIT:
printf("退出棋手信息表\n") ;
break;
default:
printf("选择错误\n");
break;
}
}
while(choice);
return 0;
}
void welcome()
{
printf("***************欢迎使用围棋棋手管理系统*****************\n");
printf("\n");
printf("1. 从本地文件导入棋手信息\n");
printf("2. 保存棋手信息至本地文件\n");
printf("3. 显示棋手信息\n");
printf("4. 查询棋手信息\n");
printf("5. 增加棋手信息\n");
printf("6. 删除棋手信息\n");
printf("7. 修改棋手信息\n");
printf("8. 统计棋手信息\n");
printf("9. 对棋手信息进行排序\n");
printf("10. 退出系统\n");
printf("\n");
printf("***********************感谢使用*************************\n");
printf("\n");
printf("请输入对应的功能键(数字):\n");
}
void InitPlayerInfo(struct PlayerInfo* ps)
{
memset(ps->data, 0, sizeof(ps->data));
ps->size = 0;
}
void DisplayPlayerInfo(struct PlayerInfo* ps)
{
if(ps->size = 0)
{
printf("信息为空\n");
}
else
{
int i = 0;
printf("%20s\t%20s\t%10s\t%12s\t%20s\n", "编号","姓名","身份","段位","等级分\n");
for(i = 0; i < ps->size; i++)
{
printf("%20s\t%20s\t%10s\t%12s\t%20f\n",
ps->data[i].id,
ps->data[i].name,
ps->data[i].identity,
ps->data[i].rank,
ps->data[i].rating);
}
}
}
void AddPlayerInfo(struct PlayerInfo* ps)
{
if(ps->size == MAX_PLAYERS)
{
printf("信息已满,无法添加\n");
}
else
{
printf("请输入棋手编号(大写英文字母 P 加 3 位正整数):");
scanf("%s", ps->data[ps->size].id));
printf("请输入棋手姓名:");
scanf("%s", ps->data[ps->size].name);
printf("请输入棋手身份(职业/业余):");
scanf("%s", ps->data[ps->size].identity);
printf("请输入棋手段位:");
scanf("%s", ps->data[ps->size].rank);
printf("请输入棋手等级分:");
scanf("%20f", &(ps->data[ps->size]));
ps->size++;
printf("添加成功!");
}
}
为什么在实现display的时候运行之后不打印我已经添加的信息呢
在实现display函数中存在几个问题导致不会打印已经添加的信息。
首先,在结构体Info中,id、name、identity和rank的类型应该是字符数组(char数组)而不是单个字符。修改代码如下: struct Info{ char id[MAX_IDENTITY]; char name[MAX_NAME]; char identity[MAX_IDENTITY]; char rank[MAX_RANK]; float rating;};
其次,在DisplayPlayerInfo函数中,判断ps->size是否为0时,应使用双等号(==)而不是赋值号(=)。修改代码如下: void DisplayPlayerInfo(struct PlayerInfo* ps){ if(ps->size == 0) { printf("信息为空\n"); } else { int i = 0; printf("%20s\t%20s\t%10s\t%12s\t%20s\n", "编号","姓名","身份","段位","等级分\n"); for(i = 0; i < ps->size; i++) { printf("%20s\t%20s\t%10s\t%12s\t%20f\n", ps->data[i].id, ps->data[i].name, ps->data[i].identity, ps->data[i].rank, ps->data[i].rating); } }}
另外,对于输入字符串的scanf函数,需要使用%s格式,并且需要传入对应的字符数组名字,而不是整个结构体的名字。修改代码如下: printf("请输入棋手编号(大写英文字母 P 加 3 位正整数):");scanf("%s", ps->data[i].id);printf("请输入棋手姓名:");scanf("%s", ps->data[i].name);printf("请输入棋手身份(职业/业余):");scanf("%s", ps->data[i].identity);printf("请输入棋手段位:");scanf("%s", ps->data[i].rank);printf("请输入棋手等级分:");scanf("%f", &(ps->data[i].rating));
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|