Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdarg.h>
- #include <ctype.h>
- #include <stdlib.h>
- //prototypes
- int scnf(char* format, ...);
- int getl(char*);
- int getch();
- int ungetch(int);
- char * allofc(int n);
- void freef(int n);
- int conv2i(char*);
- double conv2d(char*);
- void main() {
- double* a = (double *) malloc(200);
- int* d = (int *) malloc(200);
- //char* l = allofc(5);
- scnf("%f%d", a, d);
- printf("\n%f %d", *a, *d);
- }
- int scnf(char* format, ...) {
- va_list ap;
- char* b4conv = allofc(100);
- int ival;
- char* stringv;
- int i;
- char* il = allofc(1000);
- va_start(ap, format);
- getl(il); //getline
- for( ; isspace(*++format); ) //skip spaces
- ;
- --format;
- while(*format != '\0'){ //parse format
- if(*format++ == '%'){
- while(*format != '%' && *format!= '\0' && !isspace(*format)){
- switch(*format++){
- case 'd':
- i = 0;
- while(isdigit(*il))
- b4conv[i] = il[i++];
- if(isspace(*il))
- il++;
- *(va_arg(ap, int*)) = conv2i(b4conv); //hurrayy less spaghetti code woohoo
- freef(i);
- i = 0;
- break;
- case 'f':
- i = 0;
- while(isdigit(*il))
- b4conv[i] = il[i++];
- *(va_arg(ap, double*)) = conv2d(b4conv);
- freef(i);
- i = 0;
- break;
- }
- }
- }
- }
- }
- int getl(char* line) {
- int c, i;
- for(i = 0; (c = getch()) != EOF && c!= '\n'; i++)
- *line++ = c;
- if(c == '\n'){
- *line++ = c;
- *line = '\0';
- } else
- *line = '\0';
- return i;
- }
- #define MAXBUF 4000
- char buffer[MAXBUF];
- int buffp = 0;
- int getch() {
- return (buffp > 0) ? buffer[buffp--] : getchar();
- }
- int ungetch(int a) {
- buffer[buffp++] = a;
- }
- char allocbuf[MAXBUF];
- char* allocp = allocbuf;
- char* allofc(int n) {
- if(allocbuf - allocp + MAXBUF >= n) {
- allocp += n;
- return allocp - n;
- }
- return NULL;
- }
- void freef(int n) {
- if(allocp - allocbuf - n > 0)
- allocp -= n;
- }
- int conv2i(char* s) {
- int val = 0;
- for(int i = 0; s[i] >= '0' && s[i] <= '9'; i++)
- val = 10 * val + (s[i] - '0');
- return val;
- }
- double conv2d(char* s) {
- double val = 0;
- double power;
- while(isspace(*s))
- s++;
- int i = 0;
- for(; s[i] >= '0' && s[i] <= '9' && s[i] != '\0' && s[i] != '.'; i++)
- val = 10.0 * val + (s[i] - '0');
- if(s[i] == '.'){
- int exp = 1.0;
- for(power = 1.0; s[++i] >= '0' && s[i] <= '9' && s[i] != '\0'; ){
- val = val * 10.0 + (s[i] - '0');
- power *= 10.0;
- }
- return val / power;
- }
- return (double) val;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement