数据处理类小程序
/*我的*/
#include
#include
#include
#include
#include
int cs; //定义全局变量cs
class student
{
private: //定义私有成员
int num;
char name[10];
char addr[10];
char age[10];
public:
void Insert();
void Show();
void Find();
void Del();
void Sort();
void Save();
void Read();
void Saveas();
fstream iof;
};
student add[100];
void student::Insert() //用于输入的Insert对象
{
char ready=’y’;
cout<
cin>>ready;
if(ready==’Y’ || ready==’y’)
{Read();}
cout<
for(int i=0;i<100;i++)
{
cout<
cin>>add[i].num;
cout<
cin>>add[i].name;
cout<
cin>>add[i].addr;
cout<
cin>>add[i].age;
cs+=1;
cout<
cout<
cin>>ready;
if(ready==’N’ || ready==’n’)
{
cout<
Show();
break;
}
}
}
void student::Show() //用于显示的Show对象
{
for(int j=0;j
{
cout<
}
}
void student::Find() //用于查询的Find对象
{
cout<
int y;
cin>>y;
for(int a=0;a
{
bool bo=true;
if(add[a].num==y)
{
cout<
}
}
}
void student::Del() //用于删除的Del对象
{
cout<
int y2;
cin>>y2;
for(int c=0;c
{
if(add[c].num==y2)
{
add[c].num=add[c+1].num;
strcpy(add[c].name,add[c+1].name);
strcpy(add[c].addr,add[c+1].addr);
strcpy(add[c].age,add[c+1].age);
}
}
cout<
for(int w=0;w
{
cout<
}
}
//const int i2 =3;
//student zkk;
void student::Sort() //用于排序的Sort对象
{
cout<
int t; //用于交换的中间变量t
char ch1[10]; //用于交换的中间变量ch1
char ch2[10]; //用于交换的中间变量ch2
char ch3[10]; //用于交换的中间变量ch3
for(int d=0;d
{
for(int e=d+1;e
{
if(add[d].num>add[e].num)
{
t=add[d].num;
add[d].num=add[e].num;
add[e].num=t;
strcpy(ch1,add[d].name);
strcpy(add[d].name,add[e].name);
strcpy(add[e].name,ch1);
strcpy(ch2,add[d].addr);
strcpy(add[d].addr,add[e].addr);
strcpy(add[e].addr,ch2);
strcpy(ch3,add[d].age);
strcpy(add[d].age,add[e].age);
strcpy(add[e].age,ch3);
}
}
}
for(int i1=0;i1
{
cout<
}
}
void student::Save() //用于保存的Save对象
{
char re=’y’;
cout<
cout<
cin>>re;
if(re==’Y’ || re==’y’)
{
iof.open(“abc.txt”,ios::out | ios::app);
if(!iof)
{
cerr<
exit(0);
}
iof.write((char *)add,sizeof(student));
iof.close();
cout<
char re2=’y’;
cout<
cin>>re2;
if(re2==’y’ || re2==’Y’)
{
Saveas();
}
}
else
{
cout<
}
}
void student::Read()
{
/*FILE *fp;
if((fp=fopen(“abc.txt”,”rb”))==NULL)
{
printf(“文件打开失败!\n”);
exit(0);
}
for(int rr=0;rr
{
fread(&add[rr],sizeof(class student),1,fp);
printf(“学号:%-10s姓名:%4d籍贯:%4d出生年月:%-15s\n”,add[rr].num,add[rr].name,add[rr].addr,add[rr].age);
}
fclose(fp);*/
ifstream inf;
//char infname[20];
cout<
//cin.getline(infname,sizeof(infname));
inf.open(“abc.txt”,ios::in | ios::binary | ios::nocreate | ios::app);
if(!inf)
{
cerr<
exit(0);
}
for(int r = 0;r
{
inf.read((char *)&add,sizeof(class student));
cout<
}
}
void student::Saveas()
{
fstream iof;
char oname[20];
char ch;
iof.open(“abc.txt”,ios::in);
cout<
cin.getline(oname,sizeof(oname));
iof.open(oname,ios::out | ios::noreplace);
if(!iof)
{
cerr<
exit(0);
}
while(iof.get(ch)) iof.put(ch);
iof.close();
}
//程序主入口点
void main()
{
for(;;)
{
cout<
cout<
cout<
cout<
cout<
cout<
student h;
int choose;
cout<
cin>>choose;
switch(choose)
{
case 1:
h.Insert();
break;
case 2:
h.Find();
break;
case 3:
h.Del();
break;
case 4:
h.Sort();
break;
case 5:
h.Save();
break;
case 6:
cout<
exit(0);
break;
default: cout<
}
}
}
/*同学的*/
#include
#include
#include
#include
#include
#define SIZE 10
//——————————————————————————-
//函数种类
/* void dakai(int n,int j,char ch);
void save(int n,char ch);
void display(int n,char ch);//显示信息
void xiugai();
void dakai(int n,int j,char ch);//查找文件
void shuru(int n,char ch);//输入信息
void paixu(int n,char ch);
void xuanzhe(int n,char ch);//选择运行程序*/
//——————————————————————————-
struct student_type//定义结构体
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
//—————————————————————————————–
void save(int n,char ch)//保存输入信息
{
FILE *fp;
int i;
if((fp=fopen(“f:\\stu_list.txt”,”wb+”))==NULL)
{
printf(“cannot open file\n”);
exit(0);
}
for(i=0;i
if(fwrite(&stud[i],sizeof(student_type),1,fp)!=1)
printf(“file write error\n”);
fclose(fp);
}
//——————————————————————————————
void display(int n,char ch)//显示信息
{
FILE *fp;
int i;
if((fp=fopen(“f:\\stu_list.txt”,”rb”))==NULL)
{
printf(“cannot open file\n”);
exit(0);
}
for(i=0;i
{
fread(&stud[i],sizeof(struct student_type),1,fp);
printf(“姓名:%-10s学号:%4d年龄:%4d地址:%-15s\n”,stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
fclose(fp);
}
//——————————————————————————————–
void xiugai(int n,char ch)//修改学生信息
{
int z;
FILE *pf;
pf=fopen(“f:\\stu_list.txt”,”rb+”);
if(pf==NULL)
{
printf(“\n pf open Error,\n exit program.”);
return;
}
else
{
//输入学生信息
cout<
cin>>z;
fseek(pf,z*sizeof(struct student_type),2);
scanf(“%s%d%d%s”,stud[z].name,&stud[z].num,&stud[z].age,stud[z].addr);
fseek(pf,sizeof(struct student_type),0);
fwrite(&stud,sizeof(struct student_type),1,pf);
}
fclose(pf);
//save(n,ch);
//display(n,ch);
}
//———————————————————————————————–
/* struct stud
{
int num;
struct studt;
} *head;
void printnode(stud
{
while(p->next!=NULL)
{
//输出p指向的数据
cout<num<
p=p->next;//指向下一个
}
cout<num<
}
//———————————————————————————————
void dakai2()
{
node *p=new node;
head=p;//设置头指针
p->num=90;
for(int k=1;k<10;k++)
{
//产生下一个结点1
p->next=p+k;
p=p->next;
p->num=k*100;
}
p->next=NULL;
printnode(head);
}*/
//————————————————————————————————
void dakai(int n,int j,char ch)//查找文件
{
//int i=2;
//char ch1,filename[20];
FILE *fp;
//int i=0;
//display(n,ch);
//cout<
//scanf(“%s”,filename);
cout<
if ((fp=fopen(“f:\\stu_list.txt”,”rb”))==NULL)
{
printf(“cannot open this file\n”);
exit(0);
}
for(int i=0;i<4;i+=1)
{
fseek(fp,i*sizeof(struct student_type),2);
fread(&stud[i],sizeof(struct student_type),1,fp);
if(stud[i].num==j)
//{
printf(“姓名:%-10s学号:%4d年龄:%4d地址:%-15s\n”,stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
//i++;
//}
// }
/*fread(&stud[i-1],sizeof(struct student_type),i,fp);
cout<
printf(“%-10s”,stud[i-1].name);
cout<
printf(“%4d”,stud[i-1].num);
cout<
printf(“%4d”,stud[i-1].age);
cout<
printf(“%-15s\n”,stud[i-1].addr);
// }
/*
while((ch=getchar())!=’#’)
{
fputc(ch,fp);
putchar(ch);
}*/
fclose(fp);
}
//———————————————————————————————————-
void shuru(int n,char ch)//输入信息
{
int i;
cout<
cin>>n;
cout<
for(i=0;i
{
cout<
scanf(“%s%d%d%s”,stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
}
save(n,ch);
display(n,ch);
}
//———————————————————————————————————-
void paixu(int n,char ch)
{
fstream outfile,infile;
infile.open(“f:\\stu_list.txt”,ios::in);
if(!infile)
{
cerr<
exit(0);
}
student_type textline[80];
// int i=0;
// while(!infile.eof())
// {
// i++;
// infile.getline(textline,sizeof(textline));
// if
//cout<
}
infile.close();
}
//——————————————————————————————————-
/* void paixu2()
{
student_type ww;
for(int i=0;i
{
for(int j=i+1;j
{
if(stud[i].num>stu[j].num)
{
ww.num=stud[i].num;
stud[i].num=stud[j].num;
stud[j].num=ww.num;
ww.
*/
//———————————————————————————————————–
void xuanzhe(int n,char ch)//选择运行程序
{
int i,j;
if(ch==’s’)//输入
{
shuru(n,ch);
cout<
cout<
cout<
cin>>ch;
xuanzhe(n,ch);
}
else if(ch==’f’)//查找
{
cout<
cin>>j;
dakai(n,j,ch);
cout<
cout<
cout<
cin>>ch;
xuanzhe(n,ch);
}
else if(ch==’a’)//保存
{
save(n,ch);
cout<
cout<
cout<
cin>>ch;
xuanzhe(n,ch);
}
else if(ch==’p’)
{
paixu(n,ch);
cout<
cout<
cout<
cin>>ch;
xuanzhe(n,ch);
}
else if(ch==’x’)//修改
{
xiugai(n,ch);
cout<
cout<
cin>>ch;
xuanzhe(n,ch);
}
else if(ch==’d’)
{
display(n,ch);//显示信息
cout<
cout<
cin>>ch;
xuanzhe(n,ch);
}
else
{
cout<
}
}
//————————————————————————————————–
void main()//主函数
{
int n;
char ch;
cout<
cout<
cout<
cin>>ch;
xuanzhe(n,ch);
}
收起
阅读全文