C语言 求助

我是刚学C语言的新手
要编辑一个小的 商品管理系统
进货 用栈写 理货用 队列写 要求可以查出商品信息(名称,条码,进货日期)
哪位高人 知道 麻烦写一份参考下~谢谢~!

第1个回答  2020-01-13
第2个回答  2006-10-23
这是我当时编的教务信息管理系统的代码,希望能给你点帮助
/*--------------------------------------------my.h-------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>

#define MAX 10
#define N 20
#define M 10
#define ON 1
#define OFF 0

struct HEAD{ /* 头结构 */
char ADMkey[M]; /* 系统管理员密码 */
unsigned classes; /* 班级总数 */
struct CLASS * first;
};

struct CLASS{ /* 班级结构 */
unsigned no; /* 班级号码 */
unsigned students; /* 班级学生总数 */
char ADMkey[M]; /* 班级管理员密码 */
float score[MAX]; /* 班级个课平均分 */
float average; /* 班级总平均分 */
struct STUD* first;
struct CLASS* next;
};

struct STUD{ /* 学生结构 */
long number; /* 学生号码 */
unsigned subjects; /* 学生所选课程总数 */
unsigned age; /* 学生年龄 */
float average; /* 学生总平均分 */
char STUkey[M]; /* 学生密码 */
char name[N]; /* 学生姓名 */
char kind[M]; /* 学生政治面貌 */
char sex[M]; /* 学生性别 */
char bir_p[N]; /* 学生籍贯 */
char add[N]; /* 学生住址 */
long tel; /* 学生联系电话 */
struct STUD* next;
struct SUB* first;
};

struct SUB{ /* 课程结构 */
unsigned number; /* 课程号码 */
char name[N]; /* 课程名称 */
unsigned credit; /* 课程学分 */
unsigned ps_score; /* 课程平时成绩 */
unsigned tx_score; /* 课程考试成绩 */
unsigned un_score; /* 课程综合成绩 */
struct SUB* next;
};

int chk_key(int); /* 校对密码 */
int chk_score(); /* 校对成绩 */
int chk_sbj(); /* 校对课程 */
int chk_stu(); /* 校对学生 */
int chk_cla(struct HEAD *); /* 校对班级 */
int cmp_key(char *,char *); /* 比较密码 */
int set_cursor_type(int ); /* 设置光标状态 */
void box(int,int); /* 画边框 */
void reset_scr(); /* 重新设置屏幕 */
void recover(int,int,int,int); /* 恢复主菜单 */
void vertion(); /* 版本介绍 */
void tips(int); /* 提示标语 */
void method(); /* 热键介绍 */
void introduce(); /* 主要函数介绍 */
void savefile(); /* 保存 */
void openfile(); /* 打开 */
void freeroom(); /* 释放空间 */
void getkey(); /* 获取键值 */
void desktop(); /* 画屏幕 */
void outputinpages(int ,int ); /* 分页打印 */
void screen(int ,int ,int ,int ,char *p[]); /* 显示菜单 */
void write_file(struct HEAD *,FILE *); /* 保存的子函数 */
void read_file(struct HEAD *,FILE *); /* 打开的子函数 */
void input_stu(struct STUD *); /* 录入学生信息 */
void input_sbj(struct SUB *); /* 录入课程信息 */
void by_name(struct HEAD *); /* 通过姓名查找 */
void by_number(struct HEAD *); /* 通过学号查找 */
void by_age(struct HEAD *); /* 通过年龄查找 */
void by_telephone(struct HEAD *); /* 通过电话号码查找 */
void impass(struct HEAD *); /* 未通过考试的学生信息 */
void output(struct HEAD *); /* 输出一个班级所有的学生信息 */
void average(struct HEAD *); /* 输出一个班级所有的课程的平均成绩 */
void incredit(struct HEAD *); /* 输出所有的学生已得学分信息 */
void sort(struct HEAD *); /* 按成绩高低输出一览表 */
void presort(struct HEAD *,int); /* 初排序 */
void recalculate(); /* 重新计算分数 */
void scoretable(struct HEAD *); /* 按不同分数输出成绩一览表 */
void create(struct HEAD *); /* 创建数据文件 */
void mod_stu(struct HEAD *); /* 修改学生信息 */
void mod_sbj(struct HEAD *); /* 修改课程信息 */
void mod_key(struct HEAD *); /* 修改密码 */
void mod_score(struct HEAD *); /* 修改成绩 */
void del_sbj(struct HEAD *); /* 删除课程信息 */
void del_stu(struct HEAD *); /* 删除学生信息 */
void del_cla(struct HEAD *); /* 删除班级信息 */
void ins_cla(struct HEAD *); /* 插入班级信息 */
void ins_stu(struct HEAD *); /* 插入学生信息 */
void ins_sbj(struct HEAD *); /* 插入课程信息 */
void turn(struct HEAD *,int ,...); /* 翻页处理 */

FILE *fp;
struct HEAD head;
struct CLASS * p1;
struct STUD * p2;
struct SUB * p3;
int key0,key1,key,test,j=1;
char OUT0[100][5][20];
long OUT1[100][2];
unsigned OUT2[100];
char buf[50*10*2],buf1[50*2];
char *chp[ ]= {/* the main menu */
"File", "Search", "Insert", "Modify", "Delete", "Estimate", "Help",
};
char * file[]={ /* the sub menus */
"Create ",
"Open ALT+F3 ",
"Save F2 ",
"Exit F4 ",
};
char * search[]={
"By name ",
"By number ",
"By telephone ",
"By age ",
};
char * insert[]={
"Class ",
"Student ",
"Subject ",
};
char * modify[]={
"Student ",
"Subject ",
"Score ",
"Password ",
};
char * delete[]={
"Class ",
"Student ",
"Subject ",
};
char * estimate[]={
"Class Averages ",
"Impass of a class ",
"Incredit of all ",
"Export table ",
"Sort & Output ",
"Export In Levels ",
};
char * help[]={
"Introduct ",
"Hotkey F8 ",
"Edition ",
};

void main()
{
int ch,state,x,highth,s,width;
head.first=NULL;
head.classes=0;
desktop();
state=OFF; /* 设置初始状态 */

while(1){
key=0;
getkey();
inx: if(key==45||key==62) exit(0); /* press alt+x or F4*/
if(key==33||key==68){ /* press alt+f or F10*/
x=4, s=0 ,highth=4 ,width=17;
screen(highth,width,x,s,file); /* 设置屏幕 */
if(key!=33&&key!=68) goto inx; /* 左右切换处理 */
if(key0==13) /* enter键操作 */
switch(test){
case 1: create(&head); break;
case 2: openfile(); state=ON; /* 改变初始状态 */
break;
case 3: state?tips(3):tips(6); /* 提示信息 */
while(isspace(ch=getchar()));
if(ch=='y'||ch=='Y') savefile(); /* 保存 */
else reset_scr(); /* 重新设置屏幕 */
break;
case 4: state?tips(3):tips(6);
while(isspace(ch=getchar()));
if(ch=='y'||ch=='Y'){
savefile();
freeroom();
}
exit(0); /* 退出 */
default: break;
}
if(key0==27) recover(highth,width,x,s); /* esc key 恢复主菜单 */
}
if(key==31){ /*press alt+s*/
x=11, highth=4, s=1,width=17;
screen(highth,width,x,s,search);
if(key!=31) goto inx;
if(key0==13)
switch(test){
case 1: by_name(&head); goto inx;
case 2: by_number(&head); break;
case 3: by_telephone(&head); goto inx;
case 4: by_age(&head); goto inx;
default: break;
}
if(key0==27) recover(highth,width,x,s); /* esc key 恢复主菜单 */
}
if(key==23){ /* alt+i */
x=20, s=2, highth=3,width=16;
screen(highth,width,x,s,insert);
if(key!=23) goto inx;
if(key0==13)
switch(test){
case 1: ins_cla(&head); break;
case 2: ins_stu(&head); break;
case 3: ins_sbj(&head); break;
default: break;
}
if(key0==27) recover(highth,width,x,s); /* esc key 恢复主菜单 */
}
if(key==50) { /*alt+m*/
x=29, s=3, highth=4, width=16;
screen(highth,width,x,s,modify);
if(key!=50) goto inx;
if(key0==13)
switch(test){
case 1: mod_stu(&head); break;
case 2: mod_sbj(&head); break;
case 3: mod_score(&head); break;
case 4: mod_key(&head); break;
default: break;
}
if(key0==27) recover(highth,width,x,s); /* esc key 恢复主菜单 */
}
if(key==32){ /* alt+d */
x=38, s=4, highth=3,width=16;
screen(highth,width,x,s,delete);
if(key!=32) goto inx;
if(key0==13)
switch(test){
case 1: del_cla(&head); break;
case 2: del_stu(&head); break;
case 3: del_sbj(&head); break;
default: break;
}
if(key0==27) recover(highth,width,x,s); /* esc key 恢复主菜单 */
}
if(key==18){ /*print alt+e*/
x=47, s=5, highth=6, width=23;
screen(highth,width,x,s,estimate);
if(key!=18) goto inx;
if(key0==13)
switch(test){
case 1: average(&head); break;
case 2: impass(&head); break;
case 3: incredit(&head); break;
case 4: output(&head); break;
case 5: sort(&head); break;
case 6: scoretable(&head); goto inx;
default: break;
}
if(key0==27) recover(highth,width,x,s); /* esc key 恢复主菜单 */
}
if(key==35||key==59) { /*print alt+h*/
x=58, s=6, highth=3, width=21;
screen(highth,width,x,s,help);
if(key!=35&&key!=59) goto inx;
if(key0==13)
switch(test){
case 1: introduce(); break;
case 2: method(); break;
case 3: vertion(); break;
default: break;
}
if(key0==27) recover(highth,width,x,s); /* esc key 恢复主菜单 */
}
}
}

void create(struct HEAD *headp) /* 创建链表并录入基本信息 */
{
int ch,sum;
unsigned credit;
cprintf("\t Input head administer's password :");
scanf("%s",headp->ADMkey);
do{
p1=(struct CLASS *)malloc(sizeof(struct CLASS));
cprintf("\t Input class number :");
scanf("%u",&p1->no);
cprintf("\t Input class administer key words :");
scanf("%s",p1->ADMkey);
p1->next=headp->first;
headp->first=p1; /* 连接进链表 */
p1->first=NULL;
p1->students=0;
do{
sum=0;
credit=0;
p2=(struct STUD*)malloc(sizeof(struct STUD));
cprintf("\n\t Input student's number:");
scanf("%ld",&p2->number);
input_stu(p2); /* 录入学生信息 */
p2->next=p1->first; /* 连接进链表 */
p1->first=p2;
p2->first=NULL;
p2->subjects=0;
do{
p3=(struct SUB*)malloc(sizeof(struct SUB));
input_sbj(p3); /* 录入课程信息 */
sum+=p3->credit*p3->un_score;
credit+=p3->credit;
p3->next=p2->first; /* 连接进链表 */
p2->first=p3;
p2->subjects++;
cprintf("\t continue input subject?(y\\n)");
while(isspace(ch=getchar()));
}while(ch=='y'||ch=='Y');
p2->average=sum/(float)credit; /* 计算学生总平均分 */
p1->students++;
cprintf("\t continue input student?(y\\n)");
while(isspace(ch=getchar()));
}while(ch=='y'||ch=='Y');
headp->classes++;
cprintf("\t continue input class?(y\\n)");
while(isspace(ch=getchar()));
}while(ch=='y'||ch=='Y');
}

void savefile() /* 保存 */
{
char filename[N];
tips(7);
scanf("%s",filename);
if((fp=fopen(filename,"wb"))==NULL){ /* 无法打开文件 */
fprintf(stderr,"error:can't create file!\n");
return;
}
rewind(fp);
fwrite(&head,sizeof(struct HEAD),1,fp);
write_file(&head,fp);
fclose(fp);
}

void openfile() /* 打开 */
{
char filename[N];
tips(7);
scanf("%s",filename);
fp=fopen(filename,"rb");
fread(&head,sizeof(struct HEAD),1,fp);
head.first=NULL;
read_file(&head,fp);
fclose(fp);
}

void write_file(struct HEAD *headp,FILE *myfile) /*write all the node into data file*/
{
for(p1=headp->first;p1;p1=p1->next){
fwrite(p1,sizeof(struct CLASS),1,myfile);
for(p2=p1->first;p2;p2=p2->next){
fwrite(p2,sizeof(struct STUD),1,myfile);
for(p3=p2->first;p3;p3=p3->next)
fwrite(p3,sizeof(struct SUB),1,myfile);
}
}
tips(4);
}

void read_file(struct HEAD *headp,FILE *myfile) /*read data reconstruct linetable*/
{
struct CLASS buf1,*tail1;
struct STUD buf2,*tail2;
struct SUB buf3,*tail3;
int n,m,k,flag=0;
for(tail1=headp->first,n=0;n<headp->classes;tail1=p1,++n){ /*create the class linetable*/
if(fread(&buf1,sizeof(struct CLASS),1,myfile)!=1){ /*Reach file end*/
headp->classes=n;
break;
}
p1=(struct CLASS *)malloc(sizeof(struct CLASS));
*p1=buf1;
/*combine first in first out line*/
if(!headp->first) headp->first=p1;
else tail1->next=p1;
p1->next=NULL;
p1->first=NULL;
/*construct students line*/
for(m=0,tail2=p1->first;m<p1->students;tail2=p2,++m){
if(fread(&buf2,sizeof(struct STUD),1,myfile)!=1){
p1->students=m;
break;
}
p2=(struct STUD*)malloc(sizeof(struct STUD));
*p2=buf2;
/*combine first in first out line*/
if(!p1->first) p1->first=p2;
else tail2->next=p2;
p2->next=NULL;
p2->first=NULL;

for(tail3=p2->first,k=0;k<p2->subjects;tail3=p3,++k){
if(fread(&buf3,sizeof(struct SUB),1,myfile)!=1){
p2->subjects=k;
break;
}
p3=(struct SUB *)malloc(sizeof(struct SUB));
*p3=buf3;

if(!p2->first) p2->first=p3;
else tail3->next=p3;
p3->next=NULL;
} /*end of subjects' line*/
} /*end of students' line*/
flag=1;
} /*end of class's line*/
if(flag) tips(4);
else tips(5);
}

void freeroom()
{
for(p1=head.first;p1;p1=p1->next){ /* 释放空间 */
for(p2=p1->first;p2;p2=p2->next){
for(p3=p2->first;p3;p3=p3->next) free(p3);
free(p2);
}
free(p1);
}
}

void getkey()
{
while(bioskey(1)==0);
key=bioskey(0);
key=key&0xff?key&0xff:key>>8;
}

void by_number(struct HEAD *headp) /* 通过学号查找 */
{
long k;
cprintf("\t Input the student's number:");
scanf("%ld",&k);
for(p1=headp->first;p1;p1=p1->next)
for(p2=p1->first;p2;p2=p2->next)
if(p2->number==k) break;
if(p2){
cprintf("|------------------------------------------------------------------------------|");
cprintf("| %-12ld%-10s%-8s%-10s%-6u%-12s%-10s%-9ld|",p2->number,p2->name,p2->sex,p2->bir_p,p2->age,p2->add,p2->kind,p2->tel);
cprintf("|------------------------------------------------------------------------------|");
}
else cprintf("\nThe student is not exist!");
}

void by_name(struct HEAD *headp) /* 通过姓名查找 */
{
char name[20];
cprintf("\t Input the student's name:");
scanf("%s",name);
turn(headp,2,name,1,1);
}

void by_age(struct HEAD *headp) /* 通过年龄查找 */
{
unsigned age;
cprintf("\t Input the student's age:");
scanf("%u",&age);
turn(headp,1,"a",age,1);
}

void by_telephone(struct HEAD *headp) /* 通过电话号码查找 */
{
long tel;
cprintf("\t Input the student's tel:");
scanf("%ld",&tel);
turn(headp,0,"aa",1,tel);
}

void turn(struct HEAD *headp,int op,...) /* 翻页处理 */
{
char name[20],*ps=name;
long tel;
unsigned age;
int flag=0,i=0;
va_list ptr; /* 多类不同形参处理 */
va_start(ptr,op);
ps=va_arg(ptr,char *);
age=va_arg(ptr,unsigned);
tel=va_arg(ptr,long);
va_end(ptr);
for(p1=headp->first;p1;p1=p1->next)
for(p2=p1->first;p2;p2=p2->next)
if(op==2?!strcmp(p2->name,ps):op==1?p2->age==age:p2->tel==tel){ /* 考人数组 */
strcpy(OUT0[i][1],p2->name);
strcpy(OUT0[i][2],p2->sex);
strcpy(OUT0[i][3],p2->bir_p);
strcpy(OUT0[i][4],p2->add);
strcpy(OUT0[i][0],p2->kind);
OUT1[i][0]=p2->number;
OUT1[i][1]=p2->tel;
OUT2[i]=p2->age;
i++; flag=1;
}
outputinpages(i,flag);
}

void outputinpages(int i,int flag)
{
int a=0,k,n,NUM=0;
if(flag){
inx:reset_scr();
cprintf("|------------------------------------------------------------------------------|");
for(k=NUM;k<8+NUM&&k<i&&k>=0;k++){
if(flag==1)
cprintf("| %-12ld%-10s%-8s%-12s%-6u%-12s%-8s%-9ld|",
OUT1[k][0],OUT0[k][1],OUT0[k][2],OUT0[k][3],OUT2[k],OUT0[k][4],OUT0[k][0],OUT1[k][1]);
else
cprintf("|\t number:%-12ld\t name:%-13s\t SUBname:%-10s \t un_score:%-5u|",
OUT1[k][0],OUT0[k][0],OUT0[k][1],OUT2[k]);
cprintf("|------------------------------------------------------------------------------|");
}
if(i>k&&!a){
a=1;
getch();
tips(1);
}
iny:getkey();
if(key==81){ /* pagedown */
if(k<i){ NUM+=8;
goto inx;
}
else { cprintf("\r\t It is the last page, more information, please press PAGEUP !");
goto iny;
}
}
if(key==73){ /* pageup */
if(k<=8){
cprintf("\r\t It is the first page,more information,please press PAGEDOWN!");
goto iny;
}
else { NUM-=8;
goto inx;
}
}
} /* END OF if(!flag) */
else cprintf("\t The student is not exit!");
}

void impass(struct HEAD *headp) /*Failed to pass the exam*/
{
unsigned no;
int ch,flag1,flag0;

do{
flag0=flag1=OFF;
if(chk_cla(headp)) break;
for(p2=p1->first;p2;p2=p2->next){
for(p3=p2->first;p3;p3=p3->next)
if(p3->un_score<60) flag1=flag0=ON;
if(flag1)cprintf(" \t %-15ld\t %-60s",p2->number,p2->name);

for(p3=p2->first;p3;p3=p3->next)
if(p3->un_score<60) cprintf(" \t subject:%-10s\t credit:%-3u\t ps_score:%-4u\t tx_score:%-4u\t un_score:%-5u",p3->name,p3->credit,p3->ps_score,p3->tx_score,p3->un_score);
flag1=OFF;
}
if(!flag0) cprintf(" \t all the student passed the exam! ");
cprintf("\n\r -----------------------------------------本回答被网友采纳
第3个回答  2019-05-21
可以使用C++嘛。