⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6.cpp

📁 用c++写的一个控制台程序
💻 CPP
字号:
#include"iostream"
#include"fstream"
#include"iomanip"
#include"sstream"
#include"string"
#include"stdlib.h"
using namespace std;

double  synthgrade(double n,double exper,double  exam ) //--返回综合成绩函数
{
  double  syth;
  if (exper=-1)
	  syth=n*0.3+exam*0.7;
  else  syth=n*0.15+exper*0.15+exam*0.7;
	 
 return syth;
}
//---------------------------------------返回学分;

double getcard(double synth,double credit)
{
if (synth>=90)
return credit;
else if (synth>=80&&synth<90)
return credit*0.8;
else if (synth>=70&&synth<80)
return credit*0.75;
else if (synth>=60&&synth<70)
return credit*0.6;
else return 0;

} 
//------------------------------------------


void output(fstream & file) //-----输出基本学生信息
{
	
  while (!file.eof())
{

	char  student[100] ;
	file.getline (student,'/n');
	cout<<student<<endl;

}

file.close ();
}
//----------------------------------------
class stu0
{
public:
	int num;
	string name;
	string sex;
	int dormitory;
	string telecode;
	stu0 * next;
	

};

//----------------------------------------
class stu
{
public:
	int num;
	char course_code[30];
	char course_name[50];
	double  credit_card;
    double normal_grade;
    double  exper_grade;
    double  exam_grade;
	double  synth_grade;
	double  card;
	stu * next;
//************************************************
	stu()
	{
	next=NULL;
	
	}
	void input()
	{
	    cout<<"请输入学号: ";
	cin>>num;
	cout<<endl;
     	cout<<"请输入课程编号: ";
	cin>>course_code;
	cout<<endl;
		cout<<"请输入课程名称: ";
	cin>>course_name;
	cout<<endl;
		cout<<"请输入学分: ";
	cin>>credit_card;
	cout<<endl;
		cout<<"请输入平时成绩: ";
	cin>> normal_grade;
	cout<<endl;
		cout<<"请输入实验成绩: ";
	cin>>exper_grade;
	cout<<endl;
	   cout<<"请输入卷面成绩: ";
	cin>>exam_grade;
	cout<<endl; 
	synth_grade=synthgrade(normal_grade,exper_grade,exam_grade);
	card=getcard(synth_grade,credit_card);	
	
	}
//*******************************************


};
//---------------------------------------------------查找函数;
  void find (int n)
  {    stu0 student;
	   fstream file("stu.txt",ios::in );
	   char s[100];
	   int i=0;

	   while (i==0 && !file.eof())
	   {
	     file.getline(s,'/n');
	     stringstream shuchu(s);
	     shuchu>>student.num ;
	     if (student.num ==n) 
		 {
		   cout<<s; i=1;
		 }

	   }
   
   }
 void find (string  n)
  {    stu0 student;
	   fstream file("stu.txt",ios::in );
	   char s[100];
	   int i=0;

       while (i==0 && !file.eof())
	   {
	     file.getline(s,'/n');
	     stringstream shuchu(s);
	     shuchu>>student.num>>student.name  ;
	     if (student.name==n) 
		 {
		   cout<<s; i=1;
		 }

	   }
   
   }
//-------------------------------------------------
 struct stu00
 { char  s[100];
   stu00 * next;
   stu00()
   {
   next=NULL;
 
   }
 };
//-------------------------------------------------
 void dele (int n)
 {     
       stu00 * head,*p,*top;
	  
	   fstream file("stu.txt",ios::in||ios::out);
	   head=new stu00;
       top=head;
       stu0 student;
	   
	 

	   while (!file.eof())
	   { p=new stu00;
	     file.getline(p->s ,'/n');
		 stringstream shuchu(p->s );
	     shuchu>>student.num ;
	     if (student.num ==n) 
		 {
		   cout<<p->s;
		   	   
		  
		 }
         else 
		 {
			 top->next =p;
			 top=top->next ;
		 }
	   }
 
    file.close();

	fstream file1;
	file1.open ("stu.txt",ios::out);
	top=head->next ;
   
	while (top!=NULL)
	{   file1<<top->s<<endl;
	   
	    top=top->next;
	}
	file1.close();

 
 
 }
 void dele (string n)
 {     
       stu00 * head,*p,*top;
	  
	   fstream file("stu.txt",ios::in);
	   head=new stu00;
       top=head;
       stu0 student;
	   
	 

	   while (!file.eof())
	   { p=new stu00;
	     file.getline(p->s ,'/n');
		 stringstream shuchu(p->s );
	     shuchu>>student.num >>student.name ;
	     if (student.name ==n) 
		 {
		   cout<<p->s;
		   	   
		  
		 }
         else 
		 {
			 top->next =p;
			 top=top->next ;
		 }
	   }
 
    file.close();

	fstream file1;
	file1.open ("stu.txt",ios::out);
	top=head->next ;
   
	while (top!=NULL)
	{   file1<<top->s<<endl;
	   
	    top=top->next;
	}
	file1.close(); 
 
 }
//----------------------------------------------------
 void exchange(stu & a,stu & b)
 {
 	int num=b.num ;
	char course_code[30];
	strcpy(course_code ,b.course_code);
	char course_name[50];
	strcpy(course_name  ,b.course_name);
	double  credit_card=b.credit_card ;	
    double normal_grade=b.normal_grade ;
    double  exper_grade=b.exper_grade ;
    double  exam_grade=b.exam_grade ;
	double  synth_grade=b.synth_grade;
	double  card=b.card ;

	b.num =a.num;
	strcpy(b.course_code ,a.course_code);
    strcpy(b.course_name  ,a.course_name);
	b.credit_card =a.credit_card ;
	b.normal_grade =a.normal_grade ;
	b.exper_grade =a.exper_grade ;
	b.exam_grade =a.exam_grade ;
	b.synth_grade =a.synth_grade ;
	b.card =a.card;

    a.num =num;
	strcpy(a.course_code ,course_code);
    strcpy(a.course_name  ,course_name);
	a.credit_card =credit_card ;
	a.normal_grade =normal_grade ;
	a.exper_grade =exper_grade ;
	a.exam_grade =exam_grade ;
	a.synth_grade =synth_grade ;
	a.card =card;
 
 
 }
 //----------------------------------------------------
void  order (int n)
  {  stu a[100];
      int m=n;
	
	 stu student;
	   fstream file("soc.txt",ios::in );
	   char s[100];
	   while (n)
	   {
	     file.getline(s,'/n');
	     stringstream shuchu(s);
	     shuchu>>a[n-1].num>>a[n-1].course_code>>a[n-1].course_name
	    	>>a[n-1].credit_card>>a[n-1].normal_grade>>a[n-1].exper_grade
		  >>a[n-1].exam_grade>>a[n-1].synth_grade>>a[n-1].card;
	    
         cout<<a[n-1].course_name   ;
		 n--;
	   }
	   file.close();
	 
	  fstream file3("soc.txt",ios::out );
	   

	   for (int i=0;i<m;i++ )
            for (int j=0;j<m-1;j++)
			  	if (a[j].synth_grade > a[j+1].synth_grade )
				{ exchange(a[j],a[j+1]);
				  
				}
				
       
	for (i=m-1;i>=0;i--)
	{ file3<<setw(9)<<a[i].num<<setw(9)<<a[i].course_code <<setw(9)<<a[i].course_name <<"   "
	    	<<a[i].credit_card <<setw(9)<<a[i].normal_grade <<setw(9)<<a[i].exper_grade <<"   "
		  <<a[i].exam_grade <<setw(9)<<a[i].synth_grade <<setw(9)<<a[i].card <<"   "<<endl;
	  
	}
	   file3.close ();
          

 }
//----------------------------------------------------
 void save (stu * pointer)
{
	 fstream file3("soc.txt",ios::out|ios::app);
	while (pointer!=NULL)
	{ file3<<pointer->num<<setw(9)<<pointer->course_code <<setw(9)<<pointer->course_name <<setw(9)
	    	<<pointer->credit_card <<setw(9)<<pointer->normal_grade <<setw(9)<<pointer->exper_grade <<setw(9)<<
			pointer->exam_grade <<setw(9)<<pointer->synth_grade <<setw(9)<<pointer->card <<"   "<<endl;
	   pointer=pointer->next ;
	}
	   file3.close ();



}

 //----------------------------------------------------
void  order1 (int n)
  {  stu a[100];
      int m=n;
	
	 stu student;
	   fstream file("soc.txt",ios::in );
	   char s[100];
	   while (n)
	   {
	     file.getline(s,'/n');
	     stringstream shuchu(s);
	     shuchu>>a[n-1].num>>a[n-1].course_code>>a[n-1].course_name
	    	>>a[n-1].credit_card>>a[n-1].normal_grade>>a[n-1].exper_grade
		  >>a[n-1].exam_grade>>a[n-1].synth_grade>>a[n-1].card;
	    
         cout<<a[n-1].course_name   ;
		 n--;
	   }
	   file.close();
	 
	  fstream file0("soc.txt",ios::out );
	   

	   for (int i=0;i<m;i++ )
            for (int j=0;j<m-1;j++)
			  	if (a[j].synth_grade < a[j+1].synth_grade )
				{ exchange(a[j],a[j+1]);
				  
				}
				
       
	for (i=m-1;i>=0;i--)
	{ file0<<a[i].num<<setw(9)<<a[i].course_code <<setw(9)<<a[i].course_name <<"   "
	    	<<a[i].credit_card <<setw(9)<<a[i].normal_grade <<setw(9)<<a[i].exper_grade <<"   "
		  <<a[i].exam_grade <<setw(9)<<a[i].synth_grade <<setw(9)<<a[i].card <<"   "<<endl;
	  
	}
	   file0.close ();
          

 }
//------------------------------------------------------
//主选菜单函数
int menu()
{
int n;
cout<<"\n\n    ☆☆☆☆欢迎进入学籍管理系统☆☆☆☆"<<endl<<endl;
cout  <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
  <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
  <<"* *                                                 * *"<<endl
  <<"* *    1: 录入学生信息        2: 显示学生信息       * *"<<endl
  <<"* *    3: 查找学生信息        4: 删除学生信息       * *"<<endl
  <<"* *    5: 排序学生信息        6: 保存学生信息       * *"<<endl
  <<"* *    0: 退出系统                                  * *"<<endl
  <<"* *                                                 * *"<<endl
  <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
  <<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
  <<" 请选择: "  ; 
cin>>n;
return n;
}

 main()
 {
 fstream file("stu.txt",ios::in);
 fstream file1("soc.txt",ios::in|ios::out);
 stu *head,*node,*top;
	 head=new stu;
	 top=head;
	 int n;
while ( n=menu() )
{
   switch(n)
   {
 case 1:
	 
     node=new stu;
	 node->input();
	 top->next =node;
	 top=top->next ;
	 break;
 case 2:
	 
	 output(file);
	 break;
 case 3:
	 cout<<"请输入学号或姓名: ";
	 char a[10];
	 int m;
	 cin>>a;
	 if (isdigit(a[0]))
	    {  	m=atoi(a);
	        find(m);
	    }
	 else find (a);
	 break;
 case 4:
	 cout<<"请输入学号或姓名: ";
	 char aa[10];
	 int mm;
	 cin>>aa;
	 if (isdigit(aa[0]))
	    {  	mm=atoi(aa);
	        dele (mm);
	    }
	 else dele  (aa);
	 break;
 case 6:
	 save(head->next);
	 break;
 case 5:
	 cout<<"请输入信息条目个数: ";
	   int i;
	   cin>>i;
	 cout<<"升序1,降序2:   ";
	 int h;
	 cin>>h;
	 if (h==2)
	 {
	  order(i);
	 }else 
		 order1(i);
	 break;

   }
}
 
 
 
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -