本文介绍了销售苹果股票(第1号中出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人可以帮助我与我的code,我有一个问题时,即时通讯销售我的苹果,
它减去所有的股票和放大器;有时,当我输入超过股票数量变为负
Can anybody help me with my code , i have a problem when im selling my apples,it subtract all of my stocks & sometimes the number goes negative when my input exceeds the stocks
int main(void){
int choice = 0;
int days = 1, i, buyApple;
int stocks[99] = {0};
for (;;){
clrscr();
printf("Day %d\n", days);
printf("1. harvest\n");
printf("2. View Stocks\n");
printf("3. Sell\n");
printf("\nchoice: ");
scanf("%d", &choice);
if (choice == 1){
clrscr();
printf("Input No. of Apple harvested: ");
scanf("%d", &stocks[days]);
days++;
}
if (choice == 2){
clrscr();
printf("Day Stocks\n");
for (i = 1; i < days; i++){
printf("%2d %4d\n", i, stocks[i]);
}
getch();
}
if(choice == 3){
printf("Input No. of Appple to be sold: ");
scanf("%d", &buyApple);
for (i = 0; i < buyApple; i++){
stocks[i] = stocks[i] - buyApple;
}
if(stocks[i] > buyApple)
printf("Out of Stocks!");
getch();
}
}
}
期望输出:
例如我的股票是这样
Day Stocks
1 100
2 50
3 50
4 180
5 200
如果我输入号苹果出售200
if i input No. apples to be sold is 200
我的股票会变成这个样子。
my stocks will become like this
Day Stocks
1 0
2 0
3 0
4 180
5 200
在1月1日退出
当我的投入超过了股票,它会说缺货了!
,它不会继续减少我的股票
and when my Input exceeds the stocks it will say "Out of Stocks !"
and it will not continue reduce my stocks
推荐答案
在问题的评论中第三个选择的问题,正确的code应该是这样的(它的C#):
The issue in third choice as in question comments ,the correct code should be like this (it's c#) :
if (choice == 3)
{
printf("Input No. of Appple to be sold: ");
scanf("%d", &buyApple);
for (i = 1; i < sizeof(stocks); i++) //Looping on all stocks you have
{
if (buyApple <= stocks[i]) //if amount of apple less than stock apples ,then remove them from stock
{
stocks[i] = stocks[i] - buyApple;
buyApple = 0;
break;
}
else //if amount of apple is bigger than stock apples
{
if ((sizeof(stocks) - 1) == i){ //if it's the Last stock,then there is no apples in stock
printf("Out of Stocks!");
}
else //take amount of apples from current stock
{
buyApple = buyApple - stocks[i];
stocks[i] = 0;
}
}
}
getch();
}
这篇关于销售苹果股票(第1号中出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!