开天辟地的卷毛 2022-10-26 14:37 采纳率: 85.4%
浏览 24
已结题

C语言在缓冲区读取字符的问题

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int datatype;
//链表内的结构体
typedef struct node
{
    datatype data;      //节点数据域
    struct node* next;  //节点指针域
}ND, * PND;
//设计管理结构体的数据类型
typedef struct linkstack
{
    struct node* ptop;//定义栈顶指针,设定类型为链表结构体指针
    //datatype cnt;//当前栈中有几个节点(可不用)
}LST, * PLST;

PLST initstack(void)
{
    PLST pst = (PLST)malloc(sizeof(LST));
    if (pst == NULL)
        return NULL;
    pst->ptop = NULL;
    //pst->cnt = 0;//当前栈中有0个节点(可不用)
    return pst;
}

void push(PLST pst, datatype d)
{
    PND pnew = (PND)malloc(sizeof(ND));
    if (pnew == NULL)
        return;
    if (d >= 10)
    {
        d = d - 10 + 'A';
        pnew->data = d;
        pnew->next = NULL;
    }
    else
    {
        pnew->data = d;
        pnew->next = NULL;
    }
    //把pnew指向的新节点的next指向当前的栈顶ptop,(也就是链上了ptop指向的地方)
    pnew->next = pst->ptop;
    pst->ptop = pnew;
    //pst->cnt++;//栈中已有元素个数加一(可不用)
}
bool is_empty(PLST pst)
{
    //if (pst->cnt == 0)//栈中没有节点,所以返回true(可不用)
    if (pst->ptop == NULL)
        return true;
    else
        return false;
}
void pop(PLST pst, datatype *val)
{
    if (is_empty(pst))
    {
        printf("the link stack is empty\n");
        return;
    }
    *val = pst->ptop->data;
    PND ptmp = pst->ptop;       //定义一个PND类型的临时指针存ptop指针
    pst->ptop = pst->ptop->next;//ptop向下移动一个单位,方便原节点弹出。
    //pst->cnt--//栈中已有元素个数减一(可不用)
    ptmp->next = NULL;          //断开与栈的连接,指向空
    free(ptmp);
    return;
}
void dec_to_oct(PLST pst, datatype d)
{
    //1.新建空栈

    //2.循环地把d对8求余,把余数入栈
    //int n;
    while (d > 0)
    {
        //基础写法
        //n = d % 8;
        //push(pst, n);
        //d = d / 8;

        push(pst, d % 8);//数据d求一次余,将这一次求余作为形参传递给push,pst也传过去
        d = d / 8;//第一次求余之后,
    }
    int val;

    //3.把栈中的余数出栈打印
    while (1)
    {
        if (is_empty(pst))//如果栈为空,直接退出
            break;
        pop(pst, &val);/*pop出参数,运用值传参& val传到pop
                       函数中去*/
        printf("%d\t", val);
        printf("\n");
    }
}
void dec_to_every(PLST pst, datatype cover, datatype base)
{
    while (cover > 0)
    {
        push(pst, cover % base);
        cover = cover / base;
    }

    if (base >= 10)
        printf("0x");
    else
        printf("0");

    datatype val;
    while (1)
    {
        if (is_empty(pst))
            break;
        pop(pst, &val);
        if (val > 10)
            printf("%c", val);
        else
            printf("%d", val);
    }
}
void cls(char clsn)
{
    if (clsn == 'y')
    {
        system("cls");
        return;
    }
    else
        return;
}
int main(void)
{
    datatype cover, base;
    char clsn, ch;
    PLST pst = initstack();
    printf("please enter the cover num (<0 to quit): ");
    while (scanf_s("%d", &cover) && (cover > 0))
    {
        printf("please enter the base num: ");
        scanf_s("%d", &base);
        printf("\nobtatin anyother num through decimal num coversion: ");
        dec_to_every(pst, cover, base);
        while ((ch = getchar()) != '\n') //清理多余的字符
            continue;
        printf("\nwhether to claer the screen?(y/n): ");
        clsn = getchar();
        printf("\n");
        while ((clsn != 'y') && (clsn != 'n'))//判断用户是否输入准确
        {
            while (getchar() != '\n')
                continue;
            printf("please enter n or y: ");
            clsn = getchar();
        }
        cls(clsn);
        //while (getchar() != '\n')
        //    continue;
        printf("please enter the cover num again(<0 to quit): ");
    }

}

如这一段代码,151行 我想通过输入y或n,确认是否执行清屏函数,
我加了一段代码154-161行,判断如果用户输入错误输入了其他的字符或者数字,则让用户重新输入y或n。
但是当我输入 yn 或者 ny 的时候,会出现执行清屏函数,或者不执行清屏函数,要怎么改正才能在输入y或者n的时候才算正确输入。其他情况要求重新输入呀?

  • 写回答

1条回答 默认 最新

  • 於黾 2022-10-26 14:50
    关注

    那你得要求输入一个字符串,或者先循环读,读一个完整的字符串一起判断,而不是每个字符读进来直接判断

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月4日
  • 已采纳回答 10月27日
  • 创建了问题 10月26日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效