#include<iostream>
#include<string.h>
using namespace std;
int length(char str[]){
return strlen(str);
}
int isletter(char a){
if(a >= 'a'&&a <= 'z'){
return 1;
}else{
return 0;
}
}
int isnum(char a){
if(a-'0' >= 0&&a-'0' <= 9){
return 1;
}else{
return 0;
}
}
int main(){
char inp[99999];
cin >> inp;
int pos = 0;
while(pos < length(inp)){
if(inp[pos] == ' '){
pos++;
continue;
}
if(isletter(inp[pos]) == 1){
char words[99999];
int wpos = 0;
while(isletter(inp[pos]) == 1){
words[wpos] = inp[pos];
pos++;
wpos++;
}
cout << words;
cout << endl;
continue;
}
if(isnum(inp[pos]) == 1){
char nums[99999];
int npos = 0;
while(isnum(inp[pos]) == 1){
nums[npos] = inp[pos];
pos++;
npos++;
}
cout << "nums:" << nums;
cout << endl;
continue;
}
}
int b;
cin >> b;
return 0;
}
这是做的一个分词器
运行结果却与预想不一样
没初始化nums,而nums复用了words的内存,打印下地址就知道是同一个了
不知道你这个问题是否已经解决, 如果还没有解决的话://定义一个指针指向int型数组
方法一:使用new方法 int * ledstate1 = new int[4];
方法二:
int led[4] = {0};//先声明一个数组
int * ledstate1=led;//将数组直接复制给指针
我作为一名IT专家,对于c++分词器调试有一些经验和建议:
需要注意的是,对于具体如何解决问题,需要先明确问题所在,才能有针对性地解决。如果我无法了解具体问题所在,就很难提供有效的解决方法。