C語言小專案(學生資訊管理系統)
阿新 • • 發佈:2019-02-07
/*
執行環境:我是在linux裡面用gcc編譯的,在windows裡應該也能執行,用的一些檔案庫函式都是c標準庫,沒有用linux的系統呼叫(糾正一下:system("clear")這個系統呼叫是linux的,windows裡面用system("cls") )
(1)問題描述
學生資訊包括:學號,姓名,年齡,性別,出生年月,地址,電話,E-mail等。試設計一學生資訊管理系統,使之能提供以下功能:
1.系統以選單方式工作
2.學生資訊錄入功能(學生資訊用檔案儲存)---輸入
3.學生資訊瀏覽功能——輸出
4.查詢、排序功能——演算法
1、按學號查詢
2、按姓名查詢
5.學生資訊的刪除與修改(可選項)
(2)功能要求
1.介面簡單明瞭;
2.有一定的容錯能力,比如輸入的成績不在0~100之間,就提示不合法,要求重新輸入;
3.最好用連結串列的方式實現
*/
/*
介面:
-------------學生資訊管理系統---------------
1. 學生資訊錄入
2. 學生資訊瀏覽
3. 學生資訊查詢
1.按學號查詢
2.按姓名查詢
4. 學生資訊的刪除與修改
--------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
struct student_info {
int s_no;
char name[20];
int age;
char sex[10];
int birth;
char addr[30];
char tele[30];
char email[30];
struct student_info *next;
};
void menue(void);
int info_input(void);
int info_review(void);
int info_search(void);
int main(void)
{
menue();
return 0;
}
void menue(void )
{
int choose;
system("clear");
printf("-------------學生資訊管理系統---------------\n");
printf("\t\t1. 學生資訊錄入\n");
printf("\t\t2. 學生資訊瀏覽\n");
printf("\t\t3. 學生資訊查詢\n");
printf("\t\t4. 學生資訊的刪除與修改\n");
printf("\t\t0. 退出系統\n");
printf("--------------------------------------------\n");
printf("請輸入您的選擇(0~~4): ");
scanf("%d", &choose);
switch (choose) {
case 0:
exit(0);
break;
case 1:
info_input();
break;
case 2:
info_review();
break;
case 3:
info_search();
break;
}
while (choose > 4 || choose < 1) {
printf("您輸入了一個無效的選擇,請重新輸入(0-4): ");
scanf("%d", &choose);
}
}
/*
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
int open(const char *pathname, int flags, mode_t mode);
FILE *fopen(const char *path, const char *mode);
size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);
*/
int creat_list(void)
{
return 0;
}
// 1. 學生資訊錄入
int info_input(void)
{
struct student_info *head = NULL, *rear = NULL;
FILE *fp;
int flag = 1;
head = (struct student_info *)malloc(sizeof(struct student_info));
rear = head;
while (flag) {
struct student_info *stu = NULL;
stu = (struct student_info *)malloc(sizeof(struct student_info));
// FILE *fopen(const char *path, const char *mode);
fp = fopen("stuinfo.txt", "a+b");
fseek(fp, sizeof(struct student_info), 2);
system("clear");
printf("-----請依次輸入學生的資訊-----\n");
printf("學號: ");
scanf("%d", &stu->s_no);
printf("姓名: ");
scanf("%s", stu->name);
printf("年齡: ");
scanf("%d", &stu->age);
printf("性別: ");
scanf("%s", stu->sex);
printf("出生年月: ");
scanf("%d", &stu->birth);
printf("地址: ");
scanf("%s", stu->addr);
printf("電話: ");
scanf("%s", stu->tele);
printf("E-mail: ");
scanf("%s", stu->email);
fwrite(stu, sizeof(struct student_info), 1, fp);
rear->next = stu;
rear = stu;
fclose(fp);
printf("繼續輸入請按1,返回上一級選單請按2,退出請按0: ");
scanf("%d", &flag);
if (flag == 0) {
exit(0);
}
if (flag == 1) {
continue;
}
if (flag == 2) {
menue();
break;
}
}
rear->next = NULL;
return 0;
}
// 2. 學生資訊瀏覽
// size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
int info_review(void)
{
struct student_info stu;
int ret = 1;
int flag = 0;
FILE *fp = fopen("stuinfo.txt", "rb");
//int fd = open("stuinfo.txt", O_RDONLY);
if (fp == NULL) {
perror("fopen");
exit(0);
}
while (ret != 0) { /*返回值為1時表示讀取的位元組數不足sizeof(struct student_info), 返回0表示讀取成功*/
/* */
ret = fread(&stu, sizeof(struct student_info), 1, fp);
//read(fd, stdout, sizeof(struct student_info));
printf("------------------------------------------------------------------------------------------------------\n");
printf("%d\t%s\t%d\t%s\t%d\t%s\t%s\t%s\n", stu.s_no, stu.name, stu.age, stu.sex, stu.birth, stu.addr, stu.tele, stu.email);
}
fclose(fp);
printf("=====================^-^===================== ====================^-^==================\n");
printf("退出請按0, 返回上一級選單請按1: ");
scanf("%d", &flag);
if (flag == 0) {
exit(0);
}
if (flag == 1) {
menue();
exit(0);
}
return 0;
}
// 學生資訊查詢
int info_search(void)
{
system("clear");
struct student_info *stu1 = NULL, *stu2 = NULL;
stu1 = (struct student_info *)malloc(sizeof(struct student_info));
stu2 = (struct student_info *)malloc(sizeof(struct student_info));
int ret = 1, i = 0, flag = 1;
FILE *fp = fopen("stuinfo.txt", "rb");
printf("=====按姓名查詢請輸入1, 按學號查詢請輸入2=====\n");
scanf("%d", &i);
while (flag) {
if (i == 1) {
printf("請輸入學生的姓名: ");
scanf("%s", stu1->name);
do {
if (ret == 0) {
printf("沒有這個人哦^*^\n");
exit(0);
}
ret = fread(stu2, sizeof(struct student_info), 1, fp);
} while (strcmp(stu1->name, stu2->name));
printf("您要找的人資訊如下: \n");
printf("%d\t%s\t%d\t%s\t%d\t%s\t%s\t%s\n", stu2->s_no, stu2->name, stu2->age, stu2->sex, stu2->birth, stu2->addr, stu2->tele, stu2->email);
}
if (i == 2) {
printf("請輸入學生的學號: ");
scanf("%d", stu1->s_no);
do {
if (ret == 0) {
printf("沒有這個人哦^*^\n");
exit(0);
}
ret = fread(stu2, sizeof(struct student_info), 1, fp);
} while (stu2->s_no != stu1->s_no);
printf("您要找的人資訊如下: \n");
printf("%d\t%s\t%d\t%s\t%d\t%s\t%s\t%s\n", stu2->s_no, stu2->name, stu2->age, stu2->sex, stu2->birth, stu2->addr, stu2->tele, stu2->email);
}
printf("=====繼續查詢請按1,返回上一級選單請按2=====\n");
scanf("%d", &flag);
if (flag == 1)
continue;
if (flag == 2) {
menue();
break;
}
}
return 0;
}
// 學生資訊刪除
int info_delete(void)
{
}
// 學生資訊修改
int info_alter(void)
{
}
還有部分功能未完成,下次再寫吧(寫的有點煩-_-)
程式截圖: