C++课程设计题目
1.通讯录
设计一个学生类,类中有一些公有函数,能够实现插入,查询,修改,删除等功能。通过MAIN()函数测试。
2 设计一个字符串类,功能如下:
1) 能够用 “+” 来处理两个字符串的相加
2) 具有在一个字符串中搜索一个字符的功能
3) 具有在一个字符串中搜索另一个字符串的功能
4) 编写一个main()函数,测试你的字符串类的各种功能。
恩,帮一下啊
这是我写的:
#include<stdio.h>
#include<malloc.h>
#include<iostream.h>
#include<string.h>
#define len sizeof(struct school)
#define len1 sizeof(struct statitcs)
struct school
{
char name[20];
//int no;
struct school *next;
};
struct school *insert(void)
{ cout<<"********************************\n";
cout<<"特别提示:\n";
struct school *head,*rear,*p;
head=NULL;rear=NULL;
p=(struct school *)malloc(len);
cout<<"请输入姓名\n";cin>>p->name;
if(p->num==0)
return NULL;
while(p->num!=0)
{if(head==NULL)
{head=p;rear=p;}
else
{rear->next=p;
rear=p;
}
p=(struct school *)malloc(len);
cout<<"请输入姓名\n";cin>>p->name;
}
rear->next=NULL;
return head;
}
struct school *add(struct school *t)
{
struct school *s,*p;
p=(struct school *)malloc(len);
cout<<"请输入姓名\n";cin>>p->name;
s=t;
if(s==NULL)
{ p->next=s;
s=p;
return s;}
else
{while(s->next!=NULL)
{s=s->next;}
s->next=p;
p->next=NULL;
return t;}
}
void print(struct school *t)
{struct school *p;
p=t;
cout<<"\t姓名\n";
while(p!=NULL)
{cout<<p->name<<endl;
p=p->next;
}
}
void display(struct school *t,int n)
{struct school *p;
p=t;
while(p!=NULL)
{if(p->num==n)
cout<<p->name<<endl;
p=p->next;
}
}
struct school *deletes(struct school *t)
{struct school *p,*k,*k_head;int flag=0;
p=(struct school *)malloc(len);
cout<<"请输入要删除的信息\n";
cout<<"请输入姓名\n";cin>>p->name;
k=t;
while(k!=NULL)
{if((p->num==k->num)&&(k->project_num==p->project_num)&&!strcmp(p->name,k->name)&&(p->score==k->score))
{flag=1;
if(k==t)
{t=t->next;}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
void find(struct school *t)
{struct school *p;
p=t;
cout<<"\t姓名\n";
while(p!=NULL)
{cout<<p->num<<"\t\t"<<p->project_num<<"\t"<<p->name<<"\t"<<p->score<<endl;
p=p->next;
}
}
void find1(struct school *t)
{struct school *k;int flag=0;int num;
cout<<"姓名\n";
k=t;
while(k!=NULL)
{if(num==k->num)
{flag=1;cout<<k->name<<endl;}
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要的信息\n";
}
void find2(struct school *t)
{struct school *k;int flag=0;int num;
cout<<"请输入要查找的信息\n";
cout<<"请输入项目号\n";cin>>num;
cout<<"\t姓名\n";
k=t;
while(k!=NULL)
{if(num==k->project_num)
{flag=1;cout<<k->name<<endl;}
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要的信息\n";
}
void find3(struct school *t)
{struct school *k;int flag=0;char name[20];
cout<<"请输入要查找的信息\n";
cout<<"请输入姓名\n";cin>>name;
cout<<"姓名\n";
k=t;
while(k!=NULL)
{if(!strcmp(name,k->name))
{flag=1;cout<<k->name<<endl;}
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要的信息\n";
}
struct school *deletes1(struct school *t)
{struct school *k,*k_head;int flag=0;int num;
cout<<"请输入要删除的信息\n";
k=t;
while(k!=NULL)
{if(k->num==num)
{flag=1;
if(k==t)
{t=t->next;}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
struct school *deletes2(struct school *t)
{struct school *k,*k_head;int flag=0;int num;
cout<<"请输入要删除的信息\n";
k=t;
while(k!=NULL)
{if(k->project_num==num)
{flag=1;
if(k==t)
{t=t->next;}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
struct school *deletes3(struct school *t)
{struct school *k,*k_head;int flag=0;char name[20];
cout<<"请输入要删除的信息\n";
cout<<"请输入姓名\n";cin>>name;
k=t;
while(k!=NULL)
{if(!strcmp(k->name,name))
{flag=1;
if(k==t)
{t=t->next;}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
struct statitcs
{
int flag;
int total;
int no;
struct statitcs *next;
};
int total1(struct school *t,int n)
{struct school *p;
p=t;
int sum=0;
while(p!=NULL)
{if(p->num==n)
sum+=p->score;
p=p->next;
}
return sum;
}
void swap(struct statitcs *t1,struct statitcs *t2)
{struct statitcs *t;
t=(struct statitcs *)malloc(len1);
t->flag=t1->flag;t1->flag=t2->flag;t2->flag=t->flag;
t->total=t1->total;t1->total=t2->total;t2->total=t->total;
}
struct statitcs *set_place(struct statitcs *t)
{int i=1;
struct statitcs *p,*q;
p=t;
if(p==NULL)return t;
else
{p->no=1;
while(p->next!=NULL)
{q=p->next;
a:if((q->total==p->total))
{q->no=p->no;q=q->next;
if(q==NULL) return t;
goto a;}
if(q==NULL) return t;
else{p=q;i++;p->no=i;}
}
}
return t;
}
struct statitcs * sort( struct statitcs *t)
{struct statitcs *p1,*p2;
p1=t;
while(p1!=NULL)
{ p2=p1->next;
while(p2!=NULL)
{if((p1->total)<(p2->total))
swap(p1,p2);
p2=p2->next;
}
p1=p1->next;
}
return t;
}
void show(struct statitcs *t,struct school *t1)
{struct statitcs *p;
p=t;
while(p!=NULL)
{
cout<<"姓名\t"<<endl;
display(t1,p->flag);
p=p->next;
}
}
struct statitcs *creat(struct school *t)
{int a[100];int i=0;int flag1;
struct statitcs *head,*rear,*p;
struct school *k;k=t;
head=NULL;if(k==NULL)return NULL;
p=(struct statitcs *)malloc(len1);
p->flag=k->num;
p->total=total1(t,p->flag);
if(head==NULL)
head=p;rear=p;
k=k->next;
a=p->flag;
while(k!=NULL)
{flag1=1;
for(int m=0;m<=i;m++)
if(a[m]==k->num)
flag1=0;
if(flag1)
{p=(struct statitcs *)malloc(len1);
p->flag=k->num; i++;a=p->flag;
p->total=total1(t,p->flag);
rear->next=p;rear=p;}
k=k->next;
}
rear->next=NULL;
return head;
}
void swap1(struct school *t1, struct school *t2)
{struct school *t;
t=(struct school *)malloc(len);
strcmp(t->name,t1->name);strcmp(t1->name,t2->name);strcmp(t2->name,t->name);
}
struct school * sort1( struct school *t,int n)
{struct school *p1,*p2;
p1=t;
while(p1!=NULL)
{ p2=p1->next;
while(p2!=NULL&&)
{if((p1->score)<(p2->score))
swap1(p1,p2);
p2=p2->next;
}
p1=p1->next;
}
return t;
}
/*struct school *setplace1(struct school *t,int n)
{int i=1;
struct school *p,*q;
p=t;
if(p==NULL)return t;
else
{if(p->project_num==n)
p->no=1;
while(p->next!=NULL)
{q=p->next;
a:if((q->score==p->score)&&p->project_num==n&&q->project_num==n)
{q->no=p->no;q=q->next;
if(q==NULL) return t;
goto a;}
if(q==NULL) return t;
else{p=q;
if((q->score==p->score)&&p->project_num==n&&q->project_num==n)
{i++;p->no=i;}}
}
}
return t;
}
*/
void show1(struct school *t,int n)
{struct school *p;
p=t;
cout<<"<<"姓名\t"<<endl;
}
void sort_display(struct school *t)
{int a[100];int i=0;int flag1;
struct school *k;k=t;
if(k==NULL)cout<< "NULL\n";
a=k->project_num;
k=k->next;
for(int m=0;m<=i;m++)
{t=sort1(t,a[m]);
//setplace1(t,a[m]);
show1(t,a[m]);
}
}
void main()
{struct school *t;struct statitcs *s;char a;int flag=1,flag1=1,flag2=1;
t=NULL;
cout<<"\t\t***************************************************\n";
cout<<"\t\t***************************************************\n";
cin>>a;
while(a!='6')
{switch(a)
{case'1':flag=0;flag1=0;flag2=0;
t=insert();
cout<<"你输入的所有信息\n";
print(t);break;
case'2':cout<<"*****************\n1:添加一条信息\n2:退出\n*****************\n";
cin>>a;
while(a!='2')
{switch(a)
{case'1':t=add(t);break;
default:cout<<"\n请正确输入\n";break;
}
cout<<"*****************\n1:添加一条信息\n2:退出\n*****************\n";
cin>>a;
}
print(t);
break;
case'3':if(flag==1)
{cout<<"\t\t请先输入数据\n";break;}
cout<<"\t\t***************************************************\n";
cout<<"\t\t***************************************************\n";
cin>>a;
while(a!='5')
{switch(a)
{case'1':t=deletes(t); cout<<"删除后的信息\n";print(t);break;
case'2':t=deletes1(t);cout<<"删除后的信息\n"; print(t);break;
case'3':t=deletes2(t); cout<<"删除后的信息\n"; print(t);break;
case'4':t=deletes3(t); cout<<"删除后的信息\n";print(t);break;
default:cout<<"请正确输入\n";break;
}
cout<<"\t\t***************************************************\n";
cout<<"\n\t\t\t3:删除某一项目的信息\n\t\t\t4:删除某一姓名的信息\n\t\t\t5:退出\n";
cout<<"\t\t***************************************************\n";
cin>>a;
}
break;
case'4':if(flag2==1)
{cout<<"\t\t请先输入数据\n";break;}
cout<<"\t\t***************************************************\n";
cout<<"\n\t\t\t4:查找某一姓名的信息\n\t\t\t5:退出\n";
cout<<"\t\t***************************************************\n";
cin>>a;
while(a!='5')
{switch(a)
{case'1':find(t);break;
case'2':find1(t);break;
case'3':find2(t);break;
case'4':find3(t);break;
default:cout<<"请正确输入\n";break;
}
cout<<"\t\t***************************************************\n";
cout<<"\t\t\t1:查找所有信息\n\t\t\t4:查找某一姓名的信息\n\t\t\t5:退出\n";
cout<<"\t\t***************************************************\n";
cin>>a;
}
break;
case'5':if(flag1==1)
{cout<<"\t\t请先输入数据\n";break;}
cout<<"\t\t*************************************\n";
cout<<"\t\t*************************************\n";
cin>>a;
while(a!='3')
{switch(a)
{case'1':s=creat(t);s=sort(s);s=set_place(s);show(s,t);break;
case'2':sort_display(t);break;
default:cout<<"请正确输入\n";break;
}
}
break;
default:cout<<"请正确输入\n";
}
cout<<"\t\t***************************************************\n";
cout<<"\t\t***************************************************\n";
cin>>a;
}
}
[此贴子已经被作者于2006-1-10 14:20:57编辑过]
我帮你把所有的错误改正了.但这个程序还有很多地方需要优化和整理,
这你就自己完成吧!
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#define len sizeof(struct school)
#define len1 sizeof(struct statitcs)
struct school
{
char name[20];
int num;
int project_num;
float score;
struct school *next;
};
struct school *insert(void)
{
cout<<"********************************\n";
cout<<"特别提示:\n";
struct school *head,*rear,*p;
char key;
head=NULL;
rear=NULL;
p=(struct school *)malloc(len);
cout<<"请输入姓名\n";
cin>>p->name;
if(p->num==0)
return NULL;
while(p->num!=0)
{
if(head==NULL)
{
head=p;rear=p;}
else
{
rear->next=p;
rear=p;
}
p=(struct school *)malloc(len);
cout<<"还有输入吗?(Y/N)\n";
cin>>key;
if(key=='N'||key=='n')
break;
cout<<"请输入姓名\n";
cin>>p->name;
}
rear->next=NULL;
return head;
}
struct school *add(struct school *t)
{
struct school *s,*p;
p=(struct school *)malloc(len);
cout<<"请输入姓名\n";
cin>>p->name;
s=t;
if(s==NULL)
{
p->next=s;
s=p;
return s;
}
else
{
while(s->next!=NULL)
{
s=s->next;
}
s->next=p;
p->next=NULL;
return t;
}
}
void print(struct school *t)
{
struct school *p;
p=t;
cout<<"\t姓名\n";
while(p!=NULL)
{
cout<<p->name<<endl;
p=p->next;
}
}
void display(struct school *t,int n)
{
struct school *p;
p=t;
while(p!=NULL)
{
if(p->num==n)
cout<<p->name<<endl;
p=p->next;
}
}
struct school *deletes(struct school *t)
{
struct school *p,*k,*k_head;
int flag=0;
p=(struct school *)malloc(len);
cout<<"请输入要删除的信息\n";
cout<<"请输入姓名\n";
cin>>p->name;
k=t;
while(k!=NULL)
{
if((p->num==k->num)&&(k->project_num==p->project_num)&&!strcmp(p->name,k->name)&&(p->score==k->score))
{
flag=1;
if(k==t)
{
t=t->next;
}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
void find(struct school *t)
{
struct school *p;
p=t;
cout<<"\t姓名\n";
while(p!=NULL)
{
cout<<p->num<<"\t\t"<<p->project_num<<"\t"<<p->name<<"\t"<<p->score<<endl;
p=p->next;
}
}
void find1(struct school *t)
{
struct school *k;
int flag=0;
int num;
cout<<"姓名\n";
cout<<"请输入项目号\n";
cin>>num;
k=t;
while(k!=NULL)
{
if(num==k->num)
{
flag=1;
cout<<k->name<<endl;
}
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要的信息\n";
}
void find2(struct school *t)
{
struct school *k;int flag=0;int num;
cout<<"请输入要查找的信息\n";
cout<<"请输入项目号\n";
cin>>num;
cout<<"\t姓名\n";
k=t;
while(k!=NULL)
{
if(num==k->project_num)
{
flag=1;cout<<k->name<<endl;
}
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要的信息\n";
}
void find3(struct school *t)
{
struct school *k;int flag=0;char name[20];
cout<<"请输入要查找的信息\n";
cout<<"请输入姓名\n";cin>>name;
cout<<"姓名\n";
k=t;
while(k!=NULL)
{
if(!strcmp(name,k->name))
{
flag=1;cout<<k->name<<endl;
}
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要的信息\n";
}
struct school *deletes1(struct school *t)
{
struct school *k,*k_head;
int flag=0;
int num;
cout<<"请输入要删除的信息\n";
cout<<"请输入项目号\n";
cin>>num;
k=t;
while(k!=NULL)
{
if(k->num==num)
{
flag=1;
if(k==t)
{
t=t->next;
}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
struct school *deletes2(struct school *t)
{
struct school *k,*k_head;
int flag=0;
int num;
cout<<"请输入要删除的信息\n";
cin>>num;
k=t;
while(k!=NULL)
{
if(k->project_num==num)
{
flag=1;
if(k==t)
{
t=t->next;
}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
struct school *deletes3(struct school *t)
{
struct school *k,*k_head;int flag=0;char name[20];
cout<<"请输入要删除的信息\n";
cout<<"请输入姓名\n";cin>>name;
k=t;
while(k!=NULL)
{
if(!strcmp(k->name,name))
{
flag=1;
if(k==t)
{
t=t->next;
}
else
k_head->next=k->next;
}
else
k_head=k;
k=k->next;
}
if(k==NULL&&flag==0)
cout<<"无法找到你要删除的信息\n";
return t;
}
struct statitcs
{
int flag;
int total;
int no;
struct statitcs *next;
};
int total1(struct school *t,int n)
{
struct school *p;
p=t;
int sum=0;
while(p!=NULL)
{
if(p->num==n)
sum+=p->score;
p=p->next;
}
return sum;
}
void swap(struct statitcs *t1,struct statitcs *t2)
{
struct statitcs *t;
t=(struct statitcs *)malloc(len1);
t->flag=t1->flag;
t1->flag=t2->flag;
t2->flag=t->flag;
t->total=t1->total;
t1->total=t2->total;
t2->total=t->total;
}
struct statitcs *set_place(struct statitcs *t)
{
int i=1;
struct statitcs *p,*q;
p=t;
if(p==NULL)
return t;
else
{
p->no=1;
while(p->next!=NULL)
{
q=p->next;
a:if((q->total==p->total))
{
q->no=p->no;
q=q->next;
if(q==NULL)
return t;
goto a;
}
if(q==NULL)
return t;
else
{
p=q;i++;
p->no=i;
}
}
}
return t;
}
struct statitcs * sort( struct statitcs *t)
{
struct statitcs *p1,*p2;
p1=t;
while(p1!=NULL)
{
p2=p1->next;
while(p2!=NULL)
{
if((p1->total)<(p2->total))
swap(p1,p2);
p2=p2->next;
}
p1=p1->next;
}
return t;
}
void show(struct statitcs *t,struct school *t1)
{
struct statitcs *p;
p=t;
while(p!=NULL)
{
cout<<"姓名\t"<<endl;
display(t1,p->flag);
p=p->next;
}
}
struct statitcs *creat(struct school *t)
{
int a;
int i=0;
int flag1;
struct statitcs *head,*rear,*p;
struct school *k;
k=t;
head=NULL;
if(k==NULL)
return NULL;
p=(struct statitcs *)malloc(len1);
p->flag=k->num;
p->total=total1(t,p->flag);
if(head==NULL)
head=p;rear=p;
k=k->next;
a=p->flag;
while(k!=NULL)
{
flag1=1;
//for(int m=0;m<=i;m++)
if(a==k->num)
flag1=0;
if(flag1)
{
p=(struct statitcs *)malloc(len1);
p->flag=k->num;
i++;
a=p->flag;
p->total=total1(t,p->flag);
rear->next=p;rear=p;}
k=k->next;
}
rear->next=NULL;
return head;
}
void swap1(struct school *t1, struct school *t2)
{
struct school *t;
t=(struct school *)malloc(len);
strcmp(t->name,t1->name);
strcmp(t1->name,t2->name);
strcmp(t2->name,t->name);
}
struct school * sort1( struct school *t,int n)
{
struct school *p1,*p2;
p1=t;
while(p1!=NULL)
{
p2=p1->next;
while(p2!=NULL)
{
if((p1->score)<(p2->score))
swap1(p1,p2);
p2=p2->next;
}
p1=p1->next;
}
return t;
}
void show1(struct school *t,int n)
{
struct school *p;
p=t;
cout<<"姓名\t"<<endl;
}
void sort_display(struct school *t)
{
int a;
int i=0;
//int flag1;
struct school *k;k=t;
if(k==NULL)
cout<< "NULL\n";
a=k->project_num;
k=k->next;
//for(int m=0;m<=i;m++)
//{
t=sort1(t,a);
//setplace1(t,a[m]);
show1(t,a);
//}
}
void main()
{
struct school *t;
struct statitcs *s;
char a;
int flag=1,flag1=1,flag2=1;
t=NULL;
cout<<"\t\t***************************************************\n";
cout<<"\t\t***************************************************\n";
cin>>a;
while(a!='6')
{
switch(a)
{
case'1':
flag=0;
flag1=0;
flag2=0;
t=insert();
cout<<"你输入的所有信息\n";
print(t);
break;
case'2':
cout<<"*****************\n1:添加一条信息\n2:退出\n*****************\n";
cin>>a;
while(a!='2')
{
switch(a)
{
case'1':
t=add(t);
break;
default:
cout<<"\n请正确输入\n";
break;
}
cout<<"*****************\n1:添加一条信息\n2:退出\n*****************\n";
cin>>a;
}
print(t);
break;
case'3':
if(flag==1)
{
cout<<"\t\t请先输入数据\n";
break;
}
cout<<"\t\t***************************************************\n";
cout<<"\t\t***************************************************\n";
cin>>a;
while(a!='5')
{
switch(a)
{
case'1':t=deletes(t);
cout<<"删除后的信息\n";
print(t);
break;
case'2':
t=deletes1(t);
cout<<"删除后的信息\n";
print(t);
break;
case'3':
t=deletes2(t);
cout<<"删除后的信息\n";
print(t);
break;
case'4':
t=deletes3(t);
cout<<"删除后的信息\n";
print(t);
break;
default:
cout<<"请正确输入\n";
break;
}
cout<<"\t\t***************************************************\n";
cout<<"\n\t\t\t3:删除某一项目的信息\n\t\t\t4:删除某一姓名的信息\n\t\t\t5:退出\n";
cout<<"\t\t***************************************************\n";
cin>>a;
}
break;
case'4':
if(flag2==1)
{
cout<<"\t\t请先输入数据\n";
break;
}
cout<<"\t\t***************************************************\n";
cout<<"\n\t\t\t4:查找某一姓名的信息\n\t\t\t5:退出\n";
cout<<"\t\t***************************************************\n";
cin>>a;
while(a!='5')
{
switch(a)
{
case'1':
find(t);
break;
case'2':
find1(t);
break;
case'3':
find2(t);
break;
case'4':
find3(t);
break;
default:
cout<<"请正确输入\n";
break;
}
cout<<"\t\t***************************************************\n";
cout<<"\t\t\t1:查找所有信息\n\t\t\t4:查找某一姓名的信息\n\t\t\t5:退出\n";
cout<<"\t\t***************************************************\n";
cin>>a;
}
break;
case'5':
if(flag1==1)
{
cout<<"\t\t请先输入数据\n";
break;
}
cout<<"\t\t*************************************\n";
cout<<"\t\t*************************************\n";
cin>>a;
while(a!='3')
{
switch(a)
{
case'1':
s=creat(t);
s=sort(s);
s=set_place(s);
show(s,t);
break;
case'2':
sort_display(t);
break;
default:
cout<<"请正确输入\n";
break;
}
}
break;
default:cout<<"请正确输入\n";
}
cout<<"\t\t***************************************************\n";
cout<<"\t\t***************************************************\n";
cin>>a;
}
}