c語言課程設計學生成績
http://wenku..com/view/92d3d138376baf1ffc4fad1a.html
我的文庫裡面有
我以前的實訓
看你分這么高,給你做了個(可以正確運行),最近好多人找我要,是不是都是你們班的呀
#include<stdio.h>
#include<string.h>
struct Student {
char name[20];
int serial;
int scores[5];
};
main()
{
char ch,temp;
FILE *fpstu;
struct Student stu;
int i,sum=0;
float average;
printf("是否進行成績錄入(Y/N):");
ch=getchar();
temp=getchar();
if((fpstu=fopen("students.txt","a+"))==NULL)
{
printf("file open students.txt failed!\n");
system("pause");
exit(0);
}
if(ch=='Y' || ch=='y')
{
printf("姓名:");
scanf("%s",stu.name);
printf("學號:");
scanf("%d",&stu.serial);
for(i=0;i<5;i++)
{
printf("課程%d分數:",i+1);
scanf("%d",&stu.scores[i]);
}
printf("輸入完畢!\n");
if(fwrite(&stu,sizeof(struct Student),1,fpstu)!=1)
{
printf("存檔失敗!\n");
system("pause");
exit(0);
}
else
printf("存檔完畢!\n");
}
rewind(fpstu);
printf("姓名\t學號\t課程1\t課程2\t課程3\t課程4\t課程5\t總分\t平均\n");
while(!feof(fpstu))
{
if(fread(&stu,sizeof(struct Student),1,fpstu)!=1)
{
printf("讀取完畢!\n");
system("pause");
exit(0);
}
printf("%s\t%d\t",stu.name,stu.serial);
sum = 0;
for (i = 0; i < 5; i++)
{
if(stu.scores[i] >= 90)
temp = 'A';
else if(stu.scores[i] >= 80)
temp = 'B';
else if(stu.scores[i] >= 70)
temp = 'C';
else if(stu.scores[i] >= 60)
temp = 'D';
else
temp = 'E';
printf("%c\t",temp);
sum += stu.scores[i];
}
average = (float)sum / 5;
printf("%d\t%f\n",sum,average);
}
}
㈡ C語言課程設計學生成績統計
# include <iostream>
# include <fstream>
# include <string.h>
#include <conio.h>//用getch();
using namespace std;
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌Student類﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
class Student
{
public:
char name[20];
char Id[20];
int MTnum; //數學課程得分
int ENnum; //英語課程得分
int PHnum; //物理課程得分
int sum; //總分
Student * Next;
void Input()
{
cout<<"\t\t請輸入學生的姓名:"; cin>>name;
cout<<"\t\t請輸入學生的學號:"; cin>>Id;
cout<<"\t\t請輸入數學課程的成績:"; cin>>MTnum;
cout<<"\t\t請輸入英語課程的成績:"; cin>>ENnum;
cout<<"\t\t請輸入物理課程的成績:"; cin>>PHnum;
sum=MTnum+ENnum+PHnum;
}
void ReadFile(istream & in)
{
in>>name>>Id>>MTnum>>ENnum>>PHnum>>sum;
}
void Show()
{
cout<<"姓名:"<<name<<endl<<"學號:"<<Id<<endl<<"C++:"<<MTnum<<endl
<<"數學:"<<ENnum<<endl<<"外語:"<<PHnum<<endl<<"總成
績:"<<sum<<endl<<endl<<endl;
}
};
//﹌﹌﹌﹌﹌﹌﹌﹌﹌Studentmassage類﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
class Studentmassage
{
public:
Studentmassage();
~Studentmassage();
void ShowMenu();
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(Student *,Student *);
void Sort();
//void Unpass();
int ListCount();
//void Average();
void Display()
{
for(Student * p=Head->Next;p!=End;p=p->Next)
p->Show();
cout<<"輸入任意字元!繼續……";
getch();
}
void AddItem()
{
End->Input();
End->Next=new Student;
End=End->Next;
cout<<"添加成功!"<<endl;
cout<<"輸入任意字元!繼續……";
getch();
}
private:
Student * Head,* End;
ifstream in;
ofstream out;
Student *FindItem(char * name)
{
for(Student * p=Head;p->Next!=End;p=p->Next)//匹配成功則返回上一個指針,
不成功就返回空
if(!strcmp(p->Next->name,name))return p;
return NULL;
}
Student *FindID(char * Id)
{
for(Student * p=Head;p->Next!=End;p=p->Next)//匹配成功則返回上一個指針,
不成功就返回空
if(!strcmp(p->Next->Id,Id))return p;
return NULL;
}
};
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌構造函數﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Studentmassage::Studentmassage()
{
Head=new Student;
Head->Next=new Student;
End=Head->Next;
in.open("sort.txt");
if(!in)
cout<<"這是一個新系統,無學生信息。請先輸入。"<<endl;
else
{
while(!in.eof())
{
End->ReadFile(in);
if(End->name[0]=='\0')break;
End->Next=new Student;
End=End->Next;
}
in.close();
cout<<"\t\t讀取學生信息成功!"<<endl;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌析構函數﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Studentmassage::~Studentmassage()
{
Save();
for(Student * temp;Head->Next!=End;)
{
temp=Head->Next;
Head->Next=Head->Next->Next;
delete temp;
}
delete Head,End;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌菜單﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::ShowMenu()
{
cout<<"〓〓〓〓〓〓〓〓〓〓 ☆ 學 生 成 績 管 理 系 統 ☆ 〓〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓★★★★★ ★★★★★★★ ★★★★★〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 1.增加學生成績 ☆ ★〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 2.顯示學生成績 ☆ ★〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 3.排序統計成績 ☆ ★〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 4.查找學生成績 ☆ ★〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 5.刪除學生成績 ☆ ★〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 6.修改學生信息 ☆ ★〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 0.安全退出系統 ☆ ★〓〓〓〓〓〓〓〓
〓"<<endl;
cout<<"\n\t\t\n\t\t請選擇:";
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌查找函數﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Find()
{
char name[20] ,Id[10];
int x;
Student * p=NULL;
cout<<"\n\t\t*********************************\n";
cout<<"\t\t※ 1.按學生的姓名查找\n\t\t※ 2.按學生學號查找";
cout<<"\n\t\t*********************************\n請選擇:";
cin>>x;
switch(x)
{
case 1:{cout<<"\t\t請輸入要查找的學生的姓名:";cin>>name;
if(p=FindItem(name))
{
p->Next->Show();
cout<<"輸入任意字元!繼續……";
getch();
}
else
{
cout<<"\t\t沒有找到該姓名的學生!"<<'\n'<<endl;
cout<<"輸入任意字元!繼續……";
getch();
}
}break;
case 2:
{
cout<<"\t\t請輸入要查找的學生的學號:";cin>>Id;
if(p=FindID(Id))
{
p->Next->Show();
cout<<"輸入任意字元!繼續……";
getch();
}
else
{
cout<<"\t\t沒有找到該學好的學生!"<<'\n'<<endl;
cout<<"輸入任意字元!繼續……";
getch();
}
}break;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌修改信息﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::ModifyItem() //修改信息
{
char name[20];
Student * p=NULL;
cout<<"\t\t請輸入要修改的人的姓名:";cin>>name;
if(p=FindItem(name))
{
cout<<"\t\t已找到學生的信息,請輸入新的信息!"<<endl;
p->Next->Input();
cout<<"修改成功!"<<endl;
cout<<"輸入任意字元!繼續……";
getch();
}
else
{
cout<<"\t\t沒有找到!"<<endl;
cout<<"輸入任意字元!繼續……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌刪除信息﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::RemoveItem() // 刪除信息
{
char name[20];
Student * p=NULL,*temp=NULL;
cout<<"\t\t請輸入要刪除的學生的姓名:"<<endl;cin>>name;
if(p=FindItem(name))
{
temp=p->Next;
p->Next=p->Next->Next;
delete temp;
cout<<"\t\t刪除成功!"<<endl;
cout<<"輸入任意字元!繼續……";
getch();
}
else
{
cout<<"\t\t沒有找到!"<<endl;
cout<<"輸入任意字元!繼續……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Swap(Student *p1, Student *p2)//交換兩個combox變數的數據域
{
Student *temp=new Student;
strcpy(temp->name,p1->name);
strcpy(temp->Id,p1->Id);
temp->MTnum=p1->MTnum;
temp->ENnum=p1->ENnum;
temp->PHnum=p1->PHnum;
temp->sum=p1->sum;
strcpy(p1->name,p2->name);
strcpy(p1->Id,p2->Id);
p1->MTnum=p2->MTnum;
p1->ENnum=p2->ENnum;
p1->PHnum=p2->PHnum;
p1->sum=p2->sum;
strcpy(p2->name,temp->name);
strcpy(p2->Id,temp->Id);
p2->MTnum=temp->MTnum;
p2->ENnum=temp->ENnum;
p2->PHnum=temp->PHnum;
p2->sum=temp->sum;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
int Studentmassage::ListCount()//統計當前鏈表的記錄總數,返回一個整數
{
if(! Head)
return 0;
int n=0;
for(Student * p=Head->Next;p!=End;p=p->Next)
{
n++;
}
return n;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Sort()//對當前鏈表進行排序
{
cout <<"Sorting..."<<endl;
Student *p=NULL,*p1=NULL,*k=NULL;
int n=Studentmassage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->sum>k->sum)
{
Studentmassage::Swap(p,k);
}
}
cout <<"排序完成!"<<endl;
getch();
return;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌保存函數﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Save()
{
out.open("sort.txt");
for(Student *p=Head->Next;p!=End;p=p->Next)
out<<p->name<<"\t"<<p->Id<<"\t"<<p->MTnum<<"\t"
<<p->ENnum<<"\t"<<p->PHnum<<"\t"<<p->sum<<'\n';
out.close();
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌主函數﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
int main()
{
int x,i=0;
bool quit=false;
cout<<"\t\t§§§§§§§§§§§§§§§§§§§§§§§§§§"<<endl;
for(i=0;i<3;i++)
cout<<"\t\t◎\t\t\t\t\t\t ◎"<<endl;
cout<<"\t\t◎★★★★【 歡迎進入學生成績管理系統 】★★★★◎"<<endl;
for(i=0;i<3;i++)
cout<<"\t\t◎\t\t\t\t\t\t ◎"<<endl;
cout<<"\t\t§§§§§§§§§§§§§§§§§§§§§§§§§§\n"<<endl;;
Studentmassage Grade;
cout<<"按任意鍵開始……";
getch();
while(!quit)
{
system("cls");
Grade.ShowMenu();
cin>>x;
switch(x)
{
case 0:quit=true;break;
case 1:Grade.AddItem();break;
case 2:Grade.Display();break;
case 3:Grade.Sort();break;
case 4:Grade.Find();break;
case 5:Grade.RemoveItem();break;
case 6:Grade.ModifyItem();break;
}
}
return 0;
}
㈢ C語言課程設計 學生成績管理系統 詳情如下
結構體和相應操作稍加修改就是你那個 改下細節就能用
#include<iostream>
#include<string.h>
#include<iomanip>
#include<conio.h>
#include <stdlib.h>
using namespace std;
#define NULL 0
struct student
{
char name[30]; //姓名
char sex[30]; //性別
int num; //學號
int age; //年齡
double test_chinese; //語文成績
double test_math; //數學成績
double date; //總成績
struct student *next;
};
student *put_information(student *); //創建學生信息庫
student *del_information(student *); //刪除學生信息
student *insert_information(student *); //添加學生信息
student *search_name(student *); // 按姓名查找學生信息
student *search_num(student *) ; //按學號查找
student *test_totol(student *); ////總體成績
student *order(student *); // 排名
student *print_information(student *); //查看信息
int n;
int main()
{
int enternum;
student *head;
head=NULL;
cout<<"********************************************************************************"<<endl;
cout<<" 歡迎使用學生管理系統 "<<endl;
cout<<"********************************************************************************"<<endl;
cout<<" 按任意鍵執行主菜單! "<<endl;
getch();
while(1)
{
cout<<"***********************************主菜單*************************************"<<endl;
cout<<" 0鍵退出 "<<endl;
cout<<" 1鍵輸入學生信息 "<<endl;
cout<<" 2鍵輸出學習信息 "<<endl;
cout<<" 3鍵刪除學生信息 "<<endl;
cout<<" 4鍵添加學生信息 "<<endl;
cout<<" 5鍵按姓名查找學生信息 "<<endl;
cout<<" 6鍵按學號查找學生信息 "<<endl;
cout<<" 7鍵查看學生總體成績 "<<endl;
cout<<" 8鍵查看排名 "<<endl;
cin>>enternum;
switch(enternum)
{
case(0):cout<<"************************************************************************"<<endl;
cout<<endl;
cout<<" 謝謝使用高校學籍管理系統 "<<endl;
cout<<endl;
cout<<"************************************************************************"<<endl;
exit(0);
case(1):head=put_information(head);
break;
case(2):print_information(head);
break;
case(3):head=del_information(head);
break;
case(4):head=insert_information(head);
break;
case(5):head=search_name(head);
break;
case(6):head=search_num(head);
break;
case(7):head=test_totol(head);
break;
case(8):head=order(head);
break;
default:cout<<" 對不起,你只能輸入0~7鍵,請重新輸入 "<<endl;
break;
}
}
return 0;
}
student *put_information(student *head) ////////信息輸入函數
{
student *p1,*p2;
int N, choose;
if(head!=NULL)
{
cout<<"你已經輸入信息,如果還想輸入,請進入添加信息項添加信息"<<endl;
return (head);
}
cout<<"輸入你想輸入學生信息的個數:";
cin>>N;
n=0;
head=NULL;
p1=p2=new student;
while(n<N)
{
n=n+1;
p1=new student; ///////開辟一個空間
cout<<"請輸入第"<<n<<"個學生的信息:"<<endl;
{
cout<<" 姓名:";
cin>>p1->name;
loop: cout<<" 性別(1 男,2 女):";
cin>>choose;
switch(choose)
{
case(1):
strcpy(p1->sex,"男");break;
case(2):
strcpy(p1->sex,"女");break;
default:
cout<<"你的只能輸入1或2,請重新輸入!!!!";
goto loop;
}
cout<<" 學號:";
cin>>p1->num;
cout<<" 年齡:";
cin>>p1->age;
cout<<"語文成績:";
cin>>p1->test_chinese;
cout<<"數學成績:";
cin>>p1->test_math;
}
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
}
p2->next=NULL;
cout<<"*******************************************************************************"<<endl;
cout<<endl;
cout<<" 你已經成功的輸入了"<<N<<"個人的信息 "<<endl;
cout<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<endl;
return (head);
}
student *del_information(student *head) //刪除信息函數
{
char del_name[30]; //你想要刪除學生的姓名
int input;
student *p1,*p2;
if (head==NULL) //沒輸入數據
{
cout<<"對不起,你還沒輸入學生的信息!"<<endl;
return(head);
}
else
{
p1=head; //使p1指向第一個結點
loop: cout<<"請輸入你要刪除該學生的姓名:";
cin>>del_name;
while(strcmp(p1->name,del_name)!=0&&p1->next!=NULL) //輸入的姓名沒找到且還有結點
{
p2=p1;
p1=p1->next; //p1後移一個結點
}
if(strcmp(del_name,p1->name)==0) //找到要刪除的學生的信息
{
{
if(p1==head) head=p1->next; //若p1指向的是首結點,把第二個結點地址賦予head
else p2->next=p1->next; //否則將下一結點地址賦給前一結點地址
cout<<"***************************************************"<<endl;
cout<<" 刪除成功,你刪除的學生信息為: "<<endl;
cout<<" 姓名:" <<p1->name<<endl;
cout<<" 學號:" <<p1->num<<endl;
cout<<" 性別:" <<p1->sex<<endl;
cout<<" 年齡:" <<p1->age<<endl;
cout<<" 語文成績:" <<p1->test_chinese<<endl;
cout<<" 數學成績:" <<p1->test_math<<endl;
cout<<"***************************************************"<<endl;
n=n-1;
}
LOOP:cout<<"你是否想繼續刪除學生的信息(1繼續,2返回主菜單)"<<endl;
cin>>input;
switch(input)
{
case(1):goto loop;
case(2):break;
default:cout<<"你只能輸入1或2,請重新輸入!"<<endl;
goto LOOP;
}
}
else
{
cout<<" 找不到 "<<del_name<<"的信息,請重新輸入 "<<endl; //找不到信息
goto loop;
}
}
return(head);
}
student *insert_information(student *head) //添加信息函數
{
student *p1,*p2,*p;
int N=0;
head=NULL;
p=new student;
p1=p2=head;
cout<<"請輸入添加到的位置(學號)"<<endl;
cin>>p->num;
cout<<"請輸入添加學生的信息"<<endl;
{
cout<<" 姓名:";
cin>>p->name;
cout<<" 性別:";
cin>>p->sex;
cout<<" 年齡:";
cin>>p->age;
cout<<"語文成績:";
cin>>p->test_chinese;
cout<<"數學成績:";
cin>>p->test_math;
}
while(p1!=0)
{
p2=p1;
p1=p1->next;
}
if(head=NULL)
{
head=p;
p->next=NULL;
N++;
}
else
{
if(p->num==head->num)
{
p=head;
while(head!=NULL)
{
head=head->next;
head->num++;
}
N++;
}
else if(p->num==p1->num&&p->num!=head->num)
{
p2->next=p;
p->next=p1;
while(p1!=NULL)
{
p1->num++;
}
N++;
}
else if(p->num==(p1->num+1)&&p->next==NULL)
{
p=p1->next;
}
}
return (head);
}
student *search_name(student *head) //按姓名查找學生信息函數
{
student *p1,*p2;
int enternum1;
char find_name[30]; //按姓名查找所要輸入的姓名
if(head==NULL) //為空表
{
cout<<"你還沒輸入該學生的信息,請返回輸入!"<<endl;
return (head);
}
else
{
p1=head;
begin: cout<<"請輸入你要查找學生的姓名:";
cin>>find_name;
while(strcmp(find_name,p1->name)!=0&&p1->next!=NULL) ///////輸入的的姓名與已有的數據不同且後面還有學生信息
{
p2=p1;
p1=p1->next; // P1向後移一個節點
}
{
if(strcmp(find_name,p1->name)==0) ///找到了
{
cout<<"***************************************************"<<endl;
cout<<" 刪除成功,你查找的學生信息為: "<<endl;
cout<<" 姓名:" <<p1->name<<endl;
cout<<" 學號:" <<p1->num<<endl;
cout<<" 性別:" <<p1->sex<<endl;
cout<<" 年齡:" <<p1->age<<endl;
cout<<" 語文成績:" <<p1->test_chinese<<endl;
cout<<" 數學成績:" <<p1->test_math<<endl;
cout<<"***************************************************"<<endl;
hand: cout<<" 是否繼續刪除(1繼續2返回主菜單)";
cin>>enternum1;
switch(enternum1)
{
case(1):
goto begin;
case(2):
break;
default:
cout<<"你只能輸入1或2,請重新輸入!"<<endl;
goto hand;
}
}
else cout<<"**************學生信息庫沒該學生的信息!***********"<<endl;
}
}
return(head);
}
student *search_num(student *head)
{
student *p1,*p2;
int enternum2,find_num;
if(head==NULL) //為空表
{
cout<<"你還沒輸入該學生的信息,請返回輸入!"<<endl;
return(head);
}
else
{
p1=head;
begin: cout<<"請輸入你要查找學生的學號:";
cin>>find_num;
while(find_num!=p1->num&&p1->next!=NULL) ///////輸入的的數與已有的數不同且後面還有學生信息
{
p2=p1;
p1=p1->next; //P1向後移一個節點
}
{
if(find_num==p1->num) /////如果找到了
{
cout<<"***************************************************"<<endl;
cout<<" 你查找的學生信息為: "<<endl;
cout<<" 姓名:" <<p1->name<<endl;
cout<<" 學號:" <<p1->num<<endl;
cout<<" 性別:" <<p1->sex<<endl;
cout<<" 年齡:" <<p1->age<<endl;
cout<<" 語文成績:" <<p1->test_chinese<<endl;
cout<<" 數學成績:" <<p1->test_math<<endl;
cout<<"***************************************************"<<endl;
hand: cout<<"按1鍵繼續輸入2鍵返回主菜單!"<<endl;
cin>>enternum2;
switch(enternum2)
{
case(1):
goto begin;break;
case(2):
break;
default:
cout<<"你只能輸入1或2,請重新輸入!"<<endl;
goto hand;break;
}
}
else cout<<"************學生信息庫沒該學生的信息!*********"<<endl;
}
}
return (head);
}
student *test_totol(student *head) //求學生總成績,平均成績和及格率
{
student *p1 ;
int pass1=0,pass2=0; //及格人數
double sum1=0,sum2=0,mean1,mean2,pass_rate1,pass_rate2;
if(head==NULL)
{
cout<<"對不起,你還沒輸入學生信息,請返回輸入!"<<endl; //空表
return (head);
}
else
{
p1=head;
while(p1!=NULL)
{
sum1+=p1->test_chinese;
sum2+=p1->test_math;
if(p1->test_chinese>=60)pass1++;
if(p1->test_math>=60)pass2++;
p1=p1->next;
}
mean1=1.0*sum1/n;
mean2=1.0*sum2/n;
pass_rate1=(pass1/n)*100;
pass_rate2=(pass2/n)*100;
cout<<"*******************************************************"<<endl;
cout<<" 語文的平均成績為: "<<mean1 <<endl;
cout<<" 語文的及格率為: "<<pass_rate1<<"%"<<endl;
cout<<"*******************************************************"<<endl;
cout<<" 數學的平均成績為: "<<mean2 <<endl;
cout<<" 數學的及格率為: "<<pass_rate2<<"%"<<endl;
cout<<"*******************************************************"<<endl;
}
return (head);
}
student *order(student *head)
{
student *p,*q,*tail,*s;
int i=0;
tail=NULL;
while(head->next!=tail)
{
p=head;
p->date=p->test_chinese+p->test_math;
p->next->date=p->next->test_chinese+p->next->test_math;
q=p->next;
while(q->next!=tail)
{
if(p->next->date>q->next->date)
{
s=q->next;
p->next=q->next;
q->next=q->next->next;
p->next->next=q;
q=s;
}
p=p->next;
q=q->next;
}
tail=q;
cout<<"______________________________________"<<endl;
cout<<setw(8)<<"姓名"<<setw(8)<<"總分"<<setw(10)<<"名次"<<endl;
while(p!=NULL)
{
++i;
cout<<"______________________________________"<<endl;
cout<<setw(8)<<p->name<<setw(8)<<p->date<<setw(8)<<"第"<<i<<"名"<<endl;
p=p->next;
}
cout<<"______________________________________"<<endl;
}
return (head);
}
student *print_information(student *head) //查看信息函數
{
student *p1;
if(head==NULL)
{
cout<<"對不起,你還沒輸入學生信息,請返回輸入!"<<endl;
return (head);
}
else
p1=head;
cout<<"____________________________________________________________________________"<<endl;
cout<<setw(8)<<"姓名"<<"|"<<setw(8)<<"學號"<<"|"<<setw(8)<<"性別"<<"|"<<setw(8)
<<"年齡"<<"|"<<setw(8)<<"語文成績"<<"|"<<setw(8)<<"數學成績"<<"|"<<endl;
cout<<"____________________________________________________________________________"<<endl;
while(p1!=NULL)
{
cout<<setw(8)<<p1->name<<"|"
<<setw(8)<<p1->num<<"|"
<<setw(8)<<p1->sex<<"|"
<<setw(8)<<p1->age<<"|"
<<setw(8)<<p1->test_chinese<<"|"
<<setw(8)<<p1->test_math<<"|"<<endl;
cout<<"____________________________________________________________________________"<<endl;
p1=p1->next;
}
return (head);
}
㈣ c語言課程設計之學生成績管理系統設計的程序
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<string.h>
#define MAX 80
#define max 3
int nu=0;
struct classname
{
char name[20];
float score;
};
struct student
{
char no[20];
char std_name[20];
struct classname km[max];
float ave;
float sum;
int save;
};
struct student stu[MAX],*p;
void chushi()
{
int i,j;
for(i=0;i<MAX;i++)
{
for(j=0;j<20;j++)
{
stu[i].no[j]=NULL;
stu[i].std_name[j]=NULL;
stu[i].km[j].name[j]=NULL;
stu[i].km[j].score=0;
}
stu[i].ave=0;
stu[i].sum=0;
stu[i].save=0;
}
}
void av()/*求平均值*/
{
int i;
for(i=0;i<nu;i++)
{
stu[i].sum=stu[i].km[1].score+stu[i].km[2].score+stu[i].km[3].score;
stu[i].ave=stu[i].sum/3;
}
}
void first_check()
{
FILE *p;
int i,j;
struct classname frist[max];
for(i=0;i<max;i++)
{
for(j=0;j<20;j++)
frist[i].name[j]=NULL;
frist[i].score=0;
}
if ((p=fopen("c:\\kemu.txt","r"))==NULL)
{
printf("您好,歡迎使用學生成績管理系統\n\n因為您是第一次使用,請輸入科目名稱(三科)\n\n");
p=fopen("c:\\kemu.txt","w");
printf("輸入課程1名稱:");
scanf("%s",frist[0].name);
fprintf(p,"%s\n",frist[0].name);
printf("輸入課程2名稱:");
scanf("%s",frist[1].name);
fprintf(p,"%s\n",frist[1].name);
printf("輸入課程3名稱:");
scanf("%s",frist[2].name);
fprintf(p,"%s\n",frist[2].name);
}
system("cls");
fclose(p);
}
void save_nu()
{
FILE *p;
p=fopen("c:\\renshu.txt","w");
fprintf(p,"%d\n",nu);
fclose(p);
}
void Save_add(int n)
{
FILE * p;
int i;
p= fopen("c:\\cheji.txt","at");
if (p == NULL)
{
printf("文件不存在!!\n");
exit(0);
}
save_nu();
for (i = 0;i<n;i++)
if(stu[i].save==1)
{
stu[i].sum=stu[i].km[1].score+stu[i].km[2].score+stu[i].km[3].score;
stu[i].ave=stu[i].sum/3;
fprintf(p,"%s %s %2.1f %2.1f %2.1f %2.1f %2.1f \n",stu[i].no,stu[i].std_name,stu[i].km[0].score,stu[i].km[1].score,stu[i].km[2].score,stu[i].ave,stu[i].sum);
}
fclose(p);
}
void Save()
{
FILE * p;
int i;
p= fopen("c:\\cheji.txt","w");
if (p == NULL)
{
printf("文件不存在!!\n");
exit(0);
}
save_nu();
for (i = 0;i<nu;i++)
if(stu[i].save==1)
{
av();
fprintf(p,"%s %s %2.1f %2.1f %2.1f %2.1f %2.1f \n",stu[i].no,stu[i].std_name,stu[i].km[0].score,stu[i].km[1].score,stu[i].km[2].score,stu[i].ave,stu[i].sum);
}
fclose(p);
}
int read_nu()
{
FILE *p;
char ch,s[10]={'\0'};
int i=0;
p=fopen("c:\\renshu.txt","r");
if(p==NULL)
{
save_nu();
return 0;
}
ch=fgetc(p);
while(ch!='\n')
{
s[i]=ch;
ch=fgetc(p);
i++;
}
nu=atoi(s);
fclose(p);
return 0;
}
int read_km()
{
FILE *p;
int i,j=0;
char s[20]={'\0'};
chushi();
p=fopen("c:\\kemu.txt","r");
if(p==NULL)
{
printf("ERROR read_km");
return 0;
}
fgets(s,20,p);
while(strlen(s)!=0)
{
for(i=0;i<strlen(s);i++)
if(s[i]==10)
{
s[i]='\0';
break;
}
for(i=0;i<=nu;i++)
strcpy(stu[i].km[j].name,s);
for(i=0;i<20;i++)
s[i]='\0';
j++;
fgets(s,20,p);
}
}
void read()
{
FILE *p;
int i,j,n,k,z=0;
char s[50]={'\0'};
char o[10]={'\0'};
p=fopen("c:\\cheji.txt","r");
if(p==NULL)
printf("ERROR_read");
chushi();
read_km();
fgets(s,50,p);
while(strlen(s)!=0)
{
j=0;
for(i=0;i<50;i++)
{
if(s[i]!='\n')
{
n=0;
while(j==0)
{
if(s[i]!=' ')
{
stu[z].no[n]=s[i];
n++;i++;
}
else
break;
}
while(j==1)
{
if(s[i]!=' ')
{
stu[z].std_name[n]=s[i];
n++;i++;
}
else
break;
}
while(j==2)
{
if(s[i]!=' ')
{
o[n]=s[i];
n++;i++;
}
else
{
stu[z].km[0].score=atoi(o);
break;
}
}
while(j==3)
{
if(s[i]!=' ')
{
o[n]=s[i];
n++;i++;
}
else
{
stu[z].km[1].score=atoi(o);
break;
}
}
while(j==4)
{
if(s[i]!=' ')
{
o[n]=s[i];
n++;i++;
}
else
{
stu[z].km[2].score=atoi(o);
break;
}
}
while(j==5)
{
if(s[i]!=' ')
{
o[n]=s[i];
n++;i++;
}
else
{
stu[z].ave=atoi(o);
break;
}
}
while(j==6)
{
if(s[i]!=' ')
{
o[n]=s[i];
n++;i++;
}
else
{
stu[z].sum=atoi(o);
break;
}
}
for(k=0;k<10;k++)
o[k]='\0';
}
else
break;
j++;
}
for(i=0;i<50;i++)
s[i]='\0';
fgets(s,50,p);
z++;
}
}
void putin()
{
int n,i=0;
char ch;
read_km();
do
{
printf("\t\t\t\t錄入學員信息\n輸入第%d個學員的信息\n",i+1);
printf("\n輸入學生編號:");
scanf("%s",stu[i].no);
printf("\n輸入學員姓名:");
scanf("%s",stu[i].std_name);
printf("\n輸入課程%s的分數:",stu[0].km[0].name);
scanf("%f",&stu[i].km[0].score);
printf("\n輸入課程%s的分數:",stu[0].km[1].name);
scanf("%f",&stu[i].km[1].score);
printf("\n輸入課程%s的分數:",stu[0].km[2].name);
scanf("%f",&stu[i].km[2].score);
stu[i].save=1;
printf("\n\n");
i++;
n=i;
printf("是否繼續輸入?(Y/N)");
fflush(stdin);
ch=getch();
system("cls");
}
while(ch!='n'&&ch!='N');
system("cls");
if(nu==0)
{
nu=n;
Save();
}
else
{
nu=n+nu;
Save_add(n);
}
}
int putout()
{
int i;char s;
if(nu==0)
{
printf("學生信息為零!請錄入...");
return 0;
}
read();
do
{
printf("學生成績信息:\n\n");
for(i=0;i<nu;i++)
printf("學號:%s 姓名:%s\n%s分數:%2.1f\t%s分數:%2.1f\t%s分數:%2.1f\n平均分數:%2.1f\t總成績:%2.1f\n\n",stu[i].no,stu[i].std_name,stu[i].km[0].name,stu[i].km[0].score,stu[i].km[1].name,stu[i].km[1].score,stu[i].km[2].name,stu[i].km[2].score,stu[i].ave,stu[i].sum);
printf("\t\t按任意鍵返回主菜單");
fflush(stdin);
s=getch();
}
while(!s);
system("cls");
}
int sort()/*排序數據函數*/
{
struct student temp;
int i,j;
char s;
if(nu==0)
{
printf("學生信息為零!請錄入...");
return 0;
}
chushi();
read();
for(i=1;i<nu;i++)
{
for(j=1;j<=nu-i;j++)
{
if(stu[j-1].ave<stu[j].ave)
{
temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
}
}
}
do
{
printf("學生成績信息:\n\n");
for(i=0;i<nu;i++)
printf("學號:%s 姓名:%s 平均成績:%2.1f\n\n",stu[i].no,stu[i].std_name,stu[i].ave);
printf("\t\t按任意鍵返回主菜單");
fflush(stdin);
s=getch();
}
while(!s);
system("cls");
}
void find()/*查詢函數*/
{
int j,i=0;
int c=0;
char search[10]={'\0'};
char as;
if(nu==0)
{
printf("學生信息為零!請錄入...");
return 0;
}
chushi();
read();
do
{
printf("輸入要查詢課程名稱:");
scanf("%s",search);
for(j=0;j<max;j++)
if(!strcmp(stu[i].km[j].name,search))
{
c=1;
printf("\n該課程不及格學生姓名:\n");
for(i=0;i<nu;i++)
if(stu[i].km[j].score<60)
printf("%s\n",stu[i].std_name);
}
if(c==0)
printf("無此課程!");
printf("\n\t\t按任意鍵返回主菜單");
fflush(stdin);
as=getch();
}
while(!as);
system("cls");
}
void tongji()
{
int j,m,z,i=0;
char s;
if(nu==0)
{
printf("學生信息為零!請錄入...");
return 0;
}
chushi();
read();
for(z=0;z<max;z++)
{
m=stu[i].km[z].score;j=0;
printf("%s 最高分: ",stu[i].km[z].name);
for(i=0;i<nu;i++)
if(m<stu[i].km[z].score)
{
m=stu[i].km[z].score;
j=i;
}
printf("%s\t",stu[j].std_name);
j=0;i=0;m=stu[i].km[z].score;
printf("%s 最低分: ",stu[i].km[z].name);
for(i=0;i<nu;i++)
if(m>stu[i].km[z].score)
{
m=stu[i].km[z].score;
j=i;
}
printf("%s\t",stu[j].std_name);
m=0;j=0;i=0;
printf("%s 平均分: ",stu[i].km[z].name);
for(i=0;i<nu;i++)
m=m+stu[i].km[z].score;
printf("%d\n",m/nu);
m=0;i=0;
printf("%s 分數低於的60人數: ",stu[i].km[z].name);
for(i=0;i<nu;i++)
if(stu[i].km[z].score<60)
m++;
printf("%d\t",m);
m=0;j=0;i=0;
printf("%s 分數高於60的人數: ",stu[i].km[z].name);
for(i=0;i<nu;i++)
if(stu[i].km[z].score>60)
m++;
printf("%d\n\n",m);
}
do
{
printf("\t\t按任意鍵返回主菜單");
fflush(stdin);
s=getch();
}
while(!s);
system("cls");
}
void main()/*主函數*/
{
int as;
first_check();
start: printf("\n\t\t\t歡迎使用學生成績管理系統\n");
/*一下為功能選擇模塊*/
do
{
printf("\n\t\t\t\t1.錄入學員信息\n\t\t\t\t2.顯示學員信息\n\t\t\t\t3.成績排序信息\n\t\t\t\t4.查詢不及格學生\n\t\t\t\t5.統計信息\n\t\t\t\t6.退出\n");
printf("\t\t\t\t選擇功能選項:");
fflush(stdin);
read_nu();
scanf("%d",&as);
switch(as)
{
case 1:system("cls");putin();break;
case 2:system("cls");putout();break;
case 3:system("cls");sort();break;
case 4:system("cls");find();break;
case 5:system("cls");tongji();break;
case 6:system("exit");exit(0);
default:system("cls");goto start;
}
}
while(1);
/*至此功能選擇結束*/
}
㈤ C語言課程設計 學生成績管理系統
樓主 ,留個郵箱,發給你一個本人自己寫的....有什麼問題可以問我啊......
㈥ c語言課程設計學生成績管理系統
直接在網路上搜 有 你不必在提問了
㈦ c語言課程設計 學生成績管理系統
我來幫你~\(≧▽≦)/~啦,請問問題解決了嗎?
㈧ 求(C語言課程設計)學生成績管理系統
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
#define CMD_START printf("\n\n######### Start a command #########\n");
/* 用來標記一個命令執行的開始 */
#define CMD_END printf( "\n######### End a command #########\n\n");
/* 用來標記一個命令執行的結束
,這兩個語句是為了提供更好的用戶界面而寫的 */
#define DATA_FILE "data.dat"
/* 這是 數據文件名 */
#define TEMP_FILE "temp.dat"
/* 這是一個臨時的文件的名字,在刪除記錄的函數中使用的,
詳細內容參考 Delete() 函數 */
typedef struct tagStudent
{
char ID[30]; /* 學號 */
char Name[30]; /* 姓名 */
char Class[255]; /* 班級 */
char Sex; /* 性別 ,值為 F 或 f 或 M 或 m */
int Math; /* 數學成績 */
int English; /* 英語成績 */
int Compute; /* 計算機成績 */
int Philosophy; /* 哲學成績 */
int PE; /* 體育成績 */
} Student;
/* 這是學生信息結構體 */
int ShowMenu(); /* 在屏幕上列印 主菜單
的函數,它的返回值為 所選
菜單的 項目編號 */
int ReadData(FILE *, Student *); /* 從一個
打開的數據文件中讀取
記錄的函數,錯誤返回 0 */
int WriteData(FILE *, Student *); /* 向一個數據文件中 寫入
記錄的函數,錯誤返回 0 */
void Output_Rec(Student *); /* 在屏幕上 列印 一條記錄 */
void Input_Rec(Student *); /* 讓用戶輸入 記錄的 各個項目的
值,在添加記錄時用到了 */
void CopyRec(Student *, Student *); /* 復制一條記錄 到
另一個記錄中 */
/* 題目中要求的函數 */
void Print(); /* 實現查看數據文件內容的函數 */
void Add(); /* 添加記錄的函數 */
void Delete(); /* 刪除記錄的函數 */
void Statistics(); /* 對數據進行統計分析的函數 */
void Find(int); /* 實現查找功能的函數,參數決定
是按 ID 查找 還是按 Name 查找 */
int quit; /* 一個全局變數,在下面的 main()
函數中,用來決定何時退出主循環
*/
main()
{
int cmd; /* 用戶所選的 菜單 項目 的標號 */
quit = 0; /* 初始化 為 不退出 */
/* 這是程序的主循環,每次都將 主菜單列印出來,
供用戶選擇相應的 序號 來執行相應的功能 */
while (!quit)
{
cmd = ShowMenu(); /* 顯示
主菜單,並返回用戶所選擇的
菜單項 的 編號 */
CMD_START /* 在屏幕上列印一行分隔符,告訴用戶這是一個子功能的開始
*/
switch (cmd) /* 用多項分支 根據 用戶的選擇
調用 相應的函數 */
{
case 1:
Print();
break; /* 用戶選擇 1 號菜單,程序執行
查看的數據文件的函數 */
case 2:
Add();
break; /* 用戶選擇 2 號菜單,程序執行
添加記錄的函數 */
case 3:
Delete();
break; /* 用戶選擇 3 號菜單,程序執行
刪除記錄的函數 */
case 4:
Statistics();
break; /* 用戶選擇 4 號菜單,程序執行
統計數據的函數 */
case 5:
Find(5);
break; /* Find_ID ,5 號菜單執行 按
ID(學號)查找的功能 */
case 6:
Find(6);
break; /* Find_Name,6 號菜單執行 按
Name(姓名)查找的功能 */
case 7:
quit = 1; /* 用戶選擇了退出菜單 */
printf
(" Thank you for your using .\n\n Happy everyday !!\n\n Bye Bye ....\n");
break;
default:
printf(" Please Input a number between\t1\tto\t7.\n");
/* 用戶所輸入的 序號不在所處理的范圍內 */
}
CMD_END /* 列印一行分隔符,告訴用戶
他所選擇的菜單的功能已經執行完畢
*/
if (quit != 1) /* 檢查用戶是否 要求 退出 */
{
printf(" Press any key to Return Main Menu ....\n");
getch(); /* 用 一個 無回顯 的 字元輸入函數
來實現暫停執行,按 任意鍵
繼續的功能 */
}
}
}
int ShowMenu()
{
int cmd = 0; /* 保存用戶的選擇 */
/* 定義 程序所支持的菜單項目 */
char Menu_SeeData[] = "\t1 .\tView the Records in the data file\n"; /* 查看數據文件
*/
char Menu_Add[] = "\t2 .\tAdd New Record\n"; /* 添加記錄 */
char Menu_Delete[] = "\t3 .\tDelete an old Record\n"; /* 刪除記錄 */
char Menu_Statistics[] = "\t4 .\tMake a Statistics\n"; /* 統計分析 */
char Menu_Find_ID[] = "\t5 .\tFind a Record from the ID\n"; /* 按
學號(ID)
查找 */
char Menu_Find_Name[] = "\t6 .\tFind a Record from the Name\n"; /* 按
姓名(Name)
查找 */
char Menu_Quit[] = "\t7 .\tQuit\n"; /* 退出 */
/* 在屏幕上列印 主菜單 */
printf("\n\n############ Main Menu ###############\n");
printf("##############################################\n\n");
printf(Menu_SeeData);
printf(Menu_Add);
printf(Menu_Delete);
printf(Menu_Statistics);
printf(Menu_Find_ID);
printf(Menu_Find_Name);
printf(Menu_Quit);
printf("\n##############################################");
printf("\n\n Input the index of your choice : ");
scanf("%d", &cmd); /* 接受用戶 選擇 */
printf("\n");
return cmd; /* 返回用戶的輸入,交給主循環處理
*/
}
void Print() /* 列印 數據文件的 記錄內容 */
{
FILE *fp = NULL; /* 文件指針 */
Student rec; /* 存放從文件中讀取的記錄 */
int i = 0; /* 實現 計數 和 分屏列印的功能 */
fp = fopen(DATA_FILE, "rb"); /* 以 二進制讀 方式
打開數據文件 */
if (fp == NULL) /* 打開文件出錯 */
{
printf(" Can not open the data file : %s\n", DATA_FILE);
return;
}
while (ReadData(fp, &rec)) /* ReadData()
函數出錯或到文件末尾時返回
0,可以做循環條件 */
{
Output_Rec(&rec); /* 正確讀取,將記錄輸出 */
printf(" ------------------------------------------");
/* 列印一行分隔符,營造好的用戶界面 */
i ; /* 計數器 加一 */
if (i % 4 == 0) /* 顯示 4 個暫停一下 */
{
printf("\n Press any key to continue ... \n");
getch();
}
}
printf("\n The current data file have\t%d\trecord .\n", i);
fclose(fp); /* 關閉文件 */
}
void Add() /* 添加記錄 */
{
Student rec;
FILE *fp = NULL;
Input_Rec(&rec); /* 讓用戶輸入新記錄的各項內容 */
fp = fopen(DATA_FILE, "ab"); /* 以 添加 方式打開數據文件 */
if (fp == NULL)
{
printf(" Can not open the data file to write into ... \n");
return;
}
if (WriteData(fp, &rec) == 1) /* 將 新記錄 寫入文件,並檢查
是否正確寫入 */
printf("\n\n Add New Record Success \n\n");
else
printf("\n\n Failed to Write New Record into the data file \n");
fclose(fp);
}
void Delete() /* 刪除記錄 */
{
Student rec;
FILE *fpr, *fpw; /* 兩個文件指針,分別用於 讀 和
寫 */
char buf[30]; /* 接受 用戶輸入的 ID 緩沖區 */
char cmd[255]; /* 執行的系統命令 */
int del_count; /* 實際 刪除的 記錄數目 */
del_count = 0;
printf("\n Please type the ID of the record you want me to delete .");
printf("\n The ID : "); /* 提示用戶 輸入 */
scanf("%s", buf);
fpr = fopen(DATA_FILE, "rb"); /* 從
原來的記錄文件中讀取數據,跳過將要刪除的記錄
*/
if (fpr == NULL)
{
printf(" Can not open the data file to read record ... \n");
return;
}
fpw = fopen(TEMP_FILE, "wb"); /* 打開一個 臨時文件
保存不刪除的記錄 */
if (fpw == NULL)
{
printf(" Can not open the data file to write into ... \n");
return;
}
while (ReadData(fpr, &rec)) /* 讀取 要保留的記錄 */
{
if (strcmp(rec.ID, buf) != 0)
{
WriteData(fpw, &rec); /* 寫入臨時文件 ,然後刪除
原數據文件,
再將臨時文件該名為原數據文件的名字
*/
}
else
{
del_count ; /* 跳過的記錄數目,即刪除的數目 */
}
}
fclose(fpr);
fclose(fpw);
strcpy(cmd, "del "); /* 構造命令串,用 system() 函數執行
*/
strcat(cmd, DATA_FILE);
system(cmd);
rename(TEMP_FILE, DATA_FILE); /* 直接調用 C
語言的改名函數將臨時文件改名為數據文件的名字
*/
printf("\n I have delete\t%d\trecord .\n", del_count);
}
void Statistics() /* 統計分析函數 */
{
int i50, i60, i70, i80, i90; /* 平均分小於60
,60-69,70-79,80-89,>=90
的分數段的學生數目 */
float avg; /* 平均分 */
int sum, sum_high, sum_low; /* 總分,總分最高分,總分最低分 */
Student stu_high, stu_low; /* 總分最高和最低 學生的信息 */
Student stu_math_high, stu_english_high; /* 各科
最高分的學生記錄副本
*/
Student stu_compute_high, stu_philosophy_high, stu_PE_high;
Student stu_math_low, stu_english_low; /* 各科最低的學生記錄副本
*/
Student stu_compute_low, stu_philosophy_low, stu_PE_low;
FILE *fp;
Student rec;
int count; /* 一個計數器,用於判斷是否第一次讀取數據
*/
count = sum = sum_high = sum_low = i50 = i60 = i60 = i70 = i80 = i90 = 0;
fp = NULL; /* 對 數據初始化 */
fp = fopen(DATA_FILE, "rb");
if (fp == NULL)
{
printf("\nCan not open the data file to read ...\n");
return;
}
while (ReadData(fp, &rec)) /* 讀取數據 */
{
count ; /* 計數器 加一 */
sum = rec.Math rec.English rec.Compute rec.PE rec.Philosophy; /* 求和
*/
/* average */
avg = ((float)sum) / 5; /* 平均分 */
/* 下面對各個分數段進行統計 */
if (avg < 60)
i50 ;
else if (avg < 70)
i60 ;
else if (avg < 80)
i70 ;
else if (avg < 90)
i80 ;
else
i90 ;
/* highest and loeest */
if (count <= 1) /* 第一次讀取,執行初始化,不進行比較
*/
{
sum_high = sum_low = sum;
CopyRec(&stu_high, &rec);
CopyRec(&stu_low, &rec);
}
else
{
if (sum > sum_high)
{
sum_high = sum; /* 得到最高總分 */
CopyRec(&stu_high, &rec); /* 保存總分最高的學生的信息
*/
}
if (sum < sum_low)
{
sum_low = sum; /* 得到最低分 */
CopyRec(&stu_low, &rec); /* 保存總分最低的學生的信息
*/
}
}
/* subject highest and low */
if (count == 1) /* 同上面一樣,執行初始化 */
{
CopyRec(&stu_math_high, &rec);
CopyRec(&stu_english_high, &rec);
CopyRec(&stu_compute_high, &rec);
CopyRec(&stu_philosophy_high, &rec);
CopyRec(&stu_PE_high, &rec);
CopyRec(&stu_math_low, &rec);
CopyRec(&stu_english_low, &rec);
CopyRec(&stu_compute_low, &rec);
CopyRec(&stu_philosophy_low, &rec);
CopyRec(&stu_PE_low, &rec);
}
else
{
/* High */
/* 保存各科的最高分的學生的信息 */
if (rec.Math > stu_math_high.Math)
CopyRec(&stu_math_high, &rec);
if (rec.English > stu_english_high.English)
CopyRec(&stu_english_high, &rec);
if (rec.Compute > stu_compute_high.Compute)
CopyRec(&stu_compute_high, &rec);
if (rec.Philosophy > stu_philosophy_high.Philosophy)
CopyRec(&stu_philosophy_high, &rec);
if (rec.PE > stu_PE_high.PE)
CopyRec(&stu_PE_high, &rec);
/* low */
/* 保存各科的最低分的學生的信息 */
if (rec.Math < stu_math_low.Math)
CopyRec(&stu_math_low, &rec);
if (rec.English < stu_english_low.English)
CopyRec(&stu_english_low, &rec);
if (rec.Compute < stu_compute_low.Compute)
CopyRec(&stu_compute_low, &rec);
if (rec.Philosophy < stu_philosophy_low.Philosophy)
CopyRec(&stu_philosophy_low, &rec);
if (rec.PE < stu_PE_low.PE)
CopyRec(&stu_PE_low, &rec);
}
} /* While End */
if (count < 1)
{
printf("\n There is no record in the data file .\n");
}
else
{
/* average */
/* 輸出平均分的分段統計信息 */
printf("\n The count in each segment :\n");
printf("\t < 60\t:\t%d\n", i50);
printf("\t60 - 69\t:\t%d\n", i60);
printf("\t70 - 79\t:\t%d\n", i70);
printf("\t80 - 90\t:\t%d\n", i80);
printf("\t >= 90\t:\t%d\n", i90);
printf(" ------------------------------------------");
getch();
/* highest and loeest */
/* 輸出總分最高的學生的信息 */
printf("\n The Highest Mark Student:\n");
printf(" The Mark is : %d\n", sum_high);
printf(" The student is :\n");
Output_Rec(&stu_high);
/* 輸出總分最高的學生的信息 */
printf("\n The Lowest Mark Student:\n");
printf(" The Mark is : %d\n", sum_low);
printf(" The student is :\n");
Output_Rec(&stu_low);
printf(" ------------------------------------------\n");
getch();
/* subject highest and low */
/* 輸出各科最高和最低分的統計信息 */
printf(" The Highest\tMath :\n");
Output_Rec(&stu_math_high);
printf(" The Lowest Math :\n");
Output_Rec(&stu_math_low);
printf(" ------------------------------------------\n");
getch(); /* 暫停 ,按任意鍵繼續 */
printf(" The Highest English :\n");
Output_Rec(&stu_english_high);
printf(" The Lowest English :\n");
Output_Rec(&stu_english_low);
printf(" ------------------------------------------\n");
getch();
printf(" The Highest Compute :\n");
Output_Rec(&stu_compute_high);
printf(" The Lowest Compute :\n");
Output_Rec(&stu_compute_low);
printf(" ------------------------------------------\n");
getch();
printf(" The Highest Philosophy :\n");
Output_Rec(&stu_philosophy_high);
printf(" The Lowest Philosophy :\n");
Output_Rec(&stu_philosophy_low);
printf(" ------------------------------------------\n");
getch();
printf(" The Highest PE :\n");
Output_Rec(&stu_PE_high);
printf(" The Lowest PE :\n");
Output_Rec(&stu_PE_low);
printf(" ------------------------------------------\n");
}
fclose(fp);
}
void Find(int isFind_From_ID) /* 查找函數 */
{
char buf[30]; /* 接受用戶輸入的條件的緩沖區 */
Student rec;
int find_count; /* 查找到的記錄數目 */
FILE *fp;
find_count = 0;
fp = fopen(DATA_FILE, "rb");
if (fp == NULL)
{
printf("\n Can not open the data file to search ...\n");
return;
}
switch (isFind_From_ID)
{
case 5: /* ID,按 學號查找 */
printf("\n Please Input the ID : ");
scanf("%s", buf); /* 提示用戶輸入 */
while (ReadData(fp, &rec)) /* 讀取數據 */
{
if (strcmp(rec.ID, buf) == 0) /* 比較 */
{
find_count ;
Output_Rec(&rec); /* 輸出符合條件的記錄 */
printf(" ------------------------------------------\n");
}
}
break;
case 6: /* Name ,按 姓名 查找 */
printf("\n Please Input the Name : ");
scanf("%s", buf);
while (ReadData(fp, &rec))
{
if (strcmp(rec.Name, buf) == 0)
{
find_count ;
Output_Rec(&rec);
printf(" ------------------------------------------\n");
}
}
break;
default:
printf(" \nPlease type right index ...\n"); /* 處理isFind_From_ID既不是5也不是6的情況
*/
}
if (find_count > 0) /* 輸出找到的記錄數目 */
{
printf("\n Have find out\t%d\trecord\n", find_count);
}
else
{
printf
("\n I'm very sorry .\n I failed to find out the one you want .\n");
printf("\n I suggest that you change some other key words .\n");
}
fclose(fp);
}
int ReadData(FILE * fp, Student * Dest_Rec) /* 讀取數據記錄 */
{
int r;
if ((fp == NULL) || (Dest_Rec == NULL))
return 0; /* ERROR */
r = fread(Dest_Rec, sizeof(Student), 1, fp);
if (r != 1)
return 0;
return 1;
}
int WriteData(FILE * fp, Student * Src_Rec) /* 寫入數據記錄 */
{
int r;
if ((fp == NULL) || (Src_Rec == NULL))
return 0; /* ERROR */
r = fwrite(Src_Rec, sizeof(Student), 1, fp);
if (r != 1)
return 0;
return 1;
}
void Output_Rec(Student * stu) /* 在屏幕上輸出 一個學生的信息 */
{
printf("\n");
printf(" Name : %s", stu->Name);
printf("\t\tSex : ");
if (stu->Sex == 'M' || stu->Sex == 'm')
printf("Male");
else
printf("Female");
printf("\n ID : %s\t\tClass : %s\n", stu->ID, stu->Class);
printf("Math = %d\tEnglish = %d\tCompute = %d\n", stu->Math, stu->English,
stu->Compute);
printf("Philosophy = %d\t\tPE = %d\n", stu->Philosophy, stu->PE);
printf("\n");
}
void Input_Rec(Student * stu) /* 讓用戶輸入 一個學生的各項信息
*/
{
if (stu == NULL)
return;
printf("\n Name = ");
scanf("%s", stu->Name);
/* 下面這段代碼實現只能輸入 F ,f ,M ,m 的功能 */
printf("\tSex = (F|M) ");
while (1)
{
stu->Sex = getch(); /* 無回顯輸入 */
if (stu->Sex == 'F' || stu->Sex == 'f' || stu->Sex == 'M'
|| stu->Sex == 'm')
{
printf("%c", stu->Sex); /* 將用戶輸入的字元輸出,模擬正常輸入數據時的回顯
*/
break;
}
}
/* 下面 給出提示,讓用戶輸入結構體的各項內容 */
printf("\n ID = ");
scanf("%s", stu->ID);
printf("\n Class = ");
scanf("%s", stu->Class);
printf("\n Math = ");
scanf("%d", &(stu->Math));
printf("\n English = ");
scanf("%d", &(stu->English));
printf("\n Compute = ");
scanf("%d", &(stu->Compute));
printf("\n Philosophy = ");
scanf("%d", &(stu->Philosophy));
printf("\n PE = ");
scanf("%d", &(stu->PE));
printf("\n");
}
/* 因為結構體不能直接用 等號(=)賦值,寫一個賦值函數 */
void CopyRec(Student * dest_stu, Student * src_stu)
{
/* 復制 源記錄 的各項到 目標記錄 */
strcpy(dest_stu->Name, src_stu->Name);
strcpy(dest_stu->ID, src_stu->ID);
strcpy(dest_stu->Class, src_stu->Class);
dest_stu->Sex = src_stu->Sex;
dest_stu->Math = src_stu->Math;
dest_stu->English = src_stu->English;
dest_stu->Compute = src_stu->Compute;
dest_stu->Philosophy = src_stu->Philosophy;
dest_stu->PE = src_stu->PE;
}
/*
題目分析 及 演算法設計 :
題目中的各個功能都是相對獨立的,所以我將各項功能以
帶 編號 的菜單形式組織在屏幕上,用戶通過 輸入 編號
執行相應的功能。顯示菜單的代碼處於一個循環之中,當執行完一個子功能後,就又回到循環,顯示主菜單,直到用戶選擇
退出 菜單。 這種操作方式比其它機制(如:主程序
程序參數)更簡捷,不必每次用不同的參數重新運行程序,以實現相應的功能。
1. 查看文件記錄內容的實現: 用循環讀取文件內容,然後顯示在屏幕上。
因為我們的數據是以結構體的形式存放在文件中的,所以
代碼中用了塊讀取和塊寫入函數。 在循環中設置計數器來統計記錄的個數。 2.
添加記錄的實現:
讓用戶根據屏幕提示輸入數據,完成對學生信息結構體各項的賦值,待取得足夠數據後,將數據文件以「追加」方式打開,執行塊寫入,將整個結構體寫入文件。
3. 刪除記錄的實現:
學號(ID)一般不會重復,所以我在程序中讓用戶輸入想要刪除的記錄的學號(ID),然後在文件中查找,如果不是用戶想要刪除的記錄(即ID不同),就保存在一個臨時的文件中,這樣,就將想要刪除的記錄與其它記錄分離開了,最後,刪除原來的數據文件,將臨時文件的名字改為
原來數據文件的名字。 4. 統計功能的實現:
統計功能模塊分為三個小模塊:平均分的分數段統計,總分的最高和最低分統計,各科的最高和最低分統計。但我並不想分別來寫,因為它們都要對所有記錄進行掃描,而它們又互不幹擾,所以我把它們組織在一個循環中,各自都有自己的計算代碼和變數,所以這個
函數 中的局部變數 很多。 5. 查找功能的實現: 題目要求兩種查找方式:按 學號(ID) , 按 姓名(Name)。 兩者是獨立的,所以我用了一個參數 isFind_From_ID
來表明是哪種查找方式,進而在在程序內部由一個 switch() 選擇分支轉向不同的代碼段去執行。
具體的查找就是比較相應的項目是否與用戶輸入的一樣,若一樣就輸出到屏幕。 */