C语言 字符串加减问题,请个位进来看看
我这个是用atoi函数转换的,我想把输入的数加起来得出结果,可是我输入的数是字符,所以输入的数得加个空格,如:23+3-10这样才能输出结果;但是我不想加空格也能输出结果...
我这个是用atoi函数转换的,我想把输入的数加起来得出结果,可是我输入的数是字符,所以输入的数得加个空格,如:23 +3 -10这样才能输出结果;但是我不想加空格也能输出结果,该怎么做
void recon(char arr[], int *value);
int main() {
char arr[250];
char ch = 0;
int value = 0;
int a = 0;
int c = 0 ;
int i = 0;
for (i = 0; i < 3; i++) {
scanf("%s", arr);
recon(arr, &value);
c += value;
}
printf("%d\n", c);
}
void recon(char arr[], int *value) {
char temporary[250];
char ch = 0;
size_t at = 0;
char c = 0;
bzero(temporary, sizeof(char) * 32);
if (arr[0] == '+' || arr[0] == '-') {
temporary[at++] = arr[0];
}
do {
ch = arr[at];
if (ch == '+' || ch == '-' || ch == 0) {
break;
} else {
temporary[at++] = ch;
}
} while (1);
*value = atoi(temporary);
} 展开
void recon(char arr[], int *value);
int main() {
char arr[250];
char ch = 0;
int value = 0;
int a = 0;
int c = 0 ;
int i = 0;
for (i = 0; i < 3; i++) {
scanf("%s", arr);
recon(arr, &value);
c += value;
}
printf("%d\n", c);
}
void recon(char arr[], int *value) {
char temporary[250];
char ch = 0;
size_t at = 0;
char c = 0;
bzero(temporary, sizeof(char) * 32);
if (arr[0] == '+' || arr[0] == '-') {
temporary[at++] = arr[0];
}
do {
ch = arr[at];
if (ch == '+' || ch == '-' || ch == 0) {
break;
} else {
temporary[at++] = ch;
}
} while (1);
*value = atoi(temporary);
} 展开
推荐于2016-07-07
展开全部
void recon(char arr[], int *value);
int main() {
char arr[250];
char ch = 0;
int value 搭弯坦= 0;
int a = 0;
int c = 0;
int i = 0;
gets(arr);
recon(arr, &value);
printf("%d\n", value);
}
void recon(char arr[], int *value) {
char temp[250];
char ch = 0;
size_t at = 0,i=0;
char c = 0;
int s = 0;
while 知桐(arr[i]!='\0'){
at = 0;
memset(temp, 0, sizeof(char)* 32);
if 闹橘(arr[i] == '+' || arr[i] == '-') {
temp[at++] = arr[i++];
}
do {
ch = arr[i];
if (ch == '+' || ch == '-' || ch == 0) {
break;
}
else {
temp[at++] = ch; i++;
}
} while (1);
s = atoi(temp);
*value += s;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询