档案管理
题目描述
X老师管理着学校的档案室,经常会有其他的老师来档案室存文件和取文件,每次有老师来取文件时,X老师就要在档案室里苦苦的找文件。你是X老师的学生,得知了X老师找文件很辛苦后,想为X老师写个文件管理程序,这对学计算机专业的你来说只是小Case。你的程序的功能有:
1 file1 //.给新文件file1添加一个新的编号
2 file2 //根据输入的文件名file2输出其相应的文件编号。
输入
先输入一个整数n。0<n<101。代表有n次操作,接下来有n行,每行有一个整数(1或2),表示相应的操作(添加或查找),和一个文件名(由小写字母组成,长度不超过20),表示相应的操作对象。测试数据保证同一个文件不会被添加两次
输出
对于每个2型操作输出一行,即被查找文件相应的编号,若文件不存在则输出”The file does not exist”。
样例输入
5
1 xueshengziliao
2 abcd
2 xueshengziliao
1 abcd
2 abcd
样例输出
The file does not exist
1
2
写本题不是因为本题有什么特别的。而是为了记录自己发现了string的一个功能。即string x,y;可以直接对x与y进行对比,这样可以省去对比的操作。
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node
{
string s;
}word[1000];
int main()
{
int T;
cin >> T;
int n = 0;
while (T--)
{
int a;
cin >> a;
if (a == 1)
cin >> word[n++].s;
else
{
int flog = 0;
string b;
cin >> b;
for (int i = 0; i < n; i++)
if (word[i].s == b) //直接string对比即可
{
cout << i+1 << endl;
flog = 1;
break;
}
if(!flog)
cout << "The file does not exist" << endl;
}
}
return 0;
}





Comments NOTHING