這是本人做的一個電子元器件管理系統(tǒng)
希望能夠幫助到需要的人
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#define HEAD1 " 元器件管理系統(tǒng) \n"
#define HEAD2 "******************************************\n"
#define DATE "編號\t價格\t類型\t數(shù)目\t型號\t日期\t廠家\t封裝\n"
#define LEN sizeof(struct component)
struct component
{
char name[10]; //編號
double num; //價格
char type[10]; //類型.
int no; //數(shù)目
char model[30]; //型號
double date; //日期
char factory[20]; //廠家
char fz[10] ; //封裝
struct component *next;
};
int TOTAL_NUM = 0;
struct component *head = NULL;
void wrong();
void welcome (); //歡迎界面
void mainmenu (); //主界面
void record (); //記錄數(shù)據(jù)
void insert(struct component *stu); //插入數(shù)據(jù)
void display(struct component *stu); //顯示一個元器件的信息
void displayAll (); //顯示所有元器件的信息
void query_by_num (); //按價格查詢元器件信息
void readData (); //讀取文件里元器件的信息
void writeData (); //向文件寫入元器件信息
void freeAll (); //清空鏈表內容
void del (); //刪除元器件信息
void change (); //更改元器件信息
void devise (struct component *p); //選擇更改內容
void save();
/*------------------------------------主菜單------------------------------------*/
int main (void)
{
welcome ();
//顯示主菜單
mainmenu ();
return 0;
}
//顯示歡迎信息
void welcome ()
{
printf (HEAD2);
printf (HEAD1);
getchar();
system("cls"); /*清屏*/
}
void wrong()
{
printf("error!請重新選擇!\n");
getchar();
}
//系統(tǒng)主菜單
void mainmenu ()
{
int select;
select = -1;
readData ();
system("cls");
do
{
getchar();
printf (HEAD1);
printf (" [1]----添加新的元器件 \n");
printf (" [2]----統(tǒng)計元器件總數(shù) \n");
printf (" [3]----查詢信息 \n");
printf (" [4]----刪除元器件 \n");
printf (" [5]----修改元器件 \n");
printf (" [6]----保存和加載元器件 \n");
printf (" [0]----退出系統(tǒng) \n");
printf ("請輸入您的選擇:");
scanf ("%d", &select);
switch (select)
{
case 0:
getchar();
break;
case 1:
record ();
break;
case 2:
displayAll ();
break;
case 3:
query_by_num ();
break;
case 4:
del ();
break;
case 5:
change ();
break;
case 6:
save();
break;
default:
wrong();
break;
}
}
while (select != 0);
}
/*------------------------------------錄入元器件信息------------------------------------*/
void record ()
{
struct component *p0;
system("cls"); /*清屏*/
p0 = (struct component *)malloc(LEN);
printf ("\n\n\n\n\n\n\n");
printf ("\t\t請輸入元器件編號:");
scanf ("%s",p0->name);
printf ("\t\t請輸入元器件價格:");
scanf ("%lf",&p0->num);
printf ("\t\t請輸入元器件的類型:");
scanf ("%s",p0->type);
printf ("\t\t請輸入元器件的數(shù)目:");
scanf ("%d",&p0->no);
printf ("\t\t請輸入元器件的型號:");
scanf ("%s",p0->model);
printf ("\t\t請輸入元器件的日期:");
scanf ("%lf",&p0->date);
printf ("\t\t請輸入元器件的廠家:");
scanf ("%s",p0->factory);
printf ("\t\t請輸入元器件的封裝:");
scanf ("%s",p0->fz);
insert (p0); //插入數(shù)據(jù)
printf ("該元器件的信息為:\n");
printf ("-------------------------------------------------------------------------------\n");
printf (DATE);
display (p0);
}
void insert (struct component *stu)
{
struct component *p0, *p1, *p2;
p1 = head;
p0 = stu;
if (head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while ((p0->num > p1->num)&&(p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num)
{
if (head == p1)
head = p0;
else
p2->next = p0;
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
TOTAL_NUM++;
}
//瀏覽元器件信息
void display (struct component *p)
{
printf ("%s\t%.0lf\t%d\t%s\t%s\t%.0f\t%s\t%s\n", p->name, p->num, p->no, p->type, p->model, p->date, p->factory,p->fz);
}
void displayAll()
{
struct component *p;
printf("元器件總數(shù):%d\n", TOTAL_NUM);
p = head;
if (head != NULL)
{
system("cls"); /*清屏*/
printf(DATE);
printf("-------------------------------------------------------------------------------\n");
do
{
display(p);
p = p->next;
}
while(p != NULL);
}
printf ("\n");
getchar();
}
//按價格查詢元器件信息
void query_by_num ()
{
int num;
struct component *p1;
printf("請輸入元器件的價格:");
scanf("%ld", &num);
if(head==NULL)
{
printf("無元器件記錄!\n");
return;
}
p1 = head;
while (num!=p1->num && p1->next!=NULL)
p1 = p1->next;
if (num == p1->num)
{
printf (DATE);
printf ("-------------------------------------------------------------------------------\n");
display (p1);
}
else
printf ("沒有該元器件記錄,請核對!");
system("pause"); /*暫停*/
system("cls"); /*清屏*/
}
//寫入文件
void writeData ()
{
FILE* fp;
/*------------------------------------文件指針------------------------------------*/
struct component *p;
fp = fopen("1.txt", "w");
if (!fp)
{
printf("文件打開錯誤!");
return;
}
fprintf(fp,"%d\n", TOTAL_NUM);
for(p = head; p!= NULL; p= p->next)
{
fprintf(fp,"%s\t%ld\t%s\t%d\t%s\t%.0f\t%s\n", p->name, p->num, p->type, p->no, p->model, p->date, p->factory);
}
fclose (fp);
}
void freeAll ()
{
struct component *p1, *p2;
p1 = p2=head;
while(p1)
{
p2=p1->next;
free (p1);
p1 = p2;
}
}
/*------------------------------------讀取文件------------------------------------*/
void readData ()
{
FILE* fp;//文件指針
struct component *p1, *p2;
fp = fopen("F:\\xitong\\1.txt", "r");
if (!fp)
{
printf("文件打開錯誤!");
return;
}
fscanf(fp,"%d\n", &TOTAL_NUM);
head = p1 = p2 = (struct component *)malloc(LEN);
fscanf(fp,"%s\t%ld\t%s\t%d\t%s\t%lf\t%s\n", p1->name, &p1->num, p1->type, &p1->no, p1->model, &p1->date, p1->factory);
while(!feof(fp))
{
p1 = (struct component *)malloc(LEN);
fscanf(fp,"%s\t%ld\t%s\t%d\t%s\t%lf\t%s\n", p1->name, &p1->num, p1->type, &p1->no, p1->model, &p1->date, p1->factory);
p2->next = p1;
p2 = p1;
}
p2->next = NULL;
fclose(fp);
}
/*------------------------------------刪除元器件信息------------------------------------*/
void del ()
{
struct component *p1, *p2;
long int num;
if (head == NULL)
{
system("cls");
printf("無記錄!請輸入相關信息\n");
getchar;
return;
}
printf("請輸入存在的編號:");
scanf("%ld", &num);
p1 = head;
while (num != p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->num)
{
if(p1 == head)
head = p1->next;
else p2->next = p1->next;
free(p1);
TOTAL_NUM--;
}
else
printf("沒有找到此元器件!\n");
}
/*------------------------------------修改元器件信息------------------------------------*/
void change ()
{
struct component *p1, *p2;
long int num;
if (head == NULL)
{
printf ("無元器件記錄!\n");
return;
}
printf ("請輸入您要修改的元器件的價格:");
scanf ("%ld", &num);
p1 = head;
while (num != p1->num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->num)
devise (p1);
else
printf("沒有該元器件記錄,請核對!\n");
}
void devise (struct component *p)
{
int choice;
choice = -1;
do
{
printf("請選擇您要修改的元器件的信息內容:\n");
printf("+----------------------+\n");
printf("| 編號 請按 1 |\n");
printf("| 價格 請按 2 |\n");
printf("| 類型 請按 3 |\n");
printf("| 數(shù)目 請按 4 |\n");
printf("| 型號 請按 5 |\n");
printf("| 日期 請按 6 |\n");
printf("| 廠家 請按 7 |\n");
printf("| 取消 請按 0 |\n");
printf("+----------------------+\n");
printf("請輸入您的選擇:");
scanf("%d", &choice);
switch (choice)
{
case 0:
return;
case 1:
printf("請輸入新編號:");
scanf("%s", p->name);
break;
case 2:
printf("請輸入新價格:");
scanf("%d", &p->num);
break;
case 3:
printf("請輸入新類型:");
scanf("%s", p->type);
break;
case 4:
printf("請輸入新數(shù)目:");
scanf("%s", &p->no);
break;
case 5:
printf("請輸入新型號:");
scanf("%s", p->model);
break;
case 6:
printf("請輸入新日期:");
scanf("%lf", &p->date);
break;
case 7:
printf("請輸入新:");
scanf("%s", p->factory);
break;
default:
printf("\n無效選項!");
break;
}
}
while(choice != 0);
}
typedef struct node
{
struct component date;
struct node*next;
}Node,*Link;
void save()
{
int saveflag;
FILE *fp;
Node *p;
int count=0;
if((fp=fopen("F:\\xitong\\1.txt","wb"))==NULL)
{
printf("cannot open the file.\n");
getchar();
return;
}
p = p->next;
while(p)
{
if (fwrite(p,sizeof(Node),1,fp)==1)
{
p = p->next;
count++;
}
else
{break;
}
}
if(count>0)
{
getchar();
printf("save!toatal:%d\n",count);
getchar();
saveflag = 0;
}
else{
system("cls");
getchar();
}
fclose(fp);
}
全部資料51hei下載地址:
元器件管理系統(tǒng).zip
(51.96 KB, 下載次數(shù): 57)
2019-12-25 15:22 上傳
點擊文件名下載附件
|