#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函数填充,例如初始化函数中使用的pop函数。我的问题是,我使用堆栈是相当新的,有时当我尝试使用堆栈运行我的程序时,我的防病毒程序卡巴斯基将其检测为特洛伊木马。我想知道的是,我是否正确实现了堆栈。
答案 0 :(得分:0)
所有错误都已解决,您需要定义在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 */
答案 1 :(得分:-1)
我将以下代码用于任何类型的通用堆栈。
在示例中,我创建了一个int
的堆栈。然而,您可以将类型的类型设置为名称,并使用DeclareStackType(YourType)
声明堆栈的数据结构,然后使用stack__YourType foo;
来声明堆栈。有关详细信息,请参阅测试代码。
#include <malloc.h>
#define DeclareStackType(ValueType) \
typedef struct { \
int size; \
ValueType *buf; \
int index; \
} stack_ ## ValueType
#define InitStack(stack, ValueType, sz) do {stack.size = sz; stack.buf=(ValueType*)malloc(sizeof(ValueType)*sz); stack.index = 0;} while (0)
// this re-uses the already initialized buffer. Just reset index to zero.
#define ClearStack(stack) do {stack.index = 0;} while(0)
#define PushStack(stack, value) do {stack.buf[stack.index++] = value; } while(0)
#define PopStack(stack) (stack.buf[--stack.index])
#define IsStackFull(stack) (stack.index == stack.size)
#define IsStackEmpty(stack) (stack.index == 0)
#define PushStackSafe(stack, value) do {if (!IsStackFull(stack)) stack.buf[stack.index++] = value;} while(0)
#define PopStackSafe(stack) (stack.buf[IsStackEmpty(stack)? 0 : (stack.index -= 1)])
// -------- test codes (in C++, since C++ can compile C program, I am lazy to do C printf...)---------------
#include <iostream>
DeclareStackType(int);
stack_int my_integer_stack;
using namespace std;
int main(int argc, char *argv[])
{
InitStack(my_integer_stack, int, 5);
if (IsStackEmpty(my_integer_stack)) {
cout << "1. stack empty now" << endl;
}
PushStack(my_integer_stack, 1);
PushStack(my_integer_stack, 2);
ClearStack(my_integer_stack); // clear the above input
if (IsStackEmpty(my_integer_stack)) {
cout << "2. stack empty now" << endl;
}
PushStack(my_integer_stack, 3);
PushStack(my_integer_stack, 5);
PushStack(my_integer_stack, 1);
PushStack(my_integer_stack, 2);
PushStackSafe(my_integer_stack, 8);
if (IsStackFull(my_integer_stack)) {
cout << "3. stack full now" << endl;
}
PushStackSafe(my_integer_stack, 9); // won't push
cout << PopStack(my_integer_stack) << endl;
cout << PopStack(my_integer_stack) << endl;
cout << PopStack(my_integer_stack) << endl;
cout << PopStack(my_integer_stack) << endl;
cout << PopStack(my_integer_stack) << endl;
if (IsStackEmpty(my_integer_stack)) {
cout << "4. stack empty now" << endl;
}
cout << PopStackSafe(my_integer_stack) << endl; // pop will not go to index -1, yet it returns buf[0] in the stack
}