2091088157xd @ 2022-02-16 23:38:16
我代码在通过第二个测试点时,我发现加法和减法会出错,但是乘法不会出错,这是为什么啊,有大哥了解吗
#include<stdio.h>
#include<string.h>
#include<math.h>
void func(int k1, char answer[12])
{
int i = 0, k2 = k1, n = 0;
while (k2 != 0)
{
k2 /= 10;
n++;
}
while (k1 != 0)
{
answer[n - 1 - i] = k1 % 10 + '0';
k1 /= 10;
i++;
}
answer[n] = '\0';
}
void fun(char m1[6], char m2[6], char x, char m4[12])//由于题设简单,没必要追求高精度
{
int answer, k1, k2, i;
k1 = atof(m1);
k2 = atof(m2);
if (x == 'a')
{
func(k2+k1, m4);
}
else if (x == 'b')
{
if (k1 > k2)
{
func(k1 - k2, m4);
}
else if (k1 < k2)
{
func(k2 - k1, m4);
char m5[12]="-";
strcat(m5, m4);
strcpy(m4, m5);
strcpy(m5, "");
}
else {
strcpy(m4, "0");
}
}
else if (x == 'c')
{
func(k1 * k2, m4);
}
}
int main()
{
int i, j, n,h[51];
char m1[6], m2[6], m3[6], answer[51][23] = { "1" }, mark, kkk[12];
scanf("%d", &i);
for (n = 0; n < i; n++)
{
scanf("%s", m1);
if (m1[0] != 'a' && m1[0] != 'b' && m1[0] != 'c')
{
scanf("%s", m2);
if (mark == 'a')
{
strcpy(answer[n], m1);
strcat(answer[n], "+");
strcat(answer[n], m2);
strcat(answer[n], "=");
fun(m2, m3, mark, kkk);
strcat(answer[n], kkk);
strcpy(kkk, "");
}
else if (mark == 'b')
{
strcpy(answer[n], m1);
strcat(answer[n], "-");
strcat(answer[n], m2);
fun(m2, m3, mark, kkk);
strcat(answer[n], "=");
strcat(answer[n], kkk);
strcpy(kkk, "");
}
else {
strcpy(answer[n], m1);
strcat(answer[n], "*");
strcat(answer[n], m2);
strcat(answer[n], "=");
fun(m2, m3, mark, kkk);
strcat(answer[n], kkk);
strcpy(kkk, "");
}
}
else {
mark = m1[0];//储存上次运算类型的标志
scanf("%s", m2);
scanf("%s", m3);
if (m1[0] == 'a')
{
strcpy(answer[n], m2);
strcat(answer[n], "+");
strcat(answer[n], m3);
strcat(answer[n], "=");
fun(m2, m3, m1[0], kkk);
strcat(answer[n], kkk);
strcpy(kkk, "");
}
else if(m1[0]=='b')
{
strcpy(answer[n], m2);
strcat(answer[n], "-");
strcat(answer[n], m3);
fun(m2, m3, m1[0], kkk);
strcat(answer[n], "=");
strcat(answer[n], kkk);
strcpy(kkk, "");
}
else {
strcpy(answer[n], m2);
strcat(answer[n], "*");
strcat(answer[n], m3);
strcat(answer[n], "=");
fun(m2, m3, m1[0], kkk);
strcat(answer[n], kkk);
strcpy(kkk, "");
}
}
}
for (n = 0; n < i; n++)
{
printf("%s\n%d\n", answer[n],strlen(answer[n]));
}
return 0;
}```
例子:2620*6343=16618660
18
216+1149=1365
13
7761-5655=2106
14
7168-3961=3207
14
9755+3461=13216
15
但计算结果却是
2620*6343=16618660
18
216+1149=1365
13
7761-5655=2106
14
7168-3961=-1694
15
9755+3461=13216
15
我快麻了,为什么加减法会出错啊
by ud2_ @ 2022-02-17 01:13:38
是会出问题的但概率极小可以忽略。更有可能是你的代码错了比如在缺符号时读入到 m1
和 m2
却用 m2
和 m3
参与运算导致在该算 7168 - 3961 时算了 3961 - 5655。
另外编译器说:
main.c: In function 'fun':
main.c:23:25: warning: unused variable 'i' [-Wunused-variable]
23 | int answer, k1, k2, i;
| ^
main.c:23:9: warning: unused variable 'answer' [-Wunused-variable]
23 | int answer, k1, k2, i;
| ^~~~~~
main.c: In function 'main':
main.c:132:22: warning: format '%d' expects argument of type 'int', but argument 3 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]
132 | printf("%s\n%d\n", answer[n],strlen(answer[n]));
| ~^ ~~~~~~~~~~~~~~~~~
| | |
| int size_t {aka long unsigned int}
| %ld
main.c:56:17: warning: unused variable 'h' [-Wunused-variable]
56 | int i, j, n,h[51];
| ^
main.c:56:12: warning: unused variable 'j' [-Wunused-variable]
56 | int i, j, n,h[51];
| ^
main.c:71:17: warning: 'mark' may be used uninitialized in this function [-Wmaybe-uninitialized]
71 | fun(m2, m3, mark, kkk);
| ^~~~~~~~~~~~~~~~~~~~~~
by 2091088157xd @ 2022-02-17 14:27:43
@ud2_ 谢谢你看完了我的代码,我今天上来就发现了这个问题,函数的参数我输入错误了,还是谢谢你看完了我的代码
by 2091088157xd @ 2022-02-17 14:28:48
@ud2_ 不过下面的编译问题是什么意思啊
by ud2_ @ 2022-02-18 00:25:22
@2091088157xd 下面是在编译命令里加入 -Wall
后的结果,意思是
strlen
的结果(size_t
)应该用 %zu
而不是 %d
输出。mark
可能没有初始化(虽然在这题中不会发生)其实输入时可以直接 scanf("%d%d")
。如果行首是字母那么它会只吃掉空格并返回 0,否则它能正常读入并返回 2(成功读了两个数)。据此判断有没有省略运算类型能极大简化代码。
by 2091088157xd @ 2022-02-18 14:10:06
@ud2_ 学到了,谢谢 感觉c语言难就难在输入输出,你说的这个规则我读了半年大学都不知道
by Constant @ 2022-04-07 20:43:52
@2091088157xd 难在输入输出?