已存在文件out(包含联系人信息),out文件的联系人信息由姓名和电话号码组成编写程序完成一下功能:输入姓名,若文件中存在则输出该联系人信息;若不存在则输出“Not found”
时间: 2024-02-19 17:03:10 浏览: 14
好的,下面是使用 Python 语言编写的程序,用于实现您的需求:
```python
# 打开文件
with open('out', 'r') as f:
# 读取文件中的每一行
for line in f:
# 将每一行的姓名和电话号码分别读取出来
name, phone = line.strip().split()
# 判断该行的姓名是否与用户输入的相同
if name == input("请输入联系人姓名:"):
# 若相同,则输出该联系人的信息
print(f"{name}的电话号码是{phone}")
break
else:
# 若没有找到相应的联系人,则输出 Not found
print("Not found")
```
这个程序会首先打开名为 `out` 的文件,并逐行读取其中的内容。对于每一行,它会将其中的姓名和电话号码分别读取出来,并与用户输入的姓名进行比较。如果找到了相应的联系人,则输出其电话号码;如果没有找到,则输出 `Not found`。
相关问题
c语言,编写程序,从文件idcard.txt中读入若干个人的姓名和身份证号码,提取每个人的生日信息,将每个人的姓名和生日信息写入文件birthday.txt中
好的,这是一个比较简单的C语言编程任务,以下是一个可能的实现方式:
```c
#include <stdio.h>
int main()
{
FILE *in_file, *out_file;
char name[20], id[19];
int year, month, day;
// 打开输入文件
in_file = fopen("idcard.txt", "r");
if (in_file == NULL)
{
printf("无法打开输入文件!\n");
return 1;
}
// 打开输出文件
out_file = fopen("birthday.txt", "w");
if (out_file == NULL)
{
printf("无法打开输出文件!\n");
return 1;
}
// 逐行读取输入文件,提取生日信息,并写入输出文件
while (fscanf(in_file, "%s %s", name, id) == 2)
{
sscanf(id, "%4d%2d%2d", &year, &month, &day);
fprintf(out_file, "%s %04d-%02d-%02d\n", name, year, month, day);
}
// 关闭文件
fclose(in_file);
fclose(out_file);
printf("生日信息已提取并写入 birthday.txt 文件中!\n");
return 0;
}
```
该程序首先打开输入文件 `idcard.txt` 和输出文件 `birthday.txt`,然后逐行读取输入文件,每次读取一行中的姓名和身份证号码,并通过 `sscanf` 函数提取出身份证号码中的年、月、日信息。最后,将每个人的姓名和生日信息按照指定格式写入输出文件。最后,程序关闭文件并输出提示信息。
需要注意的是,本程序假定输入文件中每行仅包含一个人的姓名和身份证号码,且身份证号码的格式为 18 位数字。如果输入文件的格式与此不同,程序可能无法正常工作。
编写java程序用于记录球员的信息(队名、球员姓名、球员号码)和比赛进球数
好的,我可以帮您编写这个Java程序。以下是示例代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Player {
private String teamName;
private String playerName;
private int playerNumber;
private int goalsScored;
public Player(String teamName, String playerName, int playerNumber) {
this.teamName = teamName;
this.playerName = playerName;
this.playerNumber = playerNumber;
this.goalsScored = 0;
}
public void scoreGoal() {
this.goalsScored++;
}
public String getTeamName() {
return teamName;
}
public String getPlayerName() {
return playerName;
}
public int getPlayerNumber() {
return playerNumber;
}
public int getGoalsScored() {
return goalsScored;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Player> players = new ArrayList<>();
// 获取球员信息
System.out.print("请输入球员信息(队名、球员姓名、球员号码),输入exit结束:");
while (true) {
String input = scanner.nextLine();
if (input.equals("exit")) break;
String[] info = input.split(" ");
String teamName = info[0];
String playerName = info[1];
int playerNumber = Integer.parseInt(info[2]);
Player player = new Player(teamName, playerName, playerNumber);
players.add(player);
}
// 记录比赛进球数
System.out.print("请输入进球的球员号码,输入exit结束:");
while (true) {
String input = scanner.nextLine();
if (input.equals("exit")) break;
int playerNumber = Integer.parseInt(input);
boolean found = false;
for (Player player : players) {
if (player.getPlayerNumber() == playerNumber) {
player.scoreGoal();
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到对应号码的球员!");
}
}
// 输出球员信息和比赛进球数
for (Player player : players) {
System.out.println(player.getTeamName() + " " + player.getPlayerName() +
"(号码:" + player.getPlayerNumber() + ")的进球数为:" + player.getGoalsScored());
}
}
}
```
您可以在控制台中输入球员信息和比赛进球数,程序将记录球员信息和比赛进球数,并输出每个球员的进球数。注意,这只是一个简单的示例程序,您可以根据自己的需求进行修改和扩展。