public void IsDigit(String a, char[] b){ //是否是数字
while(m < a.length() && !Character.isSpace(b[m]) && b[m] != '(' &&b[m] != ')' &&
b[m] != '+' && b[m] != '*' && b[m] != '/' && b[m] != '>' &&b[m] != '<' && b[m] != '=' &&
b[m] != ',' &&b[m] != ';' || ((m < a.length() &&Character.isDigit(b[m])) || (m < a.length() &&
b[m] == '.') || (m < a.length() &&Character.isLetter(b[m])))){
lexical += String.valueOf(b[m]);
m++;
}
t2.setText(t2.getText() + lexical + "\t" + "数字" + "\t" + "3" + "\n");
lexical = "";
}
public void IsLetter(String a, char[] b){ //是否是字母
int i = 0;
while(m < a.length() && !Character.isSpace(b[m]) && b[m] != '(' &&
b[m] != ')' && b[m] != '+' && b[m] != '-' && b[m] != '*' &&
b[m] != '/' && b[m] != '>' && b[m] != '<' && b[m] != '=' &&
b[m] != ',' && b[m] != ';'){
lexical += lexical.valueOf(b[m]);
m++;
}
if(lexical.equals(blz[i]))
t2.setText(t2.getText() + lexical + "\t" + "保留字" + "\t" + "1" + "\n");
else
t2.setText(t2.getText() + lexical + "\t" + "标识符" + "\t" + "2" + "\n");
lexical = "";
}
public void Others(String a, char[] b){
if(b[m] == '+'){
m++;
if(m < a.length() && Character.isDigit(b[m])){
while((m < a.length() && Character.isDigit(b[m]) ) || (m < a.length() && b[m] == '.')){
lexical += lexical.valueOf(b[m]);
m++;
}
t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
}
else{
if(m < a.length() && b[m] == '+'){
t2.setText(t2.getText() + "++" + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
else{
m--;
t2.setText(t2.getText() + b[m] + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
//m++;
}
lexical = "";
}
else if(b[m] == '-'){
m++;
if(m < a.length() && Character.isDigit(b[m])){
while((m < a.length() && Character.isDigit(b[m]) ) ||(m < a.length() && b[m] == '.')){
lexical += lexical.valueOf(b[m]);
m++;
}
t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
}
else{
if(m < a.length() && b[m] == '-'){
t2.setText(t2.getText() + "--" + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
else{
m--;
t2.setText(t2.getText() + b[m] + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
//m++;
}
lexical = "";
}
else if(b[m] == '*'){
t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
else if(b[m] == '<'){
lexical += lexical.valueOf(b[m]);
m++;
if(b[m] == '='){
lexical += lexical.valueOf(b[m]);
t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
else t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
lexical = "";
}
else if(b[m] == '>'){
lexical += lexical.valueOf(b[m]);
m++;
if(b[m] == '='){
lexical += lexical.valueOf(b[m]);
t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
else t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
lexical = "";
}
else if(b[m] == '='){
lexical += lexical.valueOf(b[m]);
m++;
if(b[m] == '='){
lexical += lexical.valueOf(b[m]);
t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
else t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
lexical = "";
}
else if(b[m] == '!'){
lexical += lexical.valueOf(b[m]);
m++;
if(b[m] == '='){
lexical += lexical.valueOf(b[m]);
t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
m++;
}
else t2.setText(t2.getText() + lexical + "\t" + "运算符" + "\t" + "4" + "\n");
lexical = "";
}
else if(b[m] == ';' || b[m] == ',' || b[m] == '(' || b[m] == ')' || b[m] == '{' || b[m] == '}' || b[m] == '[' || b[m] == ']' ){
t2.setText(t2.getText() + b[m] + "\t" + "分隔符" + "\t" + "5" + "\n");
m++;
}
}
一、文件的复制
#include
#include
using namespace std;
int CopyFile(char *SourceFile,char *NewFile)
{
ifstream in;
ofstream out;
in.open(SourceFile,ios::binary);//打开源文件
if(in.fail())//打开源文件失败
{
cout<<"Error 1: Fail to open the source file."<
out.close();
return 0;
}
out.open(NewFile,ios::binary);//创建目标文件
if(out.fail())//创建文件失败
{
cout<<"Error 2: Fail to create the new file."<
in.close();
return 0;
}
else//复制文件
{
out<
in.close();
return 1;
}
}
void main()
{
char source[256],NewFile[256];
cout<<"请输入要复制的文件路径:"<
cout<<"请输入新文件的路径:"<
if(CopyFile(source,NewFile))
{
cout<<"文件已成功复制..."<
else
{
cout<<"文件复制失败..."<
cin.get();
cin.get();
}
二、文件的删除
#include
#include
#include
void main()
{
char source[256];//文件路径
cout<<"请输入要删除的文件路径:"<
/* _access(char *,int) 判断文件是否存在
存在 返回0;不存在 返回-1.
_access(const char *path,int mode)
mode的值:
00 是否存在
02 写权限
04 读权限
06 读写权限
*/
if(!_access(source,0))//如果文件存在:文件为只读无法删除
{
//去掉文件只读属性
SetFileAttributes(source,0);
if(DeleteFile(source))//删除成功
{
cout<
cin.get();
}
三 文件的重命名
#include
#include
#include
void main()
{
char source[256];//文件路径
char newname[256];
cout<<"请输入要重命名的文件路径:"<
cout<<"请输入文件的新名称:"<
if(!_access(source,0))//如果文件存在:
{
if(!rename(source,newname))//删除成功
{
cout<
else//无法重命名:文件打开或无权限执行重命名
{
cout<<"文件无法重命名(可能原因如下):"<
}
else//文件不存在
{
cout<
cin.get();
}
ifstream是文件读取类,用它来声明了对象in;ofstream是文件写入类,用它来声明了对象out。
打开文件
in.open(“文件名”),也可以在声明的时候就打开,如ifstream in(“文件名”);
用特定的方式打开文件
ifstream in(char* filename,int open_mode)
几种常见的打开方式:
ios::in – 打开一个可读取文件;
ios::out – 打开一个可写入文件;
ios::binary – 以二进制的形式打开一个文件;
ios::app – 写入的所有数据将被追加到文件的末尾;
ios::trunk – 删除文件原来已存在的内容;
ios::nocreate – 如果要打开的文件并不存在,那么以此参数调用open函数将无法进行;
ios::noreplece – 如果要打开的文件已存在,试图用open函数打开时将返回一个错误;
同时用几种模式打开文件的时候用“|”连接;
理解文件的操作就把文件当作你的电脑屏幕,ifstream类联想到cin,cin是将键盘中的数据写入到内存中,那么ifstream类就是将文件中的内容写入到内存,ofstream 也这样理解,所以数据从对象in流到x,再从x流到out对象。
最后要将文件关闭,in.closr()和out.close();