需求#

{F7FCAEE6-2F19-0037-F220-102DA76240EE}.png
實(shí)現(xiàn)難點(diǎn)##
- 1 創(chuàng)建表時(shí)
- 創(chuàng)建好學(xué)生節(jié)點(diǎn)之后,需要將創(chuàng)建好的鏈表存入文件。本項(xiàng)目是利用的c語言的文件操作。
fopen,fprint,fscanf,access,fclose幾個(gè)文件操作進(jìn)行文件的操作。
在寫入string類型的數(shù)據(jù)到文件中時(shí)需要將string類型轉(zhuǎn)換成C語言的string類型進(jìn)行操作。string.c_str()。讀取的時(shí)候也不能直接進(jìn)行讀寫,要先把數(shù)據(jù)讀入到一個(gè)字符數(shù)組中char a[20];再用數(shù)組進(jìn)行賦值操作 - 2 升序降序排序
在升序降序排列中主要問題在于鏈表的插入排序是個(gè)難點(diǎn)。在按升序排列時(shí),在遍歷有序序列時(shí),你需要找到的是比當(dāng)前需插入元素更大的元素,并在此元素前進(jìn)行插入。同理,在降序排列時(shí),你需要找到的是比當(dāng)前待插入的數(shù)據(jù)更小的數(shù)據(jù),并在其前面插入數(shù)據(jù)。 在具體實(shí)現(xiàn)中,一定要注意,鏈表始終只有一條,插入排序的精髓在于假設(shè)始終將當(dāng)前元素插入已排好序的序列,因此需要一個(gè)指針指p向當(dāng)前需要插入有序序列的,需要一個(gè)指針始終指向有序序列的頭部(q)以及其next(r),并不斷進(jìn)行遍歷。直到找到所需要的元素的位置。在他前面插入節(jié)點(diǎn)。 在升序排序中,要么你找到了比當(dāng)前待插入元素大的節(jié)點(diǎn),要么你便利完了也沒找到,如果你找到了就在相應(yīng)元素之前進(jìn)行插入,如果沒有找到,說明你是最大的,此時(shí)p已經(jīng)指向了空,直接在r后面進(jìn)行插入就行。所以判斷條件就是while(p!= null&&q->data<p->data)- 3 刪除節(jié)點(diǎn)
- 在刪除節(jié)點(diǎn)時(shí),因?yàn)槭擎湵硪虼四阈枰帽闅v的方式,用兩個(gè)指針進(jìn)行遍歷,一個(gè)指向當(dāng)前節(jié)點(diǎn)一個(gè)指向當(dāng)前節(jié)點(diǎn)前一個(gè)節(jié)點(diǎn)。注意指針的指向,空指針是沒有成員對(duì)象的說法的,如果你指向了具體的成員對(duì)象就會(huì)報(bào)錯(cuò)
- 4 在求一堆數(shù)據(jù)里面的最大最小值時(shí)很好弄的,弄一最大值,弄一個(gè)最小值,再遍歷一遍就好了
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include<math.h>
#include<io.h>
#include <stdlib.h>
using namespace std;
//定義學(xué)生節(jié)點(diǎn)
typedef struct studentNode{
string number;
string name;
int math;
int english;
int chinese;
struct studentNode *next;
}*student;
int countOfStudent ;
FILE * fp;
void ShowAllStudentInfo(student& studentLink);
//輸入學(xué)生信息
void InputStudentInfo(student& studentLink){
cout<<"************歡迎進(jìn)入學(xué)生信息創(chuàng)建菜單*************"<<endl;
cout<<"1.重新創(chuàng)建"<<endl;
cout<<"2.引用已有數(shù)據(jù)"<<endl;
int choice = 0;
int choice1 = 0;
studentLink->next = nullptr;
cin>>choice;
switch(choice){
case 1:{
cout<<"請(qǐng)依次輸入學(xué)號(hào),姓名,數(shù)學(xué)成績,英語成績,語文成績:"<<endl;
countOfStudent = 0;
while(1){
student stu = new studentNode;
countOfStudent = countOfStudent + 1;
cin>>stu->number>>stu->name>>stu->math>>stu->english>>stu->chinese;
cout<<stu->number<<stu->name<<stu->math<<stu->english<<stu->chinese;
//頭插法
stu->next = studentLink->next;
studentLink->next = stu;
cout<<"1.繼續(xù)輸入 2.已完成"<<endl;
cin>>choice1;
if (choice1 == 2)
{
//輸入完成后將學(xué)生信息寫入文件
fp= fopen("output\\studentFile.txt","w");
fprintf(fp,"%d\n",countOfStudent);
student p = studentLink->next;
while(p){
fprintf(fp,"%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(),p->math,p->english,p->chinese);
cout<<p->number<<p->name<<p->math<<p->english<<p->chinese;
p= p->next;
}
fclose(fp);
break;
}else{
cout<<"請(qǐng)依次輸入學(xué)號(hào),姓名,數(shù)學(xué)成績,英語成績,語文成績:"<<endl;
}
}
break;
}
case 2:
//到指定文件夾讀取數(shù)據(jù)
int exists = 0;
if(!access("output\\studentFile.txt",0))
exists=1;
char a[20];
char b[30];
if(exists)
{
fp= fopen("output\\studentFile.txt","rt");
fscanf(fp,"%d",&countOfStudent);
for (int i = 0;i<countOfStudent;i++)
{
student stu = new studentNode;
fscanf(fp,"%s %s %d %d %d\n",b,a,&stu->math,&stu->english,&stu->chinese);
stu->number = b;
stu->name = a;
//頭插法
stu->next = studentLink->next;
studentLink->next = stu;
}
cout<<"已加載完成!"<<"已加載"<<countOfStudent<<"條數(shù)據(jù)!"<<endl;
//cout<<stu1.name<<stu1.number<<stu1.chinese<<stu1.math<<stu1.english<<endl;
}else{
cout<<"暫無本地?cái)?shù)據(jù),請(qǐng)手動(dòng)創(chuàng)建!"<<endl;
}
fclose(fp);
break;
}
}
//顯示所有學(xué)生信息
void ShowAllStudentInfo(student& studentLink){
cout<<"************歡迎進(jìn)入學(xué)生信息瀏覽菜單*************"<<endl;
student p = studentLink->next;
cout<<"學(xué)號(hào): "<<"姓名: "<<"數(shù)學(xué): "<<"英語: "<<"語文: "<<endl;
while(p){
cout<<p->number<<" "<<p->name<<" "<<p->math<<" "<<p->english<<" "<<p->chinese<<endl;
p= p->next;
}
}
//數(shù)據(jù)的排序,并顯示
void SortAllStudentInfo(student& studentLink){
cout<<"************歡迎進(jìn)入學(xué)生信息順序顯示菜單*************"<<endl;
int choice = 0;
cout<<"1 按學(xué)號(hào)升序顯示 2 按學(xué)號(hào)降序顯示 "<<endl;
cout<<"請(qǐng)輸入您的選擇:"<<endl;
cin>>choice;
switch(choice){
case 1:{
student p;//循環(huán)指示變量,用于指向無序表的當(dāng)前待插入元素
student q;//q和r用于對(duì)已有有序序列進(jìn)行循環(huán)遍歷
student r;
student u;//臨時(shí)變量,用于控制循環(huán)
p= studentLink->next;
studentLink->next = nullptr;
while(p){
//每次循環(huán)開始前將指針指向最開始的地方
r = studentLink;
q = studentLink->next;
while( (q!=nullptr)&&q->number < p->number ){
r = q;
q = q->next;
}
//保存下一個(gè)需插入的點(diǎn)
u = p->next;
//保存好了之后進(jìn)行插入
p->next = r->next;
r->next = p;
//插入完成之后,將p指回未排序序列
p = u;
}
{
}
{
fp= fopen("output\\studentAscend.txt","w");
cout<<"************學(xué)生信息升序?yàn)g覽*************"<<endl;
student p = studentLink->next;
cout<<"學(xué)號(hào): "<<"姓名: "<<"數(shù)學(xué): "<<"英語: "<<"語文: "<<endl;
while(p){
cout<<p->number<<" "<<p->name<<" "<<p->math<<" "<<p->english<<" "<<p->chinese<<endl;
fprintf(fp,"%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(),p->math,p->english,p->chinese);
p= p->next;
}
}
fclose(fp);
break;
}
case 2:{
student p,q,r,u;
p = studentLink->next;
studentLink->next = nullptr;
while(p){
r = studentLink;
q = studentLink->next;
while((q != nullptr)&&q->number>p->number){
r = q;
q= q->next;
}
u = p->next;
p->next = r->next;
r->next = p;
p = u;
}
{
fp= fopen("output\\studentDescend.txt","w");
cout<<"************學(xué)生信息降序?yàn)g覽*************"<<endl;
student p = studentLink->next;
cout<<"學(xué)號(hào): "<<"姓名: "<<"數(shù)學(xué): "<<"英語: "<<"語文: "<<endl;
while(p){
cout<<p->number<<" "<<p->name<<" "<<p->math<<" "<<p->english<<" "<<p->chinese<<endl;
fprintf(fp,"%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(),p->math,p->english,p->chinese);
p= p->next;
}
}
fclose(fp);
break;
}
}
}
//學(xué)生信息查詢
void SearchStudentInfo(student& studentLink){
cout<<"************歡迎進(jìn)入學(xué)生信息查詢菜單*************"<<endl;
while (1)
{
cout<<"1.按照學(xué)號(hào)查詢。 2.按照姓名查詢"<<endl;
int choice = 0;
int choice1 = 0;
cin>>choice;
switch(choice){
case 1:
{
student p = studentLink->next;
cout<<"1.按照學(xué)號(hào)精確查詢。 2.按照學(xué)號(hào)模糊查詢"<<endl;
cin>>choice1;
switch(choice1){
case 1:
{
cout<<"請(qǐng)輸入精確學(xué)號(hào):"<<endl;
string num ;
cin>>num;
bool flag = false;
while(p){
if (p->number == num)
{
flag = true;
cout<<"學(xué)號(hào): "<<"姓名: "<<"數(shù)學(xué): "<<"英語: "<<"語文: "<<endl;
cout<<p->number<<" "<<p->name<<" "<<p->math<<" "<<p->english<<" "<<p->chinese<<endl;
break;
}
p=p->next;
}
if (!flag)
{
cout<<"當(dāng)前學(xué)號(hào)不存在"<<endl;
}
break;
}
case 2:
{
cout<<"請(qǐng)輸入前綴學(xué)號(hào):"<<endl;
string num ;
bool flag = false;
cin>>num;
while(p){
if (p->number.find(num,0)==0)
{
if (!flag)
{
cout<<"學(xué)號(hào): "<<"姓名: "<<"數(shù)學(xué): "<<"英語: "<<"語文: "<<endl;
};
cout<<p->number<<" "<<p->name<<" "<<p->math<<" "<<p->english<<" "<<p->chinese<<endl;
flag = true;
}
p=p->next;
}
if (!flag)
{
cout<<"當(dāng)前學(xué)號(hào)不存在"<<endl;
}
break;
}
}
break;}
case 2:
{
student p = studentLink->next;
cout<<"1.按照姓名精確查詢。 2.按照姓氏模糊查詢"<<endl;
cin>>choice1;
switch(choice1){
case 1:
{
cout<<"請(qǐng)輸入精確姓名:"<<endl;
string name ;
cin>>name;
bool flag = false;
while(p){
if (p->name == name)
{
cout<<"學(xué)號(hào): "<<"姓名: "<<"數(shù)學(xué): "<<"英語: "<<"語文: "<<endl;
cout<<p->number<<" "<<p->name<<" "<<p->math<<" "<<p->english<<" "<<p->chinese<<endl;
flag = true;
break;
}
p=p->next;
}
if (!flag)
{
cout<<"當(dāng)前姓名不存在"<<endl;
}
break;
}
case 2:
{
cout<<"請(qǐng)輸入前綴姓氏:"<<endl;
string name ;
bool flag = false;
cin>>name;
while(p){
if (p->name.find(name,0)==0)
{
if (!flag)
{
cout<<"學(xué)號(hào): "<<"姓名: "<<"數(shù)學(xué): "<<"英語: "<<"語文: "<<endl;
};
cout<<p->number<<" "<<p->name<<" "<<p->math<<" "<<p->english<<" "<<p->chinese<<endl;
flag = true;
}
p=p->next;
}
if (!flag)
{
cout<<"當(dāng)前姓氏不存在"<<endl;
}
break;
}
}
break;
}
}
int choice3 = 0;
cout<<"1.繼續(xù)查詢。 2.查詢完成"<<endl;
cin>>choice3;
if (choice3 == 2)
{
break;
}
}
}
//對(duì)學(xué)生信息進(jìn)行修改
void ModifyStudentInfo(student& studentLink){
cout<<"************歡迎進(jìn)入學(xué)生信息修改菜單*************"<<endl;
fp= fopen("output\\studentFile.txt","rt");
fscanf(fp,"%d",&countOfStudent);
fclose(fp);
while(1){
cout<<"1.添加 2.刪除 3.修改"<<endl;
int choice = 0;
cout<<"請(qǐng)輸入您想要的操作:"<<endl;
cin>>choice;
switch(choice){
case 1 :
{
cout<<"請(qǐng)依次輸入學(xué)號(hào),姓名,數(shù)學(xué)成績,英語成績,語文成績:"<<endl;
while(1){
student stu = new studentNode;
countOfStudent = countOfStudent + 1;
cin>>stu->number>>stu->name>>stu->math>>stu->english>>stu->chinese;
cout<<stu->number<<stu->name<<stu->math<<stu->english<<stu->chinese;
//頭插法
stu->next = studentLink->next;
studentLink->next = stu;
cout<<"1.繼續(xù)輸入 2.已完成"<<endl;
int choice1 = 0;
cout<<"請(qǐng)輸入選擇:"<<endl;
cin>>choice1;
if (choice1 == 2)
{
break;
}
}
break;
}
case 2:
{
cout<<"請(qǐng)輸入學(xué)生學(xué)號(hào)"<<endl;
string number1 ;
cin>>number1;
bool flag = false;
student p = studentLink;
student p1 = p->next;
while(p){
if (p1)
{
if (p1->number == number1)
{
cout<<"find"<<endl;
student p2 = p1;
p->next = p2->next;
free(p2);
flag = true;
countOfStudent--;
}
}
p= p->next;
if (p)
{
p1 = p->next;
}
}
if (!flag)
{
cout<<"無此學(xué)生!"<<endl;
}
break;
}
case 3:
{
cout<<"請(qǐng)輸入需修改學(xué)生的學(xué)號(hào):"<<endl;
string number;
cin>>number;
bool flag = false;
student p = studentLink->next;
while(p){
if (p->number == number)
{
flag = true;
cout<<"1 修改學(xué)號(hào) 2 修改姓名 3 修改數(shù)學(xué)成績 4 修改英語成績 5 修改英語成績"<<endl;
int cho = 0;
cin>>cho;
switch(cho){
case 1:{
cout<<"請(qǐng)輸入新的學(xué)號(hào):"<<endl;
string num;
cin>>num;
p->number = num;
break;
}
case 2:{
cout<<"請(qǐng)輸入新的姓名:"<<endl;
string name;
cin>>name;
p->name = name;
break;
}
case 3:{
cout<<"請(qǐng)輸入新的數(shù)學(xué)成績:"<<endl;
int num;
cin>>num;
p->math = num;
break;
}
case 4:{
cout<<"請(qǐng)輸入新的英語成績:"<<endl;
int num;
cin>>num;
p->english = num;
break;
}
case 5:{
cout<<"請(qǐng)輸入新的語文成績:"<<endl;
int num;
cin>>num;
p->chinese = num;
break;
}
}
}
p= p->next;
}
if (!flag)
{
cout<<"該學(xué)號(hào)不存在"<<endl;
}
break;
}
}
cout<<"1 繼續(xù)修改 2 修改完成"<<endl;
int choice7 = 0;
cin>>choice7;
if (choice7 == 2)
{
student p = studentLink->next;
cout<<"sssssss"<<endl;
//輸入完成后將學(xué)生信息寫入文件
fp= fopen("output\\studentFile.txt","w");
fprintf(fp,"%d\n",countOfStudent);
while(p){
fprintf(fp,"%s %s %d %d %d \n", p->number.c_str(), p->name.c_str(),p->math,p->english,p->chinese);
p= p->next;
}
fclose(fp);
break;
}
}
}
//數(shù)據(jù)統(tǒng)計(jì)功能
void StatisticStudentInfo(student& studentLink){
cout<<"1 學(xué)生平均分及總分 2 每門課程平均分機(jī)最高分最低分"<<endl;
int choice = 0;
cout<<"請(qǐng)輸入您的選擇:"<<endl;
cin>>choice;
switch(choice){
case 1:
{
//計(jì)算每個(gè)學(xué)生的平均分和總分
fp= fopen("output\\studentAverageScoreAndTotal.txt","wb");
student p = studentLink->next;
int avg =0;
int total = 0;
cout<<"學(xué)號(hào): "<<"姓名: "<<"平均分: "<<"總分: "<<endl;
while(p){
avg = (p->math + p->english+p->chinese)/3;
total = p->math + p->english+p->chinese;
fprintf(fp,"%s %s %ld %ld\n", p->number.c_str(), p->name.c_str(),avg,total);
cout<<p->number<<" "<<p->name<<" "<<avg<<" "<<total<<endl;
p= p->next;
}
fclose(fp);
break;
}
case 2:
{
int mathMax= 0;int mathMin = 65535;int mathAvg = 0;
int englishMax = 0;int englishMin = 65535;int englishAvg = 0;
int chineseMax = 0;int chinesMin = 65535;int chineseAvg = 0;
int countOfM= 0;
student p = studentLink->next;
while(p){
//遍歷數(shù)學(xué)成績
if (p->math>mathMax){
mathMax = p->math;
}
if(p->math<mathMin){
mathMin = p->math;
}
mathAvg += p->math;
if (p->english>englishMax)
{
englishMax = p->english;
}
if (p->english<englishMin)
{
englishMin = p->english;
}
englishAvg += p->english;
if (p->chinese >chineseMax)
{
chineseMax = p->chinese;
}
if (p->chinese<chinesMin)
{
chinesMin = p->chinese;
}
chineseAvg += p->chinese;
countOfM++;
p = p->next;
}
fp= fopen("output\\studentMath.txt","wb");
fprintf(fp,"數(shù)學(xué): 最高分:%d 最低分: %d 平均分: %d\n", mathMax,mathMin,mathAvg/countOfM);
cout<<"數(shù)學(xué): "<<"最高分: "<<mathMax<<" 最低分: "<<mathMin<<" 平均分: "<<mathAvg/countOfM<<endl;
fclose(fp);
fp= fopen("output\\studentEglish.txt","wb");
fprintf(fp,"英語: 最高分:%d 最低分: %d 平均分: %d\n", englishMax,englishMin,englishAvg/countOfM);
cout<<"英語: "<<"最高分: "<<englishMax<<" 最低分: "<<englishMax<<" 平均分: "<<englishAvg/countOfM<<endl;
fclose(fp);
fp= fopen("output\\studentChinese.txt","wb");
fprintf(fp,"語文: 最高分:%d 最低分: %d 平均分: %d\n", chineseMax,chinesMin,chineseAvg/countOfM);
cout<<"語文: "<<"最高分: "<<chineseMax<<" 最低分: "<<chinesMin<<" 平均分: "<<chineseAvg/countOfM<<endl;
fclose(fp);
break;
}
}
}
int main(){
cout<<"****************************歡迎來到學(xué)生信息管理系統(tǒng)***************************"<<endl;
cout<<"1.輸入學(xué)生信息"<<endl;
cout<<"2.查詢所有學(xué)生信息"<<endl;
cout<<"3.排序顯示學(xué)生信息"<<endl;
cout<<"4.查詢某一個(gè)學(xué)生具體信息"<<endl;
cout<<"5.插入新信息,修改信息,刪除信息"<<endl;
cout<<"6.數(shù)據(jù)統(tǒng)計(jì)"<<endl;
cout<<"7.退出"<<endl;
int choice = 0;
student studentLink = new studentNode ;
//studentLink->next = nullptr;
while(1){
cout<<"主菜單---請(qǐng)輸入您的選擇:";
cin>>choice;
switch(choice){
case 1 :
InputStudentInfo(studentLink);
break;
case 2 :
ShowAllStudentInfo(studentLink);
break;
case 3 :
SortAllStudentInfo(studentLink);
break;
case 4 :
SearchStudentInfo(studentLink);
break;
case 5 :
ModifyStudentInfo(studentLink);
break;
case 6 :
StatisticStudentInfo(studentLink);
break;
case 7 :
exit(0);
break;
}
}
getchar();
return 0;
}
``