需要帮助来确定输入的字符是符号,数字还是字母

时间:2017-07-26 09:50:46

标签: c++ switch-statement

我们的教授要求我们制作一个程序来确定输入的字符是符号,数字还是字母。有没有办法将if-else语句转换为PURE switch语句。我一直想知道如何。

#include <iostream>

using namespace std;
main()
{
    char a;
    cout << "Enter a single character: " ;
    cin >> a;
    switch ((a >= 65 && a <= 90) || (a >= 97 && a <= 122)) //ASCII Value 65-90 (capital letters), 97-122 (small letters)
    {
        case 1:
            cout << "You entered a letter!";
            break;
        case 0:
            if (a >= 48 && a <= 57 )    //ASCII Value 48-57 (num 0-9)
            {
                cout << "You entered a number!" << endl;
            }
            else
            {
                cout << "You entered a symbol!" << endl;
            }
            break;
    }
    return 0;
}

4 个答案:

答案 0 :(得分:1)

可能是那样的?您可以将编译时反射添加为exercise =)

#wrapper
{
	background: rgba(162,162,162,.1);
}

#banner-wrapper
{
	overflow: hidden;
	padding: 3em 0em;
	background: #E24E2A;
}

#banner
{
	overflow: hidden;
  /* set width to 100% */
	width: 98%;
  /* delete the padding */
  /* added margin */
  margin :0 1%;
	text-align: center;
	color: rgba(255,255,255,.7);
	border-bottom: 2px solid #E3E3E3;
}

#banner a
{
	color: rgba(255,255,255,.9);
}

#banner .box-left
{
	float: left;
}

#banner .box-right
{
	float: right;
}

#banner h2
{
	margin: 0em;
	padding: 0em;
	font-weight: 400;
	font-size: 1.6em;
	color: #F8F8FF;
}

#banner span
{
	display: block;
	padding-top: 0.20em;
	text-transform: uppercase;
	font-size: 1.2em;
	color: #A2A2A2;
}

.carousel {
    position:relative;
    box-shadow: 0px 1px 6px rgba(0, 0, 0, 0.64); 
    margin-top: 10px;
	margin-bottom: 10px;
}

.carousel-inner {
    position: relative;
    overflow: hidden;
	
}

.carousel-open:checked + .carousel-item {
    position: static;
    opacity: 100;
}

.carousel-item {
    position: absolute;
    opacity: 0;
    -webkit-transition: opacity 0.6s ease-out;
    transition: opacity 0.6s ease-out;
}

.carousel-item img {
    display: block;
    height: auto;
    max-width: 100%;
}

.carousel-control {
    background: rgba(0, 0, 0, 0.28);
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    display: none;
    font-size: 40px;
    height: 40px;
    line-height: 35px;
    position: absolute;
    top: 50%;
    -webkit-transform: translate(0, -50%);
    cursor: pointer;
    -ms-transform: translate(0, -50%);
    transform: translate(0, -50%);
    text-align: center;
    width: 40px;
    z-index: 10;
}

.carousel-control.prev {
    left: 2%;
}

.carousel-control.next {
    right: 2%;
}

.carousel-control:hover {
    background: rgba(0, 0, 0, 0.8);
    color: #aaaaaa;
}

#carousel-1:checked ~ .control-1,
#carousel-2:checked ~ .control-2,
#carousel-3:checked ~ .control-3 {
    display: block;
}

.carousel-indicators {
    list-style: none;
    margin: 0;
    padding: 0;
    position: absolute;
    bottom: 2%;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 10;
}

.carousel-indicators li {
    display: inline-block;
    margin: 0 5px;
}

.carousel-bullet {
    color: #fff;
    cursor: pointer;
    display: block;
    font-size: 35px;
}

.carousel-bullet:hover {
    color: #aaaaaa;
}

#carousel-1:checked ~ .control-1 ~ .carousel-indicators li:nth-child(1) .carousel-bullet,
#carousel-2:checked ~ .control-2 ~ .carousel-indicators li:nth-child(2) .carousel-bullet,
#carousel-3:checked ~ .control-3 ~ .carousel-indicators li:nth-child(3) .carousel-bullet {
    color: #428bca;
}

#title {
    width: 100%;
    position: absolute;
    padding: 0px;
    margin: 0px auto;
    text-align: center;
    font-size: 27px;
    color: rgba(255, 255, 255, 1);
    font-family: 'Open Sans', sans-serif;
    z-index: 9999;
    text-shadow: 0px 1px 2px rgba(0, 0, 0, 0.33), -1px 0px 2px rgba(255, 255, 255, 0);
}

UPD:从评论中添加其他解决方案

答案 1 :(得分:0)

switch-statements用于常量而不是条件。 因此,如果您真的只想使用一个开关盒,那么您必须涵盖所有可能的情况:

switch (a) 
{
  case 65:
  case 66:
  case 67:
  ...
  case 90:
  case 97:
  ...
  case 122:
    cout << "You entered a letter!";
    break;
  case 48:
  ...
}

在案例中没有break的情况下,程序将会通过&#34;直到包含break的第一个案例。然而,这写起来非常繁琐,而且不易阅读。最好的方法是使用if-else语句,如:

if if (a >= 48 && a <= 57)
{
  // number
} 
else ((a >= 65 && a <=90) || (a >= 97 && a <= 122)) 
{ 
  // letter
}
else
{
  // other symbol
}

答案 2 :(得分:0)

声明

我完全赞同@ muXXmit2X评论,但为了好玩,我写了a script,它生成了仅限开关的检查(从不像这样编码,这很糟糕

有时发电机在现实世界中很有用,但要小心。

#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    char a;
    cout << "Enter a single character\n";
    cin >> a;

    switch (a)
    {
        case 48: 
        case 49: 
        case 50: 
        case 51: 
        case 52: 
        case 53: 
        case 54: 
        case 55: 
        case 56: 
        case 57: cout << "You entered a number!"; break;
        case 65: 
        case 66: 
        case 67: 
        case 68: 
        case 69: 
        case 70: 
        case 71: 
        case 72: 
        case 73: 
        case 74: 
        case 75: 
        case 76: 
        case 77: 
        case 78: 
        case 79: 
        case 80: 
        case 81: 
        case 82: 
        case 83: 
        case 84: 
        case 85: 
        case 86: 
        case 87: 
        case 88: 
        case 89: 
        case 90: cout << "You entered a low-case letter!"; break;
        case 97: 
        case 98: 
        case 99: 
        case 100: 
        case 101: 
        case 102: 
        case 103: 
        case 104: 
        case 105: 
        case 106: 
        case 107: 
        case 108: 
        case 109: 
        case 110: 
        case 111: 
        case 112: 
        case 113: 
        case 114: 
        case 115: 
        case 116: 
        case 117: 
        case 118: 
        case 119: 
        case 120: 
        case 121: 
        case 122: cout << "You entered a capital letter!"; break;
        default: cout << "You entered a symbol!"; break;
    }
    return 0;
}
PS:你的教授是虐待狂。

PPS:你应该熟悉common code styles并选择一个你喜欢的,很难阅读你的代码。

答案 3 :(得分:0)

您可以避免切换案例,可以按照以下方式进行操作

main()
{
char a;
cout << "Enter a single character: " ;
cin >> a;
if(isdigit(a))
cout<<"You entered a number!"<<endl;
else{
cout << "You entered a symbol!" << endl;
}
}