设计企业员工管理系统,要求先以管理员身份登录,输入密码正确后录入某企业员工(假定不超过50人)的工资,密码限定最多输入三次,三次不成功退出系统
#include <stdio.h>
#include <string.h>
#define MAX_EMPLOYEE 50
#define ADMIN_PASSWORD "admin123"
typedef struct {
char name[20];
int salary;
} Employee;
int main(){
char password[20];
Employee employees[MAX_EMPLOYEE];
int num_employees = 0;
int password_attempts = 0;
printf("欢迎使用企业员工管理系统\n");
while (password_attempts < 3) {
printf("管理员登录\n");
printf("请输入密码:\n");
scanf("%s", password);
if (strcmp(password, ADMIN_PASSWORD) == 0) {
printf("登录成功!\n");
while (num_employees < MAX_EMPLOYEE) {
printf("请输入员工姓名(输入exit退出录入):\n");
scanf("%s", employees[num_employees].name);
if (strcmp(employees[num_employees].name, "exit") == 0) {
break;
}
printf("请输入员工工资:\n");
scanf("%d", &(employees[num_employees].salary));
num_employees++;
}
printf("录入完成,共录入%d个员工的信息。\n", num_employees);
return 0;
}
else {
password_attempts++;
if (password_attempts < 3) {
printf("密码错误,请重新输入。\n");
}
else {
printf("密码错误次数已达到上限,系统自动退出。\n");
return 0;
}
}
}
}
**
snake.c
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include "LIST.h"
#define MAP_WIDTH 20
#define MAP_HEIGHT 20
extern struct Snake *g_snake;
extern struct Snake g_food;
int g_flag = 0;
int g_direction = KEY_RIGHT;
int commitsuicide() {
struct Snake *p;
p = g_snake;
struct Snake *tmp;
tmp = g_snake->next;
while (tmp != NULL) {
if ((p->x == tmp->x) && (p->y == tmp->y)) {
return 0;
}
tmp = tmp->next;
}
return -1;
}
void initFood() {
int judge = 0;
do {
int x = rand() % 20 + 1;
int y = rand() % 20 + 1;
g_food.x = x;
g_food.y = y;
g_food.next = NULL;
if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
judge = 1;
}
} while (g_food.x == 0 || g_food.x > 18 || g_food.y == 0 || g_food.y > 18 ||
judge == 1);
}
int foodFind(int x, int y) {
if ((g_food.x == x) && (g_food.y == y)) {
return 0;
}
return -1;
}
int headTofood() {
if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
return 0;
}
return -1;
}
void initMap() {
int x;
int y;
for (x = 0; x < MAP_WIDTH; x++) {
for (y = 0; y < MAP_HEIGHT; y++) {
if (x == 0 || x == MAP_HEIGHT - 1) {
if (x == 0) {
printw("* ");
} else {
printw("* ");
}
} else {
if (y == 0 || y == MAP_WIDTH - 1) {
printw("* ");
} else {
if ((snakeFind(g_snake, x, y) == 0)) {
printw("[]");
} else if (foodFind(x, y) == 0) {
printw("++");
} else {
printw(" ");
}
}
}
}
printw("\n");
}
}
void handleKey() {
int key;
noecho();
keypad(stdscr, TRUE);
while (1) {
key = getch();
if (adjustDirection(key) == 0) {
continue;
}
}
}
int adjustDirection(int key) {
if (key == KEY_DOWN || key == KEY_UP || key == KEY_RIGHT ||
key == KEY_LEFT) {
if (((g_direction == KEY_RIGHT) && (key == KEY_LEFT)) ||
((g_direction == KEY_LEFT) && (key == KEY_RIGHT)) ||
((g_direction == KEY_UP) && (key == KEY_DOWN)) ||
((g_direction == KEY_DOWN) && (key == KEY_UP))) {
return 0;
}
}
g_direction = key;
return -1;
}
void moveSnakeauto() {
while (1) {
switch (g_direction) {
case KEY_DOWN:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
deleteSnake(&g_snake);
}
break;
case KEY_UP:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
deleteSnake(&g_snake);
}
break;
case KEY_LEFT:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
deleteSnake(&g_snake);
}
break;
case KEY_RIGHT:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
deleteSnake(&g_snake);
}
break;
default:
break;
}
clear();
if (g_flag == -1) {
initFood();
g_flag = 0;
}
if (snakeDie() == 0 || commitsuicide() == 0) {
destorySnake();
createSnake();
}
initMap();
refresh();
usleep(100000);
}
}
int main() {
pthread_t t_movesnake;
pthread_t t_getinput;
initscr();
createSnake();
initFood();
initMap();
pthread_create(&t_movesnake, NULL, (void *)moveSnakeauto, NULL);
pthread_create(&t_getinput, NULL, (void *)handleKey, NULL);
while (1);
endwin();
return 0;
}
list.c
#include <curses.h>
#include <stdlib.h>
#include "LIST.h"
struct Snake *g_snake;
struct Snake g_food;
int snakeDie() // panduansiwang
{
if (g_snake->x == 0 || g_snake->x == 19 || g_snake->y == 0 ||
g_snake->y == 19) {
return 0;
}
return -1;
}
void destorySnake() // bianlishanchu
{
struct Snake *p;
while (g_snake != NULL) {
p = g_snake;
g_snake = g_snake->next;
free(p);
}
}
void insertSnake(struct Snake **head, int x, int y) // touchafa
{
struct Snake *node;
node = (struct Snake *)malloc(sizeof(struct Snake));
node->x = x;
node->y = y;
node->next = NULL;
node->next = *head;
*head = node;
}
void createSnake() {//chushihuashe
g_snake = (struct Snake *)malloc(sizeof(struct Snake));
g_snake->x = 3;
g_snake->y = 3;
g_snake->next = NULL;
insertSnake(&(g_snake), 3, 4);
insertSnake(&(g_snake), 3, 5);
insertSnake(&(g_snake), 3, 6);
}
int snakeFind(struct Snake *head, int x, int y) {//bianlipanduan
struct Snake *p;
p = head;
while (p != NULL) {
if ((p->x == x) && (p->y == y)) {
return 0;
}
p = p->next;
}
return -1;
}
void deleteSnake(struct Snake **head) {//shanchuweiba
struct Snake *p;
struct Snake *tmp;
tmp = NULL;
p = *head;
while ((p != NULL) && (p->next != NULL)) {
if ((p->next->next == NULL)) {
tmp = p->next;
p->next = NULL;
free(tmp);
break;
}
p = p->next;
}
}
list.h
#ifndef _LIST_H_
#define _LIST_H_
struct Snake
{
int x;
int y;
struct Snake *next;
};
void insertSnake(struct Snake **head,int x,int y);
void createSnake();
int snakeFind(struct Snake *head,int x,int y);
void deleteSnake(struct Snake **head);
int foodFind(int x,int y);
int snakeDie();
void destorySnake();
#endif
Makefile
snake:
gcc snake_final.c LIST.c -lncurses -lpthread -o snakepro
针对问题,需要设计一个企业员工管理系统,以下是一些设计思路:
typedef struct{ int ID; //员工编号 char name[20]; //员工姓名 int age; //员工年龄 char position[20]; //员工职位 }Employee;
typedef struct{ Employee emp; //员工信息 float salary; //员工工资 }EmployeeSalary;
char password[20] = "123456"; //设定密码为123456 char password_input[20]; int count = 0; //记载输入密码的次数 while(count < 3){ printf("请输入管理员密码:"); scanf("%s", password_input); if(strcmp(password, password_input) == 0){ //如果输入的密码等于设定的密码 printf("登录成功!\n"); break; } else{ count++; printf("密码错误,请重新输入(剩余%d次机会)\n", 3-count); } } if(count == 3){ printf("输入密码次数超过限制,系统已退出。\n"); exit(0); //退出程序 }
//写入员工信息 FILE *fp1 = fopen("employee.dat", "wb"); Employee emp; for(int i=0; i<50; i++){ printf("请录入第%d位员工信息:\n", i+1); printf("员工编号:"); scanf("%d", &emp.ID); printf("员工姓名:"); scanf("%s", emp.name); printf("员工年龄:"); scanf("%d", &emp.age); printf("员工职位:"); scanf("%s", emp.position); fwrite(&emp, sizeof(Employee), 1, fp1); //写入一个员工的信息 } fclose(fp1);
//写入员工工资信息 FILE *fp2 = fopen("salary.dat", "wb"); EmployeeSalary es; for(int i=0; i<50; i++){ printf("请录入第%d位员工的工资信息:\n", i+1); printf("员工编号:"); scanf("%d", &es.emp.ID); printf("员工工资:"); scanf("%f", &es.salary); fwrite(&es, sizeof(EmployeeSalary), 1, fp2); //写入一个员工的工资信息 } fclose(fp2);
//查询员工信息 FILE *fp1 = fopen("employee.dat", "rb"); Employee emp; int id; printf("请输入要查询的员工编号:"); scanf("%d", &id); while(fread(&emp, sizeof(Employee), 1, fp1)){ if(emp.ID == id){ printf("查询结果:\n"); printf("员工编号:%d\n", emp.ID); printf("员工姓名:%s\n", emp.name); printf("员工年龄:%d\n", emp.age); printf("员工职位:%s\n", emp.position); break; } } fclose(fp1);
//查询员工工资信息 FILE *fp2 = fopen("salary.dat", "rb"); EmployeeSalary es; printf("请输入要查询工资的员工编号:"); scanf("%d", &id); while(fread(&es, sizeof(EmployeeSalary), 1, fp2)){ if(es.emp.ID == id){ printf("查询结果:\n"); printf("员工编号:%d\n", es.emp.ID); printf("员工姓名:%s\n", es.emp.name); printf("员工工资:%f\n", es.salary); break; } } fclose(fp2);
以上是一些简单的思路,可以根据实际需要进行完善和修改。代码实现中可以结合使用C语言中的文件操作、字符串处理和结构体等知识点。