C:如何键入名称并以“。”结束。 (点)

时间:2016-04-01 11:06:06

标签: c

我的任务是用一个小写字母写一个医生的姓氏,然后用一个点('.')结束,用户将用输出打印。耗费的任务是建立一个医疗预约系统:

#include<stdio.h>
#include<stdlib.h>
int main(){
    fflush(stdin);
    int count = 0, flag = 0;
    char Nameplate;   //last name of the doctor
    printf("Please enter the last name of your doctor(please type with only small letters):\n");
    Nameplate = getchar();
    do{
        Nameplate = getchar();
    } while (Nameplate >= 'a' && Nameplate <= 'z' &&  Nameplate == '.');
    if (!(Nameplate >= 'a' && Nameplate <= 'z' && Nameplate == '.')){
        flag = 1;
    }
    else if (Nameplate == '\n'){
        flag = 0;
        count++;
    }
    if (flag == 1){
        printf("Invalid input,");
        fflush(stdin);
        main();
    }
    else if (flag == 0){
        printf("\n Your appointment has been successfully canceled.\n\n");
    }
    return 0;
}

现在,此代码无效。如果我不使用点,它会工作,但当我输入点时,问题就开始了。

3 个答案:

答案 0 :(得分:3)

尝试

@RepositoryRestController
@RequestMapping("/clients")
@ResponseBody
public class SearchRestController {

    @Autowired
    private ClientRepository clientRepository;

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    Client readAgreement(@RequestParam(value = "query") String query,
                         @RequestParam(value = "category") String category) {
        Client client = clientRepository.findOne(Long.parseLong(query));
        ControllerLinkBuilder builder = linkTo(SearchRestController.class).slash(client.getId());

        return new Resource<>(client,
                builder.withSelfRel(),
                builder.withRel("client"));
    }
}

而不是

while ((Nameplate >= 'a' && Nameplate <= 'z') ||  (Nameplate == '.'))

答案 1 :(得分:2)

即使对小写字母和句号的测试进行了更改,您仍然存在潜在问题,因为用户可能在名称中有句点,但最后仍然没有句号。

此代码仅检查小写字母,不包括最后一个字符,然后测试最后一个字符是句点 还有一些控制名称长度。

int main()
{
    int flag, n;
    char *Nameplate = malloc(22);   //last name of the doctor
    fflush(stdin);

    while(1) {
        printf("Please enter the last name of your doctor(please type with only small letters):\n");
        printf("End name with a period/full stop\n");
        fgets(Nameplate, 20, stdin);

        /* test for all valid lower case letters */
        flag = 0;
        for(n = 0; n < strlen(Nameplate) - 2; n++) {
           if (!('a' <= Nameplate[n] && Nameplate[n] <= 'z')) {
               /* not a lower case character */
               flag = 1;
               break;
           }
        }

        /* now test for terminating period */
        if(Nameplate[strlen(Nameplate) - 2] != '.') {
            /* no period at end of name */
            flag = 2;
        }

        /* handle errors or accept */
        if(flag == 1) {
            printf("small letters only\n");
        } else if (flag == 2) {
            printf("remember to end with a period/full stop\n");
        } else {
            /* name was all lower case with terminating period so exit input */
            break;
        }
    }

    printf("\n Your appointment with %s has been successfully canceled.\n\n", Nameplate);
    free(Nameplate);
    return 0;
}

答案 2 :(得分:0)

如果我理解你想输入医生的名字来设置约会,只处理小写字符,如果最后一个字符是'.'取消约会,你可以稍微重新排列你的逻辑并完成目标仍在保留中间初始标点符号。

此外,无论何时进行输入,都要为用户提示,以便他们不会坐在那里看着闪烁的光标,想知道程序是否挂起。

处理大写/小写转换时,没有理由强制用户仅输入其中一个。您可以以透明的方式简单地检查/转换它们提供的任何输入。 ctype.h标头文件包含tolowertoupper字符转换,或者您可以简单地了解6th-bit中的7-bit ASCII是案例位并切换它根据需要完成案件转换。

这只是一个例子,有许多方法可以将各个部分组合在一起:

#include <stdio.h>

#define MAXC 64

int main (void) {

    int c = 0, cnx = 0, i = 0;
    char name[MAXC] = "";

    printf ("\n enter doctor's name (end with '.' to cancel): ");
    while (i + 1 < MAXC && (c = getchar()) != '\n' && c != EOF)
        if (('a' <= c && c <= 'z') || c == '.' || c == ' ')
            name[i++] = c;                      /* add to name        */
        else if ('A' <= c && c <= 'Z')          /* if upper-case      */
            name[i++] = c ^ (1u << 5);          /* convert to lower   */
    if (i && name[i-1] == '.') {                /* last is '.'        */
        cnx = 1;                                /* set cancel flag    */
        name[--i] = 0;                          /* overwrite last '.' */
    }
    else
        name[i] = 0;                            /* nul-terminate name */

    if (cnx)    /* appointment canceled */
        printf ("\n Your appointment with doctor '%s' has been canceled.\n\n",
                name);
    else        /* new appointment      */
        printf ("\n You have a new appointment with doctor '%s'.\n\n", name);

    return 0;
}

使用/输出

$ ./bin/appointment

 enter doctor's name (end with '.' to cancel): John J. Marks

 You have a new appointment with doctor 'john j. marks'.


$ ./bin/appointment

 enter doctor's name (end with '.' to cancel): John J. Marks.

 Your appointment with doctor 'john j. marks' has been canceled.