我使用从文件中收集的信息在C中为小型拍卖系统编写了这个程序。拍卖系统应存储出价,将开始出价/最小增加出价应用于列表,然后通过菜单系统显示结果。我相信我已经达到了项目的目标,但我现在正在尝试简化我的代码。
我不确定我的代码是否完全正确,或者是否有更简单的方法来使用更少的代码来布局信息。任何提示也将非常感谢!谢谢你的时间。
输入文件如下所示
500 40
100 20
20 7
300 55
700 120
第一个数字是“拍卖开标”
,第二个数字是“拍卖最低限额增加”
#include<stdio.h>
#include<stdlib.h>
//int main
int main() {
//open input text
FILE * ifp;
ifp = fopen("input.txt", "r");
printf("Welcome to the Silent Auction\n");
printf("\nPlease make a selection from the following:\n");
int number = 1;
int starting_bid1 = 0;
int starting_bid2 = 0;
int starting_bid3 = 0;
int starting_bid4 = 0;
int starting_bid5 = 0;
int au1_start, au1_min, au2_start, au2_min, au3_start, au3_min, au4_start, au4_min, au5_start, au5_min;
char choice [20];
fscanf(ifp, "%d %d", &au1_start, &au1_min);
fscanf(ifp, "%d %d", &au2_start, &au2_min);
fscanf(ifp, "%d %d", &au3_start, &au3_min);
fscanf(ifp, "%d %d", &au4_start, &au4_min);
fscanf(ifp, "%d %d", &au5_start, &au5_min);
printf("\nView Auctions [VIEW]\nBid on an Auction [BID]\nClose Auctions [CLOSE]\n");
scanf("%s", &choice);
if (strcmp(choice, "VIEW", "view", "View") == 0) {
printf("\nNumber Current Bid Minimum Increase\n");
if (starting_bid1 < au1_start){
printf("1 $%d $%d\n", starting_bid1, au1_start);
}
else{
printf("1 $%d $%d\n", starting_bid1, au1_min);
}
if (starting_bid2 < au2_start){
printf("2 $%d $%d\n", starting_bid2, au2_start);
}
else{
printf("2 $%d $%d\n", starting_bid2, au2_min);
}
if (starting_bid3 < au3_start){
printf("3 $%d $%d\n", starting_bid3, au3_start);
}
else{
printf("3 $%d $%d\n", starting_bid3, au3_min);
}
if (starting_bid4 < au4_start){
printf("4 $%d $%d\n", starting_bid4, au4_start);
}
else{
printf("4 $%d $%d\n", starting_bid4, au4_min);
}
if (starting_bid5 < au5_start){
printf("5 $%d $%d\n", starting_bid5, au5_start);
}
else{
printf("5 $%d D $%d\n", starting_bid5, au5_min);
}
}
else if (strcmp(choice, "BID", "bid", "Bid") == 0) {
printf("\nWhich auction would you like to bid on?\n");
scanf("%d", &number);
switch (number) {
case 1:
printf("The minimum bid is $%d.\n", au1_start);
printf("How much would you like to bid?\n");
scanf("%d", &starting_bid1);
if (starting_bid1 < au1_start){
printf("Sorry, that bid is not high enough.\n");
}
break;
case 2:
printf("The minimum bid is $%d.\n", au2_start);
printf("How much would you like to bid?\n");
scanf("%d", &starting_bid2);
if (starting_bid2 < au2_start){
printf("Sorry, that bid is not high enough.\n");
}
break;
case 3:
printf("The minimum bid is $%d.\n", au3_start);
printf("How much would you like to bid?\n");
scanf("%d", &starting_bid3);
if (starting_bid3 < au3_start){
printf("Sorry, that bid is not high enough.\n");
}
break;
case 4:
printf("The minimum bid is $%d.\n", au4_start);
printf("How much would you like to bid?\n");
scanf("%d", &starting_bid4);
if (starting_bid4 < au4_start){
printf("Sorry, that bid is not high enough.\n");
}
case 5:
printf("The minimum bid is $%d.\n", au5_start);
printf("How much would you like to bid?");
scanf("%d", &starting_bid5);
if (starting_bid5 < au5_start){
printf("Sorry, that bid is not high enough.\n");
}
default:
printf("\n");
}
}
else if (strcmp(choice, "CLOSE", "close", "Close") == 0) {
if (starting_bid1 >= au1_start){
printf("\nAuction 1 sold for %d.\n", starting_bid1);
}
else{
printf("\nAuction 1 did not sell.\n");
}
if (starting_bid2 >= au2_start){
printf("Auction 2 sold for %s.\n", starting_bid2);
}
else{
printf("Auction 2 did not sell.\n");
}
if (starting_bid3 >= au3_start){
printf("Auction 3 sold for %s.\n", starting_bid3);
}
else{
printf("Auction 3 did not sell.\n");
}
if (starting_bid4 >= au4_start){
printf("Auction 4 sold for %s.\n", starting_bid4);
}
else{
printf("Auction 4 did not sell.\n");
}
if (starting_bid5 >= au5_start){
printf("Auction 5 sold for %s.\n", starting_bid5);
}
else{
printf("Auction 5 did not sell.\n");
}
}
else{
printf("\nPlease choose either VIEW, BID, or CLOSE.\n");
}
// formatting
printf("\n");
//close file
fclose(ifp);
//return main
return 0;
}
粗体代表用户输入
最终产品应如下所示:
Welcome to the Silent Auction
Please make a selection from the following:
View Auctions [VIEW]
Bid on an Auction [BID]
Close Auctions [CLOSE]
查看
Number Current Bid Minimum Increase
1 $0.00 $500.0
2 $0.00 $100.00
3 $0.00 $20.00
4 $0.00 $300.00
5 $0.00 $700.00
Please make a selection from the following:
View Auctions [VIEW]
Bid on an Auction [BID]
Close Auctions [CLOSE]
BID
Which auction would you like to bid on?
3
The minimum bid is $20.00.
How much would you like to bid?
21
Please make a selection from the following:
View Auctions [VIEW]
Bid on an Auction [BID]
Close Auctions [CLOSE]
关闭
Auction 1 did not sell.
Auction 2 did not sell.
Auction 3 sold for $21.00!
Auction 4 did not sell.
Auction 5 did not sell.
答案 0 :(得分:3)
你错过了一个逗号:
fscanf(ifp, "%d %d", &au1_start &au1_min);
应该是
fscanf(ifp, "%d %d", &au1_start, &au1_min);
显然,编译器将其解析为(&au1_start) & au1_min
,第二个&
被解释为二进制&
(按位AND)运算符,因此警告到期
不匹配的数据类型。
答案 1 :(得分:0)
关于是否使用更少的代码更简单地布置信息的问题,最明显的解决方案是使用starting_bid
,au_start
和au_min
制作数组。然后,您可以使用单个语句使用拍卖编号对数组进行索引,而不是为5个拍卖中的每个拍卖都设置单独的if-else或case语句。