d6_商品录入及购买

Assignment

* -> Create a structre as follows

  struct Product
  {

    int productid;
    char productname[10];
    float price;
    int quantity;
 }


* product list is local to main


Create a Menu Driven program to
 1. Write a program to create a array of 5 products.
 2. Buy product

 

#include<stdio.h>
#include <stdlib.h>
void mainmenu();
void registerMenu();
void buyMenu();
void registerProduct();
void buy();
void enterProduct();
void choiceProduct();
void displayProduct();
int findProduct(int id);
void exits();

typedef struct Product
  {
    int productid;
    char productname[10];
    float price;
    int quantity;
 }product;


product proList[5];
int index=0;  //商品下标


void main()
{
    //system("color 37");  //改变窗口颜色
    int choice;

    while(1)
    {
     mainmenu();
     scanf("%d",&choice);
     switch(choice)
     {
      case 1:registerProduct();break;
      case 2:buy();break;
      default:printf("you enter the wrong choice\n");
     }
    }

}

//主菜单
void mainmenu()
{
 printf("------------------------------------\n");
 printf("----   1.register product       ----\n");
 printf("----   2.Buy product            ----\n");
 printf("------------------------------------\n");
 printf("Enter your choice:");
}


//商品录入菜单
void registerMenu()
{
 printf("------------------------------------\n");
 printf("----   1.Enter product          ----\n");
 printf("----   2.Display product        ----\n");
 printf("----   3.Exit                   ----\n");
 printf("------------------------------------\n");
 printf("Enter your choice:");
}

//购买商品菜单
void buyMenu()
{
 printf("------------------------------------\n");
 printf("----   1.Choice product         ----\n");
 printf("----   2.Choice again           ----\n");
 printf("----   3.Exit                   ----\n");
 printf("------------------------------------\n");
 printf("Enter your choice:");
}

//商品录入
void registerProduct()
{
    int choice;
    while(choice!=3)
    {
    registerMenu();
    scanf("%d",&choice);
    getchar();
    switch(choice)
     {
      case 1: enterProduct();break;
      case 2:displayProduct();break;
      case 3:exits();break;
      default:printf("you enter the wrong choice\n");
     }
    }

}

//商品购买
void buy()
{
    int choice;
    while(choice!=3)
    {
    buyMenu();
    scanf("%d",&choice);
    switch(choice)
     {
      case 1:choiceProduct();break;
      case 2:choiceProduct();break;
      case 3:exits();break;
      default:printf("you enter the wrong choice\n");
     }
    }

}

//商品详细信息录入 
void enterProduct()
{
  

       printf("Enter  product information:\n");
       printf("Enter product id: ");
       scanf("%d",&proList[index].productid);
       printf("Enter product name: ");
       scanf("%s",proList[index].productname);
       printf("Enter product price: ");
       scanf("%f",&proList[index].price);
       printf("Enter product quantity: ");
       scanf("%d",&proList[index].quantity);
       index++;  //商品下标加1
    
}
//挑选商品
void choiceProduct()
{
    int id,quantity,find;
    float total=0;
    printf("Choice Product end with id=0 and quantity=0\n");
    displayProduct();  //显示商品信息
    do
    {
        printf("Enter product id : ");
        scanf("%d",&id);
        printf("Enter product quantity: ");
        scanf("%d",&quantity);
        if(id==0 && quantity==0)
          break;               //当输入 的 id和 quantity均为0时退出循环
        find=findProduct(id);  //判断是否有该品
        if(find!=-1)
        {
          if(proList[find].quantity>=quantity)
          {
            proList[find].quantity-=quantity;  //数量减少
            total=total+proList[find].price * quantity;//计算总价格
          }
          else
          {
           printf("The quantity is not enough! choice again\n ");
          }
        }

      if(find==-1)
        printf("there is no such product!\n");
    }while (1);
    printf("You should pay %.2f RMB\n",total);  //付账

}

//显示商品信息
void displayProduct()
{
    product *ptr;
    ptr=&proList[0];
    printf("-------------------------------------------------\n");
    printf("id     name      price     quantity     \n");
       for(int i=0;i<index;i++)
       {
        printf("%d      %s       %.2f      %d\n",ptr->productid,ptr->productname,ptr->price,ptr->quantity);
        ptr++;
       }
    printf("--------------------------------------------------\n");


    }

//判断是否有该品,有i,无-1
int findProduct(int id)
{

    product *ptr;
    ptr=&proList[0];
       for(int i=0;i<index;i++)
       {
         if(id==ptr->productid)
            return i;
         ptr++; 
       } 
       return -1;

    }

//退出
void exits()
{    

  system("cls");
  main();
}

 

posted on 2014-07-28 17:15  @冰糖  阅读(195)  评论(0编辑  收藏  举报

导航