% 设置参数 % 计算每个节点的度数 degree = sum(a1~=0, 2); % 节点以概率i/N*0.1退化,i为节点度数 prob = degree./(N*10); p_failure = 0.01; % 正常节点失效的概率 p_fault = 0.1; % 节点故障的概率 t_repair = 100; % 故障节点失效前修复的时间步长 t_degrade = 20; % 退化节点失效前进入失效状态的时间步长 n_gateway = 10; % 网关节点数量 % 生成随机网络结构和节点状态 n1=20; n2=20; file = fopen('liangcengjiedian.txt', 'r'); % 去掉空格 if file ~= -1 % 确认文件已经被正确打开 data = textscan(file, '(%f,%f,%f) to (%f,%f,%f)\n'); n_gateway = length(data{1}); gateway1 = []; gateway2 = []; for i = 1:size(data{1}, 1) x1 = data{1}(i); y1 = data{2}(i); z1 = data{3}(i); x2 = data{4}(i); y2 = data{5}(i); z2 = data{6}(i); gateway1 = [gateway1; x1, y1, z1]; gateway2 = [gateway2; x2, y2, z2]; end fclose(file); % 记得关闭文件 else disp('Error: file not found or could not be opened.'); end % 使用randperm函数随机选择n_gateway个网关 n_gateway = min(n_gateway, size(gateway1, 1)); idx = randperm(size(gateway1, 1), n_gateway); gateway1 = gateway1(idx, :); gateway2 = gateway2(idx, :); state1 = zeros(n1, 1); % 第一层节点状态 state2 = zeros(n2, 1); % 第二层节点状态修改代码
时间: 2024-01-27 14:03:18 浏览: 80
borrows[borrow_count].book_id = book_id;
borrows[borrow_count].borrow_id = borrow_count + 1;
borrow_count++;
printf("Borrow book successfully.\n");
return;
}
}
printf("Book not found.\根据代码段,似乎没有要求修改的问题。不过,可以对代码进行一些改进,以提高n");
}
void return_book() {
int book_id, i;
char actual_return_date[20];
printf("Please input the book id to return: ");
scanf("%d", &book_id);
for (i = 0; i < book_count; i代码的可读性和可维护性。
1. 统一变量命名方式,建议使用下划线分隔++) {
if (books[i].book_id == book_id) {
if (books[i].status == 0) {
printf单词,例如 `p_failure` 改为 `p_failure_rate`,`t_repair` 改为 `t_repair_step`("The book is not borrowed.\n");
return;
}
printf("Please input the actual return date: ");
scanf("%s", actual_return_date);
books[i].status = 0;
strcpy(borrows[i].actual_return_date, actual_return。
2. 使用结构体或对象封装参数,以便统一管理和传递参数。例如:
```
params.degree_date);
printf("Return book successfully.\n");
return;
}
}
printf("Book not found.\n");
}
= sum(a1~=0, 2);
params.prob = params.degree./(N*10);
params.p_failure_rate = 0.01void query_borrow() {
int i;
char user_name[20];
printf("Please input your name to query borrow record:;
params.p_fault_rate = 0.1;
params.t_repair_step = 100;
params.t_degrade_step = 20;
");
scanf("%s", user_name);
printf("Your borrow record:\n");
for (i = 0; i < borrowparams.n_gateway = 10;
```
3. 将读取文件的代码封装成一个函数,以便重复使用。_count; i++) {
if (strcmp(borrows[i].user_name, user_name) == 0) {
printf("B例如:
```
function [gateway1, gateway2, n_gateway] = read_gateway_file(filename)
file = fopen(filename, 'rorrow id: %d\n", borrows[i].borrow_id);
printf("Book id: %d\n", borrows[i].book_id);
printf("Borrow date: %s\n", borrows[i].borrow_date);
printf("Return date: %s\n');
if file ~= -1
data = textscan(file, '(%f,%f,%f) to (%f,%f,%", borrows[i].return_date);
printf("Actual return date: %s\n", borrows[i].actual_return_date);
f)\n');
n_gateway = length(data{1});
gateway1 = [];
gateway2 = [];
for i = 1 printf("\n");
}
}
}
int main() {
int choice;
while (1) {
printf("Please select:size(data{1}, 1)
x1 = data{1}(i);
y1 = data{2}(i);
the operation:\n");
printf("1. Add book\n");
printf("2. Delete book\n");
printf("3. Modify book\n");
printf("4. Query book\n");
printf("5. Add category\n");
printf("6. Delete z1 = data{3}(i);
x2 = data{4}(i);
y2 = data{5}(i);
category\n");
printf("7. Modify category\n");
printf("8. Query category\n");
printf("9. Borrow book z2 = data{6}(i);
gateway1 = [gateway1; x1, y1, z1];
gateway\n");
printf("10. Return book\n");
printf("11. Query borrow record\n");
printf("0. Exit\n2 = [gateway2; x2, y2, z2];
end
fclose(file);
else
error('Error");
printf("Your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case
阅读全文