定义主函数 void main( ) { int vexnumber,arcnumber,totaltime=0; printf("请输入这个图中的节点数:"); scanf("%d",&vexnumber); printf("请输入这个图中的弧数:"); scanf("%d",&arcnumber); vexnode* Graph=(vexnode*)malloc(vexnumber*sizeof(vexnode)); CreateGraph(Graph,vexnumber,arcnumber); SearchMapPath(Graph,vexnumber,arcnumber);此函数模块流程图 }
时间: 2024-01-13 08:02:32 浏览: 78
好的,这是一个定义主函数的代码,其目的是为了创建一个图并寻找其中的路径。下面是代码解释:
```
void main() {
int vexnumber, arcnumber, totaltime = 0;
printf("请输入这个图中的节点数:");
scanf("%d", &vexnumber);
printf("请输入这个图中的弧数:");
scanf("%d", &arcnumber);
vexnode* Graph = (vexnode*)malloc(vexnumber * sizeof(vexnode));
CreateGraph(Graph, vexnumber, arcnumber);
SearchMapPath(Graph, vexnumber, arcnumber);
}
```
这个函数中,首先定义了三个变量,分别是节点数、弧数和总时间,它们的初始值都是0。接着使用 `printf()` 函数和 `scanf()` 函数获取用户输入的节点数和弧数。然后,使用 `malloc()` 函数动态分配内存,创建一个大小为 `vexnumber * sizeof(vexnode)` 的数组 `Graph`。接下来,调用 `CreateGraph()` 函数来创建图。最后,调用 `SearchMapPath()` 函数来寻找图中的路径。
相关问题
2.基于散列表的工作原理,采用除留余数法H(key) = key % p (p<m) 作为散列函数,采用线性探测法处理冲突,实现散列查找算法,自动生成包含数百、数千、数万的正整数集合构造散列表,测量不同规模散列表的平均查找长度; C语言完整代码
以下是基于散列表的查找算法的C语言完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 100000 // 散列表的最大容量
#define PRIME 99991 // 散列表容量的质数
// 定义散列表节点结构体
typedef struct {
int key;
} Node;
// 定义散列表结构体
typedef struct {
Node data[MAX_SIZE];
int count;
} HashTable;
// 初始化散列表
void init(HashTable *hashTable) {
hashTable->count = 0;
for (int i = 0; i < MAX_SIZE; i++) {
hashTable->data[i].key = 0;
}
}
// 计算散列值
int hash(int key) {
return key % PRIME;
}
// 插入元素
void insert(HashTable *hashTable, int key) {
if (hashTable->count >= MAX_SIZE) {
printf("Hash table is full\n");
return;
}
int pos = hash(key);
while (hashTable->data[pos].key != 0) {
pos = (pos + 1) % MAX_SIZE;
}
hashTable->data[pos].key = key;
hashTable->count++;
}
// 查找元素
int search(HashTable *hashTable, int key) {
int pos = hash(key);
while (hashTable->data[pos].key != key) {
pos = (pos + 1) % MAX_SIZE;
if (hashTable->data[pos].key == 0 || pos == hash(key)) {
return -1;
}
}
return pos;
}
// 生成指定数量的随机数
void generateRandomNumbers(int *arr, int n) {
srand(time(NULL));
for (int i = 0; i < n; i++) {
arr[i] = rand() % MAX_SIZE + 1;
}
}
int main() {
HashTable hashTable;
init(&hashTable);
int n;
printf("Enter the number of random numbers to generate: ");
scanf("%d", &n);
int arr[n];
generateRandomNumbers(arr, n);
// 插入元素
for (int i = 0; i < n; i++) {
insert(&hashTable, arr[i]);
}
// 测量平均查找长度
int totalLength = 0;
for (int i = 0; i < n; i++) {
int pos = search(&hashTable, arr[i]);
if (pos != -1) {
totalLength += (pos + 1);
}
}
float avgLength = (float)totalLength / n;
printf("Average search length: %.2f\n", avgLength);
return 0;
}
```
运行该程序,可以输入要生成的随机数的数量,然后程序会自动插入这些数到散列表中,并测量不同规模散列表的平均查找长度。
用C语言写一个用链表数据结构实现的图书管理系统,要求包括8个数据项:书名、ISBN、作者、出版社、出版时间、入库时间、库存量、借阅数,并完成以下操作,录入、删除、修改、查询、借书、还书、统计、输出
好的,下面是用链表数据结构实现的图书管理系统的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// 定义图书信息结构体
typedef struct Book {
char name[50]; // 书名
char ISBN[20]; // ISBN
char author[20]; // 作者
char publisher[20]; // 出版社
char publish_time[20];// 出版时间
char enter_time[20]; // 入库时间
int stock; // 库存量
int borrowed; // 借阅数
struct Book *next; // 指向下一个节点的指针
} Book;
// 定义链表头指针
Book *head = NULL;
// 函数声明
void add_book();
void delete_book();
void update_book();
void search_book();
void borrow_book();
void return_book();
void statistics();
void output();
void save_file();
void load_file();
int main() {
int choice;
load_file(); // 读取文件中的图书信息
while (1) {
printf("***************************\n");
printf("* 1. 添加图书 *\n");
printf("* 2. 删除图书 *\n");
printf("* 3. 修改图书 *\n");
printf("* 4. 查找图书 *\n");
printf("* 5. 借阅图书 *\n");
printf("* 6. 归还图书 *\n");
printf("* 7. 统计信息 *\n");
printf("* 8. 输出图书列表 *\n");
printf("* 0. 退出 *\n");
printf("***************************\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
delete_book();
break;
case 3:
update_book();
break;
case 4:
search_book();
break;
case 5:
borrow_book();
break;
case 6:
return_book();
break;
case 7:
statistics();
break;
case 8:
output();
break;
case 0:
save_file(); // 保存图书信息到文件中
exit(0);
default:
printf("输入有误,请重新输入。\n");
}
}
return 0;
}
// 添加图书
void add_book() {
Book *new_book = (Book *) malloc(sizeof(Book));
printf("请输入书名:");
scanf("%s", new_book->name);
printf("请输入ISBN:");
scanf("%s", new_book->ISBN);
printf("请输入作者:");
scanf("%s", new_book->author);
printf("请输入出版社:");
scanf("%s", new_book->publisher);
printf("请输入出版时间(例如:2022-01-01):");
scanf("%s", new_book->publish_time);
time_t t = time(NULL);
struct tm *tm = localtime(&t);
sprintf(new_book->enter_time, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
printf("请输入库存量:");
scanf("%d", &new_book->stock);
new_book->borrowed = 0;
new_book->next = NULL;
if (head == NULL) {
head = new_book;
} else {
Book *current_book = head;
while (current_book->next != NULL) {
current_book = current_book->next;
}
current_book->next = new_book;
}
printf("添加成功!\n");
}
// 删除图书
void delete_book() {
char name[50];
printf("请输入要删除的书名:");
scanf("%s", name);
if (head == NULL) {
printf("列表为空,无法删除。\n");
return;
}
Book *current_book = head;
Book *previous_book = NULL;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
if (previous_book == NULL) {
head = current_book->next;
} else {
previous_book->next = current_book->next;
}
free(current_book);
printf("删除成功!\n");
return;
}
previous_book = current_book;
current_book = current_book->next;
}
printf("图书不存在,无法删除。\n");
}
// 修改图书
void update_book() {
char name[50];
printf("请输入要修改的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
printf("请输入新的书名:");
scanf("%s", current_book->name);
printf("请输入新的ISBN:");
scanf("%s", current_book->ISBN);
printf("请输入新的作者:");
scanf("%s", current_book->author);
printf("请输入新的出版社:");
scanf("%s", current_book->publisher);
printf("请输入新的出版时间(例如:2022-01-01):");
scanf("%s", current_book->publish_time);
printf("请输入新的库存量:");
scanf("%d", ¤t_book->stock);
printf("修改成功!\n");
return;
}
current_book = current_book->next;
}
printf("图书不存在,无法修改。\n");
}
// 查找图书
void search_book() {
char name[50];
printf("请输入要查找的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
printf("书名:%s\n", current_book->name);
printf("ISBN:%s\n", current_book->ISBN);
printf("作者:%s\n", current_book->author);
printf("出版社:%s\n", current_book->publisher);
printf("出版时间:%s\n", current_book->publish_time);
printf("库存量:%d\n", current_book->stock);
printf("借阅数:%d\n", current_book->borrowed);
return;
}
current_book = current_book->next;
}
printf("图书不存在。\n");
}
// 借阅图书
void borrow_book() {
char name[50];
printf("请输入要借阅的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
if (current_book->stock > 0) {
current_book->stock--;
current_book->borrowed++;
printf("借阅成功!\n");
} else {
printf("库存不足,无法借阅。\n");
}
return;
}
current_book = current_book->next;
}
printf("图书不存在,无法借阅。\n");
}
// 归还图书
void return_book() {
char name[50];
printf("请输入要归还的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
if (current_book->borrowed > 0) {
current_book->stock++;
current_book->borrowed--;
printf("归还成功!\n");
} else {
printf("该图书没有借阅记录,无法归还。\n");
}
return;
}
current_book = current_book->next;
}
printf("图书不存在,无法归还。\n");
}
// 统计信息
void statistics() {
int total_stock = 0;
int total_borrowed = 0;
int total = 0;
Book *current_book = head;
while (current_book != NULL) {
total_stock += current_book->stock;
total_borrowed += current_book->borrowed;
total++;
current_book = current_book->next;
}
printf("图书总数:%d\n", total);
printf("库存总量:%d\n", total_stock);
printf("借阅总数:%d\n", total_borrowed);
}
// 输出图书列表
void output() {
printf("书名\tISBN\t作者\t出版社\t出版时间\t入库时间\t库存量\t借阅数\n");
Book *current_book = head;
while (current_book != NULL) {
printf("%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\n", current_book->name, current_book->ISBN,
current_book->author, current_book->publisher, current_book->publish_time,
current_book->enter_time, current_book->stock, current_book->borrowed);
current_book = current_book->next;
}
}
// 保存图书信息到文件中
void save_file() {
FILE *fp = fopen("book.txt", "w");
if (fp == NULL) {
printf("文件打开失败。\n");
return;
}
Book *current_book = head;
while (current_book != NULL) {
fprintf(fp, "%s %s %s %s %s %s %d %d\n", current_book->name, current_book->ISBN,
current_book->author, current_book->publisher, current_book->publish_time,
current_book->enter_time, current_book->stock, current_book->borrowed);
current_book = current_book->next;
}
fclose(fp);
}
// 读取文件中的图书信息
void load_file() {
char name[50];
char ISBN[20];
char author[20];
char publisher[20];
char publish_time[20];
char enter_time[20];
int stock;
int borrowed;
FILE *fp = fopen("book.txt", "r");
if (fp == NULL) {
printf("文件打开失败。\n");
return;
}
while (fscanf(fp, "%s %s %s %s %s %s %d %d\n", name, ISBN, author, publisher, publish_time, enter_time, &stock, &borrowed) != EOF) {
Book *new_book = (Book *) malloc(sizeof(Book));
strcpy(new_book->name, name);
strcpy(new_book->ISBN, ISBN);
strcpy(new_book->author, author);
strcpy(new_book->publisher, publisher);
strcpy(new_book->publish_time, publish_time);
strcpy(new_book->enter_time, enter_time);
new_book->stock = stock;
new_book->borrowed = borrowed;
new_book->next = NULL;
if (head == NULL) {
head = new_book;
} else {
Book *current_book = head;
while (current_book->next != NULL) {
current_book = current_book->next;
}
current_book->next = new_book;
}
}
fclose(fp);
}
```
其中,我们用 `Book` 结构体来存储图书信息,定义了链表头指针 `head`,并实现了以下操作:
- 添加图书
- 删除图书
- 修改图书
- 查找图书
- 借阅图书
- 归还图书
- 统计信息
- 输出图书列表
另外,我们还实现了将图书信息保存到文件和从文件中读取图书信息的函数 `save_file()` 和 `load_file()`。这样,在程序运行结束时,可以将图书信息保存到文件中,下次运行程序时可以读取文件中的图书信息。
注意:由于没有对输入做过多判断和处理,所以输入不规范的数据可能会导致程序崩溃,请谨慎使用。另外,由于时间和精力有限,代码可能存在一些潜在的问题,如有发现,请指出。
阅读全文