使用MATLAB(低级I / O)进行库存重新库存

时间:2014-10-15 14:24:30

标签: matlab if-statement file-io while-loop low-level-io

那时I / O让我很困惑。在这种情况下,我的目标是编写一个函数,该函数接收库存项目的.txt文件和另一个.txt文件的价格,并输出重新存储项目的总成本,以及具有原始库存的.txt文件数据和需要补货的物品数量。例如:

Inventory Data:

Buns #50 #63
Burgers #32 #55
Ketchup #6 #10
Hotdogs #4 #35
Mustard #7 #8

注意:标题包含在.txt文件中,并且总会有五个要重新加载的项目

我的输出文件应为:

Inventory Data:

Buns #50 #63
Burgers #32 #55
Ketchup #6 #10
Hotdogs #4 #35
Mustard #7 #8


Restocking Data:

Buns #63 #13
Burgers #55 #23
Ketchup #10 #4
Hotdogs #35 #31
Mustard #8 #1

我的输出总数应为271,因为

Prices:

Buns $2
Burgers $5
Ketchup $1
Hotdogs $4
Mustard $2

Amro非常友好地帮我弄清楚如何拉出我需要的线条,以及如何从我所拥有的东西中减去我的需要。现在我的最后一次斗争(以及在我能够更好地个性化问题之前我需要克服的最后一个障碍),正在弄清楚如何编写输出原始数据的最终数据,以及重新进货数据。它必须看起来像测试用例

 function[restock_cost] = inventory2(file, prices)

%// Reads both files
fh = fopen(file, 'rt');
C = textscan(fh, '%s', 'Delimiter','');
inventory = C{1};
fclose(fh);
Original = C{:};
Original = char(Original);

fh2 = fopen(prices, 'rt');
D = textscan(fh2, '%s', 'Delimiter','');
prices = D{1};
fclose(fh2);

%// Breaks up inventory data
inventory(1) = [];
x = regexp(inventory, '(\w+) #(\d+) #(\d+)', 'tokens', 'once');
x = vertcat(x{:});
itemName = x(:,1);
itemHave = str2double(x(:,2));
itemNeeded = str2double(x(:,3));
itemName = char(itemName);


%// Determines what needs to be restocked and how much is needed
Restock = itemNeeded - itemHave;

%// Breaks up price info
prices(1) = [];
x = regexp(prices, '(\w+) \$(\d+)', 'tokens', 'once');
x = vertcat(x{:});
item = x(:,1);
prices = str2double(x(:,2));

%// Finds the price
restock_cost = sum((Restock .* prices));

%// gets the business name
[name, ~] = strtok(file,'_');

%// Finds the size of Original(may help)
[r,c] = size(Original);
[r2,c2] = size(itemName);
[r3,c3] = size(itemNeeded);

%// Starts to write my output file.
fh2 = fopen([name '_RestockingData.txt'], 'wt');
fprintf(fh2, 'Inventory Data: \n');
fprintf(fh2, '\n');

for i = 2:r %// Don't want the header included
    fprintf(fh2, Original(i,1:c));
    fprintf(fh2,'\n');
end
fprintf(fh2, '\n');
fprintf(fh2,'\n');
fprintf(fh2, 'Restocking Data: \n');
fprintf(fh2,'\n');

for j = 1:r2
    fprintf(fh2, itemName(j,1:c2));
    fprintf(fh2,' #%d ', itemNeeded(j));
    fprintf(fh2, '#%d', Restock(j));
    fprintf(fh2,'\n');
end

end

这很接近,但我需要它输出重新上货数据,项目名称和数字之间只有一个空格。而是它给了我:

Inventory Data: 

Chips #40 #100    
Lettuce #80 #90   
Cheese #22 #60    
Tortillas #65 #100
Tomatoes #40 #70  



Restocking Data: 

Chips     #100 #60
Lettuce   #90 #10
Cheese    #60 #38
Tortillas #100 #35
Tomatoes  #70 #30

我也试过这个:

Restock_Data = {};
Restock_Data = [Restock_Data, {itemName, itemNeeded, Restock}];
Restocking = {Restock_Data};
Kill_Cell = [{Restocking}];

for j = 1:r2

    fprintf(fh2, Restock_Data(j));
    fprintf(fh2,'\n');
end

我需要将我的单元转换为其他形式,但索引它(正如你在Kill_Cell中看到的那样)是不行的。     端

这应该是一切。以下是给我的笔记以防万一:

Notes: 
   - All inventory numbers will have a # in front of them 
   - All the prices will have a $ in front of them and will always be a
     full dollar amount.
   - When creating the new output every item should be seperated by a space
       Ex. 'Chips #5 #2'
   - There will always be 5 items to stock.
   - If your output file is not EXACTLY the same as the solution txt file,
     then you will receive 0 credit for it.

1 个答案:

答案 0 :(得分:2)

以下是使用textscan读取清单文件的一种方法:

fid = fopen('inventory.txt', 'rt');
C = textscan(fid, '%s #%d #%d', 'HeaderLines',1, 'CollectOutput',true);
fclose(fid);

解析数据:

>> C
C = 
    {5x1 cell}    [5x2 int32]

>> C{1}
ans = 
    'Buns'
    'Burgers'
    'Ketchup'
    'Hotdogs'
    'Mustard'

>> C{2}
ans =
          50          63
          32          55
           6          10
           4          35
           7           8

您还可以将文件读取为字符串行的单元格数组(我之前在您的问题答案中使用过此字符串):

fid = fopen('inventory.txt', 'rt');
C = textscan(fid, '%s', 'Delimiter','');
C = C{1};
fclose(fid);

在这种情况下的结果:

>> C
C = 
    'Inventory Data:'
    'Buns #50 #63'
    'Burgers #32 #55'
    'Ketchup #6 #10'
    'Hotdogs #4 #35'
    'Mustard #7 #8'

编辑:

就像我所展示的那样,首先我们将文件读作字符串的单元格数组(文本行):

% read both files
fid = fopen('inventory.txt', 'rt');
C = textscan(fid, '%s', 'Delimiter','');
inventory = C{1};
fclose(fid);

fid = fopen('prices.txt', 'rt');
C = textscan(fid, '%s', 'Delimiter','');
prices = C{1};
fclose(fid);

接下来,我们使用正则表达式来匹配文本中的模式:

% parse inventory data
inventory(1) = [];
x = regexp(inventory, '(\w+) #(\d+) #(\d+)', 'tokens', 'once');
x = vertcat(x{:});
itemName = x(:,1)
itemHave = str2double(x(:,2))
itemNeeded = str2double(x(:,3))

% parse prices data
prices(1) = [];
x = regexp(prices, '(\w+) \$(\d+)', 'tokens', 'once');
x = vertcat(x{:});
item = x(:,1)
price = str2double(x(:,2))

最后,您可以将您的逻辑应用于重新进货库存,并打印所需的输出......