主题:怎样把结构体编写的程序改变成用类编写的程序
我这有一个程序 使用结构体编写的 现在我想把它用类来实现 但是改了几次都有错误谁能 帮我改过来呢?
#define NULL 0
#define LEN sizeof(struct student)
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
struct student
{
char name[10];
long int id;
int money;
struct student *next;
};
int n;
char ch[10];
/*******链表的建立********/
student *creat()
{
int i;
student *head,*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
printf("请输入学生情况:依次为 姓名,学号,成绩(输入分号;结束)\n");
printf("姓名:");
scanf("%s",ch);
head=NULL;
while(strcmp(ch,";")!=0)
{
p1=(struct student*)malloc(LEN);
strcpy(p1->name,ch);
printf("学号:");
scanf("%ld",&p1->id);
printf("成绩:");
scanf("%d",&p1->money);
if(n==0)
head=p1;
else
p2->next=p1;
p2=p1;
n++;
printf("姓名:");
scanf("%s",ch);
}
p2->next=NULL;
return(head);
}
/******* 链表的输出*******/
void output(struct student *head)
{
struct student *p;
int i=0;
printf("\n输入%d个学生的记录:\n",n);
printf("序号 姓名 学号 成绩\n");
p=head;
if(head!=NULL)
do
{i++;
printf("%d\t %s\t %ld\t %d",i,p->name,p->id,p->money);
printf("\n");
p=p->next;
}
while(p!=NULL);
}
/*******链表的插入*******/
struct student *insert(struct student *head)
{struct student *p1,*p2,*p3;
printf("请输入新学生的情况!\n");
p1=(struct student*)malloc(LEN);
printf("姓名:");
scanf("%s",p1->name);
printf("学生学号:");
scanf("%ld",&p1->id);
printf("成绩:");
scanf("%d",&p1->money);
p2=head;
if(head==NULL)
{head=p1;
p1->next=NULL;
}
else
{
while((p1->id>p2->id)&&(p2->next!=NULL))
{ p3=p2;
p2=p2->next;
}
if(p1->id<p2->id)
{if(head==p2)
{p1->next=head;
head=p1;}
else
p3->next=p1; p1->next=p2;
}
else
{p3->next=p1;
p1->next=p2;
}
}
n=n+1;
return(head);
}
/******链表的删除********/
struct student *delete(struct student *head,long int num)
{
struct student *p1;
printf("输入要删入的学生学号:%ld\n",num);
if(head==NULL)
{
printf("\n这是一个空表,没有删除的结点!\n");
return(head);
}
else
p1=head;
while(num!=p1->id&&p1->next!=NULL)
{
p1=p1->next;
}
if(num==p1->id);
{if(p1==head)
head=p1->next;
else
printf("删除学生学号为%ld的结点\n",num);
n=n-1;
}
printf("没有找到学生学号为%ld的结点\n",num);
return(head);
}
/*******主函数*******/
main()
{struct student *head, pers;
long i;
int k;
do
{
printf("****************************\n");
printf("1 建立全体学生档案\n");
printf("2 输出全体学生档案\n");
printf("4 添加学生档案\n");
printf("5 删除学生档案\n");
printf("6 退出本系统\n");
printf("****************************\n");
printf("请输入选择号(1--6)?");
scanf("%d",&k);
switch(k)
{case 1:head=creat();
break;
case 2: printf("学生人数为:%d人\n");
output(head);
break;
case 4:head=insert(head);
printf("\n调整后学生人数为%d人\n");
output(head);
break;
case 5:printf("请输入要删除学生的学号!\n");
scanf("%ld",&i);
head=delete(head,i);
output(head);
break;
}
} while(k!=6);
}
#define NULL 0
#define LEN sizeof(struct student)
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
struct student
{
char name[10];
long int id;
int money;
struct student *next;
};
int n;
char ch[10];
/*******链表的建立********/
student *creat()
{
int i;
student *head,*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
printf("请输入学生情况:依次为 姓名,学号,成绩(输入分号;结束)\n");
printf("姓名:");
scanf("%s",ch);
head=NULL;
while(strcmp(ch,";")!=0)
{
p1=(struct student*)malloc(LEN);
strcpy(p1->name,ch);
printf("学号:");
scanf("%ld",&p1->id);
printf("成绩:");
scanf("%d",&p1->money);
if(n==0)
head=p1;
else
p2->next=p1;
p2=p1;
n++;
printf("姓名:");
scanf("%s",ch);
}
p2->next=NULL;
return(head);
}
/******* 链表的输出*******/
void output(struct student *head)
{
struct student *p;
int i=0;
printf("\n输入%d个学生的记录:\n",n);
printf("序号 姓名 学号 成绩\n");
p=head;
if(head!=NULL)
do
{i++;
printf("%d\t %s\t %ld\t %d",i,p->name,p->id,p->money);
printf("\n");
p=p->next;
}
while(p!=NULL);
}
/*******链表的插入*******/
struct student *insert(struct student *head)
{struct student *p1,*p2,*p3;
printf("请输入新学生的情况!\n");
p1=(struct student*)malloc(LEN);
printf("姓名:");
scanf("%s",p1->name);
printf("学生学号:");
scanf("%ld",&p1->id);
printf("成绩:");
scanf("%d",&p1->money);
p2=head;
if(head==NULL)
{head=p1;
p1->next=NULL;
}
else
{
while((p1->id>p2->id)&&(p2->next!=NULL))
{ p3=p2;
p2=p2->next;
}
if(p1->id<p2->id)
{if(head==p2)
{p1->next=head;
head=p1;}
else
p3->next=p1; p1->next=p2;
}
else
{p3->next=p1;
p1->next=p2;
}
}
n=n+1;
return(head);
}
/******链表的删除********/
struct student *delete(struct student *head,long int num)
{
struct student *p1;
printf("输入要删入的学生学号:%ld\n",num);
if(head==NULL)
{
printf("\n这是一个空表,没有删除的结点!\n");
return(head);
}
else
p1=head;
while(num!=p1->id&&p1->next!=NULL)
{
p1=p1->next;
}
if(num==p1->id);
{if(p1==head)
head=p1->next;
else
printf("删除学生学号为%ld的结点\n",num);
n=n-1;
}
printf("没有找到学生学号为%ld的结点\n",num);
return(head);
}
/*******主函数*******/
main()
{struct student *head, pers;
long i;
int k;
do
{
printf("****************************\n");
printf("1 建立全体学生档案\n");
printf("2 输出全体学生档案\n");
printf("4 添加学生档案\n");
printf("5 删除学生档案\n");
printf("6 退出本系统\n");
printf("****************************\n");
printf("请输入选择号(1--6)?");
scanf("%d",&k);
switch(k)
{case 1:head=creat();
break;
case 2: printf("学生人数为:%d人\n");
output(head);
break;
case 4:head=insert(head);
printf("\n调整后学生人数为%d人\n");
output(head);
break;
case 5:printf("请输入要删除学生的学号!\n");
scanf("%ld",&i);
head=delete(head,i);
output(head);
break;
}
} while(k!=6);
}