小編給大家分享一下C語言如何實現超市計價收款系統,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內容如下
學習一個月的C語言,寫了一個簡易的超市計價收款系統

源碼如下
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 10
typedef struct //定義商品的結構體
{
char name[10];
int num;
float price;
int ID;
}Store;
Store store[M];
void shuru( ) //定義輸入商品的函數
{
int i;
int isgo;
int n;
for(i=0;i<M;i++)
{
store[i].ID=i+1;//自定義商品的ID
printf("輸入商品的名字\n");
scanf("%s",&store[i].name);
printf("輸入商品的價格\n");
scanf("%f",&store[i].price);
printf("還需要輸入商品嗎?\n");
printf("1.YES,2.NO\n");
scanf("%d",&isgo);
if(isgo!=2)//判斷是否繼續輸入商品
{
}else//不輸入商品直接跳出,展示所有商品的信息
{
for(n=0;n<=i;n++)
{
printf("商品ID\t商品名字\t商品價格\n");
printf("%d\t,%s\t,%f\n",store[n].ID,store[n].name,store[n].price);
}
i=10;
}
}
}
void shopp() //購物函數
{
int isgo=1;
int i;
int n;
int number;
int shoppid;
float pricenum;
float pricesum=0.0;
float priceguke;
printf("庫存的商品如下\n");
for(n=0;n<M;n++)
{
printf("商品ID\t商品名字\t商品價格\n");
printf("%d\t,%s\t,%f\n",store[n].ID,store[n].name,store[n].price);
if(store[n].price==0)//這里是為了不把沒定義的商品不顯示出來
{
n=M;
}
}
while(isgo)//購物
{
printf("按下你需要的商品ID和數量\n");
scanf("%d,%d",&shoppid,&number);
pricenum=(store[shoppid-1].price)*number;
pricesum=pricesum+pricenum;
printf("你購買的商品%s是%d個,價格是%f,總金額是%f\n",store[shoppid-1].name,number,pricenum,pricesum);
printf("是否繼續購物\n");
printf("0.退出,1.繼續\n");
scanf("%d",&isgo);
}
printf("輸入顧客支付的金錢\n");//結算
scanf("%f",&priceguke);
if(priceguke>=pricesum)
{
printf("收到顧客%f元,找回顧客%f元\n",priceguke,priceguke-pricesum);
}else
{
printf("收到的錢小于總金額\n");
}
printf("謝謝光臨\n");
}
main()
{
int xitong=1;
while(xitong!=3)
{
printf("-------------------歡迎登錄好浪費超市系統-----------------------\n");
printf("1.輸入商品信息\n");
printf("2.購買商品\n");
printf("3.退出系購買商品統\n");
scanf("%d",&xitong);
switch(xitong)
{
case 1:shuru();
break;
case 2:shopp();
break;
case 3:printf("歡迎再次使用好浪費超市系統\n");
}
}
}看完了這篇文章,相信你對“C語言如何實現超市計價收款系統”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。