为什么在第如果但不是第二减法工作? [英] Why does the subtraction work for the first if but not the second?

查看:128
本文介绍了为什么在第如果但不是第二减法工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每个if语句当前数组元素的ciphered_text的最后一行有26个从中减去。然而,这仅仅似乎是工作的第一部分,而不是在第二个。任何人都可以看到,为什么?顺便说一句我能是否应该使用否则,如果要不有人在这里告诉我。谢谢你。

 为(arrayelement = 0; arrayelement<大小; arrayelement ++)
    {
        ciphered_text [arrayelement] =(plain_text [arrayelement]);
        如果((int)的(ciphered_text [arrayelement])GT; = 65&放大器;及(INT)(ciphered_text [arrayelement])下; = 90)
        {
            ciphered_text [arrayelement] =(INT)(ciphered_text [arrayelement])+ Shift键;
            如果(ciphered_text [arrayelement]→90)
                ciphered_text [arrayelement] =(INT)(ciphered_text [arrayelement]) - 26日;        }否则如果((int)的(ciphered_text [arrayelement])GT; = 97&放大器;及(INT)(ciphered_text [arrayelement])下; = 122)
        {
            ciphered_text [arrayelement] =(INT)(ciphered_text [arrayelement])+ Shift键;
            如果(ciphered_text [arrayelement]≥122)
                ciphered_text [arrayelement] =(INT)(ciphered_text [arrayelement]) - 26日;
        }
    }


解决方案

我想 ciphered_text 的char [N] 的char *

失败的可能原因是那么

 (INT)(ciphered_text [arrayelement])+ Shift键;

大于127,当存储在字符 ciphered_element [arrayelement] ),它被转换为负值。 (注:超出范围的值转换为字符是实现定义,如果字符签署。)

最简单的解决将是该类型更改为 unsigned char型

On the last line of each if statement the current array element in 'ciphered_text' has 26 subtracted from it. However, this only appears to be working for the first section and not in the second. Can anyone see why? As an aside can anyone tell me whether I should be using else if or else here. Thanks.

    for(arrayelement = 0; arrayelement < size; arrayelement++)
    {
        ciphered_text[arrayelement] = (plain_text[arrayelement]);
        if ((int)(ciphered_text[arrayelement]) >= 65 && (int)(ciphered_text[arrayelement]) <= 90)
        {
            ciphered_text[arrayelement] = (int)(ciphered_text[arrayelement]) + shiftkey;
            if (ciphered_text[arrayelement] > 90)
                ciphered_text[arrayelement] = (int)(ciphered_text[arrayelement]) - 26;

        }

else if ((int)(ciphered_text[arrayelement])  >= 97 && (int)(ciphered_text[arrayelement]) <= 122)
        {
            ciphered_text[arrayelement] = (int)(ciphered_text[arrayelement]) + shiftkey;
            if (ciphered_text[arrayelement] > 122)
                ciphered_text[arrayelement] = (int)(ciphered_text[arrayelement]) - 26;
        }
    }

解决方案

I suppose ciphered_text is a char[N] or a char*?

The likely cause of failure is then that

(int)(ciphered_text[arrayelement]) + shiftkey;

is larger than 127, and when that is stored in a char (ciphered_element[arrayelement]), it is converted to a negative value. (Note: conversion of out-of-range values to char is implementation-defined if char is signed.)

The easiest fix would be to change the type to unsigned char.

这篇关于为什么在第如果但不是第二减法工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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