Linux下學(xué)生信息管理系統(tǒng)方案_第1頁
Linux下學(xué)生信息管理系統(tǒng)方案_第2頁
Linux下學(xué)生信息管理系統(tǒng)方案_第3頁
免費(fèi)預(yù)覽已結(jié)束,剩余34頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、學(xué)習(xí)并分別使用標(biāo)準(zhǔn) C的文件操作函數(shù)和Linux的系統(tǒng)調(diào)用創(chuàng)建一個(gè)對學(xué)生基本信息進(jìn)行操 作(插入、修改和刪除)的C程序,學(xué)生基本信息以結(jié)構(gòu)體的形式存儲(chǔ)在文件#include<stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<time.h>struct stu_info char stu_num12;char name10;char sex;/*O 為女生,1為男生*/char mobile_phone12;struct stu_info stu

2、dent;unsigned int number;struct stu_info stu100;FILE *in;FILE *out;int len;void inputstu();void updatestu();void deletestu();void displaystu();void commanddisp() while(ch='n'); printf("printf("|n");學(xué)生基本信息管理|n");printf("|-1.基本信息插入.-2.基本信息修改|n");printf("|-3.基

3、本信息刪除.-4.基本信息顯示printf("|-r.printf("|printf("-|n");初始化(第一次使用時(shí)用!慎用)-q.退出-|n"); 請輸入你的選擇: |n");n");void main()commanddisp();char ch;while(1)doch=getchar();if(ch='q')retur n;switch(ch)case '1':inputstu();break;case '2':updatestu();break;case 3:de

4、letestu();break;case '4':displaystu();break;case 'r':printf("ninit student tablen"); if(out=fopen("c.doc","w")=NULL) return; number=0;fwrite(&nu mber,sizeof(unsigned int),1,out); fflush(out);fclose(out);break;default:break;printf("please enter a

5、ny key to continue");getchar();getchar();system("clear");commanddisp();void inputstu()n");printf("n 請輸入學(xué)生信息char p='y'while(p='y'|p='Y')memset (&student,0,sizeof(student);printf("學(xué)號:");scanf("%s",student.stu_num);printf("姓名

6、:");scanf("%s",);printf("性別 0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf("手機(jī)號:");scanf("%s",student.mobile_phone); len=sizeof(struct stu_in fo);if(out=fopen("c.doc","r+")=NULL) return; fread(&

7、;nu mber,sizeof(unsigned int),1,out); number+;fseek(out,OL,SEEK_SET);fwrite(&number,sizeof(unsigned int),1,out); fflush(out);fseek(out,0L,SEEK_END);fwrite(&student,len,1,out);fflush(out);fclose(out);printf("是否繼續(xù)輸入Y/ Nn");fflush(stdin);dop=getchar();while(p='n');void updates

8、tu()n");printf("n 學(xué)生信息修改-printf("請輸入修改學(xué)生的學(xué)號:"); memset (&student,0,sizeof(student); scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性別v 0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf(

9、"手機(jī)號:");scanf("%s",student.mobile_phone); len=sizeof(struct stu_in fo); in=fopen("c.doc","r+");fread(&nu mber,sizeof(unsigned int),1,in); unsigned int j;long kk;for( j=O;j<number;j+)fread(&stu0,len,1,in);if(strcmp(student.stu_num,stuO.stu_num)=O) kk

10、=len;fseek(in,-kk,SEEK_CUR);fwrite(&student,len,1,in);break;fflush(in);fclose(in);if(j=number)printf("沒有這條記錄 n");in=fopen("c.doc","r");fread(&nu mber,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_in fo);fread(stu,len,number,in);unsign

11、ed int i;char f;while(1)printf("請輸入刪除學(xué)生的學(xué)號:");char stu_num12;memset(stu_num,0,sizeof(stu_num);fflush(stdin);scanf("%s",stu_num);for(i=0;i<number;i+)if(strcmp(stu_num,stui.stu_num)=0)break; if(i=number) printf("沒有這條記錄 n");elsenumber-;for(;i<number;i+)strcpy(stui.st

12、u_num,stui+1.stu_num);strcpy(,stui+1.name);stui.sex=stui+1.sex;strcpy(stui.mobile phone,stui+1.mobile phone); printf(”是否繼續(xù)刪除Y/ Nn");fflush(stdin);dof=getchar();while(f='n');if(f!='y'&& f!='Y' )break;fclose(in);out = fopen("c.doc","w")

13、;fwrite(&nu mber,sizeof(unsigned int),1,out);len=len*number;fwrite(stu,len,1,out);fflush(out);fclose(out);void displaystu() printf("n 顯示學(xué)生信息n");in=fopen("c.doc","r");fread(&nu mber,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_in fo);

14、fread(stu,len,number,in);unsigned int i;printf("n");printf("number t學(xué)號t 姓名t性別<0是女,1是男 > 手機(jī)號n"); for(i=0;i<number;i+)printf(" %3d%12s%10s%c%12sn",i,stui.stu_num,,stui.sex,stui.mobile_phone);printf("n");fclose(in);利用父子進(jìn)程間的管道通信方式,改寫實(shí)驗(yàn)3步驟6要求的程序。要

15、求啟用兩個(gè)進(jìn)程,其中父進(jìn)程接受用戶對文件的操作命令然后通過管道發(fā)給子進(jìn)程,子進(jìn)程完成對文件的實(shí)際操作。#include<stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<time.h>struct stu_info char stu_num12;char name10;char sex;/*0 為女生,1為男生*/char mobile_phone12;struct stu_info student;unsigned int n

16、umber;struct stu_info stu100;int pipefiles2;void main()Iprintf("n");printf("| 學(xué)生基本信息管理 |n");printf("|-1.基本信息插入.-2.基本信息修改 |n");printf("|-3.基本信息刪除.-4.基本信息顯示 |n");printf("|-r.初始化(第一次使用時(shí)用!慎用)|n");printf("| 請輸入你的選擇:|n:");printf(”n");pid_t p

17、id;FILE *in;FILE *out;char ch;int len;if(pipe(pipefiles)=0)pid=fork();if(pid=-1)fprintf(stderr,"Fork failure'n"); return;if(pid=O)/child.read(pipefiles0, &ch,1);switch(ch)case '1':while(1)sleep(1);len=sizeof(struct stu_info);char *ch1=(char*)malloc(sizeof(char)*len);read(pi

18、pefiles0,ch1,len);if(out=fopen("c.doc","r+")=NULL) return;fread(&nu mber,sizeof(unsigned int),1,out);number+;fseek(out,0L,SEEK_SET);fwrite(&nu mber,sizeof(unsigned int),1,out); fclose(out);if(out=fopen("c.doc","a")=NULL) return;fwrite(ch1,len,1,out);fc

19、lose(out);char f;read(pipefiles0, &f,sizeof(char);if(f!='y'&&f!='Y') return;break;case '2':sleep(1);len=sizeof(struct stu_in fo);read(pipefiles0, &student,len);in=fopen("c.doc","r");fread(&nu mber,sizeof(unsigned int),1,in);memset(stu,0

20、,sizeof(stu);len=sizeof(struct stu_in fo);fread(stu,len,number,in);unsigned int j;for( j=0;j<number;j+)if(strcmp(student.stu_num,stu j.stu_num)=0) strcpy(stu ,);stu j.sex=student.sex;strcpy(stu j.mobile_phone,student.mobile_phone); break;fclose(in);if(j=number)printf(”沒有這條記錄 n&q

21、uot;);out = fopen("c.doc","w");fwrite(&nu mber,sizeof(unsigned int),1,out);len=sizeof(struct stu_in fo);len=len*number;fwrite(&stu,len,1,out);fclose(out);Ibreak;case 3:while(1)sleep(1);char stu_num12;memset(stu_num,0,sizeof(stu_num); read(pipefiles0,stu_num,sizeof(stu_num

22、); in=fopen("c.doc","r");fread(&nu mber,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_in fo);fread(stu,len,number,in);unsigned int i;for(i=0;i<number;i+)if(strcmp(stu_num,stui.stu_num)=0)break; if(i=number) printf("沒有這條記錄 n"); elsefor(

23、;i<number-1;i+)strcpy(stui.stu_num,stui+1.stu_num);strcpy(,stui+1.name);stui.sex=stui+1.sex; strcpy(stui.mobile_phone,stui+1.mobile_phone);if(out=fopen("c.doc","r")=NULL) return;fread(&nu mber,sizeof(unsigned int),1,out);fclose(out);number-;out = fopen("c.doc

24、","w");fwrite(&nu mber,sizeof(unsigned int),1,out);len=sizeof(struct stu_in fo);len=len*number;fwrite(stu,len,1,out);fclose(out);char f;read(pipefilesO, &f,sizeof(int);if(f!='y'&&f!='Y') return;break;case '4':sleep(1);in=fopen("c.doc",

25、"r");fread(&nu mber,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_in fo);fread(stu,len,number,in);unsigned int i;printf("n");for(i=0;inumber;i+)printf("-%d- 學(xué)號:%s-姓名:%s-性別0 是女,1 是男 :%c-手機(jī)號: %s-n",i,stui.stu_num,,stui.sex,stui.mob

26、ile_phone);printf("n");fclose(in);break;case 'r':if(out=fopen("c.doc","w")=NULL) return;number=0;fwrite(&nu mber,sizeof(unsigned int),1,out);break;default :break;else/father.Idoch=getchar();while(ch='n');write(pipefiles1, &ch,1);switch(ch)case &#

27、39;1':printf("n 請輸入學(xué)生信息n");char p='y:while(p='y'|p='Y')memset (&student,0,sizeof(student);printf("學(xué)號:");scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性別v 0是女,1是男:"); dostudent.sex=get

28、char();while(student.sex='n');printf("手機(jī)號:");scanf("%s",student.mobile_phone);len=sizeof(struct stu_in fo);write(pipefiles1, &student,len);sleep(1);printf("是否繼續(xù)輸入Y/ Nn");fflush(stdin);dop=getchar();while(p='n'); write(pipefiles1,&p,sizeof(char);b

29、reak;case '2':printf("n 學(xué)生信息修改n");printf("請輸入修改學(xué)生的學(xué)號:");memset (&student,0,sizeof(student);scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性別v 0是女,1是男:");dostudent.sex=getchar();while(student.sex='

30、n');printf("手機(jī)號:"); scanf("%s",student.mobile_phone); len=sizeof(struct stu_in fo);write(pipefiles1, &student,len);sleep(2);break;case 3:printf("n 學(xué)生信息刪除n");while(1)printf("請輸入刪除學(xué)生的學(xué)號:");char stu_num12;memset(stu_num,0,sizeof(stu_num);fflush(stdin);sca

31、nf("%s",stu_num);write(pipefiles1,stu_num,sizeof(stu_num);sleep(1);printf("是否繼續(xù)刪除Y/ Nn");fflush(stdin);char f;dof=getchar();while(f='n');write(pipefiles1, &f,sizeof(int);if(f!='y'&&f!='Y')return;break;case '4':printf("n 顯示學(xué)生信息n&quo

32、t;);sleep(1);break;case 'r':printf("ninit student tablen"); sleep (2);break;default:break;學(xué)習(xí)并使用線程間的同步方法,重新改寫實(shí)驗(yàn) 3步驟6要求的程序。要求啟用兩個(gè)線程其中主線程接受用戶對文件的操作命令然后發(fā)給子線程,子線程完成對文件的實(shí)際操作(注意線程間的同步)。#include<stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#

33、include<time.h>#include<pthread.h>#include <semaphore.h> struct stu_info char stu_num12;char name10;char sex;/*0 為女生,1為男生*/char mobile phone12;struct stu_info student;unsigned int number;struct stu_info stu100;FILE *in;FILE *out;int len;sem_t bin_sem;sem_t bin_sem1;char ch;void inp

34、utstu();void updatestu();void deletestu();void displaystu();void *stuthread(void *com);void commanddisp()printf("n");printf("|-學(xué)生基本信息管理-|n");printf("|-1.基本信息插入.-2.基本信息修改-|n");printf("|-3.基本信息刪除.-4.基本信息顯示-|n");printf("|-r.初始化(第一次使用時(shí)用!慎用)-q.退出-printf("

35、| 請輸入你的選擇:-|n");printf("n");pthread_t a_thread;void *thread_result;res = sem_init (&bin_sem,0,0);if (res != 0)perror("Semaphore initialition failed"); exit(EXIT_FAILURE);|res = sem_init (&bin_sem1,0,0);if (res != 0)perror("Semaphore initialition failed"); e

36、xit(EXIT_FAILURE);res = pthread_create(&a_thread,NULL,stuthread,NULL);if(res != 0)perror("Thread creation failed");exit(EXIT FAILURE);if(ch='q')sem_post(&bin_sem);break;sem_post(&bin_sem);sem_wait (&bin_sem1);printf("please enter any key to continue");getc

37、har();getchar();system("clear");commanddisp();res = pthread _join(a_thread, &thread_result); if(res != 0)perror("Thread join failed"); exit(EXIT_FAILURE);sem_destroy(&bin_sem);sem_destroy(&bin_sem1);exit(EXIT_SUCCESS);sem_wait (&bin_sem); while(ch!='q')swi

38、tch(ch)case '1':inputstu();break;case '2':updatestu();break;case 3:deletestu();break;case '4':displaystu();break;case 'r':printf("ninit student tablen"); if(out=fopen("c.doc","w")=NULL) exit(EXIT_FAILURE);number=O;fwrite(&nu mber,size

39、of(unsigned int),1,out);fflush(out);sem_post(&bin_sem1);sem_wait (&bin_sem);pthread_exit(NULL);void inputstu()Iprintf("n 請輸入學(xué)生信息n");char p='y'while(p='y'|p='Y')memset (&student,0,sizeof(student);printf("學(xué)號:");scanf("%s",student.stu_nu

40、m);printf("姓名:");scanf("%s",);printf("性別 0是女,1是男:");dostudent.sex=getchar(); while(student.sex='n'); printf("手機(jī)號:"); scanf("%s",student.mobile_phone); len=sizeof(struct stu_in fo); if(out=fopen("c.doc","r+")=NU

41、LL) return; fread(&nu mber,sizeof(unsigned int),1,out); number+;fseek(out,0L,SEEK_SET);fwrite(&number,sizeof(unsigned int),1,out); fflush(out);fseek(out,0L,SEEK_END);fwrite(&student,len,1,out);fflush(out);fclose(out);printf("是否繼續(xù)輸入Y/Nn"); fflush(stdin);dop=getchar();while(p=

42、9;n');void updatestu()printf("n 學(xué)生信息修改n");printf("請輸入修改學(xué)生的學(xué)號:");memset (&student,0,sizeof(student);scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性別v 0是女,1是男:");dostudent.sex=getchar();while(student.sex=&

43、#39;n');printf("手機(jī)號:");scanf("%s",student.mobile_phone);len=sizeof(struct stu_in fo);in=fopen("c.doc","r+");fread(&nu mber,sizeof(unsigned int),1,in);unsigned int j;long kk;for( j=0;j<number;j+)fread(&stu0,len,1,in);if(strcmp(student.stu_num,stu

44、0.stu_num)=0)kk=len;fseek(in,-kk,SEEK_CUR);fwrite(&student,len,1,in);break;fflush(in);fclose(in);if(j=number)printf("沒有這條記錄 n");in=fopen("c.doc","r");fread(&nu mber,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);Ien=sizeof(struct stu_info);fread(stu,len,num

45、ber,in);unsigned int i;char f;while(1)Iprintf("請輸入刪除學(xué)生的學(xué)號:");char stu_num12;memset(stu_num,O,sizeof(stu_num);fflush(stdin);scanf("%s",stu_num);for(i=O;i<number;i+)if(strcmp(stu_num,stui.stu_num)=O)break; if(i=number) printf("沒有這條記錄 n");elsenumber-;for(;i<number;i+

46、)strcpy(stui.stu_num,stui+1.stu_num); strcpy(,stui+1.name); stui.sex=stui+1.sex;strcpy(stui.mobile_phone,stui+1.mobile_phone); printf("是否繼續(xù)刪除 Y/Nn");fflush(stdin);dof=getchar();while(f='n'); if(f!='y'&&f!='Y' )break;fclose(in);out = fopen("c.do

47、c","w");fwrite(&nu mber,sizeof(unsigned int),1,out);len=len*number;fwrite(stu,len,1,out);fflush(out);fclose(out);void displaystu()廠printf("n 顯示學(xué)生信息n");in=fopen("c.doc","r");fread(&nu mber,sizeof(unsigned int),1,in); memset(stu,0,sizeof(stu); len=s

48、izeof(struct stu_info);fread(stu,len,number,in);unsigned int i;printf("n");printf("numbert 學(xué)號 t姓名t性別<0是女,1是男 >手機(jī)號n");for(i=0;i<number;i+)printf(" %3d%12s%10s%c%12sn",i,stui.stu_num,,stui.sex,stui.mobile_phone);printf("n");fclose(in);學(xué)習(xí)并使用IPC中

49、信號量和共享內(nèi)存的使用方法 ,重新改寫實(shí)驗(yàn)3步驟6要求的程序。要求 啟動(dòng)多個(gè)進(jìn)程,每一個(gè)進(jìn)程都可以單獨(dú)對文件進(jìn)行操作 ,進(jìn)程間通過信號量進(jìn)行同步 ,對文件的操作映射到共享內(nèi)存中#include<stdio.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<time.h>#include <sys/shm.h>#include <sys/types.h>#include <sys/ipc.h>#include <sy

50、s/sem.h>#include "semun.h"struct stu_info char stu_num12;char name10;char sex;/*0 為女生,1為男生*/char mobile phone12;struct stu_info student;unsigned int number;struct stu_info stu100;FILE *in;FILE *out;int len;static int sem_id;void inputstu();void updatestu();void deletestu();void displays

51、tu();void set_semvalue();void del_semvalue();void semaphore_p();void semaphore_v();void commanddisp()printf(”n");printf("| 學(xué)生基本信息管理 |n");printf("|-1.基本信息插入.-2.基本信息修改-|n");printf("|-3.基本信息刪除.-4.基本信息顯示-|n");printf("|-r.初始化(第一次使用時(shí)用!慎用)-q.退出-|n");printf("

52、;|請輸入你的選擇:-|n");printf("n");void main(int argc, char shmid = shmget(key_t)1500,1,IPC_CREAT|0666);char *ch = shmat(shmid,NULL,0);commanddisp();while(1)do ch=getchar();while(*ch='n');if(*ch='q')break;switch(*ch)case '1':inputstu();break;case '2':updatestu

53、();break;argv)int shmid;srand(unsigned int)getpid();sem_id = semget(key_t)1234, 1,0666 | IPC_CREAT);if (argc > 1) set_semvalue();case 3:deletestu();break;case '4':displaystu();break;case 'r':printf("ninit student tablen"); if(out=fopen("c.doc","w")=NU

54、LL) return; number=O;fwrite(&nu mber,sizeof(unsigned int),1,out); fflush(out);fclose(out);break;default:break;printf("please enter any key to continue");getchar();getchar();system("clear");commanddisp();if (argc > 1) sleep(1);del_semvalue();void inputstu()n");printf(&

55、quot;n 請輸入學(xué)生信息char p='y'while(p='y'|p='Y')memset (&student,0,sizeof(student); printf("學(xué)號:"); scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性別v 0是女,1是男>:");student.sex=getchar();while(student

56、.sex='n');printf(” 手機(jī)號:"); scanf("%s",student.mobile_phone); len=sizeof(struct stu_in fo);if(out=fopen("c.doc","r+")=NULL) return; semaphore_p();fread(&nu mber,sizeof(unsigned int),1,out); number+;fseek(out,OL,SEEK_SET);fwrite(&number,sizeof(unsigne

57、d int),1,out); fflush(out);fseek(out,0L,SEEK_END);fwrite(&student,len,1,out);fflush(out);fclose(out);semaphore_v();printf("是否繼續(xù)輸入Y/Nn");fflush(stdin);dop=getchar();while(p='n');n");printf("n 學(xué)生信息修改printf("請輸入修改學(xué)生的學(xué)號:"); memset (&student,0,sizeof(student)

58、;scanf("%s",student.stu_num);printf("姓名:");scanf("%s",);printf("性別< 0是女,1是男:");dostudent.sex=getchar();while(student.sex='n');printf("手機(jī)號:");scanf("%s",student.mobile_phone); len=sizeof(struct stu_in fo);semaphore_p()

59、; in=fopen("c.doc","r+");fread(&nu mber,sizeof(unsigned int),1,in); unsigned int j;long kk;for( j=0;j<number;j+)fread(&stu0,len,1,in); if(strcmp(student.stu_num,stuO.stu_num)=O) kk=len;fseek(in,-kk,SEEK_CUR);fwrite(&student,len,1,in); break;fflush(in);fclose(in);se

60、maphore_v();if(j=number)printf("沒有這條記錄 n");semaphore_p();in=fopen("c.doc","r");fread(&nu mber,sizeof(unsigned int),1,in);memset(stu,0,sizeof(stu);len=sizeof(struct stu_in fo);fread(stu,len,number,in);unsigned int i;char f;while(1)Iprintf("請輸入刪除學(xué)生的學(xué)號:");cha

61、r stu_num12;memset(stu_num,0,sizeof(stu_num);fflush(stdin);scanf("%s",stu_num);for(i=0;i<number;i+)if(strcmp(stu_num,stui.stu_num)=0)break;if(i=number) printf("沒有這條記錄 n");elsenumber-;for(;i<number;i+)strcpy(stui.stu_num,stui+1.stu_num); strcpy(,stui+1.name); stui.sex=stui+1.sex;strcpy(stui.mobile_phon

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論