题解 | 自动售卖机

自动售卖机

https://www.nowcoder.com/practice/487953e6d3e3434988e0dd6960b6c9f8

`timescale 1ns/1ns

module sale(
   input                clk   ,
   input                rst_n ,
   input                sel   ,//sel=0,5$dranks,sel=1,10&=$drinks
   input          [1:0] din   ,//din=1,input 5$,din=2,input 10$
 
   output   reg  [1:0] drinks_out,//drinks_out=1,output 5$ drinks,drinks_out=2,output 10$ drinks
   output	reg        change_out   
);

parameter S0=0,S5=1,S10=2,S15=3;
reg [1:0]cs;
reg [1:0]ns;
always@(posedge clk or negedge rst_n )begin
    if(!rst_n)
        cs<=0;
    else
        cs<=ns;
end

always@(*)begin
    if(sel==0)begin
        case(cs)
        S0:begin
            if(din==0)
                ns=S0;
            else if(din==1)
                ns=S5;
            else if(din==2)
                ns=S10;
            else
                ns=S0;
        end

        S5:begin
            if(din==0)
                ns=S0;
            else if(din==1)
                ns=S5;
            else if(din==2)
                ns=S10;
            else
                ns=S0;
        end
        S10:begin
            if(din==0)
                ns=S0;
            else if(din==1)
                ns=S5;
            else if(din==2)
                ns=S10;
            else
                ns=S0;
        end
        default:ns=S0;
        endcase
    end

    else begin
        case(cs)
        S0:begin
            if(din==0)
                ns=S0;
            else if(din==1)
                ns=S5;
            else if(din==2)
                ns=S10;
            else
                ns=S0;
        end
        S5:begin
            if(din==0)
                ns=S5;
            else if(din==1)
                ns=S10;
            else if(din==2)
                ns=S15;
            else
                ns=S5;
        end

        S10:begin
            if(din==0)
                ns=S0;
            else if(din==1)
                ns=S5;
            else if(din==2)
                ns=S10;
            else
                ns=S0;
        end

        S15:begin
            if(din==0)
                ns=S0;
            else if(din==1)
                ns=S5;
            else if(din==2)
                ns=S10;
            else
                ns=S0;
        end     
        default:ns=S0;
        endcase
    end
end

always@(posedge clk or negedge rst_n)begin
    if(!rst_n)
        drinks_out<=0;
    else if(sel==0)begin
        if(ns==S5||ns==S10)
            drinks_out<=1;
        else
            drinks_out<=0;
    end
    else begin
        if(ns==S10||ns==S15)
            drinks_out<=2;
        else
            drinks_out<=0;
    end
end

always@(posedge clk or negedge rst_n)begin
    if(!rst_n)
        change_out<=0;
    else if(sel==0)begin
        if(ns==S10)
            change_out<=1;
        else
            change_out<=0;
    end
    else begin
        if(ns==S15)
            change_out<=1;
        else
            change_out<=0;
    end
end

endmodule

这题隐含了两个东西,第一个是在卖完以后不会到初始状态,会接着检测输入。第二个在买饮料2的时候,貌似当钱不够的是时候会一直等待够为止,不会吞钱重新算。

全部评论
和自动贩卖机2不一样,自动贩卖机2那道题不是连续输入的,买完一回得回到初始状态重新检测
点赞 回复 分享
发布于 04-11 23:50 黑龙江

相关推荐

码农索隆:单休一个月少休息4天,一年就是48天,平时节假日,别人3天假期,单休的两天
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 12:05
俺不中了,BOSS遇到了一个hr,我觉得我咨询的问题都很正常吧,然后直接就被拒绝了???
恶龙战士:你问的太多了,要不就整理成一段话直接问他,一个一个问不太好
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务