C++ 如何显示存储的信息?

C++ 如何显示存储的信息?,c++,c,loops,C++,C,Loops,我花了整整两周的时间才让我的程序正常运行,但我决定改变它,但我需要一些帮助,这个想法在我脑海中看起来不错,但当它编码的时候,我不知道从哪里开始:(.我希望该程序的用户输入他们的车牌号,然后在一天结束时,管理员应该能够看到每个客户的所有车牌号及其总数,如下图所示 客户1 A1546$20 我希望它能以相反的方式工作,管理员应该能够键入车牌号码和信息应该显示。我一直在想办法做到这一点,我认为唯一可行的方法是将所有信息保存在一个文件中,然后程序读取文件并显示?任何帮助都将不胜感激。 代码如下: #in

我花了整整两周的时间才让我的程序正常运行,但我决定改变它,但我需要一些帮助,这个想法在我脑海中看起来不错,但当它编码的时候,我不知道从哪里开始:(.我希望该程序的用户输入他们的车牌号,然后在一天结束时,管理员应该能够看到每个客户的所有车牌号及其总数,如下图所示

客户1 A1546$20

我希望它能以相反的方式工作,管理员应该能够键入车牌号码和信息应该显示。我一直在想办法做到这一点,我认为唯一可行的方法是将所有信息保存在一个文件中,然后程序读取文件并显示?任何帮助都将不胜感激。 代码如下:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

/*Global Declarations*/
char chContinue = 'Y';
int Continue = 1;
int  Total=1;

//************************************
//********DISCOUNT FUNCTION************
//************************************
int calculateDiscount(Total) {

    char type;
    int  Total1;
    int Discount;

    printf("\nDo you have a pilot's licence?(Y/N)\n");
    scanf("%c", &type);
    while( getchar() != '\n' ) ;

    if(type == 'Y' || type == 'y') {
    Discount = Total * 0.1;
    Total1= Total - Discount;
        printf("\nYou are eligible for a discount\nYour new price is %d ^_^",Total1);
    }

    if(type == 'N' || type == 'n') {
        printf("Thank you for parking here,please submit this ticket when leaving the compound");
    }
    return 0;
}


//************************************
//********LOOP Function****************
//************************************
void Lo_op (count,Total) {
    //char chContinue;
    char arrayname[6];
    int  Total2;
    //int Continue=0;

    printf("\nDo you wish to continue? (Y/N) ");
    scanf("%c", &chContinue);
    while( getchar() != '\n' ) ;


    if (chContinue == 'Y'|| chContinue == 'y' ) {
        Continue = 1;
    }
     else if (chContinue == 'N'|| chContinue == 'n' ) {
        printf("enter pass\n");
        scanf("%s", arrayname);
        strcmp(arrayname,"admin");
        printf ("\n>>>Welcome Administrator!<<<\nWhat would you like to do?");
    /*  printf("\n =========MENU===================\n| ");
    printf(" A) Display Total People Parked Today| \n| ");
    printf(" B) Display Today's Total            | \n| ");
    printf(" C) Exit Program                     | \n| ");
    printf("======================================\n\n"); */
        Continue = 2;
    }
    else {
        printf("Invalid input!\n");
    }

}


//************************************
//********Print Time Rates Function*****
//************************************
void Print_time () {

    printf("\n(Choose a letter corresponding with your choice)\nHow long will you be parking at our facility?\n ");
    printf("\n =========MENU===============\n| ");
    printf(" A) 30 mins               | \n| ");
    printf(" B) 1 hour                | \n| ");
    printf(" C) 2 hours               | \n| ");
    printf(" D) 5 hours               | \n| ");
    printf(" E) 1 Day                 | \n| ");
    printf(" F) Greater than 1 Day    | \n| ");
    printf("==========================\n\n");

}

//************************************
//********Print Choices For Cars********
//************************************
void  Print_carlist () {

    printf("\nChoose your type of vehicle\n===========================\n| ");
    printf("  1)Car                  |\n| ");
    printf("  2)SUV                  |\n| ");
    printf("  3)Bus                  |\n| ");
    printf("  4)Truck                |\n===========================\n\n");

}

//************************************
//********The main function.************
//***Decide Calculate and Display*******
//************************************

int main() {

    char choice;
    int Total=1, vehicle, /*Discount*/ count=1; //, Continue = 0;
    int Rate_A = 2, Rate_B = 4, Rate_C = 6, Rate_D = 8, Rate_E = 10, Rate_F = 12;
    int Car_rate = 1, Suv_rate = 2, Bus_rate = 3, Truck_rate = 4;
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "The current date/time is: %s", asctime (timeinfo) );


    printf("HELLO AND WELCOME TO THE AUTOMATED PARKING TICKETING SYSTEM\n\nPLEASE FOLLOW THE INSTRUCTIONS ON THE SCREEN\n");
    //Rates(); //Here the function is called.
         srand(time(NULL));
    while (Continue == 1) {

        printf("\n                       You are parker number %d\n ",count);
        Print_time ();
        scanf("%c",&choice);
        while( getchar() != '\n' ) ;

        switch (choice) {
        case 'A':
           Print_carlist();
                scanf("%d",&vehicle);
            while( getchar() != '\n' ) ; /* flush stdin ... */




          if((vehicle != 1) && (vehicle != 2) && (vehicle != 3) && (vehicle != 4)) {
               printf("Please follow instructions  \n");
            }

           if(vehicle == 1) {
                printf("You are parking your Car for 30 mins.\n");
                Total = Rate_A * Car_rate;
                    printf("------------------------------\nYour receipt number is A#%d\n", rand()); //generates random number for receipt
                printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                count=count+1;
                 Total=Total+1;

            }

                if(vehicle == 2) {
                    printf("You are parking your SUV for 30 mins.\n");
               Total = Rate_A * Suv_rate;
                    printf("------------------------------\nYour receipt number is A#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
          count=count+1;
          Total=Total+1;
                }

           if(vehicle == 3) {
                printf("You are parking your Bus for 30 mins.\n");
                Total = Rate_A * Bus_rate;
                    printf("------------------------------\nYour receipt number is A#%d\n", rand());
                    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                     count=count+1;
                      Total=Total+1;
                }

            if(vehicle == 4) {
                    printf("You are parking your Truck for 30 mins.\n");
                    Total= Rate_A * Truck_rate;
                    printf("------------------------------\nYour receipt number is A#%d\n", rand());
                    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                    count=count+1;
                    Total=Total+1;
                }

                Lo_op (count,Total);


                break;

            case 'B':
                Print_carlist();
                scanf("%d", &vehicle);
            while( getchar() != '\n' ) ; /* flush stdin ... */


            if((vehicle != 1) && (vehicle != 2) && (vehicle != 3) && (vehicle != 4)) {
                    printf("Please follow instructions  \n");
            }

                if(vehicle == 1) {
                    printf("You are parking your Car for 1 hour.\n");
                    Total = Rate_B * Car_rate;
                    printf("------------------------------\nYour receipt number is B#%d\n", rand());
                    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                   count=count+1;
                   Total=Total+1;
                }

            if(vehicle == 2) {
                    printf("You are parking your SUV for 1 hour.\n");
                    Total = Rate_B * Suv_rate;
                    printf("------------------------------\nYour receipt number is B#%d\n", rand());
                    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                   count=count+1;
                   Total=Total+1;
                }

                if(vehicle == 3) {
                    printf("You are parking your Bus for 1 hour.\n");
                    Total = Rate_B * Bus_rate;
                printf("------------------------------\nYour receipt number is B#%d\n", rand());
                printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                  count=count+1;
                   Total=Total+1;
            }

            if(vehicle == 4) {
                    printf("You are parking your Truck for 1 hour.\n");
                    Total=Rate_B*Truck_rate;
                    printf("------------------------------\nYour receipt number is B#%d\n", rand());
                    printf("You're total is %d",Total);
                    count=count+1;
                     Total=Total+1;
                }
               Lo_op ();
                break;

            case 'C':
                Print_carlist();
                scanf("%d",&vehicle);
            while( getchar() != '\n' ) ; /* flush stdin ... */


           if((vehicle != 1) && (vehicle != 2) && (vehicle != 3) && (vehicle != 4)) {
                printf("Please follow instructions  \n");
            }

          if(vehicle == 1) {
                printf("You are parking your Car for 2 hours.\n");
                Total = Rate_C * Car_rate;
                printf("------------------------------\nYour receipt number is C#%d\n", rand());
                printf("\nYou're total is %d", Total);
                count=count+1;
                 Total=Total+1;
                }

           if(vehicle == 2) {
                    printf("You are parking your SUV for 2hours  .\n");
                    Total = Rate_C * Suv_rate;
                printf("------------------------------\nYour receipt number is C#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
    count=count+1;
     Total=Total+1;
            }

            if(vehicle == 3) {
                printf("You are parking your Bus for 2 hours  .\n");
                Total = Rate_C * Bus_rate;
                printf("------------------------------\nYour receipt number is C#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
    count=count+1;
     Total=Total+1;
           }

         if(vehicle == 4) {
                    printf("You are parking your Truck for 2hours  .\n");
                    Total = Rate_C * Truck_rate;
                    printf("------------------------------\nYour receipt number is C#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
    count=count+1;
     Total=Total+1;
                }
                Lo_op ();
               break;

            case 'D':
          Print_carlist();
            scanf("%d", &vehicle);
                while( getchar() != '\n' ) ; /* flush stdin ... */


            if((vehicle != 1) && (vehicle != 2) && (vehicle != 3) && (vehicle != 4)) {
             printf("Please follow instructions  \n");
            }
           if(vehicle == 1) {
                printf("You are parking your Car for 5 hours.\n");
                Total = Rate_D * Car_rate;
                printf("------------------------------\nYour receipt number is D#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
    count=count+1;
     Total=Total+1;
                }
      if(vehicle == 2) {
                printf("You are parking your SUV for 5 hours  .\n");
                Total = Rate_D * Suv_rate;
                printf("------------------------------\nYour receipt number is D#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
    count=count+1;
     Total=Total+1;
                }

            if(vehicle == 3) {
                    printf("You are parking your Bus for 5 hours  .\n");
                    Total = Rate_D * Bus_rate;
                    printf("------------------------------\nYour receipt number is D#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
    count=count+1;
     Total=Total+1;
                }

            if(vehicle == 4) {
                    printf("You are parking your Truck for 5 hours  .\n");
                    Total = Rate_D * Truck_rate;
                    printf("------------------------------\nYour receipt number is D#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
    count=count+1;
     Total=Total+1;
                }
           Lo_op ();
                break;

            case 'E':
                Print_carlist();
                scanf("%d",&vehicle);
                while( getchar() != '\n' ) ; /* flush stdin ... */


                if((vehicle != 1) && (vehicle != 2) && (vehicle != 3) && (vehicle != 4)) {
                    printf("Please follow instructions  \n");
                }

                if(vehicle == 1) {
                    printf("You are parking your Car for 1 day .\n");
                    Total = Rate_E * Car_rate;
                    printf("------------------------------\nYour receipt number is E#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                     calculateDiscount(Total);
                    count=count+1;
                     Total=Total+1;
            }
                if(vehicle == 2) {
                    printf("You are parking your SUV for 1 day  .\n");
                    Total = Rate_E * Suv_rate;
                    printf("------------------------------\nYour receipt number is E#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                     calculateDiscount(Total);
                    count=count+1;
                     Total=Total+1;
                }

                if(vehicle == 3) {
                printf("You are parking your Bus for 1 day  .\n");
                    Total = Rate_E * Bus_rate;
                    printf("------------------------------\nYour receipt number is E#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                     calculateDiscount(Total);
                    count=count+1;
                     Total=Total+1;
            }

                if(vehicle == 4) {
                    printf("You are parking your Truck for 1 day  .\n");
                Total = Rate_E * Truck_rate;
                    printf("------------------------------\nYour receipt number is E#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                     calculateDiscount(Total);
                    count=count+1;
                     Total=Total+1;
                }
               Lo_op ();
                break;

            case 'F':
                Print_carlist();
                scanf("%d", &vehicle);
                while( getchar() != '\n' ) ; /* flush stdin ... */


                if((vehicle != 1) && (vehicle != 2) && (vehicle != 3) && (vehicle != 4)) {
                    printf("Please follow instructions  \n");
            }
                if(vehicle == 1) {
                printf("You are parking your Car for more than one day.\n");
                    Total = Rate_F * Car_rate;
                    printf("------------------------------\nYour receipt number is F#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                   calculateDiscount(Total);
                    count=count+1;
                     Total=Total+1;
            }

                if(vehicle == 2) {
                    printf("You are parking your SUV for more than one day.\n");
                    Total = Rate_F * Suv_rate;
                    printf("------------------------------\nYour receipt number is F#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                     calculateDiscount(Total);
                    count=count+1;
                     Total=Total+1;
                }

                if(vehicle == 3) {
                printf("You are parking your Bus for more than one day.\n");
                    Total = Rate_F * Bus_rate;
                    printf("------------------------------\nYour receipt number is F#%d\n", rand());
    printf("You're total is %d,\nPlease submit this ticket when leaving the compound\n-------------------------------",Total);
                     calculateDiscount(Total);
                    count=count+1;
                     Total=Total+1;
                }

            if(vehicle == 4) {
               printf("You are parking your Truck for more than one day.\n");
                Total = Rate_F * Truck_rate;
                printf("------------------------------\nYour receipt number is F#%d\n", rand());
                printf("You're total is %d",Total);
                calculateDiscount(Total);
                count=count+1;
                 Total=Total+1;
           }
                Lo_op ();
                break;

        default:
            printf("\nSorry your choice is not valid, please enter the letter associated with your choice!\n");
            break;
        }
    }

    return 0;

}
#包括
#包括
#包括
#包括
/*全球宣言*/
char chContinue='Y';
int Continue=1;
整数合计=1;
//************************************
//********贴现函数************
//************************************
int CalculateDiscovery(总计){
煤焦类型;
整数1;
国际折扣;
printf(“\N您是否有飞行员执照?(是/否)\N”);
scanf(“%c”,类型(&T);
而(getchar()!='\n');
如果(类型='Y'| |类型=='Y'){
折扣=总额*0.1;
Total1=总折扣;
printf(“\n您有资格享受折扣\n您的新价格为%d^ u^”,总计1);
}
如果(类型='N'| |类型='N'){
printf(“感谢您在此停车,请在离开园区时提交此车票”);
}
返回0;
}
//************************************
//********循环函数****************
//************************************
无效Lo_op(计数,总计){
//char-chContinue;
char arrayname[6];
整数2;
//int Continue=0;
printf(“\N您想继续吗?(是/否)”);
scanf(“%c”,&chContinue);
而(getchar()!='\n');
如果(chContinue=='Y'| | chContinue=='Y'){
Continue=1;
}
else if(chContinue=='N'| | chContinue=='N'){
printf(“输入pass\n”);
scanf(“%s”,arrayname);
strcmp(arrayname,“admin”);

printf(“\n>>>欢迎管理员!如果您正在学习C,文件听起来确实是一个好的开始。您还必须想出一些方法来格式化记录中所需的信息(名称、许可证等)(一行=一条记录?)


然后,您会发现,当您获得大量记录时,搜索速度非常慢,因为您的搜索将需要搜索整个文件。您可以使用二进制搜索,但添加新记录将需要插入(速度慢且繁琐)。然后,解决方案是使用数据库。SQLite可能值得一看(我从未使用过它)或者甚至使用外部RDBMS。

好吧,有点虚伪,我会尝试提供一些建议。首先,不要使用K&R风格的函数头,比如:

void Lo_op (count,Total)
始终指定类型:

Lo_op(int count, int Total)
其次,当您发现自己编写的代码与主代码一样重复时(对于4种类型的车辆和5段时间,基本上相同的代码重复)尝试找出一种消除重复的方法,例如将数据放入数组,然后根据输入访问正确的数据。下面是我如何解决问题的示意图1:

#包括
类型定义结构{
字符*名称;
整数率;
}数据对;
数据对操作[]={
{“管理”,0},
{“停车场”,1},
{“公园SUV”,2},
{“停车卡车”,3},
{“公园巴士”,4}
};
#定义num_操作(sizeof(操作)/sizeof(事务[0]))
数据对速率[]={
{“30分钟”,2},
{“1小时”,4},
{“2小时”,6},
{“5小时”,8},
{“1天”,10},
{“超过1天”,12}
};
#定义num_比率(sizeof(rates)/sizeof(rates[0]))
类型定义结构{
int型;
整数长度;
}交易;
交易记录[256];
静态事务数;
int get_值(字符常量*提示,数据常量*菜单,int max){
int值;
int i;
printf(“%s\n”,提示符);
对于(i=0;i最大值+1);
返回值;
}   
int get_action(){
返回get_值(“您想做什么?\n”、操作、num_操作);
}
int get_length(){
返回get_值(“您想停放多长时间”,速率,num_速率);
}
void show_data(int vehicle_type,int length){
printf(“您正在为%s停车。\n”,
操作[车辆类型]。名称,
速率[长度].名称);
printf(“您的总数为:$%f\n”,
(双)动作[车辆类型]。速率*速率[长度]。速率);
}
int ask_continue(){
内部检索;
printf(“您想继续吗?”);
做{
retval=getchar();
retval=tolower(retval);
}while(retval!=“n”&&retval!=“y”);
返回返回;
}
void do_admin(){
//现在,只需显示已记录交易的列表。
int i;

对于(i=0;i将数据保存到文件中可能是一个合理的选择。发布数百行代码,其中带有一个模糊的问题,如“我应该怎么做?”?“恐怕不是很好。谢谢Jerry Coffin编辑我的帖子和快速回复。我知道它有很多编码,而且有点模糊,但我所需要的只是一个小例子,告诉我该怎么做,让我走上正确的一步,这样我就可以继续,而不是要求任何人帮我做,因为我想向你们学习:)这是作业吗?通常情况下,随机数不会被认为是唯一的,不能用作收据号。与其说是作业,不如说是一个项目,我们必须开发问题和解决方案。我理解你对收据号的看法,但这是我唯一能想到的产生不同n的方法每个用户的数字使用
rand()
不一定会为每个用户生成不同的数字。在这种情况下,这可能无关紧要,但如果你在现实世界中做过类似的事情,你需要给每个人一个唯一的数字。Wo
#include <stdio.h>

typedef struct { 
    char *name;
    int rate;
} data_pair;

data_pair actions[] = {
    { "Administration", 0},
    { "Park Car", 1},
    { "Park SUV", 2},
    { "Park Truck", 3},
    { "Park Bus", 4 }
};

#define num_actions (sizeof(actions)/sizeof(transactions[0]))

data_pair rates[] = {
    { "30 minutes", 2},
    { "1 hour", 4 },
    { "2 hours", 6},
    { "5 hours", 8},
    { "1 day", 10 },
    { "More than 1 day", 12}
};

#define num_rates (sizeof(rates)/sizeof(rates[0]))

typedef struct {
    int type;
    int length;
} transaction;

transaction transactions[256];
static transaction_num;

int get_value(char const *prompt, data_pair const *menu, int max) { 
    int value;
    int i;

    printf("%s\n", prompt);
    for (i=0; i<max; i++)
        printf("%d. %s\n", i+1, menu[i].name);

    printf("Please enter a number between 1 and %d: ", max);
    do { 
        scanf("%d", &value);
    } while (value < 0 || value > max+1);
    return value;
}   

int get_action() { 
    return get_value("What do you want to do?\n", actions, num_actions);
}

int get_length() { 
    return get_value("How long do you want to park\n", rates, num_rates);
}

void show_data(int vehicle_type, int length) { 
    printf("You're parking your %s for %s.\n", 
        actions[vehicle_type].name, 
        rates[length].name);
    printf("Your total is: $%f\n", 
        (double)actions[vehicle_type].rate * rates[length].rate);
}

int ask_continue() { 
    int retval;
    printf("Do you want to continue?");
    do { 
        retval = getchar();
        retval = tolower(retval);
    } while (retval != 'n' && retval != 'y');
    return retval;
}

void do_admin() {
    // For now, just display a list of recorded transactions.
    int i;
    for (i=0; i<transaction_num; i++)
        printf("%d: %s %s\n", 
            i+1, 
            actions[transactions[i].type].name, 
            rates[transactions[i].length].name);
}

int main() {
    do { 
        int action = get_action();
        int length;
        transaction t;
        if (action == 1)
            do_admin();
        else {
            length = get_length();
            show_data(action, length);
            t.length=length;
            t.type=action;
            transactions[transaction_num++] = t;
        }

    } while (ask_continue() == 'y'); 
    return 0;
}