如何在扫描文件时使用结构实现堆栈 #包括 #包括 类型定义结构 { int客户端id; char business_name[30]; char client_first_name[20]; char client_last_name[20]; 字符地址[40]; 浮动预算; 浮子能量要求; char business_info[300]; }客户; main() { 客户c[20]; 文件*z; 无效初始化(文件*,客户端[]); 初始化(z,c); 系统(“暂停”); } 无效初始化(文件*z,客户端c[]) { int x,max=20,top=-1; 如果(顶部==最大值-1) { 返回; } top++; 对于(x=0;x
我使用以下代码作为任何类型的通用堆栈。 在本例中,我创建了一个如何在扫描文件时使用结构实现堆栈 #包括 #包括 类型定义结构 { int客户端id; char business_name[30]; char client_first_name[20]; char client_last_name[20]; 字符地址[40]; 浮动预算; 浮子能量要求; char business_info[300]; }客户; main() { 客户c[20]; 文件*z; 无效初始化(文件*,客户端[]); 初始化(z,c); 系统(“暂停”); } 无效初始化(文件*z,客户端c[]) { int x,max=20,top=-1; 如果(顶部==最大值-1) { 返回; } top++; 对于(x=0;x,c,function,stack,C,Function,Stack,我使用以下代码作为任何类型的通用堆栈。 在本例中,我创建了一个int堆栈。但是您可以将类型定义为名称,使用DeclareStackType(YourType)声明堆栈的数据结构,然后使用stack\uuuyourtype foo;声明堆栈。有关详细信息,请参阅测试代码 #include <stdio.h> #include <string.h> typedef struct { int client_id; char business_name [30]
int
堆栈。但是您可以将类型定义为名称,使用DeclareStackType(YourType)
声明堆栈的数据结构,然后使用stack\uuuyourtype foo;
声明堆栈。有关详细信息,请参阅测试代码
#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);
}
}
}
#包括
#定义DeclareStackType(ValueType)\
类型定义结构{\
整数大小\
ValueType*buf\
整数指数\
}堆栈###值类型
#定义InitStack(stack,ValueType,sz)do{stack.size=sz;stack.buf=(ValueType*)malloc(sizeof(ValueType)*sz);stack.index=0;}而(0)
//这将重新使用已初始化的缓冲区。只需将索引重置为零。
#定义ClearStack(stack)do{stack.index=0;}while(0)
#定义PushStack(stack,value)do{stack.buf[stack.index++]=value;}而(0)
#定义PopStack(stack)(stack.buf[--stack.index])
#定义IsStackFull(堆栈)(stack.index==stack.size)
#定义IsStackEmpty(堆栈)(stack.index==0)
#定义PushStackSafe(stack,value)do{if(!IsStackFull(stack))stack.buf[stack.index++]=value;}而(0)
#定义PopStackSafe(stack)(stack.buf[IsStackEmpty(stack)?0:(stack.index-=1)])
//------测试代码(C++中,C++编译C程序,我懒得做C PrtTF…)------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#包括
DeclareStackType(int);
stack_int my_integer_stack;
使用名称空间std;
int main(int argc,char*argv[])
{
InitStack(my_integer_stack,int,5);
if(IsStackEmpty(my_integer_堆栈)){
cout所有错误都已解决,直到您需要定义在菜单中调用的函数为止,等等。在构建时,确保至少启用警告gcc-Wall-Wextra-o yourprogram yourprogram.c
。除了上面的注释外,我还在下面的内联注释中添加了注意事项eded。我还提供了当前编译的结果,代码如下。完成所需函数的定义,然后编辑上面的问题,包括您遇到的任何新问题。祝您好运:
#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
}
此语句是什么void菜单(文件*,客户端[]);
在void初始化(文件*z,客户端c[])结束时执行的操作
function??很明显,您还远远没有完成代码。您通常可以将结构数组的地址作为指针传递给您的函数,但不会阻止您这样做。我怀疑您需要在fscanf(z),%d\n%[^\n]\n%中清理格式[^\,,
存在大量需要更正的错误。编译时,请确保在最低限度使用时启用警告gcc-Wall-Wextra-o yourproject yourproject.c
(或您使用的任何编译器)。这将确定关注的领域。我将添加一个已清理的版本(尽可能)下面。欢迎访问Stack Overflow。只是想跟进上面bit的评论:您目前的问题不适合此网站。如果您想查看代码,请访问codereview.stackexchange.com。如果您有实际问题(而不是一般问题)请阅读下面的内容:C的帮助,现在是C++的改写。答案是C.。我只是懒得用C++编写测试代码。测试代码不属于答案。把测试代码删掉。(从#include
开始事实上,如果您使用printf
替换所有cout
,并删除#include
和使用名称空间std
,那么整个过程将成为一个完美的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 */