两个代码之间的差异 [英] Difference between the two codes

查看:81
本文介绍了两个代码之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么第一个被拒绝(我写的)在我们学院的在线测试仪上,第二个被接受(我必须写什么,看起来完全错了)?

第一: pastebin.com/xsq6dLGY [ ^ ]

其次: pastebin.com/eM9i1FDG [ ^ ]



示例输入:

pastebin.com/eaLEz2gJ [ ^ ]



我尝试过:



我尝试了所有的样本输入,他们似乎给出了相同的结果,但我的第一个仍被拒绝。

解决方案

< blockquote>你已经解决了你认可的问题在第二个例子中找出违规行。

 sor = c  - '0'; 



它会去抛出除了最粗糙的编译器设置之外的所有警告。



错误是正确的,因为如果c很小,结果值可以变为负数,这对原始类型没有意义是一个char。在非专业术语中,你减去了两个橘子并得到了一个苹果。



你对这行代码有两个正确的修正。



1.)sor需要是一个覆盖char范围的无符号类型,所以unsigned char,unsigned int,unsigned long都可以工作。



2.)另一个解决方案是对减法进行IMPLICIT TYPECAST,这样编译器就知道你实际意味着要做到这一点并且如果发生则允许否定。



sor = (int)(C-'0');



你正在努力学习成为一名程序员,所以学习类型和规则。拒绝你的代码是正确的。任何人或uni lexx检查员都不清楚你知道自己在做什么并且已经解决了这个问题。在错误的情况下,有很多代码可能出错。



它告诉我的是你在非常宽松的环境下运行你的编译器而我建议你将错误级别调高到最大值,并且实际上很麻烦地删除被抛出的警告。这些警告包含危险,在学生环境中,特别是提交的代码应包含零警告。



为了记录,检查员永远不会挑选需要真正智能的malloc /免费问题代码在运行时所做的事情。我们使用特定工具进行内存泄漏,没有任何工具是完美的,因为顺序和时间可能很重要。所以也许学会不要嘲笑你不理解的东西。


Can someone tell me why is the first one gets rejected (what i wrote) on our college's online tester, and the second gets accepted (what i had to write and seems totally wrong) ?
First: pastebin.com/xsq6dLGY[^]
Second: pastebin.com/eM9i1FDG[^]

Sample inputs:
pastebin.com/eaLEz2gJ[^]

What I have tried:

I tried them with all the sample inputs, and they seemed to give the same results but my first one still gets rejected.

解决方案

You have already worked out the problem you commented the offending line out in the second example.

sor = c - '0';


Its going to throw warnings with all but the slackest compiler settings.

The error is correct because the result value can go negative if c is small, which has no meaning to the original type being a char. In laymans term you subtracted two oranges and got an apple.

You have two correct fixes to that line of code.

1.) sor need to be an unsigned type that covers the range of char so unsigned char, unsigned int, unsigned long would all work.

2.) The other solution is to IMPLICIT TYPECAST the subtraction so the compiler knows you actually mean to do that and have allowed for a negative if it occurs.

sor = (int)(C-'0');

You are trying to learn to be a programmer so learn types and there rules. The rejection of your code was correct on every ground. It isn't clear to anyone or the uni lexx checker that you know what you are doing and have accounted for it. There is a lot that can go wrong with that line of code in the wrong situation.

What it tells me is you are running your compiler on very loose settings and I suggest you turn the error level up to max and actually bother to remove the warnings that get thrown up. Those warnings contain dangers and in the student setting especially code submitted should contain zero warnings.

For the record the checker will never pick off malloc/free issues that requires real intelligence of what the code is doing when it runs. There are specific tools we use for memory leak and no tool is perfect at it because the order and timings can matter. So perhaps learn not to scoff at something you don't understand.


这篇关于两个代码之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆