diff options
author | sinanmohd <pcmsinan@gmail.com> | 2022-06-18 16:13:44 +0530 |
---|---|---|
committer | sinanmohd <pcmsinan@gmail.com> | 2022-06-18 16:13:44 +0530 |
commit | ff4846af7dea497d75af09acfa6c2c8ca48fd67e (patch) | |
tree | 974ad1b5f260c398405c04578b90155480250aa5 | |
parent | c55c5080200f0f31ce05485af7cce4cf4204ac40 (diff) |
5.6
-rw-r--r-- | 5.6.1.c | 80 | ||||
-rw-r--r-- | 5.6.2.c | 61 | ||||
-rw-r--r-- | 5.6.3.c | 62 | ||||
-rw-r--r-- | 5.6.4.c | 133 | ||||
-rw-r--r-- | 5.6.5.c | 47 |
5 files changed, 383 insertions, 0 deletions
@@ -0,0 +1,80 @@ +#include <stdio.h> +#include <ctype.h> + +#define MAXLEN 64 + +int sneed_getline(char str[], int max); +double atof(const char str[]); + +int +main(void) +{ + char str[MAXLEN]; + + while (sneed_getline(str, MAXLEN) > 0) + printf("%f\n\n", atof(str)); + + return 0; +} + +int +sneed_getline(char str[], int max) +{ + char input; + char *str_og = str; + + while (--max && (*str = input = getchar()) != EOF && input != '\n') + str++; + + if (!max && input != '\n') + *str = '\n', str++; + + str = '\0'; + + return str - str_og; +} + +double +atof(const char str[]) +{ + int i, sign, e_sign, j; + double val, power, e_power; + + for (i = 0; isspace(str[i]); i++) + ; + sign = (str[i] == '-') ? -1 : 1; + + if (str[i] == '-' || str[i] == '+') + i++; + + for(val = 0.0; isdigit(str[i]); i++) + val = val * 10.0 + (str[i] - '0'); + + if (str[i] == '.') + i++; + + for(power = 1.0; isdigit(str[i]); i++) { + val = 10 * val + (str[i] - '0'); + power *= 10.0; + } + + if (str[i] == 'e' || str[i] == 'E') + i++; + + e_sign = (str[i] == '-') ? -1 : 1; + + if (str[i] == '-' || str[i] == '+') + i++; + + for (e_power = 0.0; isdigit(str[i]); i++) + e_power = e_power * 10 + (str[i] - '0'); + + if (e_sign < 0) + for (j = 0; j < e_power; j++) + power *= 10.0; + else + for (j = 0; j < e_power; j++) + power /= 10.0; + + return (sign * val / power); +} @@ -0,0 +1,61 @@ +#include <stdio.h> + +#define MAXLEN 15 + +void reverse(char str[]); +char* itoa(int n, char str[]); + +int +main(void) +{ + int n = -147483647; + char str[MAXLEN]; + + printf("%d\n", n); + printf("%s\n", itoa(n, str)); + + return 0; +} + +void +reverse(char str[]) +{ + char temp, *str_og; + + str_og = str; + + while (*str++) + ; + str -= 2; + + while (str_og < str) { + temp = *str_og; + *str_og++ = *str; + *str-- = temp; + } +} + +char* +itoa(int n, char str[]) +{ + int sign; + char *str_og = str; + + /* to include the most -ve int */ + n = ((sign = n) < 0) ? -(n+1) : n-1; + + do { + *str++ = n%10 + '0'; + } while ((n /= 10) > 0); + + if (sign <0) + *str++ = '-'; + + /* part of "to include the most -ve int" */ + *str_og += 1; + *str = '\0'; + + reverse(str_og); + + return str_og; +} @@ -0,0 +1,62 @@ +#include <stdio.h> + +#define MAXLEN 1000 + +int get_line(char line[], const int max); +int strindex(char source[], char pattern[]); + +int +main(void) +{ + int found, rightmost; + char line[MAXLEN]; + + char pattern[] = "ould"; + found = 0; + + while (get_line(line, MAXLEN) > 0) + if ((rightmost = strindex(line, pattern)) >= 0) { + printf("%s >> match at char %d\n", line, rightmost+1); + found++; + } + + printf("\nSneedGrep found %d match%s\n", found, (found > 0) ? "es" : ""); + + return 0; +} + +int +get_line(char line[], const int max) +{ + int i; + char input; + + for (i = 0; (input = getchar()) != EOF && input != '\n' && i < max-2; i++) + line[i] = input; + + if (input == '\n' && i < max-2) + line[i++] = '\n'; + + line[i] = '\0'; + + return i; +} + +int +strindex(char source[], char pattern[]) +{ + int i, rightmost; + char *source_og = source; + + rightmost = -1; + + for (; *source; source++) { + for (i = 0; *(pattern +i) && *(source+i) == *(pattern+i); i++) + ; + + if (!*(pattern+i)) + rightmost = source - source_og; + } + + return rightmost; +} @@ -0,0 +1,133 @@ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <math.h> + +#define MAXOP 100 +#define NUMBER_SIG '0' + +double pop(void); +void push(double f); +char getop(char str[]); + +int +main(void) +{ + char str[MAXOP]; + char type; + double op2; + + while ((type = getop(str)) != EOF) { + switch (type) { + case NUMBER_SIG : + push(atof(str)); + break; + case '+' : + push(pop() + pop()); + break; + case '*' : + push(pop() * pop()); + break; + case '-' : + op2 = pop(); + push(pop() - op2); + break; + case '/' : + if ((op2 = pop()) == 0.0) + printf("Err: deviser cant be zero\n"); + else + push(pop() / op2); + + break; + case '%' : + op2 = pop(); + if (op2 != 0) + push(fmod(pop(), op2)); + else + printf("Err: deviser cant be zero\n"); + + break; + case '\n' : + printf("%.8g\n", pop()); + break; + default : + printf("Err: unknown command\n"); + } + } + + return 0; +} + +#define MAXVAL 100 +double val[MAXVAL]; +int sp = 0; + +double +pop(void) +{ + if (sp > 0) + return val[--sp]; + else { + printf("Err: stack empty\n"); + return 0.0; + } +} + +void +push(double f) +{ + if (sp < MAXVAL) + val[sp++] = f; + else + printf("Err: stack is full\n"); +} + +char getch(void); +void ungetch(char input); + +char +getop(char str[]) +{ + char input, *str_og; + + str_og = str; + + /* ignore blanks */ + while (isblank(input = *str = getch())); + + /* return operator */ + if (!isdigit(input) && input != '.') + return input; + + /* collect digits*/ + while (str - str_og < MAXOP && (isdigit(input=getch()) || input == '.')) + *++str = input; + + *++str = '\0'; + + if (input != EOF) + ungetch(input); + + return NUMBER_SIG; +} + +#define MAXBUFF 100 +char buff[MAXBUFF]; +int bp = 0; + +char +getch(void) +{ + return (bp > 0) ? buff[--bp] : getchar(); +} + +void +ungetch(char input) +{ + if (bp < MAXBUFF) + buff[bp++] = input; + else { + printf("Err: buffer full\n"); + } +} + @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <ctype.h> + +#define MAXLEN 1000 + +int atoi(char str[]); +int sneed_getline(char str[], int max); + +int +main(void) +{ + char str[MAXLEN]; + while (sneed_getline(str, MAXLEN)) { + printf("string ver : %s", str); + printf("int ver : %d\n", atoi(str)); + } + return 0; +} + +int +atoi(char str[]) +{ + int val; + + for (val = 0; isdigit(*str) && *str; str++) + val = val * 10 + *str -'0'; + + return val; +} + +int +sneed_getline(char str[], int max) +{ + char input; + char *str_og = str; + + while (--max && (*str = input = getchar()) != EOF && input != '\n') + str++; + + if (!max && input != '\n') + *str = '\n', str++; + + str = '\0'; + + return str - str_og -1; +} + |