作业任务
超市老板
题目
你是一个超市的老板,超市里面有很多的商品,包括
{"牛奶","面包","方便面","矿泉水","火腿肠","溜溜梅","薄荷糖","豆腐干","辣条","纸巾"},
对应的价格是:{3,2,5,1,1.5,5,10,1,0.5,1}
要求:
试编写一个程序,要求输入对应的商品名称可以查询该商品的单价
拓展要求1:
自行输入购买的商品种类和数量(不可以预先设定)
可以自动实现对商品的总价进行清算
拓展要求2:
并列出购物的小票(购买的商品名称 单价 购买个数 总价)
要求对于小票中的商品进行同种商品的合并,比如多次输入“面包”的时候,可以在小票中只显示一次合并的数量
拓展要求3:
增加 添加商品 的功能
添加新商品后,输入对应的商品名称可以查询该商品的单价,并满足以上拓展要求
示例
小票如下:
商品名 单价 数量
面包 2 2
牛奶 3 6
总价 22
而不是
小票如下:
商品名 单价 数量
面包 2 2
牛奶 3 6
面包 2 1
分析
整体分析
本题的基本要求是求商品单价,可以定义两个数组为全局变量分别存储商品名和价格,其中商品名为字符串,应使用char
类型的二维数组存储;价格为存在小数,应使用float
类型的一维数组存储。再看拓展要求中的1、2项的要求可以用局部变量解决,第3项涉及到了数据的增加,那么就需要再定义一个全局变量count
用于记录数组长度。
不难发现,无论是在基本要求还是在拓展要求中,程序都要进行一个相同的过程:在已有商品中查找,那么我们就可以定义一个函数check_name
用于查找指定商品名并返回相应结果:在找到商品时,返回对应索引;在找不到商品时就返回-1
(为什么不返回0,这样不是更方便使用if对0和非0进行判断了吗?因为查找到第一个时会返回索引0,而负数不是任何商品的索引!)
本程序功能较多,故可以在一个主菜单的死循环while(1)
中添加各个功能,而开始菜单就写在主菜单循环的开头:1.单价查询 2.商品结算 3.添加商品 0.退出程序
需要的知识
- 标准输入/输入函数scanf和printf(stdio.h)
- DOS指令函数system(stdlib.h)
cls
用于清屏、pause
可用于程序退出时 - 字符串操作函数strcmp、strcpy(string.h)
- if-else判断、for while循环语句
函数的定义
解题
首先是库的导入
#include<stdio.h> #include<string.h> #include<stdlib.h>
定义全局变量
char goods_names[256][256] = {"牛奶","面包","方便面","矿泉水","火腿肠","溜溜梅","薄荷糖","豆腐干","辣条","纸巾"}; float goods_price[256] = {3,2,5,1,1.5,5,10,1,0.5,1}; int count=10;
定义查找函数
//查找商品:找到返回索引,否则返回 -1 int check_name(char name[]){ int i; for(i=0;i<count;i++){ if(!(strcmp(name, goods_names[i]))){ return i; } } return -1; }
由于程序需要进行多次字符串的输入,就定义一个函数内变量
char input_str[256];
在循环开始时输出主菜单,并读取用户输入的数据
printf("菜单:\n1.单价查询\n2.商品结算并列出小票\n3.添加商品\n0.退出程序\n\n请输入指令(1/2/3/0):"); scanf("%s", &input_str);
在查询完商品后使用进行清屏
system("cls");
判断进入购物车指令
else if(!(strcmp(input_str, "2")))
定义局部变量
//定义购物车 {索引:数量} x 256 int cart[256][2]; int cart_count=0;
搜索商品
system("cls"); printf("请输入要购买的商品名称:"); scanf("%s", &input_str); int resp = check_name(input_str); while(resp == -1){ printf("查询无此商品,请重新输入:"); scanf("%s", &input_str); resp = check_name(input_str); }
输入数量
printf("请输入要购买的%s数量:", input_str); int item_count; scanf("%d", &item_count); while(item_count <= 0 ){ printf("数量需大于0,请重新输入要购买的%s数量:", input_str); scanf("%d", &item_count); }
遍历购物车是否有重复商品 ,如果有则添加,并改变flag的值
int i; int flag=1; for(i=0;i<cart_count;i++){ if(resp==cart[i][0]){ cart[i][1] += item_count; flag = 0; break; } }
如果找不到重复商品则向购物车内添加项目
if(flag){ cart[cart_count][0] = resp; cart[cart_count][1] = item_count; cart_count++; }
一个二级菜单
printf("添加成功!\n输入1继续添加商品输入其他内容退出(1/Others):"); scanf("%s", &input_str); if(strcmp(input_str, "1")){ break; }
清屏并输出购物明细
system("cls"); printf("您的购物详单如下:"); int i; float total_cost=0; for(i=0;i<cart_count;i++){ printf("\n商品名:%s,数量:%d,单价:%.2f,小计:%.2f", goods_names[cart[i][0]], cart[i][1], goods_price[cart[i][0]], cart[i][1]*goods_price[cart[i][0]]); total_cost += cart[i][1]*goods_price[cart[i][0]]; } printf("\n\n总计消费%.2f元\n\n~~~~~~~~~~~~~~~~~~~~~~~~\n", total_cost);
新增商品
else if(!(strcmp(input_str, "3")))
清屏并输入商品名,存在则直接进入下一次循环输入商品名
system("cls");
printf("请输入要添加商品的名称:");
scanf("%s", &input_str);
int resp=check_name(input_str);
if(resp!=-1){
printf("\n该商品已存在,添加失败!\n\n");
continue;
}
读入价格,并保留两位
然后count自增
使用%.2f可以保留两位小数输出
printf("请输入商品价格(最小位数为:分,即小数点后两位):");
float input_price;
scanf("%f", &input_price);
input_price = ((int)(input_price*100))/100.0;
strcpy(goods_names[count], input_str);
goods_price[count] = input_price;
count++;
printf("添加成功!商品名:%s,价格:%.2f\n\n", input_str, input_price);
完整代码
//Shop program written by moyi
//Date: 2020/10/21
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char goods_names[256][256] = {"牛奶","面包","方便面","矿泉水","火腿肠","溜溜梅","薄荷糖","豆腐干","辣条","纸巾"};
float goods_price[256] = {3,2,5,1,1.5,5,10,1,0.5,1};
int count=10;
//查找商品:找到返回索引,否则返回 -1
int check_name(char name[]){
int i;
for(i=0;i<count;i++){
if(!(strcmp(name, goods_names[i]))){
return i;
}
}
return -1;
}
int main(){
//程序启动
char input_str[256];
printf("This is a shop program\nWritten by moyi\nDate:2022/10/20\n\n");
//主菜单循环
while(1){
printf("菜单:\n1.单价查询\n2.商品结算并列出小票\n3.添加商品\n0.退出程序\n\n请输入指令(1/2/3/0):");
scanf("%s", &input_str);
//查询价格
if(!(strcmp(input_str, "1"))){
system("cls");
printf("请输入你要查询商品的名称:");
scanf("%s", &input_str);
system("cls");
int resp=check_name(input_str);
if(resp == -1){
printf("查无此商品\n\n");
}else{
printf("找到商品:%s,价格为:%.2f\n\n", input_str, goods_price[resp]);
}
//二级菜单
}else if(!(strcmp(input_str, "2"))){
//定义购物车 {索引:数量} x 256
int cart[256][2];
int cart_count=0;
//循环添加预购买的商品
while(1){
//清屏
system("cls");
printf("请输入要购买的商品名称:");
scanf("%s", &input_str);
int resp = check_name(input_str);
while(resp == -1){
printf("查询无此商品,请重新输入:");
scanf("%s", &input_str);
resp = check_name(input_str);
}
printf("请输入要购买的%s数量:", input_str);
int item_count;
scanf("%d", &item_count);
while(item_count <= 0 ){
printf("数量需大于0,请重新输入要购买的%s数量:", input_str);
scanf("%d", &item_count);
}
//遍历购物车是否有重复商品
int i;
int flag=1;
for(i=0;i<cart_count;i++){
if(resp==cart[i][0]){
cart[i][1] += item_count;
flag = 0;
break;
}
}
//没找到
if(flag){
cart[cart_count][0] = resp;
cart[cart_count][1] = item_count;
cart_count++;
}
printf("添加成功!\n输入1继续添加商品输入其他内容退出(1/Others):");
scanf("%s", &input_str);
if(strcmp(input_str, "1")){
break;
}
}
system("cls");
printf("您的购物详单如下:");
int i;
float total_cost=0;
for(i=0;i<cart_count;i++){
printf("\n商品名:%s,数量:%d,单价:%.2f,小计:%.2f", goods_names[cart[i][0]], cart[i][1], goods_price[cart[i][0]], cart[i][1]*goods_price[cart[i][0]]);
total_cost += cart[i][1]*goods_price[cart[i][0]];
}
printf("\n\n总计消费%.2f元\n\n~~~~~~~~~~~~~~~~~~~~~~~~\n", total_cost);
//添加商品
}else if(!(strcmp(input_str, "3"))){
system("cls");
printf("请输入要添加商品的名称:");
scanf("%s", &input_str);
int resp=check_name(input_str);
if(resp!=-1){
printf("\n该商品已存在,添加失败!\n\n");
continue;
}
printf("请输入商品价格(最小位数为:分,即小数点后两位):");
float input_price;
scanf("%f", &input_price);
input_price = ((int)(input_price*100))/100.0;
strcpy(goods_names[count], input_str);
goods_price[count] = input_price;
count++;
printf("添加成功!商品名:%s,价格:%.2f\n\n", input_str, input_price);
//退出
}else if(!(strcmp(input_str, "0"))){
break;
}else{
//清屏、错误提示
system("cls");
printf("输入错误,请重新输入!\n\n");
}
}
return 0;
}
未完待续...
字典
题目
题目解释
1、预先设定50组英语词汇(附件txt文件),和与其对应的50组词、50组中文解释。
2、通过查询英语词汇,可得到其【中文翻译】。
拓展要求:
1、程序能进行循环。
2、增加 添加词汇 的功能
添加新词汇后可以查到该词汇的中文翻译和解释。
分析
解题
排序
题目
题目解释
用户需输入10个整数,程序对其进行排序。
要求:
奇数全在前面,偶数全在后面,并且按照从小到大的顺序输出。
拓展要求:
1、 增加循环;
2、 输出后,可以添加新数字,仍要求奇数全在前面,偶数全在后面,并且按照从小到大的顺序输出。
示例:
输入:9 96 23 21 6 200 2 28 92 10
输出:9 21 23 2 6 10 28 92 96 200
分析
解题
写在最后
祝各位未来的程序员节日快乐!
评论 (0)