C 从命令行读取和连接房间
首先,这是我的输入文件C 从命令行读取和连接房间,c,struct,C,Struct,首先,这是我的输入文件 Hall Study Cellar Kitchen * Study Hall Garden * Cellar Hall * Kitchen Hall Garden * Garden Study Kitchen * *后面的第一行是一个房间,其余的是该房间的门。我的输出应该是这样的: Hall 1. Study 2. Cellar 3. Kitchen enter door number> 3 Kitchen
Hall
Study
Cellar
Kitchen
*
Study
Hall
Garden
*
Cellar
Hall
*
Kitchen
Hall
Garden
*
Garden
Study
Kitchen
*
*后面的第一行是一个房间,其余的是该房间的门。我的输出应该是这样的:
Hall
1. Study
2. Cellar
3. Kitchen
enter door number> 3
Kitchen
1. Hall
2. Garden
enter door number>
但我得到的只是:
Hall
1. Study
2. Cellar
3. Kitchen
4. *
Study
1. Hall
2. Garden
3. *
Cellar
1. Hall
2. *
Kitchen
1. Hall
2. Garden
3. *
Garden
1. Study
2. Kitchen
3. *
gets here showrooms
gets here showroom
room name: Hall
0 Hall
Segmentation fault
很可能是connect()函数崩溃了,因为我只从showRooms中得到最后3行,而它应该打印所有内容。
这是我的节目:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
#define BMAX 100
struct room * rooms[MAX];
int rp; // room count
// struct room - name, array of up to 4 doors, number of doors
struct room {char * name; struct door * doors[4]; int dp;};
// struct door - name for the room it connects to, & a pointer to that room
struct door {char * name; struct room * room;};
struct door * newDoor(char * name){
struct door * d; // pointer d to the address of door
d = (struct door *) malloc(sizeof(struct door));
d->name = name; // name of new door is name
d->room = NULL; // NULL room pointer
return d;
};
struct room * newRoom(char * name){
int i = 0;
struct room * r; // pointer r to the address of room
printf("%s\n",name);
//printf("New room is %s\n",name);
r = (struct room *) malloc(sizeof(struct room));
r->name = name; // name of new room is name
for (i = 0; i < 4; i++){ // sets all doors to null for now
r->doors[i] = NULL;
}
r->dp = 0; // no doors
return r;
};
showRoom(struct room * r){
printf("gets here showroom\n");
int i;
printf("room name: %s\n", r->name);
for (i = 0; i < (r->dp); r++){
printf("%d %s\n", i,r->doors[i]->name);
}
}
showRooms(){
printf("gets here showrooms\n");
int i;
for (i = 0; i < rp; i++){
showRoom(rooms[i]);
}
}
char * readLine(FILE * fin){
char buffer[BMAX];
int i,j;
char ch;
char * l;
i = 0;
ch = getc(fin);
if (ch == EOF)
return NULL;
while (ch!='\n' && i < (BMAX -1)){
buffer[i] = ch;
i++;
ch = getc(fin);
}
if (ch != '\n')
while (ch != '\n')
ch = getc(fin);
buffer[i] = '\0';
l = malloc((i+1) * sizeof(char));
for (j = 0; j <= i; j++)
l[j] = buffer[j];
l[j] = '\0';
return l;
}
readRooms(FILE * fin){
char * l;
rp = 0;
while((l = readLine(fin)) != NULL)
{
if(rp > MAX)
{
printf("it's too many rooms\n");
exit(0);
}
rooms[rp] = newRoom(l);
printf("", l);
while(strncmp(l,"*")!=0)
{
if((rooms[rp] -> dp) > 4){
printf("it's too many doors\n");
exit(0);
}
rooms[rp] -> doors[rooms[rp] -> dp] = newDoor(l);
rooms[rp] -> dp = (rooms[rp] -> dp) + 1;
l = readLine(fin);
printf("%d. %s\n", rooms[rp] -> dp, l);
}
rp++;
}
}
connect()
{
printf("gets here connect\n");
int i,j,k;
for(i = 0; i < rp; i++)
for(j = 0; j < (rooms[i]->dp); j++)
{ for(k = 0; k < rp; k++)
if(strcmp(rooms[k]->name,rooms[i]->doors[j]->name) == 0)
{ rooms[i]->doors[j]->room = rooms[k];
break;
}
if(k == rp)
{ printf("can't find %s\n",rooms[i]->doors[j]->name);
exit(0);
}
}
}
int main(int argc,char ** argv){
FILE * fin;
struct room * r; // current room
// struct door * d;
int d;
if((fin=fopen(argv[1],"r"))==NULL)
{ printf("cannot open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
//printf("11"); fflush(stdout);
readRooms(fin);
fclose(fin);
showRooms();
connect();
r = rooms[0];
while(1)
{ showRoom(r);
printf("enter door number> ");
scanf("%d",&d);
printf("%d",r->dp);
if(d >= (r->dp))
printf("bad door number\n");
else
r = r->doors[d]->room;
}
return EXIT_SUCCESS;
}
#包括
#包括
#定义最大值10
#定义bmax100
结构房间*房间[最大值];
int rp;//房间数
//结构房间-名称,最多4扇门的阵列,门数
结构房间{char*name;结构门*doors[4];int-dp;};
//struct door—它所连接的房间的名称,指向该房间的指针(&a)
结构门{char*name;结构房间*room;};
结构门*新门(字符*名称){
struct door*d;//指向门地址的指针d
d=(结构门*)malloc(结构门大小);
d->name=name;//新门的名称为name
d->room=NULL;//空房间指针
返回d;
};
结构房间*新房间(字符*名称){
int i=0;
struct room*r;//指向room地址的指针r
printf(“%s\n”,名称);
//printf(“新房间是%s\n”,名称);
r=(结构房间*)malloc(结构房间大小);
r->name=name;//新房间的名称为name
对于(i=0;i<4;i++){//现在将所有门设置为空
r->doors[i]=NULL;
}
r->dp=0;//无门
返回r;
};
陈列室(结构室*r){
printf(“gets here showroom\n”);
int i;
printf(“房间名称:%s\n”,r->name);
对于(i=0;i<(r->dp);r++){
printf(“%d%s\n”,i,r->doors[i]->name);
}
}
陈列室(){
printf(“到达此处展示室\n”);
int i;
对于(i=0;i
这里有几个问题
首先,您的reading函数找不到正确的星号,因为您使用的strncmp
参数数量错误,并且没有使用
中的原型。然后,您应该首先读取循环外的字符串,然后作为在循环中执行的最后一项操作:
void readRooms(FILE * fin)
{
char *l;
rp = 0;
while ((l = readLine(fin)) != NULL) {
if (rp > MAX) {
printf("it's too many rooms\n");
exit(0);
}
rooms[rp] = newRoom(l);
l = readLine(fin);
while(l && strcmp(l, "*") != 0) {
if ((rooms[rp]->dp) > 4) {
printf("it's too many doors\n");
exit(0);
}
rooms[rp]->doors[rooms[rp]->dp] = newDoor(l);
rooms[rp]->dp = (rooms[rp]->dp) + 1;
printf("%d. %s\n", rooms[rp]->dp, l);
l = readLine(fin);
}
rp++;
}
}
注意while
条件中的l
附加检查,因为getline在找到文件结尾时可能返回NULL
。还请注意,readLine
的实现每次都会泄漏内存,因为您分配了内存,但从未释放内存。您还可以在分配的内存之外进行写操作,因为j
在循环之后将是i+1
——您已经循环到了“j name”);
对于(i=0;i<(r->dp);i++){
printf(“%d%s\n”,i,r->doors[i]->name);
}
}
这至少可以让你在房间里走动。但是,您的代码有更多问题,尤其是内存管理。请将未显式返回值的函数标记为
void
。和#include
用于strcmp
。您应该“#include”并以高警告级别编译。然后您将看到对strncmp的调用不正确。请正确设置代码格式。现在很难阅读。很好,我没有看到r++是最主要的东西,哈哈。谢谢!
void showRoom(struct room * r)
{
int i;
printf("room name: %s\n", r->name);
for (i = 0; i < (r->dp); i++) {
printf("%d %s\n", i, r->doors[i]->name);
}
}