【发布时间】:2012-09-12 08:45:05
【问题描述】:
我正在尝试编写一个对一串字符进行加减乘除的程序。我现在的程序是弄清楚如何将输入字符串分成两个字符串,然后执行适当的 +-/*。
输入应该是这样 abc+aaa
输出应该是 abc + aaa = bcd
如何将字符串转换为整数字符串?
#include <stdio.h>
#include <math.h>
#include <string.h>
int main() {
printf("This is a pseudo arithmetic program");
char input[10];
input[10] = '\0';
char first [9];
first[9] = '\0';
char last [9];
last[9] = '\0';
int i = 0;
int b;
int e;
while (input[0] != '0') {
if (input[0] == 0){
return -1;
}
printf("\nEnter a math problem in SOS format using only lowercase letters up to 9 characters");
printf("\nEx: abc+abc... type '0' to quit \n");
scanf("%s", input);
int x = 0;
x = strlen(input);
if (strchr(input, '+')){
for (i = 0; i <= x; i++) {
if (i == '+')
strncpy(first, &input[0], i-1);
i = 0;
}
for (i = x; i >= input[0]; i--) {
if (i == '+')
strncpy(last, &input[i], x);
i = 0;
}
printf("%s", first);
printf(" + ");
printf("%s", last);
printf(" = %d", first + last);
}
【问题讨论】:
-
那么输入字符串的基数是什么?看起来可能以 26 为底,带有 a=(第一个非零值)?如果是这样,那么零值是多少?可能我只是愚蠢地不知道 SOS 格式是什么,但 Google 似乎没有帮助。
-
只需将 2 个字符加起来,然后按字符减去
'a'。 -
我不确定你所说的基数是什么意思,但我相信 a-z 等于 0-25,而 A-Z 等于 26-41。所以如果程序要解决这个问题 a - a 那么答案应该是 Z。我认为根本不存在零值。
-
nhahtdh 你能详细说明一下吗?