检查一下代码错误与否

#includeint main(){ int x,y; char f; printf("操作数 运算符 操作数"); scanf("%d,%c,%d",&x,&f,&y); if(y==0&&f=='/') printf("除法分母不能为0!"); else { switch(f) { case '+':printf("%d",x+y);break; case '-':printf("%d",x-y);break; case '':printf("%d",xy);break; case '/':printf("%d",x/y);break; default:printf("输入错误!");break; }} return 0;}这个代码错误在哪里?

把scanf里面的逗号去掉即可,因为如果加了逗号,输入里也需要输入逗号和它匹配才能正确读取到数据,所以去除它们接即可。

修改如下:

参考链接:

#include<stdio.h>
int main() {
    
    int x,y;
    char f;
    printf("操作数 运算符 操作数:\n");
    scanf("%d%c%d",&x,&f,&y);
    
    if(y==0&&f=='/')
        printf("除法分母不能为0!");
    else {
        switch(f) {
            case '+':
                printf("%d",x+y);
                break;
            case '-':
                printf("%d",x-y);
                break;
            case '*':
                printf("%d",x*y);
                break;
            case '/':
                printf("%d",x/y);
                break;
            default:
                printf("输入错误!");
                break;
        }
    }
    return 0;
}

img

  • 这篇博客也许可以解决你的问题👉 :常用的函数代码编写
  • 除此之外, 这篇博客: 单链表完整代码中的 单链表 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node{
        int data;
        struct Node *next;
    }Node, *LinkedList;
     
    Node insert(LinkedList head, Node *node, int index) {
        Node *p, ret;
        p = &ret;
        ret.data = 0;
        ret.next = head;
        while (p->next && index) {
            p = p->next;
            --index;
        }
        if (index == 0) {
            node->next = p->next;
            p->next = node;
            ret.data = 1;
        }
        return ret;
    }
    void output(LinkedList head) {
        Node *p = head;
        while (p) {
            printf("%d", p->data);
            if (p->next) {
                printf(" ");
            }
            p = p->next;
        }
        printf("\n");
    }
    
    void clear(LinkedList head) {
        Node *p, *q;
        p = head;
        while (p) {
            q = p->next;
            free(p);
            p = q;
        }
        return ;
    }
    
    int main() {
        LinkedList linkedlist = NULL;
        Node *p, ret;
        int n, num, loc;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            scanf("%d%d", &loc, &num);
            p = (Node *)malloc(sizeof(Node));
            p->data = num;
            p->next = NULL;
            ret = insert(linkedlist, p, loc);
            linkedlist = ret.next;
            printf("%s\n", ret.data ? "success" : "failed");
        }
        output(linkedlist);
        clear(linkedlist);
        return 0;
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node{
        int data;
        struct Node *next;
    }Node, *LinkedList;
    
    LinkedList insert(LinkedList head, Node *node, int index) {
        if (head == NULL) {
            if (index != 0) {
                printf("failed\n");
                return head;
            }
            head = node;
            printf("success\n");
            return head;
        }
        if (index == 0) {
            node->next = head;
            head = node;
            printf("success\n");
            return head;
        }
        Node *current_node = head;
        int count = 0;
        while (current_node->next != NULL && count < index - 1) {
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1) {
            node->next = current_node->next;
            current_node->next = node;
            printf("success\n");
            return head;
        }
        printf("failed\n");
        return head;
    }
    
    void output(LinkedList head) {
        if (head == NULL) {
            return;
        }
        Node *current_node = head;
        while (current_node != NULL) {
            printf("%d ", current_node->data);
            current_node = current_node->next;
        }
        printf("\n");
    }
    
    Node delete_node(LinkedList head, int index) {
        Node ret, *p, *q;
        ret.data = 0;
        ret.next = head;
        p = &ret;
        while(p->next && index) {
            p = p->next;
            --index;
        }
        if(p->next) {
            q = p->next;
            p->next = q->next;
            free(q);
            ret.data = 1;
        }
        return ret;
    }
    
    LinkedList reverse(LinkedList head) {
        Node ret;
        ret.next = NULL;
        Node *p, *q;
        p = head;
        while(p) {
            q = p->next;
            p->next = ret.next;
            ret.next = p;
            p = q;
        }
        return ret.next;
    }
    
    void clear(LinkedList head) {
        Node *current_node = head;
        while (current_node != NULL) {
            Node *delete_node = current_node;
            current_node = current_node->next;
            free(delete_node);
        }
    }
    
    int main() {
        LinkedList linkedlist = NULL;
        int n;
        Node ret;
        scanf("%d", &n);
        while(n --) {
            int w;
            scanf("%d", &w);
            if(w == 1) {
                int a, b;
                scanf("%d%d", &a, &b);
                Node *node = (Node *)malloc(sizeof(Node));
                node->data = b;
                node->next = NULL;
                linkedlist = insert(linkedlist, node, a);
            }
            if(w == 2) {
                output(linkedlist);
            }
            if(w == 3) {
                int a;
                scanf("%d", &a);
                ret = delete_node(linkedlist, a);
                linkedlist = ret.next;
                printf("%s\n", ret.data ? "success" : "failed");
            }
            if(w == 4) {
                linkedlist = reverse(linkedlist);
            }    
        }
        clear(linkedlist);
        return 0;
    }
    
  • 您还可以看一下 传智老师的多角度带你编写更规范的黑盒测试用例课程中的 等价类设计测试用例的步骤小节, 巩固相关知识点