📄 libcb.c
字号:
#include "libcb.h"#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX 1024#define MAXSTR "%1024s"/* This is part of the runtime system. It is linked with the C code * the compiler produces when compiling Asterix source. It is not part * of the compiler itself. */int cb_ReadLine(array *a){ size_t size; char str [MAX]; if (fgets(str, MAX, stdin) == 0) return 0; size = strlen(str); delete_array(*a); *a = new_array(size, 1); memcpy((**a).data.c, str, size); return 1;}int cb_WriteLine(void){ return putc('\n', stdout) == '\n';}int cb_ReadString(array *a){ size_t size; char str [MAX]; if (scanf(MAXSTR, str) != 1) return 0; size = strlen(str); delete_array(*a); *a = new_array(size, 1); memcpy((**a).data.c, str, size); return 1;}int cb_WriteString(array a){ unsigned i; if (a == 0) runtime_error( "WriteString is called with a NULL string"); for (i = 0; i < a->size; i ++) { int ch = a->data.c [i]; if (putc(ch, stdout) != (unsigned char) ch) return 0; } return 1;}int cb_ReadInt(int *i){ return scanf("%d", i) == 1;}int cb_WriteInt(int i){ return printf("%d", i) >= 0;}int cb_ReadReal(double *d){ return scanf("%lf", d) == 1;}int cb_WriteReal(double d){ return printf("%f", d) >= 0;}int cb_ReadChar(char *c){ return scanf("%c", c) == 1;}int cb_WriteChar(char c){ return putc(c, stdout) == (unsigned char) c;}array cb_StringCopy(array orig){ array copy; if (orig == 0) runtime_error( "StringCopy is called with a NULL string"); copy = new_array(orig->size, 1); memcpy(copy->data.c, orig->data.c, orig->size); return copy;}int cb_StringToInt(array a){ int value = 0, index; if (a == 0) runtime_error( "StringToInt is called with a NULL string"); for (index = 0; index < a->size; index ++) if (!isdigit((int) a->data.c [index])) break; else value *= 10, value += a->data.c [index] - '0'; return value;}array cb_IntToString(int i){ size_t size; array a; char str [16]; sprintf(str, "%d", i); size = strlen(str); a = new_array(size, 1); memcpy(a->data.c, str, size); return a;}array cb_RealToString(double d){ size_t size; array a; char str [64]; sprintf(str, "%g", d); size = strlen(str); a = new_array(size, 1); memcpy(a->data.c, str, size); return a;}double cb_StringToReal(array a){ char str [MAX]; if (a == 0) runtime_error( "StringToReal is called with a NULL string"); if (a->size >= MAX) runtime_error("constant too large"); memcpy(str, a->data.c, a->size); str [a->size] = '\0'; return atof(str);}double cb_IntToReal(int i){ return (double) i;}int cb_RealToInt(double d){ return (int) d;}int cb_CharToInt(char c){ return (int) c;}char cb_IntToChar(int i){ return (char) i;}double cb_Random(void){ return (double) rand() / RAND_MAX;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -