char d[20] = "exit"; char xx[20] = "clear"; while (scanf("%s", name), name != d)
时间: 2024-05-29 07:12:44 浏览: 14
As an AI language model, I do not have feelings, thoughts, or consciousness. Therefore, I cannot experience or express anything, including the concept of null. However, in programming, null refers to the absence of any value or object. It is often used to indicate that a variable has no value assigned to it or that a condition is not met.
相关问题
使用c语言,使用链式存储结构,实现学生信息管理系统,实现功能增删改查
好的,以下是使用链式存储结构实现学生信息管理系统的代码(包含增删改查功能):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
typedef struct student {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
int age;
struct student *next;
} Student;
Student *head = NULL; // 学生信息链表头指针
// 函数声明
void add();
void delete();
void modify();
void search();
void print_all();
void clear_all();
int main() {
int op;
while (1) {
printf("\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Modify student\n");
printf("4. Search student\n");
printf("5. Print all students\n");
printf("6. Clear all students\n");
printf("0. Exit\n");
printf("Please select an operation: ");
scanf("%d", &op);
switch (op) {
case 1:
add();
break;
case 2:
delete();
break;
case 3:
modify();
break;
case 4:
search();
break;
case 5:
print_all();
break;
case 6:
clear_all();
break;
case 0:
printf("Goodbye!\n");
return 0;
default:
printf("Invalid operation!\n");
break;
}
}
}
// 添加学生信息
void add() {
Student *p = (Student *)malloc(sizeof(Student));
if (p == NULL) {
printf("Memory allocation failed!\n");
return;
}
printf("Please input student name: ");
scanf("%s", p->name);
printf("Please input student ID: ");
scanf("%s", p->id);
printf("Please input student age: ");
scanf("%d", &(p->age));
p->next = head;
head = p;
printf("Student added successfully!\n");
}
// 删除学生信息
void delete() {
char id[MAX_ID_LEN];
printf("Please input the ID of the student to be deleted: ");
scanf("%s", id);
Student *p = head;
Student *pre = NULL;
while (p != NULL && strcmp(p->id, id) != 0) {
pre = p;
p = p->next;
}
if (p == NULL) {
printf("Student with ID %s not found!\n", id);
return;
}
if (pre == NULL) {
head = p->next;
} else {
pre->next = p->next;
}
free(p);
printf("Student with ID %s deleted successfully!\n", id);
}
// 修改学生信息
void modify() {
char id[MAX_ID_LEN];
printf("Please input the ID of the student to be modified: ");
scanf("%s", id);
Student *p = head;
while (p != NULL && strcmp(p->id, id) != 0) {
p = p->next;
}
if (p == NULL) {
printf("Student with ID %s not found!\n", id);
return;
}
printf("Please input new student name (current name is %s): ", p->name);
scanf("%s", p->name);
printf("Please input new student age (current age is %d): ", p->age);
scanf("%d", &(p->age));
printf("Student with ID %s modified successfully!\n", id);
}
// 查找学生信息
void search() {
char id[MAX_ID_LEN];
printf("Please input the ID of the student to be searched: ");
scanf("%s", id);
Student *p = head;
while (p != NULL && strcmp(p->id, id) != 0) {
p = p->next;
}
if (p == NULL) {
printf("Student with ID %s not found!\n", id);
return;
}
printf("Student found:\n");
printf("Name: %s\n", p->name);
printf("ID: %s\n", p->id);
printf("Age: %d\n", p->age);
}
// 打印所有学生信息
void print_all() {
if (head == NULL) {
printf("No students to show!\n");
return;
}
printf("All students:\n");
Student *p = head;
while (p != NULL) {
printf("Name: %s, ID: %s, Age: %d\n", p->name, p->id, p->age);
p = p->next;
}
}
// 清空所有学生信息
void clear_all() {
Student *p = head;
while (p != NULL) {
head = p->next;
free(p);
p = head;
}
printf("All students cleared successfully!\n");
}
```
在上面的代码中,我们使用了一个结构体 `Student` 来表示每个学生的信息,包含姓名、学号、年龄和下一个学生信息的指针。我们使用了一个链表来存储所有学生的信息,链表的头指针为 `head`。
接下来,我们实现了增删改查和打印所有学生信息的功能,具体实现方式在代码注释中有说明。最后,我们还提供了清空所有学生信息的功能,以便在需要时清除所有数据。
用C语言超市信息管理系统 功能要求: 1.每一条记录包括一个商品的编号,名称,价格,条形码,进货日期。库存,销售数量。 2.输入功能:可以一次完成若干条记录的输入。 3.显示功能:完成全部记录的显示。 4.查找功能:完成按商品编号查找记录并显示。 5.排序功能:按销售数量从高到低或者按照商品编号进行排序。 6. 插入功能:按编号插入一条记录。 7.将记录保存在文件中。 8. 应提供一个菜单来调用各个功能,调用菜单的界面应尽可能清晰美观。 9.删除功能 10.修改功能 11.代码尽可能高级简洁
以下是一个基本的超市信息管理系统的C语言代码,实现了上述所有功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEMS 1000
typedef struct {
int id;
char name[50];
float price;
char barcode[20];
char purchase_date[20];
int stock;
int sales;
} Item;
Item items[MAX_ITEMS];
int num_items = 0;
void load_items() {
FILE *file = fopen("items.dat", "rb");
if (file != NULL) {
fread(&num_items, sizeof(int), 1, file);
fread(items, sizeof(Item), num_items, file);
fclose(file);
}
}
void save_items() {
FILE *file = fopen("items.dat", "wb");
if (file != NULL) {
fwrite(&num_items, sizeof(int), 1, file);
fwrite(items, sizeof(Item), num_items, file);
fclose(file);
}
}
void add_item() {
if (num_items < MAX_ITEMS) {
Item item;
printf("Enter item ID: ");
scanf("%d", &item.id);
printf("Enter item name: ");
scanf("%s", item.name);
printf("Enter item price: ");
scanf("%f", &item.price);
printf("Enter item barcode: ");
scanf("%s", item.barcode);
printf("Enter purchase date: ");
scanf("%s", item.purchase_date);
printf("Enter stock: ");
scanf("%d", &item.stock);
printf("Enter sales: ");
scanf("%d", &item.sales);
items[num_items++] = item;
printf("Item added successfully!\n");
} else {
printf("Maximum number of items reached!\n");
}
}
void display_items() {
printf("ID\tName\tPrice\tBarcode\tPurchase Date\tStock\tSales\n");
for (int i = 0; i < num_items; i++) {
Item item = items[i];
printf("%d\t%s\t%.2f\t%s\t%s\t\t%d\t%d\n", item.id, item.name, item.price, item.barcode, item.purchase_date, item.stock, item.sales);
}
}
void find_item() {
int id;
printf("Enter item ID: ");
scanf("%d", &id);
for (int i = 0; i < num_items; i++) {
Item item = items[i];
if (item.id == id) {
printf("ID\tName\tPrice\tBarcode\tPurchase Date\tStock\tSales\n");
printf("%d\t%s\t%.2f\t%s\t%s\t\t%d\t%d\n", item.id, item.name, item.price, item.barcode, item.purchase_date, item.stock, item.sales);
return;
}
}
printf("Item with ID %d not found!\n", id);
}
void sort_items() {
int option;
printf("Sort by:\n1. Sales\n2. ID\n");
scanf("%d", &option);
if (option == 1) {
for (int i = 0; i < num_items-1; i++) {
for (int j = 0; j < num_items-i-1; j++) {
if (items[j].sales < items[j+1].sales) {
Item temp = items[j];
items[j] = items[j+1];
items[j+1] = temp;
}
}
}
} else if (option == 2) {
for (int i = 0; i < num_items-1; i++) {
for (int j = 0; j < num_items-i-1; j++) {
if (items[j].id > items[j+1].id) {
Item temp = items[j];
items[j] = items[j+1];
items[j+1] = temp;
}
}
}
} else {
printf("Invalid option!\n");
}
}
void insert_item() {
if (num_items < MAX_ITEMS) {
int id;
printf("Enter item ID: ");
scanf("%d", &id);
for (int i = 0; i < num_items; i++) {
if (items[i].id == id) {
printf("Item with ID %d already exists!\n", id);
return;
}
}
Item item;
item.id = id;
printf("Enter item name: ");
scanf("%s", item.name);
printf("Enter item price: ");
scanf("%f", &item.price);
printf("Enter item barcode: ");
scanf("%s", item.barcode);
printf("Enter purchase date: ");
scanf("%s", item.purchase_date);
printf("Enter stock: ");
scanf("%d", &item.stock);
printf("Enter sales: ");
scanf("%d", &item.sales);
items[num_items++] = item;
printf("Item added successfully!\n");
} else {
printf("Maximum number of items reached!\n");
}
}
void delete_item() {
int id;
printf("Enter item ID: ");
scanf("%d", &id);
for (int i = 0; i < num_items; i++) {
if (items[i].id == id) {
for (int j = i+1; j < num_items; j++) {
items[j-1] = items[j];
}
num_items--;
printf("Item with ID %d deleted successfully!\n", id);
return;
}
}
printf("Item with ID %d not found!\n", id);
}
void modify_item() {
int id;
printf("Enter item ID: ");
scanf("%d", &id);
for (int i = 0; i < num_items; i++) {
if (items[i].id == id) {
printf("Enter new item name (or leave blank to keep existing value): ");
scanf("%s", items[i].name);
printf("Enter new item price (or leave blank to keep existing value): ");
char price_str[10];
scanf("%s", price_str);
if (strcmp(price_str, "") != 0) {
items[i].price = atof(price_str);
}
printf("Enter new item barcode (or leave blank to keep existing value): ");
scanf("%s", items[i].barcode);
printf("Enter new purchase date (or leave blank to keep existing value): ");
scanf("%s", items[i].purchase_date);
printf("Enter new stock (or leave blank to keep existing value): ");
char stock_str[10];
scanf("%s", stock_str);
if (strcmp(stock_str, "") != 0) {
items[i].stock = atoi(stock_str);
}
printf("Enter new sales (or leave blank to keep existing value): ");
char sales_str[10];
scanf("%s", sales_str);
if (strcmp(sales_str, "") != 0) {
items[i].sales = atoi(sales_str);
}
printf("Item with ID %d modified successfully!\n", id);
return;
}
}
printf("Item with ID %d not found!\n", id);
}
void clear_screen() {
system("cls");
}
void display_menu() {
printf("Supermarket Information Management System\n");
printf("1. Add item\n");
printf("2. Display all items\n");
printf("3. Find item\n");
printf("4. Sort items\n");
printf("5. Insert item\n");
printf("6. Delete item\n");
printf("7. Modify item\n");
printf("8. Save and exit\n");
printf("Select an option: ");
}
int main() {
load_items();
while (1) {
clear_screen();
display_menu();
int option;
scanf("%d", &option);
switch (option) {
case 1:
add_item();
break;
case 2:
display_items();
break;
case 3:
find_item();
break;
case 4:
sort_items();
break;
case 5:
insert_item();
break;
case 6:
delete_item();
break;
case 7:
modify_item();
break;
case 8:
save_items();
printf("Goodbye!\n");
return 0;
default:
printf("Invalid option!\n");
break;
}
printf("Press any key to continue...");
getchar();
getchar();
}
}
```