gpt4 book ai didi

c - 从文件扫描时如何使用结构实现堆栈

转载 作者:行者123 更新时间:2023-11-30 19:52:07 29 4
gpt4 key购买 nike

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

typedef struct
{
int client_id;
char business_name [30];
char client_first_name [20];
char client_last_name [20];
char address [40];
float budget;
float energy_requirements;
char business_info [300];
}Client;

main()
{
Client c[20];
FILE*z;
void initialise (FILE*,Client []);
initialise (z,c);
system ("PAUSE");
}

void initialise(FILE*z,Client c[])
{
int x,max=20,top=-1;
if (top==max-1)
{
return;
}
top++;
for (x=0;x<20;x++)//Assigns all places in the structure to -1 and NULL
{
c[x].client_id=-1;
strcpy(c[x].business_name,"NULL");
strcpy(c[x].client_first_name,"NULL");
strcpy(c[x].client_last_name,"NULL");
strcpy(c[x].address,"NULL");
c[x].budget=-1;
c[x].energy_requirements=-1;
strcpy(c[x].business_info,"NULL");
}
z=fopen ("Novus.txt","r");
for (x=0;x<20;x++)//Replaces values in structure with data from text file
{
fscanf (z,"%d\n %[^\n]\n %[^\n]\n %[^\n]\n %[^\n]\n%f\n%f\n %[^\n]\n\n",&c[x].client_id,c[x].business_name,c[x].client_first_name,c[x].address,&c[x].budget,&c[x].energy_requirements,c[x].business_info);
}
fclose (z);
void menu (FILE*,Client []);
menu (z,c);
}

void menu (FILE*z,Client c[])
{
int choice;
do{
printf ("1.Add Client\n2.Change Client Information\n3.Delete Client\n4.Search Client\n5.Calculate Energy Requirements\n6.View Clients\n7.Terminate Program\nChoose an option from above:");
scanf ("%d",&choice);
}while (choice<1||choice>7);
if (choice==1)
{
system ("cls");
void accept (FILE*,Client []);
accept (z,c);
}
if (choice==2)
{
system ("cls");
void change (FILE*,Client []);
change (z,c);
}
if (choice==3)
{
system ("cls");
void destroy (FILE*,Client []);
destroy (z,c);
}
if (choice==4)
{
system ("cls");
void search (FILE*,Client []);
search (z,c);
}
if (choice==5)
{
system ("cls");
void energy (FILE*,Client []);
energy (z,c);
}
if (choice==6)
{
system ("cls");
void view (FILE*,Client []);
view (z,c);
}
if (choice==7)
{
system ("cls");
void end (FILE*,Client []);
end (z,c);
}
}

void accept (FILE*z,Client c[])//Accepts data from the user.
{
int max=20,top=-1;
int y=0,num,choice,choice2;
if (top==max-1)
{
return;
}
top++;
printf("How Many Clients Do You Want To Add:");
scanf ("%d",&num);
system ("cls");
while (y<num)
{
printf ("\nEnter Client ID:");
scanf ("%d",&c[y].client_id);
printf ("Enter Buisness Name:");
scanf (" %[^\n]",c[y].business_name);
printf ("Enter Client First Name:");
scanf (" %[^\n]",c[y].client_first_name);
printf ("Enter Client Last Name:");
scanf (" %[^\n]",c[y].client_last_name);
printf ("Enter Buisness Address:");
scanf (" %[^\n]",c[y].address);
printf ("Enter Client Budget:");
scanf ("%f",&c[y].budget);
printf ("Enter Client Energy Requirements:");
scanf ("%f",&c[y].energy_requirements);
printf ("Enter Buisness Information:");
scanf (" %[^\n]",c[y].business_info);
y++;
}
do {//Asks the user if they want to enter more data or terminate program.
printf ("\n\nDo You Want To:\n1.EnterMore Clients\n2.Continue\n");
scanf ("%d",&choice);
}while (choice<1 || choice>2);
if (choice==1)
{
void accept (Client []);
accept (c);
}
else if (choice==2)
{
do{
printf ("\n\nDo You Want To:\n1.Go Back To The Main Menu\n2.Exit\n");
scanf ("%d",&choice2);
}while (choice2<1 || choice2>2);
if (choice2==1)
{
void menu (Client[]);
menu (c);
}
else if (choice2==2)
{
void end (FILE*,Client []);
end (z,c);
}
}
}

我只是想知道我是否正确地将数据推送到堆栈,以及是否可以使用上面的方法将信息从文件扫描到堆栈。

我想做的是设计一个可以用作堆栈的结构,并且可以使用 pop 函数填充该结构,例如在初始化函数中使用的函数。我的问题是,我对使用堆栈相当陌生,有时当我尝试使用堆栈运行我的程序时,我的防病毒程序卡巴斯基将其检测为特洛伊木马。我想知道的是我是否正确实现了堆栈。

最佳答案

所有错误都已得到解决,直到您需要定义在menu中调用的函数等...构建时,确保启用警告gcc -Wall -Wextra至少 -o yourprogram yourprogram.c 。除了上面的评论之外,我还在下面首先需要注意的地方添加了内联评论。我还提供了代码后当前编译的结果。完成所需函数的定义,然后编辑上面的问题并包含您遇到的任何新问题。祝你好运:

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

typedef struct {
int client_id;
char business_name[30];
char client_first_name[20];
char client_last_name[20];
char address[40];
float budget;
float energy_requirements;
char business_info[300];
} Client;

int main () {
Client c[20];
FILE *z;
void initialise (FILE *, Client[]);
initialise (z, c);
system ("PAUSE");
return 0;
}

void initialise (FILE * z, Client c[]) {
int x;
int max = 20;
int top = -1;
if (top == (max - 1)) {
return;
}
top++;
for (x = 0; x < 20; x++) //Assigns all places in the structure to -1 and NULL
{
c[x].client_id = -1;
strcpy (c[x].business_name, "NULL");
strcpy (c[x].client_first_name, "NULL");
strcpy (c[x].client_last_name, "NULL");
strcpy (c[x].address, "NULL");
c[x].budget = -1;
c[x].energy_requirements = -1;
strcpy (c[x].business_info, "NULL");
}
z = fopen ("Novus.txt", "r");
for (x = 0; x < 20; x++) //Replaces values in structure with data from text file
{
fscanf (z,
"%d %[^\n]s %[^\n]s %[^\n]s %f %f %[^\n]s", /* format may need further help */
&c[x].client_id, c[x].business_name, c[x].client_first_name,
c[x].address, &c[x].budget, &c[x].energy_requirements,
c[x].business_info);
}
fclose (z);
void menu (FILE *, Client[]); /* must be previously declared/defined */
menu (z, c); /* you just closed 'z' two line up ?? */
}

void menu (FILE * z, Client c[]) {
int choice;
do {
printf
("1.Add Client\n2.Change Client Information\n3.Delete Client\n4.Search Client\n5.Calculate Energy Requirements\n6.View Clients\n7.Terminate Program\nChoose an option from above:");
scanf ("%d", &choice);
} while (choice < 1 || choice > 7);
if (choice == 1) {
system ("cls");
void accept (FILE *, Client[]); /* must be previously declared/defined */
accept (z, c);
}
if (choice == 2) {
system ("cls");
void change (FILE *, Client[]); /* must be previously declared/defined */
change (z, c);
}
if (choice == 3) {
system ("cls");
void destroy (FILE *, Client[]); /* must be previously declared/defined */
destroy (z, c);
}
if (choice == 4) {
system ("cls");
void search (FILE *, Client[]); /* must be previously declared/defined */
search (z, c);
}
if (choice == 5) {
system ("cls");
void energy (FILE *, Client[]); /* must be previously declared/defined */
energy (z, c);
}
if (choice == 6) {
system ("cls");
void view (FILE *, Client[]); /* must be previously declared/defined */
view (z, c);
}
if (choice == 7) {
system ("cls");
void end (FILE *, Client[]); /* must be previously declared/defined */
end (z, c);
}
}

void accept (FILE * z, Client c[]) //Accepts data from the user.
{
int max = 20;
int top = -1;
int y = 0, num, choice, choice2;
if (top == (max - 1)) {
return;
}
top++;
printf ("How Many Clients Do You Want To Add:");
scanf ("%d", &num); /* with multiple uses of scanf, you will need to flush the input buffer to */
system ("cls"); /* eliminate remaining '\n' chars that remain after the user presses 'Enter' */
while (y < num) { /* a simple 'int c;... do {c = getchar();} while (c != '\n'); after each scanf */
printf ("\nEnter Client ID:"); /* will suffice. (note 'c' is declared as an 'int' */
scanf ("%d", &c[y].client_id);
printf ("Enter Buisness Name:");
scanf (" %[^\n]", c[y].business_name);
printf ("Enter Client First Name:");
scanf (" %[^\n]", c[y].client_first_name);
printf ("Enter Client Last Name:");
scanf (" %[^\n]", c[y].client_last_name);
printf ("Enter Buisness Address:");
scanf (" %[^\n]", c[y].address);
printf ("Enter Client Budget:");
scanf ("%f", &c[y].budget);
printf ("Enter Client Energy Requirements:");
scanf ("%f", &c[y].energy_requirements);
printf ("Enter Buisness Information:");
scanf (" %[^\n]", c[y].business_info);
y++;
}
do { //Asks the user if they want to enter more data or terminate program.
printf ("\n\nDo You Want To:\n1.EnterMore Clients\n2.Continue\n");
scanf ("%d", &choice);
} while (choice < 1 || choice > 2);
if (choice == 1) {
accept (z, c);
} else if (choice == 2) {
do {
printf
("\n\nDo You Want To:\n1.Go Back To The Main Menu\n2.Exit\n");
scanf ("%d", &choice2);
} while (choice2 < 1 || choice2 > 2);
if (choice2 == 1) {
menu (z, c);
} else if (choice2 == 2) {
end (z, c);
}
}
}

当前编译器错误:

$ gcc -Wall -Wextra -o bin/addrstack addrstack.c

addrstack.c: In function ‘accept’:
addrstack.c:148:17: warning: implicit declaration of function ‘end’ [-Wimplicit-function-declaration]
end (z, c);
^
addrstack.c:97:18: note: previous declaration of ‘end’ was here
void end (FILE *, Client[]); /* must be previously declared/defined */
^
addrstack.c:148:17: error: incompatible implicit declaration of function ‘end’
end (z, c);
^
addrstack.c:97:18: note: previous implicit declaration of ‘end’ was here
void end (FILE *, Client[]); /* must be previously declared/defined */

关于c - 从文件扫描时如何使用结构实现堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26644721/

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