15.c

来自「自己做的常用库和实现的数据结构。public domain.」· C语言 代码 · 共 124 行

C
124
字号
#include "my.h"
#define MAX_LEN 5
/*
typedef enum { RAND, CODE, PTR } elem_type_tag;
typedef enum { ADD=4, SUB=4, MUL=3, DIV=3, MOD=3, MIN=2, BRAC=1 } opcode_priority_tag;

typedef struct {
	char opcode;
	opcode_priority_tag tag;
} opcode_struct;

typedef struct glist_node {
	elem_type_tag tag;
	union {
		double oprand;
		opcode_struct opcode_unit;
		struct glist_node *new_head;
	} type;
	struct glist_node *next;
} GLIST;

void parse_oprand(const char **input_buf, char *tmp_buf, bool *negative_flag, double *oprand_unit)
{
	size_t i;
	const char *p = *input_buf, *q, *head_tag, *tail_tag;

	if(*p == '(') {
		
	}
	*negative_flag = false;
	if(*p == '-' && isdigit( *(p+1)) && (p == input_buf || *(p-1) == '(')) {
		*negative_flag = true; p++;
	}
	if(isdigit(*p)) {
		for(head_tag = p++; *p; p++) {
			if(! isdigit(*p) && *p != '.') {
				tail_tag = --p;
				break;
			}
		}
		for(i=0, q = *negative_flag ? --head_tag : head_tag; q <= tail_tag; i++, q++) 
			tmp_buf[i]= *q;
		tmp_buf[i]= '\0';
		oprand_unit = atof(tmp_buf);
	} else {
		fprintf(stderr, "Fatal: input format error");
		exit(EXIT_FAILURE);
	}
}
void parse_opcode(const char **input_buf, char *tmp_buf, size_t tmp_buf_nmemb, bool negative_flag)
{
	const char *p = *input_buf;
	opcode_priority_tag tag, tag2;
	get_opcode_priority_tag(*p, tag);
	parse_oprand(&p, tmp_buf, tmp_buf_nmemb, negative_flag);
	get_opcode_priority_tag(*p, tag2);
	if(tag >= tag2) {
		bc.tag = CODE;
		
	} else {
		bc.tag = PTR;
	
	}
}
#if (t == '+')
#define OPCODE_FLAG +
#elif (t == '-')
#define OPCODE_FLAG -
#elif (t == '*')
#define OPCODE_FLAG *
#elif (t == '/')
#define OPCODE_FLAG /
#elif (t == '%')
#define OPCODE_FLAG %
#endif

void get_opcode_priority_tag(int t, opcode_priority_tag *tag)
{
	switch(t) {
		case '+':
		case '-':
			*tag = ADD;
			break;
		case '*':
		case '/':
		case '%':
			*tag = MUL;
			break;
	}
}

void parse_input(const char *input_buf, char *tmp_buf, size_t tmp_buf_nmemb)
{
	const char *p;
	bool negative_flag;
	double oprand_unit;
	GLIST bc;
	for(p = input_buf; *p; ) {
		parse_oprand(&p, tmp_buf, tmp_buf_nmemb, &negative_flag, &oprand_unit);
		en_glist(&bc, oprand_unit);
		parse_opcode(&p, tmp_buf, tmp_buf_nmemb, negative_flag);
	}
}
*/

void preprocess_input(char *input_buf, size_t nmemb)
{
	int c, i=0;
	while( (c=getchar()) != '\n' && c!=EOF && i < nmemb-1) {
		if(! isspace(c))
			input_buf[i++] = c;
	}
	input_buf[i] = '\0';
}
void main()
{
	char input_buf[MAX_LEN+1], tmp_buf[MAX_LEN];
	preprocess_input(input_buf, MAX_LEN+1);	
	puts(input_buf);
	/*
	parse_input(input_buf, tmp_buf, MAX_LEN+1);
	*/
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?