#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <windows.h> #define M 50 struct date { int year; int month; int day; }; struct library { int book_num; char book_name[40]; char author_name[10]; float book_price; char publisher[40]; struct date publish_time; char book_state[2]; char reader_name[10]; int reader_num; }; int input(struct library book[]); void list(struct library book[], int n); void search_name(struct library book[], int n); __________________填空____________________________; void sort_name(struct library book[], int n); int borrow_book(struct library book[], int n); ___________________填空___________________________; void saverecord(struct library book[], int n); int loadrecord(struct library book[]); int menu(); void main() { struct library b[M]; int length; while(1) { switch(menu()) { case 1:length = input(b); if(length == -1) break; else saverecord(b, length); break; case 2:length = loadrecord(b); list(b, length); break; case 3:length = loadrecord(b); search_name(b, length); break; //case 4:length = loadrecord(b); //___________填空__________; //saverecord(b, length); //break; //case 5:length=loadrecord(b); //length=Delete_num(b, length); //saverecord(b, length); //break; //case 6:length = loadrecord(b); //borrow_book(b,length); //saverecord(b,length); //break; //case 7:length = loadrecord(b); //________填空______________; //saverecord(b,length); //break; case 8:exit(0); } } } int menu() { int c; system("mode con:cols=400 lines=30000"); system("color 0b"); printf("\t\t\t\t\t-----------------------------------------------\n"); printf("\t\t\t\t\t+ 图书信息管理系统 +\n"); printf("\t\t\t\t\t-----------------------------------------------\n"); printf("\t\t\t\t\t\t\t ****************\n"); printf("\t\t\t\t\t\t\t 1.输入图书信息\n"); printf("\t\t\t\t\t\t\t 2.显示图书信息\n"); printf("\t\t\t\t\t\t\t 3.按图书名标查询图书信息\n"); printf("\t\t\t\t\t\t\t 4.按图书名称从小到大排序图书信息\n"); printf("\t\t\t\t\t\t\t 5.按图书编号删除图书信息\n"); printf("\t\t\t\t\t\t\t 6.借书\n"); printf("\t\t\t\t\t\t\t 7.还书\n"); printf("\t\t\t\t\t\t\t 0.退出\n"); printf("\t\t\t\t\t\t\t ****************\n"); do { printf("\n Enter your choice(0-7):"); if (scanf("%d" , &c) != 1) { while (getchar() != '\n') continue; continue; } }while (c<0 || c>7); return c; }
时间: 2024-04-10 22:35:13 浏览: 48
函数原型或函数定义:
```c
void sort_name(struct library book[], int n);
```
调用sort_name函数的代码:
```c
length = loadrecord(b);
sort_name(b, length);
saverecord(b, length);
```
注意:在调用sort_name函数之前,需要先通过loadrecord函数将图书信息加载到数组b中,然后再调用sort_name函数进行排序,最后再通过saverecord函数保存排序后的图书信息。
相关问题
could you please finish it without using <string.h>, <ctype.h> or <stdbool.h>
Sure, here's the modified code without using `<string.h>`, `<ctype.h>`, or `<stdbool.h>`:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_WORD_SIZE 80
#define ASCII_SIZE 128
void print_colored(char* text, char* color) {
printf("\033[%sm%s\033[0m", color, text);
}
int strlen(char* str) {
int len = 0;
while (*str != '\0') {
len++;
str++;
}
return len;
}
int isalpha(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
int isdigit(int c) {
return c >= '0' && c <= '9';
}
int isspace(int c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v';
}
int strcmp(char* str1, char* str2) {
while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2) {
str1++;
str2++;
}
return *str1 - *str2;
}
char* strstr(char* haystack, char* needle) {
int needle_len = strlen(needle);
if (needle_len == 0) {
return haystack;
}
while (*haystack != '\0') {
if (*haystack == *needle) {
char* h = haystack;
char* n = needle;
while (*h != '\0' && *n != '\0' && *h == *n) {
h++;
n++;
}
if (*n == '\0') {
return haystack;
}
}
haystack++;
}
return NULL;
}
void decode(char* encoded_text, int n, char* decoded_text) {
int i = 0;
while (*encoded_text != '\0') {
decoded_text[i] = ((*encoded_text - n) % ASCII_SIZE + ASCII_SIZE) % ASCII_SIZE;
encoded_text++;
i++;
}
decoded_text[i] = '\0';
}
int main() {
char word[MAX_WORD_SIZE + 1];
int n;
char encoded_text[ASCII_SIZE];
fgets(word, MAX_WORD_SIZE + 1, stdin);
scanf("%d", &n);
getchar(); // read the newline character after n
int i = 0;
char c;
while ((c = getchar()) != EOF && c != '\n') {
encoded_text[i] = c;
i++;
}
encoded_text[i] = '\0';
char decoded_text[ASCII_SIZE];
decode(encoded_text, n, decoded_text);
if (strstr(decoded_text, word) != NULL) {
print_colored("YES\n", "32"); // green color
} else {
print_colored("NO\n", "31"); // red color
}
return 0;
}
```
I had to define some of the missing functions (`strlen()`, `isalpha()`, `isdigit()`, `isspace()`, `strcmp()`, and `strstr()`) using basic string manipulation techniques, since they are not available in the standard C library. The `decode()` function was modified to fill the `decoded_text` buffer directly, without returning a value.
I hope this meets your requirements! Let me know if you have any questions.
#include<stdio.h> #include<ctype.h> #include<stdlib.h> #include<string.h> char *str(char *name){ char *s1="hello "; strcat(s1,name); return s1; } int main(){ char*name; name=(char*)malloc(20); printf("输入你的名字:"); gets(name); printf("%s",str(name)); }如何修改代码
在您提供的代码中,存在几个需要进行修改的地方。首先,`gets()` 函数是不安全的,已被废弃,应该使用更安全的替代函数 `fgets()`。其次,在 `str()` 函数中,您将字符串常量 `s1` 的地址返回,这是不安全的,因为 `s1` 是一个局部变量,函数结束后会被销毁。下面是修改后的代码:
```c
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char* str(const char* name) {
char* s1 = malloc(strlen(name) + 7); // 为新字符串分配足够的内存
strcpy(s1, "hello ");
strcat(s1, name);
return s1;
}
int main() {
char name[20];
printf("输入你的名字: ");
fgets(name, sizeof(name), stdin); // 使用fgets()获取用户输入
name[strcspn(name, "\n")] = '\0'; // 去除输入字符串中的换行符
printf("%s", str(name));
free(str(name)); // 释放通过malloc分配的内存
return 0;
}
```
这样修改后的代码更安全,避免了不安全的函数和内存问题。同时,我们也对用户输入的字符串进行了处理,去除了可能存在的换行符。请注意在使用完成后释放通过 `malloc` 分配的内存,以避免内存泄漏。