c的課程設計
❶ C語言程序設計 (學生選修課程設計)
這是我做的,你看是否滿意?可能有點大,但也沒辦法呀,你的題目也比較大,呵呵!所以,如果滿意,多給我追加點分!
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct course
{
char number[15],name[25];
int kind,time,lessontime,practicetime,credit,term;
}type;
FILE *fp1;
void overview(); //瀏覽函數,負責瀏覽整個課程信息
void seek(); //查詢函數,負責查詢課程信息
void choose_course();//選課函數,負責讓用戶選課
void out(type temp);
void input();
int main()
{
int n,i;
if((fp1=fopen("course_information.txt","wb"))==NULL)
{printf("創建文件失敗!\n");exit(0);}
printf("請輸入要存儲的課程數目:\n");
scanf("%d",&n);
printf("開始創建文件,請輸入課程信息:\n\n");
for(i=0;i<n;i++)
{
printf("請輸入第%d門課程的信息:\n",i+1);
input();
printf("\n");
}
printf("如想瀏覽整個課程信息,請輸入1;如想查詢課程信息,請輸入2; 如想進行選課,請輸入3;如想結束選修課系統,請輸入0!\n");
while((scanf("%d",&n))!=EOF)
{
if(n==1)
overview();
if(n==2)
seek();
if(n==3)
choose_course();
if(n==0)
exit(0);
printf("\n\n如想繼續操作,只要按規則輸入你要進行的操作即可!\n規則:如想瀏覽整個課程信息,請輸入1;如想查詢課程信息,請輸入2;如想進行選課,請輸入3!\n");
}
printf("歡迎您使用此程序進行選課,謝謝!\n");
fclose(fp1);
return 0;
}
void input()
{
course c_a;
printf("請輸入課程編碼: ");
scanf("%s",c_a.number);
printf("請輸入課程名: ");
scanf("%s",c_a.name);
printf("請輸入課程性質:限選課,請輸入1;選修課,請輸入2;必修課,請輸入3! ");
scanf("%d",&c_a.name);
printf("請輸入課程總學時: ");
scanf("%d",&c_a.time);
printf("請輸入課程授課時間: ");
scanf("%d",&c_a.lessontime);
printf("請輸入課程實驗或實踐時間: ");
scanf("%d",&c_a.practicetime);
printf("請輸入課程學分: ");
scanf("%d",&c_a.credit);
printf("請輸入課程所在的學期,比如第二學期,就輸入2即可。");
scanf("%d",&c_a.term);
fwrite(&c_a,sizeof(struct course),1,fp1);//將一個結構體元素寫入文件中
}
void out(type temp)
{
printf("課程代碼: %s\n課程名: %s\n",temp.number,temp.name);
printf("課程名: %s\n",temp.name);
if(temp.kind==1)
printf("課程性質: Limited optional course\n");
else if(temp.kind==2)
printf("課程性質: Optional course\n");
else if(temp.kind==3)
printf("課程性質: Required Courses\n");
else
printf("該編碼系統不認識,即無對應的課程性質存在!\n");
printf("課程總學時: %d\n課程授課學時: %d\n實驗或上機學時: %d\n學分: %d\n課程開課學期: %d\n\n",temp.time,temp.lessontime,temp.practicetime,temp.credit,temp.term);
}
void overview()
{
rewind(fp1);
course temp;
printf("整個課程信息如下:\n");
while((fread(&temp,sizeof(type),1,fp1))!=0)
out(temp);
}
void seek()
{
int judge,credit=0,kind=0;
char a='N';
course temp;
printf("如想按學分查詢,請輸入1;如想按課程性質,請輸入2:\n");
scanf("%d",&judge);
rewind(fp1); //將文件指針位置置為開頭
if(judge==1)
{
printf("請輸入要查詢的學分:\n");
scanf("%d",&credit);
while((fread(&temp,sizeof(type),1,fp1))!=0)
if(credit==temp.credit)
out(temp);
}
else if(judge==2)
{
printf("請輸入你要查找課程的性質(限選課,請輸入1;選修課,請輸入2;必修課,請輸入3):");
scanf("%d",&kind);
while((fread(&temp,sizeof(type),1,fp1))!=0)
if(temp.kind==kind)
out(temp);
}
else
printf("不好意思,無此類查詢!\n");
}
void choose_course()
{
rewind(fp1);
course temp;
int judge=1,n=0,time=0,credit=0;
char choose[20][20];
r1: printf("請開始填寫課程編號進行選課:\n");
while(judge==1)
{
printf("請輸入你所選課程的標號: ");
scanf("%s",choose[n]);
n++;
printf("如想繼續選課,請輸入1;如想提交,請輸入0!\n");
scanf("%d",&judge);
}
while((fread(&temp,sizeof(type),1,fp1))!=0)
{
for(int i=0;i<n;i++)
if(strcmp(temp.number,choose[i])==0)
{time=time+temp.time;credit=temp.credit;break;}
}
if(time<270||credit<40)
goto r1;
printf("你所選的課為:\n");
while((fread(&temp,sizeof(type),1,fp1))!=0)
{
for(int i=0;i<n;i++)
if(strcmp(temp.number,choose[i])==0)
{out(temp);break;}
}
}
❷ C課程設計
/******頭文件(.h)***********/
#include "stdio.h" /*I/O函數*/
#include "stdlib.h" /*標准庫函數*/
#include "string.h"/*字元串函數*/
#include "ctype.h" /*字元操作函數*/
#define M 50 /*定義常數表示記錄數*/
typedef struct /*定義數據結構*/
{
char name[20]; /*姓名*/
char units[30]; /*單位*/
char tele[10]; /*電話*/
}ADDRESS;
/******以下是函數原型*******/
int enter(ADDRESS t[]); /*輸入記錄*/
void list(ADDRESS t[],int n); /*顯示記錄*/
void search(ADDRESS t[],int n); /*按姓名查找顯示記錄*/
int delete(ADDRESS t[],int n); /*刪除記錄*/
int add(ADDRESS t[],int n); /*插入記錄*/
void save(ADDRESS t[],int n); /*記錄保存為文件*/
int load(ADDRESS t[]); /*從文件中讀記錄*/
void display(ADDRESS t[]); /*按序號查找顯示記錄*/
void sort(ADDRESS t[],int n); /*按姓名排序*/
void qseek(ADDRESS t[],int n); /*快速查找記錄*/
void (); /*文件復制*/
void print(ADDRESS temp); /*顯示單條記錄*/
int find(ADDRESS t[],int n,char *s) ; /*查找函數*/
int menu_select(); /*主菜單函數*/
/******主函數開始*******/
main()
{
int i;
ADDRESS adr[M]; /*定義結構體數組*/
int length; /*保存記錄長度*/
clrscr(); /*清屏*/
for(;;)/*無限循環*/
{
switch(menu_select()) /*調用主菜單函數,返回值整數作開關語句的條件*/
{
case 0:length=enter(adr);break;/*輸入記錄*/
case 1:list(adr,length);break; /*顯示全部記錄*/
case 2:search(adr,length);break; /*查找記錄*/
case 3:length=delete(adr,length);break; /*刪除記錄*/
case 4:length=add(adr,length); break; /*插入記錄*/
case 5:save(adr,length);break; /*保存文件*/
case 6:length=load(adr); break; /*讀文件*/
case 7:display(adr);break; /*按序號顯示記錄*/
case 8:sort(adr,length);break; /*按姓名排序*/
case 9:qseek(adr,length);break; /*快速查找記錄*/
case 10:();break; /*復制文件*/
case 11:exit(0); /*如返回值為11則程序結束*/
}
}
}
/*菜單函數,函數返回值為整數,代表所選的菜單項*/
menu_select()
{
char s[80];
int c;
gotoxy(1,25);/*將游標定為在第25行,第1列*/
printf("press any key enter menu......\n");/*提示壓任意鍵繼續*/
getch(); /*讀入任意字元*/
clrscr(); /*清屏*/
gotoxy(1,1);
printf("********************MENU*********************\n\n");
printf(" 0. Enter record\n");
printf(" 1. List the file\n");
printf(" 2. Search record on name\n");
printf(" 3. Delete a record\n");
printf(" 4. add record \n");
printf(" 5. Save the file\n");
printf(" 6. Load the file\n");
printf(" 7. display record on order\n");
printf(" 8. sort to make new file\n");
printf(" 9. Quick seek record\n");
printf(" 10. the file to new file\n");
printf(" 11. Quit\n");
printf("***********************************************\n");
do{
printf("\n Enter you choice(0~11):"); /*提示輸入選項*/
scanf("%s",s); /*輸入選擇項*/
c=atoi(s); /*將輸入的字元串轉化為整型數*/
}while(c<0||c>11); /*選擇項不在0~11之間重輸*/
return c; /*返回選擇項,主程序根據該數調用相應的函數*/
}
/***輸入記錄,形參為結構體數組,函數值返回類型為整型表示記錄長度*/
int enter(ADDRESS t[])
{
int i,n;
char *s;
clrscr(); /*清屏*/
printf("\nplease input num \n"); /*提示信息*/
scanf("%d",&n); /*輸入記錄數*/
printf("please input record \n"); /*提示輸入記錄*/
printf("name unit telephone\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
{
scanf("%s%s%s",t[i].name,t[i].units,t[i].tele); /*輸入記錄*/
printf("----------------------------------------------\n");
}
return n; /*返回記錄條數*/
}
/*顯示記錄,參數為記錄數組和記錄條數*/
void list(ADDRESS t[],int n)
{
int i;
clrscr();
printf("\n\n*******************ADDRESS******************\n");
printf("name unit telephone\n");
printf("------------------------------------------------\n");
for(i=0;i<n;i++)
printf("%-20s%-30s%-10s\n",t[i].name,t[i].units,t[i].tele);
if((i+1)%10==0) /*判斷輸出是否達到10條記錄*/
{
printf("Press any key continue...\n"); /*提示信息*/
getch(); /*壓任意鍵繼續*/
}
printf("************************end*******************\n");
}
/*查找記錄*/
void search(ADDRESS t[],int n)
{
char s[20]; /*保存待查找姓名字元串*/
int i; /*保存查找到結點的序號*/
clrscr(); /*清屏*/
printf("please search name\n");
scanf("%s",s); /*輸入待查找姓名*/
i=find(t,n,s); /*調用find函數,得到一個整數*/
if(i>n-1) /*如果整數i值大於n-1,說明沒找到*/
printf("not found\n");
else
print(t[i]); /*找到,調用顯示函數顯示記錄*/
}
/*顯示指定的一條記錄*/
void print(ADDRESS temp)
{
clrscr();
printf("\n\n********************************************\n");
printf("name unit telephone\n");
printf("------------------------------------------------\n");
printf("%-20s%-30s%-10s\n",temp.name,temp.units,temp.tele);
printf("**********************end***********************\n");
}
/*查找函數,參數為記錄數組和記錄條數以及姓名s */
int find(ADDRESS t[],int n,char *s)
{
int i;
for(i=0;i<n;i++)/*從第一條記錄開始,直到最後一條*/
{
if(strcmp(s,t[i].name)==0) /*記錄中的姓名和待比較的姓名是否相等*/
return i; /*相等,則返回該記錄的下標號,程序提前結結束*/
}
return i; /*返回i值*/
}
/*刪除函數,參數為記錄數組和記錄條數*/
int delete(ADDRESS t[],int n)
{
char s[20]; /*要刪除記錄的姓名*/
int ch=0;
int i,j;
printf("please deleted name\n"); /*提示信息*/
scanf("%s",s);/*輸入姓名*/
i=find(t,n,s); /*調用find函數*/
if(i>n-1) /*如果i>n-1超過了數組的長度*/
printf("no found not deleted\n"); /*顯示沒找到要刪除的記錄*/
else
{
print(t[i]); /*調用輸出函數顯示該條記錄信息*/
printf("Are you sure delete it(1/0)\n"); /*確認是否要刪除*/
scanf("%d",&ch); /*輸入一個整數0或1*/
if(ch==1) /*如果確認刪除整數為1*/
{
for(j=i+1;j<n;j++) /*刪除該記錄,實際後續記錄前移*/
{
strcpy(t[j-1].name,t[j].name); /*將後一條記錄的姓名拷貝到前一條*/
strcpy(t[j-1].units,t[j].units); /*將後一條記錄的單位拷貝到前一條*/
strcpy(t[j-1].tele,t[j].tele); /*將後一條記錄的電話拷貝到前一條*/
}
n--; /*記錄數減1*/
}
}
return n; /*返回記錄數*/
}
/*插入記錄函數,參數為結構體數組和記錄數*/
int add(ADDRESS t[],int n)/*插入函數,參數為結構體數組和記錄數*/
{
ADDRESS temp; /*新插入記錄信息*/
int i,j;
char s[20]; /*確定插入在哪個記錄之前*/
printf("please input record\n");
printf("************************************************\n");
printf("name unit telephone\n");
printf("--------------------------------------------------\n");
scanf("%s%s%s",temp.name,temp.units,temp.tele); /*輸入插入信息*/
printf("------------------------------------------------\n");
printf("please input locate name \n");
scanf("%s",s); /*輸入插入位置的姓名*/
i=find(t,n,s); /*調用find,確定插入位置*/
for(j=n-1;j>=i;j--) /*從最後一個結點開始向後移動一條*/
{
strcpy(t[j+1].name,t[j].name); /*當前記錄的姓名拷貝到後一條*/
strcpy(t[j+1].units,t[j].units); /*當前記錄的單位拷貝到後一條*/
strcpy(t[j+1].tele,t[j].tele); /*當前記錄的電話拷貝到後一條*/
}
strcpy(t[i].name,temp.name); /*將新插入記錄的姓名拷貝到第i個位置*/
strcpy(t[i].units,temp.units); /*將新插入記錄的單位拷貝到第i個位置*/
strcpy(t[i].tele,temp.tele); /*將新插入記錄的電話拷貝到第i個位置*/
n++; /*記錄數加1*/
return n; /*返回記錄數*/
}
/*保存函數,參數為結構體數組和記錄數*/
void save(ADDRESS t[],int n)
{
int i;
FILE *fp; /*指向文件的指針*/
if((fp=fopen("record.txt","wb"))==NULL) /*打開文件,並判斷打開是否正常*/
{
printf("can not open file\n");/*沒打開*/
exit(1); /*退出*/
}
printf("\nSaving file\n"); /*輸出提示信息*/
fprintf(fp,"%d",n); /*將記錄數寫入文件*/
fprintf(fp,"\r\n"); /*將換行符號寫入文件*/
for(i=0;i<n;i++)
{
fprintf(fp,"%-20s%-30s%-10s",t[i].name,t[i].units,t[i].tele);/*格式寫入記錄*/
fprintf(fp,"\r\n"); /*將換行符號寫入文件*/
}
fclose(fp);/*關閉文件*/
printf("****save success***\n"); /*顯示保存成功*/
}
/*讀入函數,參數為結構體數組*/
int load(ADDRESS t[])
{
int i,n;
FILE *fp; /*指向文件的指針*/
if((fp=fopen("record.txt","rb"))==NULL)/*打開文件*/
{
printf("can not open file\n"); /*不能打開*/
exit(1); /*退出*/
}
fscanf(fp,"%d",&n); /*讀入記錄數*/
for(i=0;i<n;i++)
fscanf(fp,"%20s%30s%10s",t[i].name,t[i].units,t[i].tele); /*按格式讀入記錄*/
fclose(fp); /*關閉文件*/
printf("You have success read data from file!!!\n"); /*顯示保存成功*/
return n; /*返回記錄數*/
}
/*按序號顯示記錄函數*/
void display(ADDRESS t[])
{
int id,n;
FILE *fp; /*指向文件的指針*/
if((fp=fopen("record.txt","rb"))==NULL) /*打開文件*/
{
printf("can not open file\n"); /*不能打開文件*/
exit(1); /*退出*/
}
printf("Enter order number...\n"); /*顯示信息*/
scanf("%d",&id); /*輸入序號*/
fscanf(fp,"%d",&n); /*從文件讀入記錄數*/
if(id>=0&&id<n) /*判斷序號是否在記錄范圍內*/
{
fseek(fp,(id-1)*sizeof(ADDRESS),1); /*移動文件指針到該記錄位置*/
print(t[id]); /*調用輸出函數顯示該記錄*/
printf("\r\n");
}
else
printf("no %d number record!!!\n ",id); /*如果序號不合理顯示信息*/
fclose(fp); /*關閉文件*/
}
/*排序函數,參數為結構體數組和記錄數*/
void sort(ADDRESS t[],int n)
{
int i,j,flag;
ADDRESS temp; /*臨時變數做交換數據用*/
for(i=0;i<n;i++)
{
flag=0; /*設標志判斷是否發生過交換*/
for(j=0;j<n-1;j++)
if((strcmp(t[j].name,t[j+1].name))>0) /*比較大小*/
{
flag=1;
strcpy(temp.name,t[j].name); /*交換記錄*/
strcpy(temp.units,t[j].units);
strcpy(temp.tele,t[j].tele);
strcpy(t[j].name,t[j+1].name);
strcpy(t[j].units,t[j+1].units);
strcpy(t[j].tele,t[j+1].tele);
strcpy(t[j+1].name,temp.name);
strcpy(t[j+1].units,temp.units);
strcpy(t[j+1].tele,temp.tele);
}
if(flag==0)break; /*如果標志為0,說明沒有發生過交換循環結束*/
}
printf("sort sucess!!!\n"); /*顯示排序成功*/
}
/*快速查找,參數為結構體數組和記錄數*/
void qseek(ADDRESS t[],int n)
{
char s[20];
int l,r,m;
printf("\nPlease sort before qseek!\n"); /*提示確認在查找之前,記錄是否已排序*/
printf("please enter name for qseek\n"); /*提示輸入*/
scanf("%s",s); /*輸入待查找的姓名*/
l=0;r=n-1; /*設置左邊界與右邊界的初值*/
while(l<=r) /*當左邊界<=右邊界時*/
{
m=(l+r)/2; /*計算中間位置*/
if(strcmp(t[m].name,s)==0) /*與中間結點姓名欄位做比較判是否相等*/
{
print(t[m]); /*如果相等,則調用print函數顯示記錄信息*/
return ; /*返回*/
}
if(strcmp(t[m].name,s)<0) /*如果中間結點小*/
l=m+1; /*修改左邊界*/
else
r=m-1; /*否則,中間結點大,修改右邊界*/
}
if(l>r) /*如果左邊界大於右邊界時*/
printf("not found\n"); /*顯示沒找到*/
}
/*復制文件*/
void ()
{
char outfile[20]; /*目標文件名*/
int i,n;
ADDRESS temp[M]; /*定義臨時變數*/
FILE *sfp,*tfp; /*定義指向文件的指針*/
clrscr();/*清屏*/
if((sfp=fopen("record.txt","rb"))==NULL) /*打開記錄文件*/
{
printf("can not open file\n"); /*顯示不能打開文件信息*/
exit(1); /*退出*/
}
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示信息*/
scanf("%s",outfile); /*輸入目標文件名*/
if((tfp=fopen(outfile,"wb"))==NULL) /*打開目標文件*/
{
printf("can not open file\n"); /*顯示不能打開文件信息*/
exit(1); /*退出*/
}
fscanf(sfp,"%d",&n); /*讀出文件記錄數*/
fprintf(tfp,"%d",n);/*寫入目標文件數*/
fprintf(tfp,"\r\n"); /*寫入換行符*/
for(i=0;i<n;i++)
{
fscanf(sfp,"%20s%30s%10s\n",temp[i].name,temp[i].units,
temp[i].tele); /*讀入記錄*/
fprintf(tfp,"%-20s%-30s%-10s\n",temp[i].name,
temp[i].units,temp[i].tele); /*寫入記錄*/
fprintf(tfp,"\r\n"); /*寫入換行符*/
}
fclose(sfp); /*關閉源文件*/
fclose(tfp); /*關閉目標文件*/
printf("you have success file!!!\n"); /*顯示復製成功*/
}
❸ 如何做C語言課程設計
想偷懶的話就寫一個控制台程序......
完全不需要圖形化的函數什麼的.......
工程,文件操作這些都沒什麼實。
我這邊有一套不錯的C語言課程設計,如果你喜歡可以拿去直接用,我還沒有在網上放過。
❹ C語言課程設計
(C語言)職工信息管理
有任何問題可以到這里留言:
http://moonrosa.googlepages.com/liuyan.htm
【要求】
職工信息包括職工號,姓名,性別,年齡,學歷,工資,住址,電話等(職工號不相等)。試設計一職工信息管理系統,使之能夠提供下列功能:
(1)系統以菜單方式工作
(2)職工信息錄入功能(職工信息用文件保存)
(3)職工信息瀏覽功能
(4)職工信息查詢功能,查詢方式:
1)按學歷查詢
2)按職工號查詢
(5)職工信息刪除,修改功能(可選項)。
#include<string.h>
void menu()
{
int n,w1;
do
{
printf("\t\t************************************************\n\n");
printf("\t\t************************************************\n\n");
printf("\t\t *** choose function ************\n\n");
printf("\t\t *** 1 Enter new data ************\n\n");
printf("\t\t *** 2 Modify data ************\n\n");
printf("\t\t *** 3 Search by people.xueli and num*****\n\n");
printf("\t\t *** 4 Browse data ************\n\n");
printf("\t\t *** 5 add data ************\n\n");
printf("\t\t *** 6 Exit ************\n\n");
printf("\t\t************************************************\n\n");
printf("\t\t************************************************\n\n");
printf("Choose your number(1-6):[ ]\b\b");
scanf("%d",&n);
if(n<1||n>6)
w1=1;
else w1=0;
}
while(w1==1);
switch(n)
{
case 1:enter();break;
case 2:modify();break;
case 3:search();break;
case 4:browse();break;
case 5:add();break;
case 6:exit(0);
}
}
main()
{
system("cls");
menu();
}
#define N 100
struct people
{
char num[100];
char name[15];
char sex[20];
char age[20];
char xueli[20];
char gong[20];
char address[20];
char telephone[20];
}people[N];
# include <stdio.h>
enter()
{
int i,n;
printf("How many people(0-%d)?:",N-1);
scanf("%d",&n);
printf("\n Enter data now\n\n");
for(i=0;i<n;i++)
{
printf("\n Input %dth people record.\n",i+1);
input(i);
}
if(i!=0)save(n);
printf_back();
}
browse()
{
int i,j,n;
n=load();
printf_face();
for(i=0;i<n;i++)
{
if((i!=0)&&(i%10==0))
{
printf("\n\nPass any key to continue ....");
getch();
puts("\n\n");
}
printf_one(i) ;
}
printf("\tThere are %d record.\n",n);
printf("\nPass any key to back ...");
getch();
menu();
}
add()
{
int i,n,m,k;
FILE*fp;
n=load();
printf("How many people are you want to add(0-%d)?:",N-1-n);
scanf("%d",&m);
k=m+n;
for(i=n;i<k;i++)
{
printf("\nInput %dth people record.\n",i-n+1 );
input(i);
}
if((fp=fopen("Pro.txt","ab"))==NULL)
{
printf("cannot open file\n");
}
for(i=n;i<k;i++)
if(fwrite(&people[i],sizeof(struct people),1,fp)!=1)
printf("file write error\n");
fclose(fp);
printf_back();
}
search()
{
int i,n,k,w1=1,w2,w3,w4,m,a;
struct people p;
n=load();
do
{
printf("\n\nWhich way do you want to choose? \n\t1).By xueli 2).By num [ ]\b\b");
scanf("%d",&m);
switch(m)
{
case 1:
do
{ k=-1;
printf("\n\nEnter xeuli that you want to search! xueli.");
scanf("%s",p.xueli);
printf_face();
for(i=0;i<n;i++)
if(strcmp(p.xueli,people[i].xueli)==0)
{ k=i;
printf_one(k);break;
}
if(k==-1)
{ printf("\n\nNO exist!please");
printf("\n\nAre you again?\n\t1).again 2).NO and back [ ]\b\b");
scanf("%d",&w1);
if(w1==2)
printf_back();
}
}
while(k==-1&&w1==1);break;
case 2:
do
{k=-1;
printf("\n\nEnter num that you want to search! num.");
scanf("%s",p.num);
printf_face();
for(i=0;i<n;i++)
if(strcmp(p.num,people[i].num)==0)
{k=i;
printf_one(k);break;
}
if(k==-1)
{printf("\n\nNO exist!please");
printf("\n\nAre you again?\n\t1).again 2).NO and back [ ]\b\b");
scanf("%d",&w1);
if(w1==2)
printf_back();
}
}
while(k==-1&&w1==1);break;
}
w4=0;w3=0;
if(k!=-1)
{printf("\n\nWhat do you want to do?\n\t 1).Search 2).Modify 3).Delete 4).Back menu [ ]\b\b");
scanf("%d",&w2);
switch(w2)
{case 2:w3=modify_data(k,n);break;
case 3:{printf("\nAre you sure?\n\t 1).Sure 2).No and back [ ]\b\b");
scanf("%d",&w4);
if(w4==1)
for(a=k;a<n;a++)
{strcpy(people[a].num,people[a+1].num);
strcpy(people[a].name,people[a+1].name);
strcpy(people[a].sex,people[a+1].sex);
strcpy(people[a].age,people[a+1].age);
strcpy(people[a].xueli,people[a+1].xueli);
strcpy(people[a].gong,people[a+1].gong);
strcpy(people[a].address,people[a+1].address);
strcpy(people[a].telephone,people[a+1].telephone);
}
break;
}
}
if(w3==1||w4==1)
{save(n);
printf("\n\nSuccessful.^_^.");
printf("\n\nWhant do you want to do? \n\t 1).Search another 2).Back [ ]\b\b" );
scanf("%d",&w2);
}
}
}
while(w2==1);
menu();
}
modify()
{struct people p;
FILE *fp;
int i,n,k,w0=1,w1,w2=0;
n=load();
do
{
k=-1;
printf_face();
for(i=0;i<n;i++)
{if((i!=0)&&(i%10==0))
{printf("\n\nRemember NO.which needed modify.pass any key to contiune ...");
getch();
puts("\n\n");
}
printf_one(i);
}
do
{printf("\n\nEnter NO.that you want to modify! NO.:");
scanf("%s",p.num);
for(i=0;i<n;i++)
if(strcmp(p.num,people[i].num)==0)
{k=i;
p=people[i];
}
if(k==-1)printf("\n\nNO exist!please again");
}while(k==-1);
printf_face();
printf_one(k);
w1=modify_data(k,n);
if(w1==1)
{printf("\nSuccessful ^_^.\n\nAre you modify another ?\n\n\t 1).Yes 2).Back with save\t[ ]\b\b");
scanf("%d",&w0);
w2=1;
}
else
{w0=0;
if(w2==1)
people[k]=p;
}
if(w0!=1&&w2==1)
save(n);
}while(w0==1);
menu();
}
save(int n)
{FILE *fp;
int i;
if((fp=fopen("Pro.txt","wb"))==NULL)
{printf("\nCannot open file\n");
return NULL;
}
for(i=0;i<n;i++)
if(people[i].num!=0)
if(fwrite(&people[i],sizeof(struct people),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
load()
{FILE *fp;
int i;
if((fp=fopen("Pro.txt","rb"))==NULL)
{printf("\nCannot open file\n");
return NULL;
}
for(i=0;!feof(fp);i++)
fread(&people[i],sizeof(struct people),1,fp);
fclose(fp);
return(i-1);
}
input(int i)
{
no_input(i,i);
printf("num:");
scanf("%s",people[i].num);
printf("name:");
scanf("%s", people[i].name) ;
printf("sex:");
scanf("%s",people[i].sex);
printf("age:");
scanf("%s",people[i].age);
printf("xueli:");
scanf("%s",people[i].xueli);
printf("gong:");
scanf("%s",people[i].gong);
printf("address:");
scanf("%s",people[i].address);
printf("telephone:");
scanf("%s",people[i].telephone);
}
modify_data(int i)
{int c,w1;
do
{puts("\nmodify by=>\n\n 1).num 2).name 3).sex 4).ages 5).xueli 6)gong 7)address 8)telephone ");
printf("Which you needed?:[ ]\b\b");
scanf("%d",&c);
if(c>8||c<1)
{puts("\nChoice error!Please again!");
getchar();
}
}while(c>8||c<1);
do
{switch(c)
{
case 1:printf("num:");scanf("%s",people[i].num);break;
case 2:printf("name:");scanf("%s",people[i].name);break;
case 3:printf("sex:");scanf("%s",people[i].sex);break;
case 4:printf("age:");scanf("%s",people[i].age);break;
case 5:printf("xueli:");scanf("%s",people[i].xueli);break;
case 6:printf("gong:");scanf("%s",people[i].gong);break;
case 7:printf("address:");scanf("%s",people[i].address);break;
case 8:printf("telephone:");scanf("%s",people[i].telephone);break;
}
puts("\nNow:\n");
printf_face();
printf_one(i);
printf("\nAre you sure?\n\n\t 1).Sure 2).No and remodify 3).Back without save in this time [ ]\b\b");
scanf("%d",&w1);
}
while(w1==2);
return(w1);
}
no_input(int i,int n)
{int j,k,w1;
do
{w1=0;
for(j=0;people[i].num[j]!='\0';j++)
if(people[i].num[j]>'9')
{puts("Input error!Only be made up of(0-9).Please reinput!\n");
w1=1;break;
}
if(w1!=1)
for(k=0;k<n;k++)
if(k!=i&&strcmp(people[k].num,people[i].num)==0)
{puts("This record is exist.please reinput!\n");
}
}
while(w1==1);
}
printf_face()
{
printf("\nnum name sex ages xueli gong address telephone \n");
}
printf_one(int i)
{
int j;
printf("%7s%7s%7s%7s%7s%7s%10s%12s\n",people[i].num,people[i].name,people[i].sex,people[i].age,people
[i].xueli,people[i].gong,people[i].address,people[i].telephone);
}
printf_back()
{
int j,w;
printf("\n\n\tSuccessful.^_^\n\n");
printf("What do you want you to do?\n\n\t1).Browse all now\t2).Back: [ ]\b\b");
scanf("%d",&w);
if(w==1)
browse();
else menu();
}
參考資料:http://moonrosa.googlepages.com/cyuyan_zgglxt
❺ c課程設計
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class controlloer //圖書管理員
{
private:
int number;
int age;
char name[20];
public:
controlloer();
controlloer(int c,int a,char b[20] );
int getnumber();
int getage();
char *getname();
void reworkage(int i);
void reworkname(char a[20]);
};
controlloer::controlloer()
{
number=0;
char b[20]="no one";
age=0;
strcpy(name,b);
}
controlloer::controlloer(int c,int a,char b[20])
{
number=c;
age=a;
strcpy(name,b);
}
int controlloer::getage()
{return age;}
char *controlloer::getname()
{return name;}
int controlloer::getnumber()
{return number;}
void controlloer::reworkage(int i)
{age=i;}
void controlloer::reworkname(char a[20])
{strcpy(name,a);}
class reader //讀者(學生,老師)
{
private:
int number;
int age;
char name[20];
char ye[20];
char borrowbook[20];
public:
reader();
reader(int c,int a,char b[20],char d[20],char e[20]);
int getnumber();
int getage();
char *getye();
char *getname();
char * getborrowbook();
void reworkborrowbook(char a[20]);
};
reader::reader()
{
char a[20]="沒有人";
char b[20]="無職稱";
char c[20]="沒有借書";
number=0;
age=0;
strcpy(name,a);
strcpy(ye,b);
strcpy(borrowbook,c);
}
reader::reader(int a,int c,char b[20],char d[20],char e[20])
{
number=a;
age=c;
strcpy(name,b);
strcpy(ye,d);
strcpy(borrowbook,e);
}
int reader::getnumber(){return number;}
int reader::getage(){return age;}
char *reader::getye(){return ye;}
char *reader::getname(){return name;}
char * reader::getborrowbook(){return borrowbook;}
void reader::reworkborrowbook(char a[20]){strcpy(borrowbook,a);}
struct book //圖書
{
int num;
char name[20];
char people[20];
};
void store() //存入圖書信息
{
int i,c,b;
char a[20],d[20];
ifstream file("num.txt");
file>>b;
file.close();
cout<<"現有的圖書個數:";
cout<<b<<endl;
cout<<"請輸入你要輸入的圖書信息個數:"<<endl;
cin>>c;
book *shu=new book[c];
cout<<"輸入圖書信息(編號,名字,作者名:)"<<endl;
for(i=0;i<c;i++)
{
cin>>shu[i].num;
cin>>a>>d;
strcpy(shu[i].name,a);
strcpy(shu[i].people,d);
b++;
}
ofstream outfile("stu.txt",ios_base::app);
for(i=0;i<c;i++)
{
outfile.write((char *)&shu[i],sizeof(shu[i]));
}
outfile.close();
cout<<"存入成功!"<<endl;
ofstream tfile("num.txt");
tfile<<b;
tfile.close();
}
void outbook() //輸出圖書信息
{
int i,h,j,g,z;
char k ;
ifstream tfile("num.txt");
if(!tfile)
{
cout<<" 文件不存在! ";
tfile.close();
cout<<"你是否想創建(輸入Y或N)";
cin>>k;
if(k=='N') exit(0);
else {ofstream tfile("num.txt");g=0;tfile<<g;}
}
else tfile>>g;
cout<<"本圖書館已經有"<<g<<"本圖書"<<endl;
if(g==0)
{cout<<"圖書館里沒有書!"<<endl;exit(0);}
else
{ book *shu=new book[g];
ifstream is("stu.txt",ios_base::binary);
if(is)
{
for(i=0;i<g;i++)
{
is.read((char *)&shu[i],sizeof(shu[i]));
}
for(i=0;i<g;i++)
{
cout<<"編號 名字 作者名"<<endl;
cout<<shu[i].num<<" "<<shu[i].name<<" "<<shu[i].people<<endl;
}
}
else
{
cout<<"ERROR:cannot open file'stu.txt'"<<endl;
}
is.close();
ifstream file("readernum.txt");
if(file) { file>>z; file.close();}
else { cout<<"沒有文件";}
cout<<"已有的讀者個數:";
cout<<z<<endl;
cout<<"輸入你的編號:";
cin>>j;
cout<<"你想借哪本書(輸入編號):";
cin>>h;
reader *people=new reader[z];
ifstream out("reader.txt",ios_base::binary);
if(out)
{
for(i=0;i<z;i++)
{
out.read((char *)&people[i],sizeof(people[i]));
}
}
else
{
cout<<"ERROR:cannot open file'stu.txt'"<<endl;
}
out.close();
people[j-1].reworkborrowbook(shu[h-1].name);
cout<<"編號 年齡 名字 職業 借書情況 "<<endl;
cout<<people[j-1].getnumber()<<" "<<people[j-1].getage()<<" "<<people[j-1].getname()<<" "<<people[j-1].getye()<<" "<<people[j-1].getborrowbook()<<endl;
ofstream outfile("reader.txt",ios_base::binary);
for(i=0;i<z;i++)
{
outfile.write((char *)&people[i],sizeof(people[i]));}
outfile.close();
}
cout<<"借閱成功!";
}
void controller() //圖書管理員信息
{
int b,i,d,h=0;
ifstream file("controlloernum.txt");
if(file) { file>>b; file.close();cout<<"圖書管理員個數:"<<b<<endl;}
else
{
cout<<"沒有圖書管信息理員"<<endl;
cout<<"你是否想創建(請輸入Y或N):";
cin>>d;
if(d='y') {ofstream infile("controlloernum.txt");infile<<h;exit(0);}
else exit(0);
}
ifstream is("controlloer.txt",ios_base::binary);
if(is)
{
controlloer *bookpeople=new controlloer[b];
for(i=0;i<b;i++)
{
is.read((char *)&bookpeople[i],sizeof(bookpeople[i]));
}
for(i=0;i<b;i++)
{
cout<<"編號 年齡 名字 "<<endl;
cout<<bookpeople[i].getnumber()<<" "<<bookpeople[i].getage()<<" "<<bookpeople[i].getname()<<endl;
}
}
else
{
cout<<"ERROR:cannot open file'stu.txt'"<<endl;
cout<<"沒有這個文件夾"<<endl;
cout<<"你是否想創建(請輸入Y或N):";
cin>>d;
if(d='y') {ofstream infile("controlloer.txt");exit(0);}
else exit(0);
}
is.close();
}
void libary() //圖書館的總信息
{
system("cls");
system("color 2e");
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"學校圖書館由本部圖書館及彭州校區圖書館組成.館舍面積41048平方米";
cout<<" (其中現有館舍面積11578 平方米,在建圖書館館舍面積29470平方米),";
cout<<"各院系資料室面積1120平方米。學校擁有紙質文獻198萬余冊, ";
cout<<"生均81.17冊,電子文獻161.7萬余種。近三年校圖書館每年進書量均超過15萬冊."<<endl;
}
void storecontroller() //存入新的管理員的信息
{
int i,b,c,h=0;
char a[20],d;
ifstream file("controlloernum.txt");
if(file)
{
file>>b; file.close();cout<<"已有的圖書管理員個數:";
cout<<b<<endl;
}
else
{
cout<<"沒有圖書管信息理員"<<endl;
cout<<"你是否想創建(請輸入y或n):";
cin>>d;
if(d=='y')
{ofstream infile("controlloernum.txt");infile<<h;infile.close();b=0;}
else exit(0) ;
}
ofstream outfile("controlloer.txt",ios::app);
cout<<"請輸入圖書管理員的編號和年齡,名字:";
cin>>c>>i>>a;
controlloer bookpeople(c,i,a);
cout<<"編號 年齡 名字 "<<endl;
cout<<bookpeople.getnumber()<<" "<<bookpeople.getage()<<" "<<bookpeople.getname()<<endl;
outfile.write((char *)&bookpeople,sizeof(bookpeople));
outfile.close();
b++;
ofstream tfile("controlloernum.txt");
tfile<<b;
tfile.close();
cout<<"保存成功!";
}
void reworkcontrolloer() ///修改圖書管理員
{
int i,b,h,j,g;
char d[20];
ifstream file("controlloernum.txt");
if(file) { file>>b; file.close();}
else {cout<<"沒有圖書管信息理員"; exit(0);}
cout<<"已有的圖書管理員個數:";
cout<<b<<endl;
controlloer *bookpeople=new controlloer[b];
ifstream is("controlloer.txt",ios_base::binary);
if(is)
{
for(i=0;i<b;i++)
{
is.read((char *)&bookpeople[i],sizeof(bookpeople[i]));
}
for(i=0;i<b;i++)
{
cout<<"編號 年齡 名字 "<<endl;
cout<<bookpeople[i].getnumber()<<" "<<bookpeople[i].getage()<<" "<<bookpeople[i].getname()<<endl;
}
}
else
{
cout<<"ERROR:cannot open file'stu.txt'"<<endl;
}
is.close();
cout<<"請輸入你要修改的管理員編號:";
cin>>h;
cout<<"輸入年齡,名字:";
cin>>g>>d;
bookpeople[h-1].reworkage(g);
bookpeople[h-1].reworkname(d);
cout<<"該了後的名字:"<<bookpeople[h-1].getname();
ofstream tfile("controlloer.txt",ios_base::binary);
for(j=0;j<b;j++)
{
tfile.write((char *)&bookpeople[j],sizeof(bookpeople[j]));
}
tfile.close();
}
❻ C語言課程設計 簡單的.....
Answer To Question 1 & 2 :
充分利用ISO組織於1999年通過的C語言國際標准中的庫函數即可實現第一題。利用簡單的雙重循環即可解決問題二,用遞歸演算法不是好的選擇。
第一題完整源碼:
(注意:本源碼中,函數GetWeekDay的返回值僅僅表示輸入參數是否「正確/有效」(例如前三個參數使用2001,2,29就是無效參數),能否得到預期的星期值;真正的星期值則是通過輸出變數返回給主調函數的。)
////////////////////////////////////////////
#include <time.h>
#include <stdio.h>
int GetWeekDay(int nYear,int nMon,int nDay, int *npWeekDay);
int main()
{
int nMyWeekDay;
int nResult;
nResult = GetWeekDay(2007,1,10,&nMyWeekDay);
return 0;
}
//輸入日期,返回星期的C語言函數
int GetWeekDay(int nYear,int nMon,int nDay, int *npWeekDay)
{
int nRetVal = 0;
time_t tMyDateTime;
struct tm tmMyDate,*tmpMyDate;
if((nYear<1900||nYear>=3000)
|| (nMon<1 || nMon>12)
|| (nDay<1||nDay>31) )
{
nRetVal = 1;
*npWeekDay = -1;
return nRetVal;
}
//memset(tmMyDate,0,sizeof(struct tm));
tmMyDate.tm_year = nYear-1900;
tmMyDate.tm_mon = nMon-1;
tmMyDate.tm_mday = nDay;
tmMyDate.tm_hour =16;
tmMyDate.tm_min =30;
tmMyDate.tm_sec =30;
tMyDateTime = mktime(&tmMyDate);
if(tMyDateTime<0)
{
nRetVal = 1;
*npWeekDay = -1;
return nRetVal;
}
tmpMyDate = localtime(&tMyDateTime);
*npWeekDay = tmpMyDate->tm_wday;
return nRetVal;
}
//Over
////////////////////////////////////////////////////
第二題:
#include <time.h>
#include <stdio.h>
int Sum(int n, int a);
int main()
{
//出題者可以自己將此處改為要求操作人鍵入N/A的值
int n=5,a=2,s;
s = Sum(n,a);
return 0;
}
//解決問題的函數
int Sum(int n, int a)
{
int nSum =0;
int nTmp;
int i,j;
for(i=1;i<=n; i++)
{
nTmp =a;
for(j=1;j<=i;j++)
{
nTmp += a*10*(j-1);
}
nSum += nTmp;
}
return nSum;
}
❼ C 課程設計
#include<iostream>
#include<iomanip.h>
#include<math.h>
usingnamespacestd;
intmain()
{
doublesample[6]={6.22,5.46,4.70,3.85,2.48,0.70},ym[6],r[6];
doubleb0,b1,b2,b3;
intflag,i;
b0=sample[0];
for(b2=-10;b2<10;b2+=0.01)
for(b3=-10;b3<10;b3+=0.01)
{
flag=1;
b1=sample[1]-b0-b2-b3;
for(i=2;i<6;i++)
if(fabs(b1-((sample[i]-b0)/i-b2*i-b3*i*i))>0.025)
{flag=0;break;}
if(flag)
{
cout<<"b0=6.22,b1="<<b1<<",b2="<<b2<<",b3="<<b3<<endl;
cout<<"------------------------------ ";
cout<<"xyymr% ";
cout<<"------------------------------ ";
for(i=0;i<6;i++)
{
ym[i]=b0+b1*i+b2*i*i+b3*i*i*i;
r[i]=fabs(ym[i]-sample[i])/sample[i]*100;
cout<<setw(2)<<i<<setw(6)<<sample[i];
cout<<setw(7)<<ym[i]<<setw(7)<<setprecision(2)<<r[i]<<endl;
}
cout<<"------------------------------ ";
return0;
}
}
return0;
}