编写程序exp5_6.c请判断花瓶是谁打碎的。教师花瓶碎了班级只有四个小同学老师
时间: 2024-03-12 14:43:28 浏览: 21
我能理解您的问题。以下是一个简单的程序exp5_6.c,可以帮助您判断花瓶是谁打碎的。
```c
#include <stdio.h>
#include <string.h>
int main()
{
char culprit[20]; // 存储罪犯的名字
char suspects[4][20] = {"小明", "小红", "小刚", "小李"}; // 可疑人员名单
printf("请问谁打碎了花瓶?\n");
scanf("%s", culprit);
int i;
for (i = 0; i < 4; i++) {
if (strcmp(culprit, suspects[i]) == 0) { // 如果输入的名字在可疑人员名单中
printf("%s 是罪犯!\n", culprit);
break;
}
}
if (i == 4) { // 如果名字不在可疑人员名单中
printf("没有找到罪犯。\n");
}
return 0;
}
```
这个程序首先定义了一个字符数组 `culprit` 来存储罪犯的名字,以及一个二维字符数组 `suspects` 来存储可疑人员名单。然后程序会让用户输入罪犯的名字,接着使用 for 循环遍历可疑人员名单,查找输入的名字是否在名单中。如果找到了罪犯,程序会输出罪犯的名字;如果没有找到,程序会输出“没有找到罪犯”。
相关问题
(2) 请使用字节流的方式读出文件exp5_1.txt中内容,然后将文件中的所有奇数写入文件ex5_3.txt中,使用字符流的方式读出文件exp5_2.txt中内容,然后将文件中的所有偶数写入文件exp5_4.txt中。(exp4_2.java)
以下是exp5_2.java的代码实现:
```java
import java.io.*;
public class exp5_2 {
public static void main(String[] args) {
// 读取exp5_1.txt中的内容
try {
FileInputStream fileInputStream = new FileInputStream("exp5_1.txt");
FileOutputStream fileOutputStream = new FileOutputStream("exp5_3.txt");
int b;
while ((b = fileInputStream.read()) != -1) {
if (b % 2 == 1) {
fileOutputStream.write(b);
fileOutputStream.write(' ');
}
}
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 读取exp5_2.txt中的内容
try {
FileReader fileReader = new FileReader("exp5_2.txt");
FileWriter fileWriter = new FileWriter("exp5_4.txt");
int c;
while ((c = fileReader.read()) != -1) {
if (c % 2 == 0) {
fileWriter.write(c);
fileWriter.write(' ');
}
}
fileReader.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
程序使用字节流的方式读出文件exp5_1.txt中的内容,然后将文件中的所有奇数写入文件ex5_3.txt中;使用字符流的方式读出文件exp5_2.txt中的内容,然后将文件中的所有偶数写入文件exp5_4.txt中。
程序中使用了FileInputStream和FileOutputStream来读写文件,并使用FileReader和FileWriter来读写文本文件。在读取文件时,程序使用了while循环,并用read()方法读取文件的每个字节或字符,直到读到文件结尾为止。
请注意,这里程序并没有关闭文件流,实际应用中应该使用try-with-resources或手动关闭文件流来避免资源泄漏。
1.(1) 分别使用FileWriter 和 BufferedWriter 往文件exp5_1.txt和文件exp5_2.txt中写入1万个随机整数(数与数之间用空格隔开),并在控制台上显示程序运行的时间。(exp5_1.java) (提示:用时采用方法System.currentTimeMillis()求时间差。)2.(2) 请使用字节流的方式读出文件exp5_1.txt中内容,然后将文件中的所有奇数写入文件ex5_3.txt中,使用字符流的方式读出文件exp5_2.txt中内容,然后将文件中的所有偶数写入文件exp5_4.txt中。(exp4_2.java)3.(3) 请使用File类的相关方法,遍历(1)、(2)两题所读写的文件所在的目录下的所有文件。(exp5_3.java)
好的,你的第一个问题有三个小问题,我来一一回答。
1. 首先,使用FileWriter往文件exp5_1.txt中写入1万个随机整数(数与数之间用空格隔开)可以这样实现:
```java
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Exp5_1 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis(); // 记录开始时间
FileWriter fw = new FileWriter("exp5_1.txt");
Random random = new Random();
for (int i = 0; i < 10000; i++) {
fw.write(random.nextInt() + " ");
}
fw.close();
long end = System.currentTimeMillis(); // 记录结束时间
System.out.println("使用FileWriter写入文件exp5_1.txt用时:" + (end - start) + "ms");
}
}
```
使用BufferedWriter往文件exp5_2.txt中写入1万个随机整数(数与数之间用空格隔开)可以这样实现:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Exp5_1 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis(); // 记录开始时间
BufferedWriter bw = new BufferedWriter(new FileWriter("exp5_2.txt"));
Random random = new Random();
for (int i = 0; i < 10000; i++) {
bw.write(random.nextInt() + " ");
}
bw.close();
long end = System.currentTimeMillis(); // 记录结束时间
System.out.println("使用BufferedWriter写入文件exp5_2.txt用时:" + (end - start) + "ms");
}
}
```
2. 接着,使用字节流的方式读出文件exp5_1.txt中内容,然后将文件中的所有奇数写入文件exp5_3.txt中,使用字符流的方式读出文件exp5_2.txt中内容,然后将文件中的所有偶数写入文件exp5_4.txt中可以这样实现:
```java
import java.io.*;
public class Exp5_2 {
public static void main(String[] args) throws IOException {
// 读取exp5_1.txt中的所有奇数,写入exp5_3.txt中
FileInputStream fis = new FileInputStream("exp5_1.txt");
FileOutputStream fos = new FileOutputStream("exp5_3.txt");
int num;
while ((num = fis.read()) != -1) {
if (num % 2 != 0) { // 判断是否为奇数
fos.write(num);
}
}
fis.close();
fos.close();
// 读取exp5_2.txt中的所有偶数,写入exp5_4.txt中
FileReader fr = new FileReader("exp5_2.txt");
FileWriter fw = new FileWriter("exp5_4.txt");
int n;
while ((n = fr.read()) != -1) {
if (n % 2 == 0) { // 判断是否为偶数
fw.write(n);
}
}
fr.close();
fw.close();
}
}
```
3. 最后,使用File类的相关方法,遍历(1)、(2)两题所读写的文件所在的目录下的所有文件可以这样实现:
```java
import java.io.File;
public class Exp5_3 {
public static void main(String[] args) {
// 遍历exp5_1.txt所在目录下的所有文件
File file1 = new File("exp5_1.txt");
File dir1 = file1.getParentFile(); // 获取目录
System.out.println("exp5_1.txt所在目录下的所有文件:");
File[] files1 = dir1.listFiles(); // 获取目录下的所有文件
for (File f : files1) {
System.out.println(f.getName());
}
// 遍历exp5_2.txt所在目录下的所有文件
File file2 = new File("exp5_2.txt");
File dir2 = file2.getParentFile(); // 获取目录
System.out.println("exp5_2.txt所在目录下的所有文件:");
File[] files2 = dir2.listFiles(); // 获取目录下的所有文件
for (File f : files2) {
System.out.println(f.getName());
}
}
}
```
希望我的回答能够帮到你!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)