这周的c++学习
首先是c++课设已经写了一部分功能,准备后面慢慢写,先应付期末考试。
(已经完善过的代码)
代码实现了增删改查文件读取保存等功能并且有部分防止非法数据功能
可以简单地实现C++作业的检测
#include<iostream>
#include<fstream>
#include<string.h>
#include<windows.h>
using namespace std;
typedef class birthday//年龄的类
{
public:
int year;
int month;
int day;
}bir;
typedef class StuNode//学生信息类
{
public:
int num;
string name;
string sex;
int english;
string major;
bir birthday1;
string address;
StuNode *next;
}STU;
class student:private StuNode //学生类继承人类
{
public:
student()//初始化头结点并给其申请空间
{
head=new STU;head->next=NULL;
}
void create();//建立链表
void find();//查找函数
void add();//增加函数
void modify();//修改学生信息
void bianli();//遍历函数
void delet();//删除函数
void paixu();//排序函数
void census();//按性别统计人数
void read();//读取文件信息函数
void save();//信息保存到文件函数
int fangzhichongfu(int n);//防止学号重复
void show();//菜单
~student(){};
private:
STU *head;
STU *tail;
};
int main()
{
student gn;
while(1)
{
gn.show();
char select[128];
cout<<"请输入调用功能"<<endl;
cin>>select;
int a=strlen(select);
if(a==1)
{
switch(select[0])
{
case '1':system("cls");gn.create();break;
case '2':system("cls");gn.delet();break;
case '3':system("cls");gn.read(); break;
case '4':system("cls");gn.find();break;
case '5':system("cls");gn.census();break;
case '6':system("cls");gn.paixu();break;
case '7':system("cls");gn.save();break;
case '8':system("cls");gn.add();break;
case '9':return 0;
}
}
else if(a>=2&&a<=3)
{
switch(select[0]+select[1])
{
case 'a':system("cls");gn.modify();break;
case 'b':system("cls");gn.bianli();break;
default:system("cls");printf("输入指令错误\n");system("pause");break;
}
}
else if(a>3)
{
printf("指令错误\n");
system("pause");
}
}
}
void student::show()//菜单显示函数
{
cout<<"*************************************************"<<endl;
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<<" 请选择:"<<endl;
}
int student::fangzhichongfu(int n)//防止学号重复函数
{
STU *ss;
int a=n;
ss=head;
while(ss)
{
if(ss->num==a)
{
return 0;
}
ss=ss->next;
}
return 1;
}
void student::create()//链表的建立函数
{
tail=head;
STU *p,*bl;
bl=head;
int n;
int h;//此处不会用c++清除缓存防止输入人数时乱输用c代替
tt:printf("请输入学生个数\n");
h=scanf("%d",&n);
if(h!=1)
{fflush(stdin);
goto tt;
}
for(n;n>0;n--)
{
p=new STU;//给p申请个结构体大小的空间
int m;
while(1)
{
cout<<"请输入学号"<<endl;
cin>>p->num;
m=fangzhichongfu(p->num);
if(m==0)
{cout<<"输入学号重复请重输入"<<endl;
continue;
}
else
break;
}
cout<<"请输入名字"<<endl;
cin>>p->name;
cout<<"请输入性别性别只存在 男 m 女 w"<<endl;
string j="m";//这里可以限制性别只为男女
string q="w";
while(1)
{
cin>>p->sex;
if(!strcmp(p->sex.c_str(),j.c_str()))
break;
else if(!strcmp(p->sex.c_str(),q.c_str()))
break;
else
{printf("你输入了新物种请重新输入\n");
continue;}
}
cout<<"请输入英语成绩"<<endl;
cin>>p->english;
cout<<"所选专业:"<<endl;
cin>>p->major;
cout<<"生日:"<<endl;
cin>>p->birthday1.year>>p->birthday1.month>>p->birthday1.day;
cout<<"请输入家庭住址"<<endl;
cin>>p->address;
p->next=NULL;//
tail->next=p;//让tail的指针域指向p
tail=p;//让p变成tail可以连接下一次循环
}
cout<<"创建链表成功" <<endl;
system("pause");
}
void student::bianli()//遍历函数
{
STU *bl;
bl=head->next;
while(bl)
{
cout<<"学号:"<<bl->num<<"\t";
cout<<"姓名:"<<bl->name<<"\t";
cout<<"性别:"<<bl->sex<<"\t";
cout<<"英语成绩:"<<bl->english<<"\t";
cout<<"所选专业:"<<bl->major<<"\t";
cout<<"生日:"<<bl->birthday1.year<<" "<<bl->birthday1.month<<" "<<bl->birthday1.day<<endl;
cout<<"家庭住址:"<<bl->address<<endl;
bl=bl->next;
}system("pause");
}
void student::find()//查找函数
{
STU *f;
f=head->next;//定义一个f指针指向head后面的节点
STU *q;
q=head->next;
if(q==NULL)
{
cout<<"暂无学生信息请指令功能一后调用此功能\n"<<endl;
system("pause");
return;
}
string fname;
cout<<"请输入所要查询的学生姓名"<<endl;
cin>>fname;
while(f)
{
if(!strcmp(fname.c_str(),f->name.c_str()))//比较所找名字与链表中的名字
{
cout<<"学号:"<<f->num<<"\t";
cout<<"姓名:"<<f->name<<"\t";
cout<<"性别:"<<f->sex<<"\t";
cout<<"英语成绩:"<<f->english<<"\t";
cout<<"所选专业:"<<f->major<<"\t";
cout<<"生日:"<<f->birthday1.year<<" "<<f->birthday1.month<<" "<<f->birthday1.day<<endl;
cout<<"家庭住址:"<<f->address<<endl;
}
f=f->next; //使其遍历链表
} system("pause");
}
void student::modify()//修改学生信息函数
{
STU *mod;
mod=head->next;
if(mod==NULL)
{
cout<<"暂无学生信息请指令功能一后调用此功能\n"<<endl;
system("pause");
return;
}
int numm;
cout<<"请输入所要修改的学生的学号"<<endl;
cin>>numm;
while(mod)
{
if(numm==mod->num)
{
cout<<"请重新输入学号"<<endl;
cin>>mod->num;
cout<<"请重新输入名字"<<endl;
cin>>mod->name;
cout<<"请重新输入性别性别只存在 男 m 女 w"<<endl;
string j="m";
string q="w";
while(1)
{
cin>>mod->sex;
if(!strcmp(mod->sex.c_str(),j.c_str()))
break;
else if(!strcmp(mod->sex.c_str(),q.c_str()))
break;
else
{printf("你输入了新物种请重新输入\n");
continue;}
}
cout<<"重新请输入英语成绩"<<endl;
cin>>mod->english;
cout<<"重新输入所选专业:"<<endl;
cin>>mod->major;
cout<<"重新输入生日:"<<endl;
cin>>mod->birthday1.year>>mod->birthday1.month>>mod->birthday1.day;
cout<<"请重新输入家庭住址"<<endl;
cin>>mod->address;
break;
}
else
mod=mod->next;
}
cout<<"修改成功" <<endl;
system("pause");
}
void student::add()//增加学生信息函数
{
if(head->next==NULL)
{
cout<<"请先导入学生信息或者创建一个学生链表"<<endl;
}
else
{
STU* f=head;
STU* p=new STU;//增加学生信息需要一个有空间的指针接收学生信息并插入到链表当中
cout<<"请输入你想添加的学生信息"<<endl;
int m;
while(1)
{
cout<<"请输入学号"<<endl;
cin>>p->num;
m=fangzhichongfu(p->num);
if(m==0)
{cout<<"输入学号重复请重输入"<<endl;
continue;
}
else
break;
}
cout<<"请输入名字"<<endl;
cin>>p->name;
cout<<"请输入性别性别只存在 男 m 女 w"<<endl;
string j="m";
string q="w";
while(1)
{
cin>>p->sex;
if(!strcmp(p->sex.c_str(),j.c_str()))
break;
else if(!strcmp(p->sex.c_str(),q.c_str()))
break;
else
{printf("你输入了新物种请重新输入\n");
continue;}
}
cout<<"请输入英语成绩"<<endl;
cin>>p->english;
cout<<"所选专业:"<<endl;
cin>>p->major;
cout<<"生日:"<<endl;
cin>>p->birthday1.year>>p->birthday1.month>>p->birthday1.day;
cout<<"请输入家庭住址"<<endl;
cin>>p->address;
while(f->next!=NULL)//这个地可以让f指针指向尾节点增加学生信息时直接插入到尾节点
{
f=f->next;
tail=f;
}
tail->next=p;//让tail的指针域指向p;
tail=p;//使p便成尾节点;
p->next=NULL;
cout<<"添加成功"<<endl;
}
}
void student::delet()//删除学生信息函数
{
if(head->next==NULL)
{
cout<<"空链表无学生信息返回主菜单"<<endl;
system("pause");
return;
}
STU *de1,*de2;
de1=head;
de2=head->next;
int n;
cout<<"请输入所要删除学生的学号"<<endl;
cin>>n;
while(de2)
{
if(n==de2->num)
{
int quxiao;
cout<<"是否确定删除该学生信息按1确定按0取消"<<endl;
cin>>quxiao;
if(quxiao==0)
break;
de1->next=de2->next;//如果找到学生信息使该节点前一个节点指向该节点后一个节点;
delete de2;//释放该节点
de2=NULL;
break;
}
else
{
de1=de2;
de2=de2->next;
}
}
cout<<"删除成功"<<endl;
system("pause");
}
void student::paixu()//排序函数
{
STU *p,*q,*r,*w;
p=head;
q=head->next;
if(p->next==NULL){
printf("无学生信息");
system("pause");
return ;}
while(p->next!=NULL)
{
while(q->next!=NULL)
{
if(p->next->english<q->next->english)//排序根据冒泡排序的方式进行;
{
r=p->next;//使定义的一个容器指针接收p的指针域数据
p->next=q->next;//让p的指针域接收q的指针域;
q->next=q->next->next;//让q的指针域接收q后面的节点的指针域;
p->next->next=r;
}
else
{
q=q->next;
}
}
p=p->next;//移动排序
q=p->next;
}
w=head->next;
cout<<"按英语成绩排名如下"<<endl;
while(w)
{
cout<<"学号:"<<w->num<<"\t";
cout<<"姓名:"<<w->name<<"\t";
cout<<"性别:"<<w->sex<<"\t";
cout<<"英语成绩:"<<w->english<<"\t";
cout<<"所选专业:"<<w->major<<"\t";
cout<<"生日:"<<w->birthday1.year<<" "<<w->birthday1.month<<" "<<w->birthday1.day<<endl;
cout<<"家庭住址:"<<w->address<<endl;
w=w->next;
}
system("pause");
}
void student::census()//统计学生人数函数
{
cout<<"********************************"<<endl;
cout<<" 1.输出当前所有学生数量 "<<endl;
cout<<" 2.输出你所要查询性别的人数 "<<endl;
cout<<"********************************"<<endl;
char cen[10];
cout<<"请输入选择的功能"<<endl;
cin>>cen;
if(cen[0]=='1')
{
int tt=0;
STU *t;
t=head->next;
while(t)
{
tt++;
t=t->next;
}
cout<<tt<<endl;
system("pause");
}
else if(cen[0]=='2')
{
STU *tj;
tj=head->next;
if(tj==NULL)
{
cout<<"暂无学生信息请指令功能一后调用此功能\n"<<endl;
system("pause");
return;
}
int sexshu=0;
string fname;
cout<<"请输入所要查询的学生性别"<<endl;
cin>>fname;
while(tj)
{
if(!strcmp(fname.c_str(),tj->sex.c_str()))
{
sexshu++;
}
tj=tj->next;
}
cout<<"统计该性别人数为"<<sexshu<<endl;
system("pause");
}
else
cout<<"输入指令有误"<<endl;
}
void student::save()//学生信息的保存函数
{
ofstream of1;//定义一个写的文件指针
of1.open("D:\\caozuo.txt",ios::out);//打开一个文件
if(!of1.is_open())
{
cout<<"open caozuo.txt error!"<<endl;
}
else
{
STU* s=head->next;
while(s)
{
of1<<s->num<<" "<<s->name<<" "<<s->sex<<" "<<s->english<<" "<<s->major<<" "<<s->birthday1.year<<" "<<
s->birthday1.month<<" "<<s->birthday1.day<<" "<<s->address<<endl;
s=s->next;
}
of1.close();//关闭文件
}
ofstream of2;
of1.open("D:\\guankan.txt",ios::out);
if(!of1.is_open())
{
cout<<"open guankan.txt error!"<<endl;
}
else
{
STU* b=head->next;
while(b)
{
of1<<"学号:"<<b->num<<" "<<"姓名:"<<b->name<<" "<<"性别"<<b->sex<<" "<<"英语成绩:"<<b->english<<" "<<"专业:"<<b->major<<" "<<"生日:"<<b->birthday1.year<<" "<<
b->birthday1.month<<" "<<b->birthday1.day<<" "<<"家庭住址"<<b->address<<endl;
b=b->next;
}
of1.close();
}
cout<<"保存成功!"<<endl;
system("pause");
}
void student::read()//学生信息的导入函数
{
ifstream ifs;//定义一个读文件的指针
ifs.open("D:\\caozuo.txt",ios::in); //以读的方式打开文件
if(!ifs.is_open())
{
cout<<"open caozuo.txt error!"<<endl;
}
STU* p;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
while(!ifs.eof())
{
STU* s=new STU;
ifs>>s->num>>s->name>>s->sex>>s->english>>s->major>>s->birthday1.year>>s->birthday1.month>>s->birthday1.day>>s->address;
if(ifs.get()==-1)
{
break;
}
p->next=s;
p=s;
p->next=NULL;
}
ifs.close();
cout<<"读取成功!"<<endl;
}
在学习c++的过程中发现许多小细节,Const 与static的区别与总结
自己也写了关于const的代码另外其中有strcpy函数,需要注意的是其使用会存在覆盖之前数据的现象,
另外strcat函数的使用时在字符串后追加一串字符。
`#include<iostream>
#include<string.h>
using namespace std;
int main()
{
//int a[]={2,4,6,8,10,12},*p=a;
//
//cout<<p+1<<" "<<*(p+1);
const char *s1="AB",*s2="BCDEFG";
char s3[20]="DF";
strcpy(s3,s1);
cout<<strlen(strcat(s3,s2));
return 0;
} ``
```c
另外就是一些零碎的知识了掌握不牢固的东西记录一下
在这里data就是sk类型的结构体 *p就是定义的一个sk类型的指针;
用它指向data是符合的。
```#include<iostream>
#include<string>
using namespace std;
struct sk
{
int a;
float b;
}data,*p;
int main()
{
p=&data;
(*p).a;
}
```c