C验证订单

时间:2016-03-12 04:15:22

标签: c

任何人都可以帮我处理我的代码,如果我的订单超出可用库存或者不是

,我在3. Order验证订单时会遇到问题
int main(void){

    int choice = 0;
    int year = 1, i, com;
    int storage[99] = {0};

    for (;;) {

        clrscr();

           printf("Year %d\n\n", year);

           printf("1. Import\n");
           printf("2. Storage\n");
           printf("3. Order\n");

           printf("\nchoice: ");

        scanf("%d", &choice);


        if (choice == 1){ // import

            clrscr();
            printf("Enter sets of computer's imported: ");
            scanf("%d", &storage[year]);
            year++;
        }

        if (choice == 2){ // storage

            clrscr();
            printf("Year    sets\n");

            for (i = 1; i < year; i++){

            printf("%2d     %4d\n", i, storage[i]);

        }

         getch();

        }
        if(choice == 3){ //order

            printf("Enter Sets of Computer ordered: ");
            scanf("%d", &com);

        for (i = 0; com && i < 99; i++){

        if (com <= storage[i]){

            storage[i] = storage[i] - com;
            com = 0;    
        }
        if (com > storage[i]){
        printf("Sorry we have No enough Stocks !");

        }
        if (storage[i] <= com){

            com = com - storage[i];
            storage[i] = 0;
        }
    }


        }
    }
}

例如:

我在choice 1. Import

中导入了3次

Year 1我导入500台计算机

Year 2 i导入1000台计算机

Year 3 i导入800台计算机

查看我的2. Storage时会显示此内容

Year    sets
 1      500
 2     1000
 3      800  

我的2. Storage

中共有2300台计算机

现在在3. Order我输入的计算机集合为5000

5000超出了我2. Storage中的库存,因此我预计订单将无法继续显示Sorry we have No enough Stocks !

但是再次查看2. Storage时会显示错误的输出

Year    sets
 1        0
 2        0
 3        0

你可以看到它现在是空的

0 个答案:

没有答案