📄 cpp1.cpp
字号:
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>
struct EMP
{
char name[25];
float salary;
int deleted;
};
struct EMP stu[100];
void input();
void search();
void number_Delete();
void name_Delete();
void display();
void insert();
int cishu=0;
main()
{
int c;
long curpos, length;// Variables for finding file size
EMP e; //Employee object for data storing & manipulation
strcpy(e.name,"NULL"); // Default values are
e.salary = 0; // assigned
e.deleted = 0;
do
{
printf("\n\t\t\t ********************\n");
printf("\t\t\t ----工资管理系统----\n");
printf("\t\t\t ********************\n");
printf("\n1 - 添加工资记录\n");
printf("\n2 - 显示工资记录\n");
printf("\n3 - 根据姓名查询工资数据\n");
printf("\n4 - 根据姓名删除工资数据\n");
printf("\n5 - 根据姓名修改工资数据\n");
printf("\n0 - 退出系统\n");
printf("\n请输入(1,2,3,4,5,6,0)进行选择:");
c = getch();
switch(c)
{
case 1 :
input();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
FILE* file;
FILE* temp_file;
name_Delete();
break;
case 5:
Edits();
break;
case 0: //退出
exit(0);
}
printf("\n\t按任意键返回到主菜单重新进行选择...");
getch();
system("cls");
}while(1);
return 0;
}
void input() //信息录入
{
system("cls");
printf("\t\t\t\t1.添加工资记录\n");
printf("\n请输入姓名: "); // ( but it is not saved into a file)
scanf("%s",e.name);
printf("请输入工资值: ");
scanf("%f",&e.salary);
e.deleted = 0;
FILE* file;
if((file = fopen("EMP.DAT","a"))==NULL)
{
printf("\n数据保存失败");
continue;
}
fwrite((char*)&e,sizeof(e),1,file);
fclose(file);
printf("\n此人数据已经成功保存");
printf("是否继续输入?(Y/N)");
ch=getch();
}
while(ch!='n');
}
void display() //输出学生信息
{
system("cls");
FILE* file;
printf("\t\t\t\t2.已存档工资记录\n");
if((file = fopen("EMP.DAT","r")) == NULL) {
printf("\n没有可导入的数据文件,请先建立一个新的工资数据管理文件");
continue;
}
curpos = ftell(file); // Current position of filepointer is stored in curpos
fseek(file, 0L, SEEK_END);// File pointer is moved 0 bytes from file end
// ie,file pointer is at the file's last byte
length = ftell(file); // Now current position is file's length
fseek(file, curpos, SEEK_SET); // File pointer is restored to file's begining
long num_recs = length / sizeof(e);// Length of each record in the file
for(long l = 0;l<num_recs;l++) { // Recursivley retrieves & prints each record in file
fread((char*)&e,sizeof(e),1,file);
printf("\n姓名: %s",e.name);
printf("\n工资: %.2f",e.salary);
}
fclose(file);
printf("\n\n\t所有记录条目总和: %d",num_recs);
ch=getch();
goto exit;
}
while(ch!='n');
exit: 0;
}
void name_Delete()
{
system("cls");
printf("\t\t\t\t4.根据姓名删除工资数据\n");
if((file = fopen("EMP.DAT","r")) == NULL) {
printf("没有已记录的工资数据文件,请先建立");
continue;
}
if((temp_file = fopen("EMP_TEMP.DAT","w")) == NULL) {
printf("Failed to create temp file");
continue;
}
printf("请输入姓名: ");
char f[25];
scanf("%s",f);
long curpos, length;
curpos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, curpos, SEEK_SET);
long num_recs = length / sizeof(e);
int found = 0;
for(long l = 0;l<num_recs;l++) {
fread((char*)&e,sizeof(e),1,file);
if((strcmp(e.name,f))==0) {
e.deleted = 1; // If record found sets it deleted flag to true
found = 1;
}
fwrite((char*)&e,sizeof(e),1,temp_file);
// Write all records read to a temporory file
}
fclose(file);
fclose(temp_file);
if(found == 0) {
printf("此人记录不存在,请重新操作");
unlink("EMP_TEMP.DAT");//If record was not found deletes temp file
//& continues main command loop
continue;
}
//If record was found rewrites all records from temp file to EMP.DAT
//where deleted flag is 0
//& deletes the tem file in the end
file = fopen("EMP.DAT","w");
temp_file = fopen("EMP_TEMP.DAT","r");
curpos = ftell(temp_file);
fseek(temp_file, 0L, SEEK_END);
length = ftell(temp_file);
fseek(temp_file, curpos, SEEK_SET);
num_recs = length / sizeof(e);
for(l = 0;l<num_recs;l++)
{
fread((char*)&e,sizeof(e),1,temp_file);
if(e.deleted == 0) {
fwrite((char*)&e,sizeof(e),1,file);
}
}
fclose(file);
fclose(temp_file);
printf("此人记录成功删除");
unlink("EMP_TEMP.DAT");
}
void search()
{
system("cls");
printf("\t\t\t3.根据姓名查询工资数据\n");
FILE* file;
if((file = fopen("EMP.DAT","r")) == NULL) { //Checks to see if file is accesible
printf("没有已记录的工资数据文件,请先建立");
continue;
}
//Gets employee name to perform search
printf("请输入姓名: ");
char f[25];
scanf("%s",f);
//Finds file size
long curpos, length;
curpos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, curpos, SEEK_SET);
long num_recs = length / sizeof(e);
int found = 0; // To check if record was found or not
for(long l = 0;l<num_recs;l++) {
// Recursivly checks each name if found prints details
// & sets found to 1
fread((char*)&e,sizeof(e),1,file);
if((strcmp(e.name,f))==0) {
printf("\n已找到,记录如下:");
printf("\n姓名: %s",e.name);
printf("\n工资: %.2f",e.salary);
found = 1;
}
}
if(found == 0)
printf("此人记录不存在。");
}
void Edits()
{
FILE* file;
system("cls");
printf("\t\t\t5.根据姓名修改工资数据\n");
if((file = fopen("EMP.DAT","r")) == NULL) {
printf("没有已保存的工资数据文件,请先建立");
continue;
}
printf("请输入姓名: ");
char f[25];
scanf("%s",f);
long curpos, length;
curpos = ftell(file);
fseek(file, 0L, SEEK_END);
length = ftell(file);
fseek(file, curpos, SEEK_SET);
long num_recs = length / sizeof(e);
int found = 0;
//Variables to store new name & salary
char new_name[25];
float new_sal;
EMP new_emp;
long write_at;//Position of record in to edit
for(long l = 0;l<num_recs;l++) {
fread((char*)&e,sizeof(e),1,file);
if((strcmp(e.name,f))==0) {//If record exists in file get new values
printf("输入新名字: ");
scanf("%s",new_name);
printf("输入新工资数: ");
scanf("%f",&new_sal);
//Copies values to a new EMP object
strcpy(new_emp.name,new_name);
new_emp.salary = new_sal;
new_emp.deleted = 0;
found = 1;
write_at = l; // Position to write new record
break;
}
}
fclose(file);
if(found == 1) {
//Calculates byte posiotn to write
write_at = write_at * sizeof(new_emp);
//Mode r+b opens a file to be writable & readable in binary
if((file = fopen("EMP.DAT","r+b")) == NULL) {
printf("Editing failed");
continue;
}
fseek(file,write_at,SEEK_SET);
fwrite((char*)&new_emp,sizeof(new_emp),1,file);
fclose(file);
printf("记录修改成功");
}else {
printf("没有找到此人的记录");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -