acm.jlu.edu.cn-2005-The Dir Command

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 Specification

There 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 Specification

For 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 Input

autoexec.bat
boot.ini
bootfont.bin
config.sys
io.sys
msdos.sys
pagefile.sys
*
4
dir autoexec.bat
dir boot*.*
dir ??.sys
dir *.*

Sample Output

autoexec.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; } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章