又是 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.