The Dir Command
We all know the dos command "dir" . It can list the files in the computer . When we use the file
name with "?" , "*" as the parameter , it can list the files you need . Input SpecificationThere are several lines (at most 100) each contain a file name and terminated by a line containing '*' .
Then a line contains a integer N and followed N lines . Each line contain a command "dir" with parameter. Output SpecificationFor each "dir" print the result , each file name in a line in lower case . If nothing found print
"no such files" in a single line . Print a blank line after each result . Note that the command "dir" will
treat the capital letter and small letter as the same . Sample Inputautoexec.bat
boot.ini
bootfont.bin
config.sys
io.sys
msdos.sys
pagefile.sys
*
4
dir autoexec.bat
dir boot*.*
dir ??.sys
dir *.*
Sample Outputautoexec.bat
boot.ini
bootfont.bin
io.sys
autoexec.bat
boot.ini
bootfont.bin
config.sys
io.sys
msdos.sys
pagefile.sys
开始打算只用C的后来发现对输入的了解不够没办法了
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
void main()
{
char files[100][20];
int idx=0;
int idxf=0;
char ch;
scanf("%c",&ch);
while(ch!='*')
{
if(ch>='A'&&ch<='Z')
ch='a'+ch-'A';
files[idxf][idx++]=ch;
if(ch=='\n')
{
files[idxf][idx]='\0';
idx=0;
idxf++;
}
ch=getchar();
}
int n;
scanf("%d\n",&n);
for(int i=0;i<n;i++)
{
char com[20];//输入的命令保存在com
cin>>com;//滤掉dir
cin>>com;
for(int j=0;j<strlen(com);j++)
if(com[j]>='A'&&com[j]<='Z')
com[j]='a'+com[j]-'A';
bool found=false;
for(int j=0;j<idxf;j++)
{
int p=0,q=0;//p指向当前一个文件名中字字符,q指向命令中的字符
L1:if(files[j][p]==com[q])//字符相等,pass
{
p++;q++;
goto L1;
}
if(files[j][p]=='\n'&&strlen(com)==q)//两个字符串同时到头了,ok
{
printf("%s",files[j]);
found=true;
continue;
}
if(com[q]=='*')//'*'的匹配
{
if(com[q+1]=='\0')//如果*是命令的最后一个字符,ok
{
cout<<files[j];
found=true;
continue;
}
while(p<strlen(files[j])&&files[j][p]!=com[q+1])//匹配多个字符,直到有相同字符
p++;
q++;
goto L1;
}
if(files[j][p]!='\n'&&com[q]=='?')//匹配?,要保证文件名没到结束
{
p++;q++;
goto L1;
}
}
if(!found)
printf("no such files\n");
cout<<endl;
}
}