我编写了一个函数,它接受用户预算和购物清单,并提示用户她想要的商品数量。这个函数目前有两个问题。当我用20作为预算进行测试时,我知道我应该能买10个菠菜,但它只允许我买1个菠菜。这很奇怪,因为我可以正确地购买剩下的物品。第二个问题是,我想通过添加行来显示所购买商品的data.frame。然而,只显示最后购买的项目!你知道如何解决这两个问题吗?
how.many<-function(fruit, number){
string<-paste("How many",fruit,"?",sep=" ")
fruit.number<-readline(string)
print("fruit.number")
print(fruit.number)
print("number")
print(number)
while(fruit.number > number){
print("ERROR: too many for the budget")
string<-paste("How many",fruit,"?",sep=" ")
fruit.number<-readline(string)
}
return(as.numeric(fruit.number))
}
grocery.list <- function(file,budget) {
item.count<-0
updated.price=budget
#purchased.items <- data.frame(count= numeric(0), item= character(0), price = numeric(0),quantity=numeric(0))
purchased.items <- data.frame(count= numeric(0), item= character(0), price = numeric(0),quantity=numeric(0))
outf<-read.csv(file,header=FALSE)
colnames(outf)<-c("item","price")
mat = as.matrix(outf)
colnames(mat) <- NULL
for (i in 1:dim[1])
{
print(mat[i,2])
print(updated.price/as.numeric(mat[i,2]))
number=how.many(mat[i,1],updated.price/as.numeric(mat[i,2]))
print(number)
updated.price=updated.price- (number* (as.numeric(mat[i,2])) )
print(updated.price)
if (number>0){
item.count=item.count+1
purchased.items<-cbind(item.count,mat[i,1],as.numeric(mat[i,2]),number)
}
if (updated.price<=0){
break
}
}
colnames(purchased.items)=c(" ","item","price", "quantity" )
return(purchased.items )
}
n=grocery.list("groceries.csv",20)
print(n)
所以while
的循环是怎样的。这里的菠菜很多功能都不起作用!我真的不明白为什么,因为它适用于其他项目。
下面是一个杂货店。csv的示例:
spinach,2.00
rice,3.00
toilet paper,4.00
bread,2.40
milk,3.10
apple,0.40
这是我得到的结果列表:
> source("grocery.R")
[1] "2.0"
[1] 10
How many spinach ?1
[1] "fruit.number"
[1] "1"
[1] "number"
[1] 10
[1] 1
[1] 18
[1] "3.0"
[1] 6
How many rice ?3
[1] "fruit.number"
[1] "3"
[1] "number"
[1] 6
[1] 3
[1] 9
[1] "4.0"
[1] 2.25
How many toilet paper ?4
[1] "fruit.number"
[1] "4"
[1] "number"
[1] 2.25
[1] "ERROR: too many for the budget"
How many toilet paper ?2
[1] 2
[1] 1
[1] "2.4"
[1] 0.4166667
How many bread ?5
[1] "fruit.number"
[1] "5"
[1] "number"
[1] 0.4166667
[1] "ERROR: too many for the budget"
How many bread ?1
[1] "ERROR: too many for the budget"
How many bread ?0
[1] 0
[1] 1
[1] "3.1"
[1] 0.3225806
How many milk ?2
[1] "fruit.number"
[1] "2"
[1] "number"
[1] 0.3225806
[1] "ERROR: too many for the budget"
How many milk ?1
[1] "ERROR: too many for the budget"
How many milk ?0
[1] 0
[1] 1
[1] "0.4"
[1] 2.5
How many apple ?0
[1] "fruit.number"
[1] "0"
[1] "number"
[1] 2.5
[1] 0
[1] 1
item price quantity
[1,] "3" "toilet paper" "4" "2"
how.many<-function(fruit, number){
string<-paste("How many",fruit,"?",sep=" ")
fruit.number<-as.numeric(readline(string))
while(fruit.number > number){
print("ERROR: too many for the budget")
string<-paste("How many",fruit,"?",sep=" ")
fruit.number<-as.numeric(readline(string))
}
return(as.numeric(fruit.number))
}
grocery.list <- function(file,budget) {
item.count<-0
updated.price=budget
final.price<-0
purchased.items <- data.frame(count= numeric(0), item= character(0), price = numeric(0),quantity=numeric(0))
outf<-read.csv(file,header=FALSE)
mat = as.matrix(outf)
colnames(mat) <- NULL
m.dim=dim(mat)
for (i in 1:m.dim[1])
{
if (updated.price-as.numeric(mat[i,2])>0)
{
number=how.many(mat[i,1],updated.price/as.numeric(mat[i,2]))
updated.price=updated.price- (number* (as.numeric(mat[i,2])) )
if (number>0){
item.count=item.count+1
new_row=data.frame(item=mat[i,1],price=as.numeric(mat[i,2]),quantity=number)
purchased.items<-rbind(purchased.items,new_row)
}
}
}
colnames(purchased.items)=c("item","price", "quantity" )
print(purchased.items)
print(purchased.items)
p.mat=as.matrix(purchased.items)
colnames(p.mat)<-NULL
rownames(p.mat)<-NULL
p.dim<-dim(p.mat)
for (i in 1:p.dim[1])
final.price<- final.price+ (as.numeric(p.mat[i,2]))*(as.numeric(p.mat[i,3]))
return(final.price)
}
gl=grocery.list("groceries.csv",10)
string<-paste("Your bill is $",gl,sep="")
print(string)