嗯嗯,第二篇

  入学后经过两个月的学习我终于写了一个在当时看起来像个样子的程序,就是一个链表排序。现在看来这个程序写的相当的挫啊,介于与当时的水平我个人觉得还是不错了,毕竟才学了两个月吗。嗯嗯,接下来上代码(大家不要喷啊,我就是想记录一下自己的成长历程)

// 动态链表.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#define NULL 0
int n;
struct student
{
    int num;
    int score;
    string name;
    int classes;
    student *next;
};
int main ()
{
    void average(student *head,int n);
    void help(void);
    void MAX(student *head);
    void MIN(student *head);
    student *classes_arry_down(student *head,int n);
    student *classes_arry_up(student *head,int n);
    student *name_arry_up(student *head,int n);
    student *name_arry_down(student *head,int n);
    student *num_arry_down(student *head,int n);
    student *num_arry_up(student *head,int n);
    student *score_arry_up(student *head,int n);
    student *score_arry_down(student *head,int n);
    student *creat(void);
    void print(student *);
    student *delet(student *head,int num);
    student *insert(student *head);
    void save(student *head);
    student *open(void);
    student *head;
    help();
    char judger;
    string command;
    string NEWBOOK="newbook",SCORE_ARRY_UP="score_arry_up",DELETE="delete",INSERT="insert",PRINT="print",SAVE="save",OPEN="open",HELP="help"
        ,SCORE_ARRY_DOWN="score_arry_down",NUM_ARRY_UP="num_arry_up",NUM_ARRY_DOWN="num_arry_down",NAME_ARRY_UP="name_arry_up",NAME_ARRY_DOWN="name_arry_down"
        ,CLASS_ARRY_DOWN="class_arry_down",CLASS_ARRY_UP="class_arry_up",MAX_="max",MIN_="min",AVERAGE="score_average";
    for(;(judger=getchar());)
        if(judger=='#')
        {
                cin>>command;
                if(command==NEWBOOK)
                {head=creat();}
                else if(command==HELP)
                {help();}
                else if(command==SAVE)
                {save(head);}
                else if(command==OPEN)
                {head=open();print(head);}
                else    
                    cout<<"error command!"<<endl;
        }
        else if(judger=='$')
        {
            cin>>command;
            if(command==DELETE)
            {
                  int num;
                  cout<<"please put in the number of the member you want to delet: "<<endl;
                  cin>>num;
                  head=delet(head,num);
                  print(head);
            }
            else if(command==INSERT)
            {
                  head=insert(head);
            }
            else if(command==PRINT)
                {print(head);}
            else if(command==SCORE_ARRY_UP)
            {
                head=score_arry_up(head,n);
                print(head);
            }
            else if(command==SCORE_ARRY_DOWN)
            {
                head=score_arry_down(head,n);
                    print(head);
            }
            else if(command==NUM_ARRY_UP)
            {
                head=num_arry_up(head,n);
                print(head);
            }
            else if(command==NUM_ARRY_DOWN)
            {
                head=num_arry_down(head,n);
                print(head);
            }
            else if(command==NAME_ARRY_UP)
            {
                head=name_arry_up(head,n);
                print(head);
            }
            else if(command==NAME_ARRY_DOWN)
            {
                head=name_arry_down(head,n);
                print(head);
            }
            else if(command==CLASS_ARRY_DOWN)
            {
                head=classes_arry_down(head,n);
                print(head);
            }
            else if(command==CLASS_ARRY_UP)
            {
                head=classes_arry_up(head,n);
                print(head);
            }
            else if(command==MAX_)
            {MAX(head);}
            else if(command==MIN_)
            {MIN(head);}
            else if(command==AVERAGE)
            {average(head,n);}
            else    cout<<"error command!"<<endl;
        }
    return 0;
}
student *creat(void)
{
    
    int PUTIN();
    student *head=NULL,*p1,*p2;
    int a=1;
    n=0;
    p2=p1=new student;
    cout<<"please put in student "<<a<<". num "<<endl;
    p1->num=0;
    p1->score=0;
    p1->name="0";
    p1->classes=0;
    PUTIN();
    p1->num=PUTIN();
    if(p1->num!=0)
    {
        cout<<"please put in student "<<a<<". score "<<endl;
        p1->score=PUTIN();
        cout<<"please put in student "<<a<<". name "<<endl;
        cin>>p1->name;
        cout<<"please put in student "<<a<<". class "<<endl;
        PUTIN();
        p1->classes=PUTIN();
        a++;
    }
    for(;p1->num!=0;)
    {
        n++;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=new student;
        p1->num=0;
        p1->score=0;
        p1->name="0";
        p1->classes=0;
        cout<<"please put in student "<<a<<". num "<<endl;
        p1->num=PUTIN();
            if(p1->num!=0)
    {
        cout<<"please put in student "<<a<<". score "<<endl;
        p1->score=PUTIN();
        cout<<"please put in student "<<a<<". name "<<endl;
        cin>>p1->name;
        PUTIN();
        cout<<"please put in student "<<a<<". class "<<endl;
        p1->classes=PUTIN();
        a++;
    }

    }
    p2->next=NULL;
    return (head);
}

void print(student *head)
{
    student *p;
    cout<<endl<<"now ,these"<<"  "<<n<<"  "<<"records are:"<<endl;
    p=head;
    if(p!=NULL)
    {
        for(;p!=NULL;)
        {
            cout<<setiosflags(ios::left)<<"num:"<<setw(6)<<p->num<<"   "<<"score:"<<setw(6)<<p->score<<"    "<<setw(12)<<"name:"<<setw(12)<<p->name<<"    "<<setw(12)<<"class:"<<setw(12)<<p->classes<<endl;
            p=p->next;
        }
    }
}
student *score_arry_up(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->score>p1->score)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->score>p1->score)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}
student *score_arry_down(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->score<p1->score)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->score<p1->score)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}
student *num_arry_down(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->num<p1->num)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->num<p1->num)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}
student *num_arry_up(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->num>p1->num)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->num>p1->num)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}
student *name_arry_up(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->name>p1->name)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->name>p1->name)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}
student *name_arry_down(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->name<p1->name)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->name<p1->name)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}
student *classes_arry_down(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->classes<p1->classes)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->classes<p1->classes)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}
student *classes_arry_up(student *head,int n)
{
    student *p1,*p2,*p3;
    int i,j;
    p1=p2=p3=head;
    for(j=0;j<n-1;j++)
    {
        p1=p2=p3=head;
        p1=p1->next;
        if(p2->classes>p1->classes)
        {
            p2->next=p1->next;
            p1->next=p2;
            head=p1;
            p3=p1;
            p1=p2;
            
        }
        else
        {
            p2=p1;
        }
        for(i=0;i<n-2-j;i++)
        {
            p1=p1->next;
            if(p2->classes>p1->classes)
            {
                p2->next=p1->next;
                p1->next=p2;
                p3->next=p1;
                p3=p1;
                p1=p2;
                
            }
            else
            {
                p3=p2;
                p2=p1;
            }
        }
    }
    return head;
}

student *delet(student *head,int num)
{
    student *p1,*p2;
    p1=head;
    if(head==NULL)
    {cout<<"list is null"<<endl;return head;}
    while(p1->num!=num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p1->num==num)
    {
        if(p1==head)
            head=p1->next;
        else
        {
            p2->next=p1->next;
        }
        n--;
        delete p1;
    }
    else
        cout<<"can't find :"<<num<<endl;
    return head;
}
student *insert(student *head)
{
    student *p1,*p0,*p2;
    p0=new student;
    cout<<"please put in num , score , name , class.please enter other num."<<endl;
    cin>>p0->num>>p0->score>>p0->name>>p0->classes;
    p1=head;
    if(head==NULL)
    {head=p0;p0->next=NULL;}
    else
    {
        while(p1->next!=NULL)
        {
            if(p0->num==p1->num)
            {cout<<"The number you entered having exist"<<endl;return head;}
            else
                p1=p1->next;
        }
        if(p0->num==p1->num)
            {cout<<"The number you entered having exist"<<endl;return head;}
        while((p0->num>p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<=p1->num)
        {
            if(p1==head)
            {head=p0;p0->next=p1;}
            else
                {p2->next=p0;
            p0->next=p1;}
        }
        else
        {p1->next=p0;p0->next=NULL;}
    }
        n++;
        print(head);
    return head;
        
}
void save(student *head)
{
    ofstream outfile("f1.dat",ios::out);
    if(!outfile)
    {
        cout<<"open error!"<<endl;
        exit(1);
    }
        student *p;
    p=head;
    if(p!=NULL)
    {
        for(;p!=NULL;)
        {
            outfile<<p->num<<" "<<p->score<<" "<<p->name<<" "<<p->classes<<" ";
            p=p->next;
        }
    }
    outfile<<"0 0";
    outfile.close();
}
student *open(void)
{
    ifstream infile("f1.dat",ios::in);
    if(!infile)
    {cout<<"open error!"<<endl;exit(1);}
        student *head=NULL,*p1,*p2;
    n=0;
    p2=p1=new student;
    infile>>p1->num>>p1->score>>p1->name>>p1->classes;
    for(;p1->num!=0;)
    {
        n++;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=new student;
        infile>>p1->num >>p1->score>>p1->name>>p1->classes;
    }
    p2->next=NULL;
    infile.close();
    return (head);
}
void help(void)
{
    cout<<"********************************************************************************"<<endl;
    cout<<"instructions:"<<endl;
    cout<<"#newbook 新建工作表,以0结束输入."<<endl<<"#save 保存工作表."<<endl<<"#open 打开工作表."<<endl<<"#help 帮助."<<endl;
    cout<<"$delete 删除数据成员."<<endl<<"$insert 插入新的数据成员."<<endl<<"$成员名称_arry_up 按升序将成员排序.如:$score_arry_up 按升序将成绩排列"<<endl<<"$成员名称_arry_down 按降序将成员列 如: $score_arry_down 按降序将成绩排列."<<endl;
    cout<<"$print 刷新工作表."<<endl<<"$max 求出最大成绩,$min 求出最小成绩."<<endl<<"$score_average 求平均成绩."<<endl;
    cout<<"********************************************************************************"<<endl;
    cout<<endl;
}
void MAX(student *head)
{
    student *p1,*p0;
    p0=new student;
    p1=head;
    if(head==NULL)
    {
        cout<<"list is null."<<endl;
        return;
    }
    p0->score=p1->score;
    while(p1->next!=NULL)
    {
        if(p1->score>p0->score)
        {
                p0->score=p1->score;
        }
        p1=p1->next;
    }
        if(p1->score>p0->score)
        {
                p0->score=p1->score;
        }
        p1=head;
        while(p1->next!=NULL)
        {
            if(p1->score==p0->score)
                  cout<<p1->name<<"'s score is MAX his score is"<<"[ "<<p1->score<<" ]  "<<"his num is"<<" [ "<<p1->num<<" ]  "<<"his class is"<<"[ "<<p1->classes<<" ] "<<endl;
            p1=p1->next;

        }
            if(p1->score==p0->score)
                  cout<<p1->name<<"'s score is MAX his score is"<<"[ "<<p1->score<<" ]  "<<"his num is"<<" [ "<<p1->num<<" ]  "<<"his class is"<<"[ "<<p1->classes<<" ] "<<endl;
}
void MIN(student *head)
{
    student *p1,*p0;
    p0=new student;
    p1=head;
    if(head==NULL)
    {
        cout<<"list is null."<<endl;
        return;
    }
    p0->score=p1->score;
    while(p1->next!=NULL)
    {
        if(p1->score<p0->score)
        {
                p0->score=p1->score;
        }
        p1=p1->next;
    }
        if(p1->score<p0->score)
        {
                p0->score=p1->score;
        }
        p1=head;
        while(p1->next!=NULL)
        {
            if(p1->score==p0->score)
                  cout<<p1->name<<"'s score is MIN his score is"<<"[ "<<p1->score<<" ]  "<<"his num is"<<" [ "<<p1->num<<" ]  "<<"his class is"<<"[ "<<p1->classes<<" ] "<<endl;
            p1=p1->next;

        }
            if(p1->score==p0->score)
                  cout<<p1->name<<"'s score is MIN his score is"<<"[ "<<p1->score<<" ]  "<<"his num is"<<" [ "<<p1->num<<" ]  "<<"his class is"<<"[ "<<p1->classes<<" ] "<<endl;
}
int PUTIN()
{
    int judge(int *s);
    int *s,b,c,d=1;
    s=&b;
    c=judge(s);
    for(;!c;)
    {
        if(d)
             cout<<"error! please enter again!"<<endl;
        d=0;
        c=judge(s);
        d=1;
    }
    
    return b;
}
int judge(int *s)
{
    *s=0;
    char a;
    int i=1;
    for(;(a=getchar())!='\n';)
        if(a>='0'&&a<='9')
            *s=*s*10+a-'0';
        else
            i=0;
    return i;
}
void average(student *head,int n)
{
    int a=0;
    float b;
    student *p1;
    p1=head;
    while(p1->next!=NULL)
    {
        a=a+p1->score;
        p1=p1->next;
    }
    a=a+p1->score;
    b=float(a)/float(n);
    cout<<"average score is "<<b<<endl;
}


当时用了大量的if—else语句,现在看来用个switch语句更合适些啊,还是纪念一下吧,毕竟是当时第一个过百行的程序啊。(没有注释,这个编程习惯不好不好啊)



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值