我想要的只是用户输入的foo数组,并从其左右打印操作。
我尝试添加以下代码,但无法获得相同的结果。
我不是写char foo [29] = "1021+2551";
而是这样写:
int i;
printf("Enter the number and opperator");
for(i=0; i<1; i++) // I used i < 1 b/c I want to get only one line input
{
scanf("%c", foo[i]);
}
void main(){
int index;
char foo[29] = "1021+2551";
int len = strlen(foo);
for (int i=0; i < len; i++)
{
if(foo[i] == '+' || foo[i] == '-' || foo[i] == '*' || foo[i] == '/' || foo[i] == '%'){
char op = foo[i];
printf("%c", op);
index = i;
}
}
char left;
for(int j=0; j < index; j++){
left = printf("%c",foo[j]);
}
char right;
for(int k=index + 1; k < len; k++){
right = printf("%c",foo[k]);
}
}
如果我在用户输入中输入100 + 200,则结果应为
+100200
答案 0 :(得分:2)
简单而又快速的事情:)
int main(){
int i;
char foo[29];
printf("Enter the number and opperator\n");
fgets(foo, 29 , stdin);
int index;
int len = strlen(foo);
for (int i=0; i < len; i++)
{
if(foo[i] == '+' || foo[i] == '-' || foo[i] == '*' || foo[i] == '/' || foo[i] == '%'){
char op = foo[i];
printf("%c", op);
index = i;
}
} printf(" ");
char left;
for(int j=0; j < index; j++){
left = printf("%c",foo[j]);
} printf(" ");
char right;
for(int k=index + 1; k < len; k++){
right = printf("%c",foo[k]);
}
return 0;
}