暑假自学(2)

今天小学期是用链表做学生管理系统:

成果代码如下:

#include<iostream>
#include<fstream>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<windows.h>
using namespace std;
//学生类
class StudentInfo
{
public:
void Create();
void Input(StudentInfo *head);//输入学生的信息
void Output(StudentInfo *head);//输出学生的信息
void Delete(StudentInfo *head);//删除学生的信息
void Add(StudentInfo *head);//添加学生的信息
void Change(StudentInfo *head);//修改学生的信息
void Search(StudentInfo *head);//查找学生的信息
void Sort(StudentInfo *head);//学生成绩排序
void Save(StudentInfo *head); //保存学生的信息
void Load(StudentInfo *head);//加载学生的信息
void Count(StudentInfo *head);

int num;
char name[20];
char sex[4];
float Chinese;
float English;
float Math;
float sum;
float average;
StudentInfo *next;
private:
};
void StudentInfo::Create()
{
char a[10];
cout<<"请输入文件名:"<<endl;
cin>>a;
fstream file(a);
if(!file)
{
cout<<"Error!"<<endl;
exit(1);
}
file.close();
}
void StudentInfo::Input(StudentInfo *head)//输入学生的信息
{
system("cls");
StudentInfo *p1,*p2;
p1=head;
int N;
cout<<"请输入学生的总人数"<<endl;
cin>>N;
for(int i=0;i<N;i++)
{
p2= new StudentInfo ;
p1->next=p2;
p1=p1->next;
cout<<"请输入学生的信息:(学号,姓名,性别,语文成绩,数学成绩,英语成绩) "<<endl;
cin>>p1->num>>p1->name>>p1->sex>>p1->Chinese>>p1->Math>>p1->English;
p1->sum=p1->Chinese+p1->Math+p1->English;
p1->average=(p1->sum)/3;
}
p1->next=NULL;
}
void StudentInfo::Output(StudentInfo *head)//浏览学生的信息
{
system("cls");
StudentInfo *p1,*p2;

p1=head->next;
int r=0;//名次
cout<<"学号 "<<"姓名 "<<"性别 "<<"语文"
<<" "<<"数学"<<" "<<"英语"<<" "<<" "<<"平均分"<<" "<<" "<<"总分"
<<" "<<" "<<"排名"<<endl;
while(p1!=NULL)
{
cout<<p1->num<<" "<<p1->name<<" "<<p1->sex<<" "<<p1->Chinese<<" "<<p1->Math<<" "<<p1->English<<" "<<" ";
cout<<p1->average<<" "<<" ";
cout<<p1->sum<<" "<<" ";
cout<<r+1<<endl;
r++;
p1=p1->next;
}
}
void StudentInfo::Delete(StudentInfo *head)//删除学生的信息
{
system("cls");
StudentInfo *p1,*p2;
char n;
cout<<"请输入要删除的位置:"<<endl;
cin>>n;
int a;
a=n-'0';
if(a>=0)
{
if(a>=0 && a<=count)
{
p1=head;
p2=p1->next;
while(--a)
{
p1=p1->next;
p2=p2->next;
}
p1->next=p2->next;
}
else
{
cout<<"error!"<<endl ;
}
}
else
{
cout<<"error!"<<endl ;
}
}
void StudentInfo::Add(StudentInfo *head)//添加学生的信息
{
system("cls");
StudentInfo *p1,*p2;
StudentInfo *p3;
char n;
cout<<"请输入要添加的位置 "<<endl;
cin>>n;
int a;
a=n-'0';
if(a>0)
{
if(a>=0 && a<=count)
{
p1=head;
p2=p1->next;
while(--a)
{
p2=p2->next;
p1=p1->next;
}
p3=new StudentInfo;
cout<<"请输入你的信息:(学号,姓名,性别,语文,数学,英语)"<<endl;
cin>>p3->num>>p3->name>>p3->sex>>p3->Chinese>>p3->Math>>p3->English;
p3->sum=p3->Chinese+p3->Math+p3->English;
p3->average=(p3->sum)/3;
p3->next=p2;
p1->next=p3;
}
else
{
cout<<"error!"<<endl ;
}
}
else
{
cout<<"error"<<endl ;
}
}
void StudentInfo::Change(StudentInfo *head)//修改学生的信息
{
system("cls");
StudentInfo *p1,*p2;
StudentInfo *p3;
char n;
cout<<"请选择你要修改的位置 :"<<endl;
cin>>n;
int a;
a=n-'0';
if(a>0)
{
if(a>=0 && a<=count)
{
p1=head;
p2=p1->next;
while(--a)
{
p1=p1->next;
p2=p2->next ;
}
cout<<"请输入你的信息(学号,姓名,性别,语文,数学,英语)"<<endl;
cin>>p2->num>>p2->name>>p2->sex>>p2->Chinese>>p2->Math>>p2->English;
p2->sum=p2->Chinese+p2->Math+p2->English;
p2->average=(p2->sum)/3;
}
else
{
cout<<"error!"<<endl ;
}
}
else
{
cout<<"error!"<<endl ;
}
}
void StudentInfo::Search(StudentInfo *head)//查找学生的信息
{
system("cls");
StudentInfo *p1;
int p;
int i=0;
int stunum;
char stuname[5];
p1=head->next;
cout<<"请选择按学号查找(1)按姓名查找(2)"<<endl;
cin>>p;
if(p==1)
{
system("cls");
cout<<"请输入要查找的学号 "<<endl;
cin>>stunum;
while(p1!=NULL)
{
if(stunum==p1->num)
{
cout<<"学号 "<<"姓名 "<<"性别 "<<"语文"
<<" "<<"数学"<<" "<<"英语"<<" "<<" "<<"平均分"<<" "<<" "<<"总分"
<<" "<<" "<<endl;
cout<<p1->num<<" "<<p1->name<<" "<<p1->sex<<" "<<p1->Chinese<<" "<<p1->Math<<" "<<p1->English<<" "<<" ";
cout<<p1->average<<" "<<" ";
cout<<p1->sum<<" "<<" ";
}
p1=p1->next;
}
}else if(p==2)
{
system("cls");
cout<<"请输入要查找的姓名 "<<endl;
cin>>stuname;
while(p1!=NULL)
{
if(strcmp(p1->name,stuname)==0)
{
cout<<"学号 "<<"姓名 "<<"性别 "<<"语文"
<<" "<<"数学"<<" "<<"英语"<<" "<<" "<<"平均分"<<" "<<" "<<"总分"
<<" "<<" "<<endl;
cout<<p1->num<<" "<<p1->name<<" "<<p1->sex<<" "<<p1->Chinese<<" "<<p1->Math<<" "<<p1->English<<" "<<" ";
cout<<p1->average<<" "<<" ";
cout<<p1->sum<<" "<<" ";
}
p1=p1->next;
}
}else
{
cout<<"Error "<<endl;
}
}
void StudentInfo::Sort(StudentInfo *head)//排序
{
StudentInfo *p1;
StudentInfo *p2;
p1=head;
int n=0;
int a=0;
char b[20];
int i,j;
while(p1->next)
{
p1=p1->next ;
n++;
}
for(i=0;i<n-1;i++)
{
p2=head->next;
p1=p2->next;
for(j=0;j<n-i-1;j++)
{
if(p1->average >p2->average )
{
strcpy(b,p2->name);
strcpy(p2->name ,p1->name );
strcpy(p1->name,b);
strcpy(b,p2->sex);
strcpy(p2->sex ,p1->sex);
strcpy(p1->sex,b);
a=p2->num ;
p2->num =p1->num ;
p1->num =a;
a=p2->Chinese;
p2->Chinese=p1->Chinese;
p1->Chinese=a;
a=p2->Math ;
p2->Math =p1->Math ;
p1->Math =a;
a=p2->English ;
p2->English =p1->English ;
p1->English =a;
a=p2->average;
p2->average =p1->average ;
p1->average =a;

a=p2->sum ;
p2->sum =p1->sum ;
p1->sum =a;
}
p1=p1->next;
p2=p2->next;
}
}
}
void StudentInfo::Save(StudentInfo *head) //保存学生的信息
{
StudentInfo *p;
ofstream os;
os.open("sco.txt");
p=head->next ;
while(p)
{
os<<p->num<<" "<<p->name<<" "<<p->sex<<" "<<p->Chinese<<" " <<p->Math<<" " <<p->English <<" "<<p->sum<<" "<<p->average;
p=p->next;
os<<endl;
}
os.close();
}
void StudentInfo:: Load(StudentInfo *head)//加载学生的信息
{
StudentInfo *p,*p2;
ifstream is;
is.open("sco.txt");
p2=head;
while(1)
{
p=new StudentInfo;
is>>p->num>>p->name>>p->sex>>p->Chinese
>>p->Math>>p->English >>p->sum>>p->average;
if(is.fail() )//判断是否到文件结尾
{
delete p;
p2->next =NULL;
break;
}
p2->next =p;
p2=p2->next;
}
is.close();
}
int count;
void StudentInfo::Count(StudentInfo *head)
{
StudentInfo *p;
count=0;
p=head;
while(p->next!=NULL)
{
count++;
p=p->next;
}
}
void welcome()
{
cout<<"学生成绩管理系统"<<endl;
system("color 1");
}
void menu()
{

cout<<" ~~1. 输入学生的信息 ~~"<<endl;
cout<<" ~~2. 浏览学生的信息 ~~"<<endl;
cout<<" ~~3. 删除学生的信息   ~~"<<endl;
cout<<" ~~4. 添加学生的信息 ~~"<<endl;
cout<<" ~~5. 修改学生的信息 ~~"<<endl;
cout<<" ~~6. 查找学生的信息 ~~"<<endl;
cout<<" ~~7. 排序学生的成绩 ~~"<<endl;
cout<<" ~~8. 保存学生的信息 ~~"<<endl;
cout<<" ~~9. 加载学生的信息 ~~"<<endl;
cout<<" ~~0. 退出学生信息系统 ~~"<<endl;
cout << "学生成绩管理系统:";
cout << "1 创建" << endl;
cout << "2 导入" << endl;//
cout << "3 显示" << endl;//
cout << "4 输入" << endl;//
cout << "5 删除" << endl;//
cout << "6 修改 " << endl;//
cout << "7 查询" << endl;//
cout << "8 汇总" << endl;
cout << "9 排序" << endl;//
cout << "10 导出" << endl;//
cout << "11 插入" << endl;//
cout << "12 退出" << endl;//
cout << "请选择:" << endl;
system("color b");
}
int main()
{
StudentInfo h;
system("cls");
welcome();
Sleep(1000);
int i;
StudentInfo *head=new StudentInfo;

while(1)
{
system("cls");
menu();
cout<<"哈喽,小可爱,请输入你要进行的操作 :"<<endl;
cin>>i;
switch(i)
{
case 1: cout<<"创建文件"<<endl;h.Create();break;
case 2: cout<<"保存学生的信息 "<<endl;h.Save(head);system("pause");h.Count(head);break;
case 3: cout<<"浏览学生的信息 "<<endl;h.Output(head);system("pause");h.Count(head);break;
case 4: cout<<"输入学生的信息 "<<endl;h.Input(head);system("pause");h.Count(head);break;
case 5: cout<<"删除学生的信息 "<<endl;h.Delete(head);system("pause");h.Count(head);break;
case 6: cout<<"修改学生的信息 "<<endl;h.Change(head);system("pause");h.Count(head);break;
case 7: cout<<"查询学生的信息 "<<endl;h.Search(head);system("pause");h.Count(head);break;
case 8: //
case 9: cout<<"学生成绩的排序 "<<endl;h.Sort(head);system("pause");h.Count(head);break;
case 10: cout<<"加载学生的信息 "<<endl;h.Load(head);system("pause");h.Count(head);break;
case 11: cout<<"添加学生的信息 "<<endl;h.Add(head);system("pause");h.Count(head);break;
case 12: cout<<"谢谢使用,欢迎下次光临 "<<endl;system("pause");exit(0);
default: cout<<"输入错误"<<endl;
}
}
return 0;
}

因课本不够生动,去找了java网课,但网课很多都是零基础,所以就跳着把前面的看了看。

然后就是HelloWorld:

package test;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("helloworld!");
}
}

原文地址:https://www.cnblogs.com/buxiang-Christina/p/13263363.html