gpt4 book ai didi

c - C 中的简单拍卖系统 - 提示?

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:49 25 4
gpt4 key购买 nike

我使用从文件中收集的信息为一个小型拍卖系统用 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]

出价

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.

最佳答案

你少了一个逗号:

fscanf(ifp, "%d %d", &au1_start &au1_min);

应该是

fscanf(ifp, "%d %d", &au1_start, &au1_min);

显然编译器将其解析为 (&au1_start) & au1_min,第二个 &被解释为二进制 & (按位与)运算符,因此警告,由于到不匹配的数据类型。

关于c - C 中的简单拍卖系统 - 提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com