回 帖 发 新 帖 刷新版面

主题:求高手解惑(关于结构体内部字符串的输入与输出

typedef struct car    
{
        char carnum[10];
        char carloc[5]; 
        char intime[5];
        char outtime[5];
}carpark;

typedef struct freestack
{
        carpark freeposition;
        struct freestack *next;
}*freehead;

freehead freed=(struct freestack *)malloc(sizeof(struct freestack));

scanf("%s",(freed->freeposition).carloc);//1

char c[10];
scanf("%s",c);
strcpy((freed->freeposition).carloc,c);//2
........
printf("%s",(fred->freeposition).carloc);
采用这两种方法都无法对carloc 赋值并输出,望高手指出错误,感激不尽

回复列表 (共7个回复)

沙发

两种方法应该都是对的,你是不是少放了中间的什么问题代码

板凳


貌似不是唉,就在输入代码下直接写输出代码,执行输出代码输出的还是未知的字符串

3 楼

贴一下完整的程序吧,另外,把你的测试用例也贴上

4 楼

呵呵,麻烦了,看别人的程序是很痛苦的,因为输入输出有错误,所以还有很多地方没修改,麻烦你把输入输出做一下修改就可以了,谢谢了   

停车场管理:
设停车场是个可停放N辆车的狭长通道,且只有一个大门可供汽车进出。在停车场内,汽车按到达的先后次序,由北向南依次排列(假设大门在最南端)。若车场内已停滞N辆车,则后来的汽车须在门外的便道上等候,当有车开走时,便道上的第一辆车即可开入,当停车场内某辆车要离开时,在它之后进图的车辆必须先退出车场为它让路,待该辆车开出大门后,其它车辆再按元次序返回车场,每辆车离开车场时,应按其停留时间缴费(在便道上停车免费)
试编写程序,模拟上述管理过程。要求以顺序栈模拟停车场,以链队列模拟便道。从终端读入汽车到达或离去的数据,每组数据包括三项:是”到达“还是”离去“;汽车车牌号码;”到达“或”离去的时刻。与每组输入信息相应的输出信息为:如果是到达的车辆,则输出其在停车场中或便道上的位置;如果是离去的车辆,则输出其在停车场的时间和应缴的费用。
           


#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define maxsize 50
#define full 1
#define empty 1
#define availuable 0
#define error 0
#define ok 1
#define tarriff 15

typedef struct car    //车的信息 
{
        char carnum[10];//车牌号 
        char carloc[5];//车的位置 
        char intime[5];//车进入的时间 
        char outtime[5];//车驶出时间 
}carpark;

typedef struct carstack //车的栈道 
{
        carpark carport[maxsize];   //栈道里能停的车的最大值为maxsize 
        int top;      //最外面的车子的位置 
}*stackpoint;

typedef struct freestack
{
        carpark freeposition;//便道里能停的车的最大值为maxsize
        struct freestack *next;//涉及链表中节点的删除
}*freehead;

typedef struct tempstack          //临时车道 
{
        carpark carport[maxsize]; //临时车道里能停的车的最大值为maxsize 
        int top;
}*tempposit;                    //临时位置

    stackpoint charge=(struct carstack *)malloc(sizeof(struct carstack)); //为车道开辟空间 
    freehead freed=(struct freestack *)malloc(sizeof(struct freestack));
    tempposit temp=(struct tempstack *)malloc(sizeof(struct tempstack));
    freehead s=(struct freestack *)malloc(sizeof(struct freestack));
    

void initcarstack(stackpoint charge, freehead free)  //清空车道 
{
     charge->top=-1;
     free->next=NULL;
     //temp->top=-1;
}

int isempty(stackpoint charge)
{
    if (charge->top==-1)
    {
                        printf("The stack is empty");
                        return empty;
    }
    else 
    return 0;
}



int isfull(stackpoint charge)             //判断收费车栈道是否已满 
{
    if (charge->top==maxsize) 
    {
                              printf("The charge stack is full");
                              return full;
    }
    else 
    return availuable;
}


int findcar(char *num)
{
    int i=0;
    while(strcmp(charge->carport[i].carnum,num)!=0)
    {
                                           if(i>charge->top) 
                                           {
                                                     printf("ERROR!!This car is not found!!");
                                                     return 0;
                                           }
                                           i++;
    }
    return i;
}

void movefreecar()
{
     printf("The car %s has came in \nplease input the time now: ",freed->freeposition.carnum);
     if(freed->next!=NULL)
     {
                          charge->carport[charge->top]=freed->freeposition;
                          freed=freed->next;
     }
     scanf("%s",charge->carport[charge->top].intime);
     return;
}

int  printmess()
{
     int tag=charge->top;
     if(isempty(charge))
     return empty;
        printf("车牌号       停放位置      驶入时间       \n");
     do
     {  
           printf("%s       %s           %s\n",(charge->carport[tag]).carnum, (charge->carport[tag]).carloc, (charge->carport[tag]).intime); 
     }while (tag--);
}
 
int parkingcost(int i)   //输入车牌号计算停车费用 
{
    int cost;
    scanf("%s",(charge->carport[i]).outtime);
    cost=((charge->carport[i]).intime-(charge->carport[i]).outtime)*tarriff;
    return cost;
}


5 楼

void movestackcar(char *num)         //栈道里的车离开 
{
     int tag =findcar(num);
     int topa=charge->top;
     if(tag)
     {
            printf("The car is not found in car stack");
            return;
     }
     while (tag<topa)
     {//此处应该利用另一个栈 
           charge->carport[tag]=charge->carport[tag+1];
           tag++;
     }
     charge->top=charge->top-1;
     if(freed->next!=NULL)
     {
                          movefreecar();
                          charge->top=charge->top+1; 
     }    
     printf("The car %s has left off",num);  
}


int movefreestack(char *num)          //移动便道上的车 
{
     freehead s=freed;
     freehead p=freed;
     while(strcmp(p->freeposition.carnum,num)!=0 && p->next!=NULL);
     p=p->next;
     if(strcmp((p->next)->freeposition.carnum,num)!=0)
     {
                                   printf("The car is not recorded");
                                   return error;
     }
     s=p->next;
     p->next=p->next->next;
     free(s);
     return ok;
}
 
 
void checkin()           //登记进入车辆车号,车辆进入时间,安排车辆位置
{
     freehead p=freed;
     char c[10];
     printf("please input the car number:");
     scanf("%s",c); 
     if(isfull(charge))
     {
                       printf("The car stack is full,please park the car in the free stack\n");
                       if(freed->next==NULL)
                       {
                                            strcpy((freed->freeposition).carnum,c);
                                            printf("please input the carport location:");
                                            scanf("%s",(freed->freeposition).carloc);
                                            p=freed;
                       }
                       else 
                       {
                            s=(freehead)malloc(sizeof(struct freestack));
                            strcpy((s->freeposition).carnum,c);
                            scanf("%s",(s->freeposition).carloc);
                            p->next=s;
                            p=s;
                       }
     }
     else
     {
         printf("The car stack  is availuable\n");
         if(charge->top==-1)
         {
                            strcpy((freed->freeposition).carnum,c);
                            printf("please input the carport location:");
                            scanf("%s",(charge->carport[charge->top]).carloc);
                            printf("please input the time now:");
                            scanf("%s",(charge->carport[charge->top]).intime);
                            charge->top=charge->top+1;
         }
         else
         {
             strcpy((freed->freeposition).carnum,c);
             printf("please input the carport location:");
             scanf("%s",charge->carport[charge->top].carloc);
             printf("please input the time now:");
             scanf("%s",charge->carport[charge->top].intime);
             charge->top=charge->top+1;
         }
     }
}
                       

6 楼

void checkout()          //登记出行车辆车号,并登记出行时间 ,并调用parkigncost计算停车费用 
{
     int i;
     char che[10];
     printf("please input the car number:");
     scanf("%s",che);
     i=findcar(che); 
     movestackcar(che); 
     if(!i)
     {
           printf("Parking free!");
           return ;
     }
     printf("parking cost is %d", parkingcost(i));
       
          


int main()
{
    int flow=1;
    int choice;
    initcarstack(charge, freed);
    printf("please input your choice:\n  【1】check in    【2】check out  \n  【3】view        【4】 exit\n");
    scanf("%d",&choice);
    while (flow)                       //选择功能 
    {
          switch (choice)
          {
           case 1:       checkin();break;        //登记进入 
           case 2:       checkout();break;        //登记驶出 
           case 3:       printmess();break;        //显示当前车位信息 
           case 4:       return ok;
           }
  printf("please input your choice:\n  【1】check in    【2】check out  \n  【3】view        【4】 exit\n");
  scanf("%d",&choice);
    }
    getchar();
    getchar();
}

7 楼


呵呵,谢谢高手,我找到哪里错了,是个很低级的错误        

我来回复

您尚未登录,请登录后再回复。点此登录或注册