标题:C语言 刚学习几天的萌新求教
只看楼主
yu1358785799
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-10-1
结帖率:0
已结贴  问题点数:20 回复次数:1 
C语言 刚学习几天的萌新求教
C语言,求解一元二次方程 输出a,b,c当系数
对于a,b,c都等于0的情况时
scanf里面a=b=c==0输出变成-1#j什么意思
是不是得塔里面除数不能等于0啊
如果是这样该如何解决呢
蟹蟹
搜索更多相关主题的帖子: 等于 C语言 学习 输出 scanf 
2021-10-04 23:03
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:10 
https://blog.

//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

static inline int vidv(int num, int den)
{
#define DIV_EXCEPTION 0 
    if (!den) return DIV_EXCEPTION;
    
    return num / den;
}

#define int0
#define callback int0
char* callback log_info(unsigned char errno)
{
    static char *log[256] = {
        [DIV_EXCEPTION] = "arithmetic exception[div zero]"
    };
    
    return log[errno];
}

int main(int argc, char *argv[])
{
    int a = 0, b = 0, c = 0;
    
    puts(log_info(vidv(a, (b + c))));

    return 0;
}


[此贴子已经被作者于2021-10-5 07:41编辑过]

2021-10-05 07:39
温雪琪
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2021-10-5
得分:10 
2021-10-05 20:38
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
//online parser: https://www.bccn.net/run/?lang=c
程序代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>

typedef unsigned char uint8_t;
static unsigned short exp_state;
static unsigned long *vint;
#define MAGIC_EXCEPTION 0x9C
#define post_exception(_exp) exp_state = ((_exp << 8) | MAGIC_EXCEPTION);
#define chk_exception() (MAGIC_EXCEPTION == (exp_state & 0xff))
#define clr_exception() (exp_state &= 0xff00)

static inline int vdiv(int num, int den)
{
#define DIV_EXCEPTION 0
    if (!den) {
        post_exception(DIV_EXCEPTION);
//#define PRINT_ON
#ifdef PRINT_ON
        printf("exp_state = 0x%x\n", exp_state);
#endif
        return DIV_EXCEPTION;
    }
    
    return num / den;
}

#define callback
char* callback log_info(uint8_t errno)
{
    static char *log[256] = {
        [DIV_EXCEPTION] = "WARNING: arithmetic exception[div zero]"
    };
    
    return log[errno];
}

void register_vector_tbl(void)
{
    vint = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC,
    MAP_ANONYMOUS | MAP_SHARED , 0 , 0);
    
    vint[DIV_EXCEPTION] = (unsigned long)log_info;
#ifdef PRINT_ON
    printf("log_info = %p\n", log_info);
    printf("vint[%d] =  0x%lx\n", DIV_EXCEPTION, vint[DIV_EXCEPTION]);
#endif
}

void trig_int(uint8_t arg)
{
typedef char* (*vect_log)(unsigned char);
#ifdef PRINT_ON
    printf("log_info = %p\n", log_info);
    printf("vint[%d] =  0x%lx\n", DIV_EXCEPTION, vint[DIV_EXCEPTION]);
#endif
    puts(((vect_log)vint[DIV_EXCEPTION])(arg));

    clr_exception();
#ifdef PRINT_ON
    printf("exp_state = 0x%x\n", exp_state);
#endif
}

int main(int argc, char *argv[])
{
    uint8_t a = 0, b = 0, c = 0, errno;
    int i;

    register_vector_tbl();
#define TST_PROCEDURE_CNT 25
    for (i = 0; i <= TST_PROCEDURE_CNT; i++) {
        printf("[%d]:a = %d, b = %d, c = %d\n", i, a, b, c);
        errno= vdiv(a, (b + c));
        if (chk_exception()) trig_int(errno);
        a = ((uint8_t *)main)[i] % 10; 
        b = ((uint8_t *)main)[i + 1] % 10; 
        c = ((uint8_t *)main)[i + 2] % 10;
    }

    return 0;
}


output sample:
[0]:a = 0, b = 0, c = 0
WARNING: arithmetic exception[div zero]
[1]:a = 5, b = 2, c = 7
[2]:a = 2, b = 7, c = 9
[3]:a = 7, b = 9, c = 2
[4]:a = 9, b = 2, c = 1
[5]:a = 2, b = 1, c = 6
[6]:a = 1, b = 6, c = 2
[7]:a = 6, b = 2, c = 7
[8]:a = 2, b = 7, c = 5
[9]:a = 7, b = 5, c = 6
[10]:a = 5, b = 6, c = 2
[11]:a = 6, b = 2, c = 7
[12]:a = 2, b = 7, c = 7
[13]:a = 7, b = 7, c = 4
[14]:a = 7, b = 4, c = 8
[15]:a = 4, b = 8, c = 9
[16]:a = 8, b = 9, c = 8
[17]:a = 9, b = 8, c = 0
[18]:a = 8, b = 0, c = 8
[19]:a = 0, b = 8, c = 9
[20]:a = 8, b = 9, c = 9
[21]:a = 9, b = 9, c = 0
[22]:a = 9, b = 0, c = 8
[23]:a = 0, b = 8, c = 9
[24]:a = 8, b = 9, c = 0
[25]:a = 9, b = 0, c = 0
WARNING: arithmetic exception[div zero]
2021-10-12 12:05
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned char uint8_t;
static unsigned short exp_state;
#define EMU_ISR
#ifndef EMU_ISR
static unsigned long *vint;
#else
static unsigned char *vint;
#endif/* EMU_ISR */
#define MAGIC_EXCEPTION 0x9C
#define post_exception(_exp) exp_state = ((_exp << 8) | MAGIC_EXCEPTION);
#define chk_exception() (MAGIC_EXCEPTION == (exp_state & 0xff))
#define clr_exception() (exp_state &= 0xff00)

static inline int vdiv(int num, int den)
{
#define DIV_EXCEPTION 0
    if (!den) {
        post_exception(DIV_EXCEPTION);
//#define PRINT_ON
#ifdef PRINT_ON
        printf("exp_state = 0x%x\n", exp_state);
#endif
        return DIV_EXCEPTION;
    }
    
    return num / den;
}

#define callback
char* callback log_info(uint8_t errno)
{
    static char *log[256] = {
        [DIV_EXCEPTION] = "WARNING: arithmetic exception[div zero]"
    };
//#define ASM_MARKABLE_INS
#ifdef ASM_MARKABLE_INS
    __asm__ ("xor %esi, %esi");
    __asm__ ("xor %edi, %edi");
#endif 
    //len = 28 - 4(ASM_MARKABLE_INS) = 24
    return log[errno];
}

void register_vector_tbl(void)
{
    vint = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC,
    MAP_ANONYMOUS | MAP_SHARED , 0 , 0);
#ifndef EMU_ISR
    vint[DIV_EXCEPTION] = (unsigned long)log_info;
#else/* EMU_ISR */
    //emu loading IRQ_0
#define REAL_ISR_OFFSET 1024
#define PREFIX_OP_LEN 5
#define INTEL_NOP 0x90
#ifndef ASM_MARKABLE_INS
#define HRD_LEN_4_LOG_INFO (29 - 4)
#else
#define HRD_LEN_4_LOG_INFO 29
#endif
    memcpy(&vint[DIV_EXCEPTION + REAL_ISR_OFFSET], log_info, HRD_LEN_4_LOG_INFO);
    /* clr ins pipe by filling Mr.nop */
    memset(&vint[PREFIX_OP_LEN], INTEL_NOP, REAL_ISR_OFFSET - PREFIX_OP_LEN);
    //prefix opcode jmp dword ptr[rip + 0x3FB]
    vint[DIV_EXCEPTION + 0] = 0xe8;
    /* 4 quickly location */
    vint[DIV_EXCEPTION + 1] = 0xFB;/* thanks 2 NOP(0x90) 0~0x3FB all works well */
    vint[DIV_EXCEPTION + 2] = 0x03;/* maybe its a positive bug lalala~~ */
    vint[DIV_EXCEPTION + 3] = 0;
    vint[DIV_EXCEPTION + 4] = 0;
#endif/* EMU_ISR */
#if !defined(EMU_ISR) && defined(PRINT_ON)
    printf("log_info = %p\n", log_info);
    printf("vint[%d] =  0x%lx\n", DIV_EXCEPTION, vint[DIV_EXCEPTION]);
#endif
}

void trig_int(uint8_t arg)
{
#ifndef EMU_ISR 
#ifdef PRINT_ON
    printf("log_info = %p\n", log_info);
    printf("vint[%d] =  0x%lx\n", DIV_EXCEPTION, vint[DIV_EXCEPTION]);
#endif
typedef char* (*vect_log)(unsigned char);
    puts(((vect_log)vint[DIV_EXCEPTION])(arg));
#else/* EMU_ISR */
    if (0 == arg) __asm__ (".byte 0x68, 0x0, 0x0, 0x0, 0x0");
typedef char* (*visr)(void);
    puts(((visr)&vint[0])());
#endif/* EMU_ISR */
    clr_exception();
#ifdef PRINT_ON
    printf("exp_state = 0x%x\n", exp_state);
#endif
}

int main(int argc, char *argv[])
{
    uint8_t a = 0, b = 0, c = 0, errno;
    int i;

    register_vector_tbl();
#define TST_PROCEDURE_CNT 25
    for (i = 0; i <= TST_PROCEDURE_CNT; i++) {
        printf("[%d]:a = %d, b = %d, c = %d\n", i, a, b, c);
        errno= vdiv(a, (b + c));
        if (chk_exception()) trig_int(errno);
        a = ((uint8_t *)main)[i] % 10; 
        b = ((uint8_t *)main)[i + 1] % 10; 
        c = ((uint8_t *)main)[i + 2] % 10;
    }
#ifdef ASM_MARKABLE_INS
    /* checking irq length */
    system("gcc *.c -o v.out");
    system("objdump -d v.out");
#endif
    munmap(vint, getpagesize());
    
    return 0;
}


over!
2021-10-13 17:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-507055-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.138586 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved