0

我使用从文件中收集的信息,用 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. 
4

2 回答 2

3

你少了一个逗号:

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

应该

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

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

于 2013-04-03T00:42:57.920 回答
0

关于您是否有更简单的方法来使用更少的代码来布置信息的问题,最明显的解决方案是使用 和starting_bid制作au_start数组au_min。然后,不用为 5 次拍卖中的每一次都使用单独的 if-else 或 case 语句,您可以使用一条语句使用拍卖编号对数组进行索引。

于 2013-04-03T00:51:25.140 回答