数组分割问题及存储问题
本帖最后由 littleTD 于 2013-06-20 09:22:38 编辑 昨天很感谢本版网友的帮助 所以重新写了一次user constructor
这次是数组分割的问题了
完整程序 请见http://bbs.csdn.net/topics/390493562?page=1#post-394819057
polynomial.cpp中user constructor重写过了
诚心求教
Polynomial::Polynomial( const char*up){
int coef= 0;
int power=0;
int temp = 0;
int sig = 1;
locale loc;
char *aft = new char[5];
char *bef = new char[5];
char* cur = new char [30];//pointer to the items
char* po = strdup(up);//copy the string
// strdup is different with strcpy
//set coefficients
for(int k = 0; k<10; k++){
coefficients_[k]=0;
}
//cutting the array then get coefficients
for(int j = 0; j<30; j++){
cur = strtok(po, " ");
//get the real size of array
for(int z = 0; z < 30; z++){
if(cur[z]!= '\0'){
temp += 1;
}else{
break;
}
}//end counter cur size
//print as the out file
if(*cur != '+'|| *cur != ' ' || *cur == '-'){
po = po + temp;
}else{
cout << "term is " << cur <<endl;
}//end print
for(int i = 0; i<10; i++){
if(*cur == '-'||' -'||'- '||' - '){
sig = -1;
cur ++;
}//set the signal for negative item
if(isalpha(*cur,loc)){
while(*cur != 'x'){
bef = cur;//get the number befor x
}
if(*cur == 'x'){
cur ++;//move to next element
aft = cur;//get the number after x
}
coef = atoi(bef);
power = atoi(aft);
coefficients_[power] = coef * sig;
}
else if(isdigit(*cur,loc)){//no x signal
power = 0;
bef = cur;
coef = atoi(bef);
coefficients_[power] = coef * sig;
}
}
//po = po+temp;//move the checked item
}
}//end user constructor
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
char *string,
const char *control1,//连续出现时视为中间夹空token
const char *control2 //连续出现时视为中间无空token
)
{
unsigned char *str;
const unsigned char *ctrl1 = (const unsigned char *)control1;
const unsigned char *ctrl2 = (const unsigned char *)control2;
unsigned char map1[32],map2[32];
static char *nextoken;
static char flag=0;
unsigned char c;
int L;
memset(map1,0,32);
memset(map2,0,32);
do {
map1[*ctrl1 >> 3]
[解决办法]
= (1 << (*ctrl1 & 7));
} while (*ctrl1++);
do {
map2[*ctrl2 >> 3]
[解决办法]
= (1 << (*ctrl2 & 7));
} while (*ctrl2++);
if (string) {
if (control2[0]) {
L=strlen(string);
while (1) {
c=string[L-1];
if (map2[c >> 3] & (1 << (c & 7))) {
L--;
string[L]=0;
} else break;
}
}
if (control1[0]) {
L=strlen(string);
c=string[L-1];
if (map1[c >> 3] & (1 << (c & 7))) {
string[L]=control1[0];
string[L+1]=0;
}
}
str=(unsigned char *)string;
}
else str=(unsigned char *)nextoken;
string=(char *)str;
while (1) {
if (0==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
string++;
str++;
} else {
flag=1;
str++;
}
} else if (1==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=0;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=2;
break;
} else str++;
} else {//2==flag
if (!*str) return NULL;
if (map1[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
flag=0;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
} else {
string=(char *)str;
str++;
flag=1;
}
}
}
nextoken=(char *)str;
if (string==(char *)str) return NULL;
else return string;
}
void main()
{
strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some more tokens, ");
strcpy(seps1,",\n");strcpy(seps2," \t");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234
[解决办法]
LIYI
[解决办法]
China
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK");
strcpy(seps1,"
[解决办法]
");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK");
strcpy(seps1,"");strcpy(seps2,"
[解决办法]
");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
------解决方案--------------------
OK");
strcpy(seps1,"
[解决办法]
");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,a,,b,,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,,");
strcpy(seps1,",");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
}
//
//[A string of ,,tokens
//
//and some more tokens,]
//Tokens:
// <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>,
//[1234
[解决办法]
LIYI
[解决办法]
China
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK]
//Tokens:
// <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>,
//[1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK]
//Tokens:
// <1234>, <LIYI>, <010>, <201110260000>, <OK>,
//[1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK]
//Tokens:
// <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>,
//[a]
//Tokens:
// <a>,
//[a,b]
//Tokens:
// <a>, <b>,
//[a,,b]
//Tokens:
// <a>, <>, <b>,
//[,a]
//Tokens:
// <>, <a>,
//[a,]
//Tokens:
// <a>, <>,
//[,a,,b]
//Tokens:
// <>, <a>, <>, <b>,
//[,,a,,b,,]
//Tokens:
// <>, <>, <a>, <>, <b>, <>, <>,
//[,]
//Tokens:
// <>, <>,
//[,,]
//Tokens:
// <>, <>, <>,
//[,,,]
//Tokens:
// <>, <>, <>, <>,
4.In the Number Of Elements text box on the Data tab, type 1.
5.Click OK to set the breakpoint on myArray [0].
Setting a Breakpoint When the Initial Element of an Array Has a Specific Value
To break when the initial element of an array has a specific value
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type an expression containing the initial element of the array (myArray[0]==1, for example).
4.In the Number Of Elements text box, type 1.
5.Click OK to set the breakpoint on myArray [0].
Setting a Breakpoint When a Particular Element of an Array Changes Value
To break when a particular element of an array changes value
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type the element of the array (myArray[12], for example).
4.In the Number Of Elements text box, type 1.
5.Click OK to set the breakpoint on myArray [12].
Setting a Breakpoint When Any Element of an Array Changes Value
To break when any element of an array changes value
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type the first element of the array (myArray[0]).
4.In the Number Of Elements text box, type the number of elements in the array.
5.Click OK to set the breakpoint on myArray.
Setting a Breakpoint When Any of the First n Elements of an Array Change Value
To break when any of the first n elements of an array change value
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type the first element of the array (myArray[0], for example).
4.In the Number Of Elements text box, type n (for example, 10).
5.Click OK to set the breakpoint on myArray[0] through myArray[9].
Setting a Breakpoint When the Location Value of a Pointer Changes
To break when the location value of a pointer changes
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type the pointer variable name (p, for example).
4.Click OK to set the breakpoint.
Setting a Breakpoint When the Value at a Location Pointed to Changes
To break when the value at a location pointed to changes
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type the dereferenced pointer variable name (*p or p->next, for example).
4.Click OK to set the breakpoint.
Setting a Breakpoint When an Array Pointed to by a Pointer Changes
To break when an array pointed to by a pointer changes
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type the dereferenced pointer variable name (*p, for example).
4.In the Number Of Elements text box, type the length of the array in elements. For example, if the pointer is a pointer to double, and the array pointed to contains 100 values of type double, type 100.
5.Click OK to set the breakpoint.
Setting a Breakpoint When the Value at a Specified Memory Address Changes
To break when the value at a specified memory address changes
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type the memory address for the byte.
For a word or doubleword memory address, enclose the address in parentheses, and precede it with a cast operator. For example, WO(21406036) for the word at memory location 21406036. Use the cast operator BY for a byte (optional), WO for a word, or DW for a doubleword. (The debugger interprets all integer constants as decimal unless they begin with zero (0) for octal or zero and x (0x) for hexadecimal.)
4.In the Number Of Elements text box, type the number of bytes, words, or doublewords to monitor. If you used the BY operator in the Expression field, specify the number of bytes. If you used WO, specify the number of words. If you used DW, specify the number of doublewords.
5.Click OK to set the breakpoint.
Setting a Breakpoint When a Register Changes
To break when a register changes
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type a register mnemonic, such as CS.
4.In the Number Of Elements text box, type the number of bytes to monitor.
5.Click OK to set the breakpoint.
Setting a Breakpoint When a Register Expression Is True
To break when a register expression is true
1.From the Edit menu, click Breakpoints.
2.Click the Data tab of the Breakpoints dialog box.
3.In the Expression text box, type an expression that contains a boolean comparison operator, such as CS==0.
4.In the Number Of Elements text box, type the number of bytes to monitor.
5.Click OK to set the breakpoint.
Note When you set a data breakpoint, the debugger places the variable or variables used into a special debug register, if possible. The number of debug registers is limited. (Intel 80386 and later CPUs provide four debug registers. Motorola 680X0 and PowerPC chips have no debug registers.) Furthermore, stacked-based variables (parameters) cannot be placed into debug registers. If a breakpoint variable cannot be placed into a debug register, the debugger must examine the variable’s memory location after every instruction to determine whether the contents have changed. These extra memory accesses reduce execution speed of the program with the debugger. In some cases, the program may appear to hang. Performance may be especially slow if you are debugging a remote application.
[解决办法]
VS20XX:
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]
srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
b[(a=rand()%11)]=0;
Sleep(100);
b[(a=rand()%11)]=1;
Sleep(100);
b[(a=rand()%11)]=2;
Sleep(100);
b[(a=rand()%11)]=3;
Sleep(100);
b[(a=rand()%11)]=4;
Sleep(100);
}
return 0;
}