自动更新数据文件和显示? [英] Automatic update data file and display?

查看:120
本文介绍了自动更新数据文件和显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发一个系统,这个应用程序允许用户预定机票座位。我想实现自动系统(功能),其中应用程序可以供用户选择最好的座位。

I am trying to develop a system where this application allows user to book ticket seat. I am trying to implement an automatic system(a function) where the app can choose the best seats for the user.

我当前的数据库(seats.txt)文件存储如果一个很好的格式为:

My current database(seats.txt) file store in this way(not sure if its a good format:

X000X
00000
0XXX0

其中X是指座椅被占用,0表示没有。

where X means the seat is occupied, 0 means nothing.

用户登录到我的系统后,并选择选择最适合你的,用户会提示输入他/她想要多少个座位(我做了这部分),现在,如果用户输入:2,我将检查从第一行,看看是否有任何空座位,如果是,那么我分配(这是一个简单的办法,一旦我得到这个工作,我会写一个更好的自动预订算法)

After user login to my system, and choose the "Choose best for you", the user will be prompt to enter how many seats he/she want (I have done this part), now, if user enter: 2, I will check from first row, see if there is any empty seats, if yes, then I assign(this is a simple way, once I get this work, I will write a better "automatic-booking" algorithm)

我尝试使用sed,awk中,grep来打..但它只是不能工作(我是新来bash编程,刚开始学习的bash 5天前)。

I try to play with sed, awk, grep.. but it just cant work (I am new to bash programming, just learning bash 3 days ago).

任何人都可以帮忙吗?

FYI:本seats.txt格式并不一定是这样的。它也可以是,所有座椅,存放在1排,如:X000X0XXX00XXX

FYI: The seats.txt format doesn't have to be that way. It can also be, store all seats in 1 row, like: X000X0XXX00XXX

感谢=)

推荐答案

下面的东西,让你开始。如果它采取或空,跟踪的行和列的数量,从您的文件,并显示每个座位该脚本读取所有的时间。

Here's something to get you started. This script reads in each seat from your file and displays if it's taken or empty, keeping track of the row and column number all the while.

#!/bin/bash

let ROW=1
let COL=1

# Read one character at a time into the variable $SEAT.
while read -n 1 SEAT; do
    # Check if $SEAT is an X, 0, or other.
    case "$SEAT" in
        # Taken.
        X)  echo "Row $ROW, col $COL is taken"
            let COL++
            ;;

        # Empty.
        0) 
            echo "Row $ROW, col $COL is EMPTY"
            let COL++
            ;;

        # Must be a new line ('\n').
        *)  let ROW++
            let COL=1
            ;;
    esac
done < seats.txt

注意我们在 seats.txt 在脚本的末尾喂,不是开头。这很奇怪,但是这是UNIX雅。奇怪的是,整个while循环的行为就像一个大的命令:

Notice that we feed in seats.txt at the end of the script, not at the beginning. It's weird, but that's UNIX for ya. Curiously, the entire while loop behaves like one big command:

while read -n 1 SEAT; do {stuff}; done < seats.txt

&LT; 末在 seats.txt 供稿循环作为一个整体,并具体在命令。

The < at the end feeds in seats.txt to the loop as a whole, and specifically to the read command.

这篇关于自动更新数据文件和显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆