C语言 #include <stdio.h> #include <stdlib.h> #include <time.h> int Random() { int r; srand(rand()+time(NULL)); r=rand()%4+1; }//随机数生成 int* Jud(const char map[][10],int x,int y) { static int j[4]={1}; if(map[x][y-1]!='*'||y==0) j[0]=0;//left if(map[x][y+1]!='*'||y==9) j[1]=0;//right if(map[x-1][y]!='*'||x==0) j[2]=0;//up if(map[x+1][y]!='*'||x==9) j[3]=0;//down return j; }//执行判断 void Print(char map[][10]) { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) printf("%c ",map[i][j]); printf("\n"); } }//输出地图 int main() { char map[10][10]; int i,j,m;//x接受随机值 for(i=0;i<10;i++) { for(j=0;j<10;j++) map[i][j]='*'; }//初始化 map[0][0]='A';//第一个是A //Print(map); int x=0,y=0;//记录坐标 int end=1;//标志结束 char c='A'; while(1) { int* j; j=Jud(map,x,y); while(1) { int m=Random(); if(!j[0]&&!j[1]&&!j[2]&&!j[3]) end=0; break; if(!j[m-1]) continue; else break; } switch(m) { case 1: map[x][--y]=c++; break; case 2: map[x][++y]=c++; break; case 3: map[--x][y]=c++; break; case 4: map[++x][y]=c++; break; } if(end=0||c=='Z') Print(map); break; } return 0; }查错
时间: 2024-02-01 21:04:07 浏览: 130
在代码中,有几个问题需要修正:
1. 在 Random 函数中,虽然使用了 srand 函数进行随机数种子的初始化,但是在生成随机数时却没有调用 rand 函数,应该加上如下代码:
```
r = rand() % 4 + 1;
```
2. 在 Jud 函数中,虽然返回了 j 数组的指针,但是在函数内部没有对 j 数组进行初始化,应该加上如下代码:
```
for (int k = 0; k < 4; k++) {
j[k] = 1;
}
```
3. 在主函数中,while 循环的条件语句中,使用了错误的赋值操作符“=”,应该改为条件判断符“==”:
```
if (end == 0 || c == 'Z')
```
4. 在 while 循环中,判断语句的括号不正确,应该改为如下代码:
```
while (end && (j[0] || j[1] || j[2] || j[3]))
```
修改后的代码如下:
相关问题
Write a program to simulate a process of translation from a logical address to physical address. Assumptions 1. Assume the file la.txt includes the sequence of generated addresses from CPU. 2. Use a part of memory as backing store that store data for a process. 3. The backing store size is 128 bytes 4. The size of process p is 128 bytes. 5. The contents of p is included in a file pdata.bin which is a binary file. 6. Use a part of memory as RAM. The size of physical memory is 256 bytes, from 0 to 255. All the physical memory is available, allocating starting from beginning in sequence. That is, allocate frame 0 first, then frame 1, then frame 2…. 7. The size of a frame is 32 bytes, i.e., 5 bits for the offset in a frame, total number of frames is 8. 8. At beginning, no page table is available for process p. Requirements Write a program to 1. Setup a simulating backing store in memory. Read the data from pdata.bin to this backing store. 2. Initialize a page table for process p, set the frame number to be -1 for each page, indicating that the page is not loaded into memory yet. 3. Read logical addresses one by one from la.txt. 4. For each logical address, a) if its page has been loaded into physical memory, simply find the frame number in the page table, then generate physical address, find and print out the physical address and data inside this address. b) if the page is used for the first time, i.e., in page table, its frame number is -1,then the page that contains this address should be loaded into a free frame in physical memory (RAM). Then update the page table by adding the frame number to the right index in the page table. Then repeat 4a). Refer to Figure 1 for the relationships and how physical memory, backing store, and CPU are simulated. Figure 1 How physical memory, backing store and CPU are simulated in this program assignment Hints: a) use a memory block pointed by a pointer or use an array as a simulation of backing store b) use functions fread or mmap for the binary file read. Search through the Internet for the usage of these functions. c) Use an array to simulate the memory. d) Use bit operators &, |, <<, and >> to get the bits in a logic address or form a physical address e) Use char for the type of data in the process, use unsigned char (8 bits) for the type of address. Coding & Submission 1. Using pure C to finish this program. 2. Put all the codes in one .c file named PA3_#####.c, replace “#####” as the last 5 digits of your student ID. 3. Put pdata.txt and la.txt in the same folder as PA3_#####.c, which the need .txt file can be open directly by filename instead of absolute path. 4. Submit only the .c file mentioned above.使用C语言完成
```
#include <stdio.h>
#include <stdlib.h>
#define BACKING_STORE_SIZE 128
#define PAGE_SIZE 32
#define NUM_PAGES 4
#define NUM_FRAMES 8
#define FRAME_SIZE 32
#define PHYSICAL_MEMORY_SIZE NUM_FRAMES * FRAME_SIZE
int page_table[NUM_PAGES];
unsigned char backing_store[BACKING_STORE_SIZE];
unsigned char physical_memory[PHYSICAL_MEMORY_SIZE];
void load_page(int page_num, int frame_num) {
int byte_offset = page_num * PAGE_SIZE;
int frame_offset = frame_num * FRAME_SIZE;
for (int i = 0; i < PAGE_SIZE; i++) {
physical_memory[frame_offset + i] = backing_store[byte_offset + i];
}
}
int main() {
FILE *la_file = fopen("la.txt", "r");
FILE *pdata_file = fopen("pdata.bin", "rb");
if (la_file == NULL || pdata_file == NULL) {
printf("Error opening file\n");
return 1;
}
// populate backing store with process data
fread(backing_store, sizeof(unsigned char), BACKING_STORE_SIZE, pdata_file);
// initialize page table
for (int i = 0; i < NUM_PAGES; i++) {
page_table[i] = -1;
}
// read logical addresses from file
int logical_address;
while (fscanf(la_file, "%d", &logical_address) != EOF) {
// calculate page number and offset
int page_num = logical_address / PAGE_SIZE;
int page_offset = logical_address % PAGE_SIZE;
// check if page is in memory
if (page_table[page_num] != -1) {
int frame_num = page_table[page_num];
int physical_address = (frame_num * FRAME_SIZE) + page_offset;
printf("Logical address: %d, Physical address: %d, Data: %c\n",
logical_address, physical_address, physical_memory[physical_address]);
} else {
// find a free frame in physical memory
int free_frame = -1;
for (int i = 0; i < NUM_FRAMES; i++) {
if (page_table[i] == -1) {
free_frame = i;
break;
}
}
// if no free frame is found, use a random one (for simplicity)
if (free_frame == -1) {
free_frame = rand() % NUM_FRAMES;
page_table[free_frame] = -1;
}
// load page into free frame
load_page(page_num, free_frame);
page_table[page_num] = free_frame;
// print physical address
int physical_address = (free_frame * FRAME_SIZE) + page_offset;
printf("Logical address: %d, Physical address: %d, Data: %c\n",
logical_address, physical_address, physical_memory[physical_address]);
}
}
fclose(la_file);
fclose(pdata_file);
return 0;
}
```
使用c语言,import random # 生成随机数 num1 = random.randint(10, 99) num2 = random.randint(10, 99) # 判断用户输入是否合法 while True: try: guess = int(input('请输入两位数:')) if guess < 10 or guess > 99: raise ValueError('输入不合法,请重新输入') break except ValueError as e: print(e) # 判断是否中奖 if guess == num1 * 10 + num2 or guess == num2 * 10 + num1: print('you win 1000') elif guess % 10 == num1 % 10 or guess % 10 == num2 % 10 or guess // 10 == num1 // 10 or guess // 10 == num2 // 10: print('you win 500') elif guess % 10 == num1 % 10 or guess % 10 == num2 % 10 or guess // 10 == num1 // 10 or guess // 10 == num2 // 10: print('you win 100') else: print('you win nothing')
很抱歉,C语言中没有`import`和`random`库,需要使用其他方式生成随机数。以下是使用C语言实现生成随机数和判断中奖的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int num1, num2, guess;
// 生成随机数
srand(time(NULL));
num1 = rand() % 90 + 10;
num2 = rand() % 90 + 10;
// 判断用户输入是否合法
while (1) {
printf("请输入两位数:");
scanf("%d", &guess);
if (guess < 10 || guess > 99) {
printf("输入不合法,请重新输入\n");
} else {
break;
}
}
// 判断是否中奖
if (guess == num1 * 10 + num2 || guess == num2 * 10 + num1) {
printf("you win 1000\n");
} else if (guess % 10 == num1 % 10 || guess % 10 == num2 % 10 || guess / 10 == num1 / 10 || guess / 10 == num2 / 10) {
printf("you win 500\n");
} else if (guess % 10 == num1 / 10 || guess % 10 == num2 / 10 || guess / 10 == num1 % 10 || guess / 10 == num2 % 10) {
printf("you win 100\n");
} else {
printf("you win nothing\n");
}
return 0;
}
```
解释一下:首先使用`srand()`和`rand()`函数结合时间戳生成两个随机数num1和num2,然后在while循环中判断用户输入是否合法,如果不合法则继续循环,直到输入合法为止。接着根据题目的规则,分别判断用户是否中奖,输出相应的奖金。注意判断是否匹配数字的时候,需要将两个随机数的个位和十位分别进行比较,可以使用`%`和`/`运算符。
阅读全文