用于显示用户输入数据的程序是字母还是数字

时间:2017-10-07 19:12:53

标签: c

我在C中编写了以下代码。此代码帮助我们查找用户输入的字符是字母还是数字

$ python3 manage.py runserver
Performing system checks...

System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.

System check identified 1 issue (0 silenced).
October 07, 2017 - 23:23:06
Django version 1.11.4, using settings 'my_django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[07/Oct/2017 23:23:16] "GET /admin/ HTTP/1.1" 302 0
[07/Oct/2017 23:23:16] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1654
Internal Server Error: /admin/login/
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/sites.py", line 393, in login
    return LoginView.as_view(**defaults)(request)
  File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/views.py", line 90, in dispatch
    return super(LoginView, self).dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/views/generic/edit.py", line 183, in post
    return self.form_valid(form)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/views.py", line 119, in form_valid
    auth_login(self.request, form.get_user())
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/__init__.py", line 139, in login
    request.session.cycle_key()
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/sessions/backends/base.py", line 311, in cycle_key
    self.create()
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/sessions/backends/db.py", line 50, in create
    self._session_key = self._get_new_session_key()
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/sessions/backends/base.py", line 164, in _get_new_session_key
    if not self.exists(session_key):
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/sessions/backends/db.py", line 46, in exists
    return self.model.objects.filter(session_key=session_key).exists()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 670, in exists
    return self.query.has_results(using=self.db)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 517, in has_results
    return compiler.has_results()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 853, in has_results
    return bool(self.execute_sql(SINGLE))
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 871, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 435, in as_sql
    from_, f_params = self.get_from_clause()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 660, in get_from_clause
    clause_sql, clause_params = self.compile(from_clause)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 373, in compile
    sql, params = node.as_sql(self, self.connection)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/datastructures.py", line 144, in as_sql
    base_sql = compiler.quote_name_unless_alias(self.table_name)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 364, in quote_name_unless_alias
    r = self.connection.ops.quote_name(name)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/dummy/base.py", line 20, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
[07/Oct/2017 23:23:35] "POST /admin/login/?next=/admin/ HTTP/1.1" 500 207729

代码对我来说似乎很好,但它不起作用。请帮忙。我得到一个错误 - 指针和整数之间的比较。我在数组中输入的字母也存储为 - [a,b,c,d ....]或abcd ....

2 个答案:

答案 0 :(得分:3)

更改:

char c[1]

到:

char c

因为char c[1]表示一个char的数组,但您想要的是char

否则,你应该像这样使用数组:

char c[1];
scanf("%c", c);

(因为scanf将一个指针作为参数,在这种情况下c已经是一个指针,因为它实际上是一个地址),然后改变条件:

if (c==a[i])

到:

if (c[0] == a[i])

准确无误。

答案 1 :(得分:1)

在我看来,检测字符类型的更好方法是比较ascii中的第一个和最后一个字符。

#include <stdio.h>
#include <conio.h>

int main()
{
    char c;

    printf("Enter the character of your choice: ");
    scanf("%c", &c);

    if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
        printf("The character is an alphabet");
    }
    else if(c >= '0' && c <= '9') {
        printf("The character is an number");
    }
    else {
        printf("The character is not valid");
    }

    return 0;
}

对不起我的语言。