C语言 刚学习几天的萌新求教
C语言,求解一元二次方程 输出a,b,c当系数对于a,b,c都等于0的情况时
scanf里面a=b=c==0输出变成-1#j什么意思
是不是得塔里面除数不能等于0啊
如果是这样该如何解决呢
蟹蟹
#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编辑过]
#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; }
#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; }