?? 學(xué)生成績shu.cpp
字號:
#include <iostream.h> // cin 及 cout
#include <stdlib.h>
#include <malloc.h> // 用到申請內(nèi)存函數(shù) malloc() 和釋放內(nèi)存函數(shù) free()
#include <string.h> // 字符串處理
#include <stdio.h> // 文件操作(讀文件)
struct address /*家庭地址*/
{
char city[10]; /*城市*/
char town[10]; /*縣城*/
char village[10]; /*鄉(xiāng)鎮(zhèn)*/
};
struct telephone /*聯(lián)系方式*/
{
char SJ[50]; /*手機*/
char JD[30]; /*家庭電話*/
char XD[30]; /*學(xué)校電話*/
};
struct person /*個人信息*/
{
char name[20]; /*名字*/
char sex[10] ; /*性別*/
char MZ[16]; /*民族*/
char GJ[17]; /*國籍*/
char XL[19]; /*學(xué)歷*/
};
struct score //成績
{
char num[20]; //學(xué)號
char english[20];
char chinese[20];
char math[20];
char physics[20];
};
typedef struct linknode //定義節(jié)點的類型
{
char address[100]; //地址
char birthday[100]; //出生日期
struct score sc; //成績
struct person pe; //個人信息
struct telephone te; //聯(lián)系方式
bool flag;
struct linknode* next;
}nodetype;
class List
{
nodetype* head;
public:
List();
List::~List();
linknode* creatlist(int); //創(chuàng)建鏈表
int listlen(); //返回鏈表長度
nodetype* findnode(int); //通過查找序號返回節(jié)點的指針
nodetype* find(char c[]); //通過查找姓名返回節(jié)點的指針
int find2(char c[]); //通過查找姓名返回節(jié)點的序號
nodetype* insnode(int); //插入節(jié)點
void delnode(int); //刪除節(jié)點
nodetype* load(); //初始化:從外部讀入數(shù)據(jù)
void readstr(FILE *f,char *string); //讀行函數(shù)
bool check(char *a, char *b); //對比兩個字符串是否相等
void help(); //顯示幫助菜單
void editperson(nodetype*); //編輯個人說明
void editscore(nodetype*); //編輯學(xué)科成績
void edittelephone(nodetype*); //編輯聯(lián)系方式
void dispname(); //顯示所有學(xué)生姓名
void dispnode(nodetype* p); //顯示一個學(xué)生的所有信息
void dispperson(nodetype*); //顯示一個學(xué)生的個人說明
void dispscore(nodetype*); //顯示一個學(xué)生的學(xué)科成績
void disptelephone(nodetype*); //顯示一個學(xué)生的聯(lián)系方式
};
bool List::check(char *a, char *b) //對比兩個字符串是否相等
{
int i;
int j=strlen(b);
for(i=0; i<j; i++)
{
if(*a==*b)
{
a++;
b++;
}
else
return 0;
}
return 1;
}
nodetype* List::creatlist (int n) //創(chuàng)建鏈表
{
nodetype *h=NULL, *s, *t;
int i=1;
for(int j=0; j<n; j++)
{
if(i==1) //創(chuàng)建第一個節(jié)點
{
h=(nodetype*)malloc(sizeof(nodetype));
h->next=NULL;
t=h;
}
else //創(chuàng)建其余節(jié)點
{
s=(nodetype*)malloc(sizeof(nodetype));
s->next=NULL;
t->next=s;
t=s; //t 始終指向生成的單鏈表的最后一個節(jié)點
}
i++;
}
head=h;
return h;
}
void List::readstr(FILE *f,char *string)
{
do
{
//①: 先讀入一行文本
fgets(string, 255, f); //fgets(): 從文件 f 讀入長度為 255-1 的字符串
// 并存入到 string 中
} while ((string[0] == '/') || (string[0] == '\n'));
return;
}
nodetype* List::load()
{
FILE *fp;
nodetype *p;
char c[255];
int num;
if((fp=fopen("student.txt", "r"))==NULL)
{
cout<<"打開文件失敗"<<endl;
return 0;
}
readstr(fp, c);
sscanf(c, "The Length Of Link: %d", &num); //獲取鏈表長度
p=creatlist(num); //創(chuàng)建鏈表
for(int i=0; i<num; i++)
{
readstr(fp, c);
strcpy(p->address, c);
readstr(fp, c);
strcpy(p->birthday, c);
readstr(fp, c);
strcpy(p->sc.num, c);
readstr(fp, c);
strcpy(p->sc.chinese, c);
readstr(fp, c);
strcpy(p->sc.english, c);
readstr(fp, c);
strcpy(p->sc.math, c);
readstr(fp, c);
strcpy(p->sc.physics, c);
readstr(fp, c);
strcpy(p->pe.name, c);
readstr(fp, c);
strcpy(p->pe.sex, c);
readstr(fp, c);
strcpy(p->pe.GJ, c);
readstr(fp, c);
strcpy(p->pe.MZ, c);
readstr(fp, c);
strcpy(p->pe.XL, c);
readstr(fp, c);
strcpy(p->te.SJ, c);
readstr(fp, c);
strcpy(p->te.JD, c);
readstr(fp, c);
strcpy(p->te.XD, c);
p=p->next;
}
fclose(fp);
return p;
}
void List::dispnode(nodetype* p) //顯示一個學(xué)生的所有信息
{
if(p!=NULL)
{
dispperson(p);
dispscore(p);
disptelephone(p);
}
}
void List::dispname() //顯示所有學(xué)生姓名
{
nodetype* p=head;
cout<<"現(xiàn)有的學(xué)生: "<<endl;
if(p==NULL)
cout<<"沒有任何學(xué)生數(shù)據(jù)"<<endl;
while(p!=NULL)
{
cout<<"姓名: "<<p->pe.name;
p=p->next;
}
}
int List::listlen() //返回鏈表長度
{
int i=0;
nodetype* p=head;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}
nodetype* List::findnode (int i) //通過查找序號返回節(jié)點的指針
{
nodetype* p=head;
int j=1;
if( i>listlen()||i<=0 ) // i 上溢或下溢
return NULL;
else
{
while( p!=NULL && j<i ) //查找第 i 個節(jié)點并由 p 指向該節(jié)點
{
j++;
p=p->next;
}
return p;
}
}
nodetype* List::find(char c[]) //通過查找姓名返回節(jié)點的指針
{
nodetype* p=head;
int j=1;
strcat(c, "\n"); //從外部讀入的字符串末尾都帶了一個換行符
while( p!=NULL && !(check(c, p->pe.name))) //查找第 i 個節(jié)點并由 p 指向該節(jié)點
{
j++;
p=p->next;
}
return p;
}
int List::find2(char c[]) //通過查找姓名返回節(jié)點的序號
{
nodetype* p=head;
int j=1;
strcat(c, "\n"); //從外部讀入的字符串末尾都帶了一個換行符
while( p!=NULL && !(check(c, p->pe.name))) //查找第 i 個節(jié)點并由 p 指向該節(jié)點
{
j++;
p=p->next;
}
return j;
}
nodetype* List::insnode(int i)
{
nodetype *h=head, *p, *s;
s=(nodetype*)malloc(sizeof(nodetype)); //創(chuàng)建節(jié)點 s
s->next=NULL;
if(i==0) //i=0 時 s 作為該單鏈表的第一個節(jié)點
{
s->next = h;
h=s; //重新定義頭節(jié)點
}
else
{
p=findnode(i); //查找第 i 個節(jié)點,并由 p 指向該節(jié)點
if(p!=NULL)
{
s->next=p->next;
p->next=s;
}
else cout<<"輸入的 i 值不正確"<<endl;
}
head=h;
return s;
}
void List::delnode(int i) //刪除第 i 個節(jié)點
{
nodetype *h=head, *p=head, *s;
int j=1;
if(i==1) //刪除第一個節(jié)點
{
h=h->next;
free(p);
}
else
{
p=findnode(i-1); //查找第 i-1 個節(jié)點,并由 p 指向這個節(jié)點
if(p!=NULL && p->next!=NULL)
{
s=p->next; // s 指向要刪除的節(jié)點
p->next=s->next;
free(s);
}
else
cout<<"輸入的 i 值不正確"<<endl;
}
head=h;
}
void List::editperson(nodetype* p)
{
char c[100];
cout<<"請輸入姓名: "<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->pe.name, c);
cout<<"請輸入性別:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->pe.sex, c);
cout<<"請輸入生日(格式舉例:1982-1-1): "<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->birthday, c);
cout<<"請輸入民族:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->pe.MZ, c);
cout<<"請輸入國籍:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->pe.GJ, c);
cout<<"請輸入學(xué)歷:"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->pe.XL, c);
cout<<"請輸入家庭住址(例如:廣西玉林市解放路11號)"<<endl;
cin>>c;
strcat(c, "\n");
strcpy(p->address, c);
cout<<"編輯個人信息完成!"<<endl;
dispperson(p);
}
void List::editscore(nodetype* p)
{
char a[50];
cout<<"請輸入學(xué)號: "<<endl;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -