📄 keywordsstatic.cpp
字号:
#include <iostream>
using namespace std;
#include <fstream>
#include <string>
bool isLetter(char c)
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z') return true;
else return false;
}
int Search(char a[9][10],const char *b,int n)
{
int left=0;int right=n-1;int mid=0;
while(left<=right)
{
mid=(left+right)/2;
if(strcmp(a[mid],b)==0) return mid;
else if(strcmp(a[mid],b)<0) left=mid+1;
else right=mid-1;
}
return -1;
}
//源文件没有注释
int main()
{
int count[9]={0}; //存放关键字出现的次数
char A[9][10]={"break","char","default","else","for","int","return","switch","while"};
char filename[30];
//输入的时候一定要是...:/.../...格式
cout<<"请输入源程序代码.cpp 文件的绝对路径:"<<endl;
cin>>filename;
ifstream infile(filename,ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
abort();
}
char buffer[100]; //存放一行字符
while(!infile.eof())
{
infile.getline(buffer,100,'\n');
int len=strlen(buffer);
for(int i=0;i<len;i++)
{
if(isLetter(buffer[i]))
{
string str = "";
str +=buffer[i];
while(isLetter(buffer[++i]))
str +=buffer[i];
int r=Search(A,str.c_str(),9);
if(r!=-1) count[r]++;
}
}
}
infile.close();
for(int j=0;j<9;j++)
if(count[j])
{
cout.width(15);
cout<<A[j]<<": "<<count[j]<<endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -