K&R 05: command line arguments
This commit is contained in:
parent
6e453f88f1
commit
32237d4c72
|
@ -0,0 +1,38 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void swap(int v[], int i, int j)
|
||||
{
|
||||
int temp;
|
||||
|
||||
temp = v[i];
|
||||
v[i] = v[j];
|
||||
v[j] = temp;
|
||||
}
|
||||
|
||||
void qsort(int v[], int left, int right)
|
||||
{
|
||||
int i, last;
|
||||
void swap(int v[], int i, int j);
|
||||
|
||||
if (left >= right)
|
||||
return;
|
||||
swap(v, left, (left+right)/2);
|
||||
last = left;
|
||||
for (i = left+1; i <= right; i++)
|
||||
if (v[i] < v[left])
|
||||
swap(v, ++last, i);
|
||||
swap(v, left, last);
|
||||
qsort(v ,left, last-1);
|
||||
qsort(v, last+1, right);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 4;
|
||||
int arr[] = {5, 4, 3, 2, 1};
|
||||
|
||||
qsort(arr, 0, 5);
|
||||
while(i--)
|
||||
printf("%i", arr[i]);
|
||||
printf("\n");
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,12 @@
|
|||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(int argc, int* argv[])
|
||||
{
|
||||
while (--argc > 0)
|
||||
printf((argc > 1) ? "%s " : "%s", *++argv);
|
||||
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
|
@ -0,0 +1,94 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
#define MAXOP 100
|
||||
#define NUMBER '0'
|
||||
|
||||
|
||||
int getop(char []);
|
||||
void push(double);
|
||||
double pop(void);
|
||||
|
||||
|
||||
/* calc: a simple implementation for an rpn calculator. Exc 5-10 */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int type, i;
|
||||
double op2;
|
||||
|
||||
if (argc == 1)
|
||||
return 0;
|
||||
|
||||
i = 1;
|
||||
while (argv[i] != NULL) {
|
||||
printf("value: \n\tstring: %s\n", argv[i]);
|
||||
type = getop(argv[i]);
|
||||
printf("type: \n\tchar: %c\n", type);
|
||||
printf("\tint: %i\n", type);
|
||||
|
||||
switch (type) {
|
||||
case NUMBER:
|
||||
push(atof(argv[i]));
|
||||
break;
|
||||
case '+':
|
||||
push(pop() + pop());
|
||||
break;
|
||||
case '*':
|
||||
push(pop() * pop());
|
||||
break;
|
||||
case '-':
|
||||
op2 = pop();
|
||||
push(pop() - op2);
|
||||
break;
|
||||
case '/':
|
||||
op2 = pop();
|
||||
if (op2 == 0.0)
|
||||
printf("error: zero divisior\n");
|
||||
else
|
||||
push(pop() / op2);
|
||||
break;
|
||||
default:
|
||||
printf("error: unkown command\n");
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
printf("\t%.8g\n", pop());
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#define MAXVAL 100
|
||||
int sp = 0;
|
||||
double val[MAXVAL];
|
||||
|
||||
void push(double f)
|
||||
{
|
||||
if (sp < MAXVAL)
|
||||
val[sp++] = f;
|
||||
else
|
||||
printf("error: stack full, can't push %g\n", f);
|
||||
}
|
||||
|
||||
double pop(void)
|
||||
{
|
||||
if (sp > 0)
|
||||
return val[--sp];
|
||||
else {
|
||||
printf("error: stack empty\n");
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
int getop(char s[])
|
||||
{
|
||||
if(isdigit(s[0]) || (s[0] == '-' && s[1] != '\0'))
|
||||
return NUMBER;
|
||||
else
|
||||
return s[0];
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
#!/bin/tcc -run
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define MAXLINE 1000
|
||||
|
||||
int getln(char *line, int max);
|
||||
|
||||
/* find: print lines that match pattern from 1st argument */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char line[MAXLINE];
|
||||
long lineno = 0;
|
||||
int c, except = 0, number = 0, found = 0;
|
||||
|
||||
while (--argc > 0 && (*++argv)[0] == '-')
|
||||
while (c = *++argv[0])
|
||||
switch (c){
|
||||
case 'x':
|
||||
except = 1;
|
||||
break;
|
||||
case 'n':
|
||||
number = 1;
|
||||
break;
|
||||
default:
|
||||
printf("find: illegal option %c\n", c);
|
||||
argc = 0;
|
||||
found = -1;
|
||||
break;
|
||||
}
|
||||
if (argc != 1)
|
||||
printf("Usage: find -x -n pattern\n");
|
||||
else
|
||||
while (getln(line, MAXLINE) > 0){
|
||||
lineno++;
|
||||
if ((strstr(line, *argv) != NULL) != except) {
|
||||
if (number) printf("%ld: ", lineno);
|
||||
printf("%s", line);
|
||||
found++;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/* getln: get line into s, return length */
|
||||
int getln(char s[], int lim)
|
||||
{
|
||||
int c, i;
|
||||
|
||||
i = 0;
|
||||
while (--lim > 0&& (c = getchar()) != EOF && c != '\n')
|
||||
s[i++] = c;
|
||||
if (c == '\n') /* line's end */
|
||||
s[i++] = c;
|
||||
s[i] = '\0'; /* string's end */
|
||||
return i;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/* getln: get line into s, return length */
|
||||
int getln(char s[], int lim)
|
||||
{
|
||||
int c, i;
|
||||
|
||||
i = 0;
|
||||
while (--lim > 0&& (c = getchar()) != EOF && c != '\n')
|
||||
s[i++] = c;
|
||||
if (c == '\n') /* line's end */
|
||||
s[i++] = c;
|
||||
s[i] = '\0'; /* string's end */
|
||||
return i;
|
||||
}
|
|
@ -2,6 +2,13 @@
|
|||
#include <stdio.h>
|
||||
|
||||
|
||||
/* prints: print a string */
|
||||
void prints(char *c)
|
||||
{
|
||||
while(*c != '\0' && putchar(*c++))
|
||||
;
|
||||
}
|
||||
|
||||
void test01()
|
||||
{
|
||||
int x = 1, y = 2, z[10];
|
||||
|
@ -23,12 +30,20 @@ void test02(int l)
|
|||
|
||||
}
|
||||
|
||||
void test03()
|
||||
{
|
||||
char s[] = "lmao"; /* chars array */
|
||||
char *c;
|
||||
c = s;
|
||||
printf("%c\n", *(c+1));
|
||||
*s = 'k';
|
||||
printf("%s\n", s);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
test02(4);
|
||||
test02();
|
||||
test02();
|
||||
|
||||
test03();
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
#define ALLOCSIZE 10000
|
||||
|
||||
static char allocbuf[ALLOCSIZE];
|
||||
static char *allocp = allocbuf; /* next free position */
|
||||
|
||||
char *alloc(int n)
|
||||
{
|
||||
if(allocbuf + ALLOCSIZE - allocp >= n){ /* it fits */
|
||||
allocp += n;
|
||||
return allocp - n;
|
||||
} else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void afree(char *p)
|
||||
{
|
||||
if(p >= allocbuf && p < allocbuf + ALLOCSIZE)
|
||||
allocp = p;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAXLINES 5000
|
||||
#define MAXLEN 1000
|
||||
|
||||
char *lineptr[MAXLINES];
|
||||
|
||||
int readlines(char *lineptr[], char lines[], int nlines);
|
||||
void writelines(char *lineptr[], int nlines);
|
||||
void qsort(char *lineptr[], int left, int right);
|
||||
|
||||
|
||||
|
||||
/* 5-7 exc */
|
||||
int main()
|
||||
{
|
||||
int nlines;
|
||||
char lines[MAXLINES * MAXLEN];
|
||||
|
||||
if ((nlines = readlines(lineptr, lines, MAXLINES)) >= 0){
|
||||
qsort(lineptr, 0, nlines - 1);
|
||||
writelines(lineptr, nlines);
|
||||
return 0;
|
||||
} else {
|
||||
printf("error: input too big to sort\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int getln(char *, int);
|
||||
|
||||
/* readlines: read input lines */
|
||||
int readlines(char *lineptr[], char lines[], int maxlines)
|
||||
{
|
||||
int len, nlines;
|
||||
char line[MAXLEN];
|
||||
char *lineend;
|
||||
lineend = lines + (MAXLINES * MAXLEN);
|
||||
|
||||
nlines = 0;
|
||||
while ((len = getln(line, MAXLEN)) > 0)
|
||||
if (nlines >= maxlines || lines + len >= lineend)
|
||||
return -1;
|
||||
else {
|
||||
line[len - 1] = '\0';
|
||||
strcpy(lines, line);
|
||||
lineptr[nlines++] = lines;
|
||||
lines += len;
|
||||
}
|
||||
return nlines;
|
||||
}
|
||||
|
||||
void writelines(char *lineptr[], int nlines)
|
||||
{
|
||||
while(nlines-- > 0)
|
||||
printf("%s\n", *lineptr++);
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/* getln: get line into s, return length */
|
||||
int getln(char s[], int lim)
|
||||
{
|
||||
int c, i;
|
||||
|
||||
i = 0;
|
||||
while (--lim > 0&& (c = getchar()) != EOF && c != '\n')
|
||||
s[i++] = c;
|
||||
if (c == '\n') /* line's end */
|
||||
s[i++] = c;
|
||||
s[i] = '\0'; /* string's end */
|
||||
return i;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAXLINES 5000
|
||||
#define MAXLEN 1000
|
||||
|
||||
char *lineptr[MAXLINES]; /* pointer to text lines */
|
||||
|
||||
int readlines(char *lineptr[], int nlines);
|
||||
void writelines(char *lineptr[], int nlines);
|
||||
|
||||
void qsort(char *lineptr[], int left, int right);
|
||||
|
||||
|
||||
/* sort arrays in alphabetical order */
|
||||
int main()
|
||||
{
|
||||
int nlines;
|
||||
|
||||
if ((nlines = readlines(lineptr, MAXLINES)) >= 0){
|
||||
qsort(lineptr, 0, nlines-1);
|
||||
writelines(lineptr, nlines);
|
||||
return 0;
|
||||
} else {
|
||||
printf("error: input too big to sort\n");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* qsort: sort v[left]...v[right] into increasing order */
|
||||
|
||||
void swap(char *v[], int i, int j)
|
||||
{
|
||||
char *temp = v[i];
|
||||
v[i] = v[j];
|
||||
v[j] = temp;
|
||||
}
|
||||
|
||||
void qsort(char *v[], int left, int right)
|
||||
{
|
||||
int i, last;
|
||||
void swap(char *v[], int i, int j);
|
||||
|
||||
if (left >= right)
|
||||
return;
|
||||
swap(v, left, (left+right)/2);
|
||||
last = left;
|
||||
for (i = left+i; i <= right; i++)
|
||||
if (strcmp(v[i], v[left]) < 0 ) /* TODO rewrite to compare the length */
|
||||
if (strcmp(v[i], v[left]) < 0 )
|
||||
swap(v, ++last, i);
|
||||
swap(v, left, last);
|
||||
qsort(v, left, last-1);
|
||||
qsort(v, last+1, right);
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAXLEN 1000
|
||||
|
||||
int getln(char *, int);
|
||||
char *alloc(int);
|
||||
|
||||
/* readlines: read input lines */
|
||||
int readlines( char *lineptr[], int maxlines)
|
||||
{
|
||||
int len, nlines;
|
||||
char *p, line[MAXLEN];
|
||||
|
||||
nlines = 0;
|
||||
while ((len = getln(line, MAXLEN)) > 0)
|
||||
if (nlines >= maxlines || (p = alloc(len)) == NULL)
|
||||
return -1;
|
||||
else{
|
||||
line[len-1] = '\0';
|
||||
strcpy(p, line);
|
||||
lineptr[nlines++] = p;
|
||||
}
|
||||
return nlines;
|
||||
}
|
||||
|
||||
/* writelines: write output lines */
|
||||
void writelines(char *lineptr[], int nlines)
|
||||
{
|
||||
while(nlines-- > 0)
|
||||
printf("%s\n", *lineptr++);
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
#include <stdio.h>
|
||||
#define ALLOCSIZE 10000
|
||||
|
||||
static char allocbuf[ALLOCSIZE];
|
||||
static char *allocp = allocbuf; /* next free position */
|
||||
|
||||
char *alloc(int n)
|
||||
{
|
||||
if(allocbuf + ALLOCSIZE - allocp >= n){ /* it fits */
|
||||
allocp += n;
|
||||
return allocp - n;
|
||||
} else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void afree(char *p)
|
||||
{
|
||||
if(p >= allocbuf && p < allocbuf + ALLOCSIZE)
|
||||
allocp = p;
|
||||
}
|
||||
|
||||
int strlen(char *s)
|
||||
{
|
||||
char *p = s;
|
||||
|
||||
while (*p != '\0')
|
||||
p++;
|
||||
return p - s;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
printf("%i", strlen("king"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ int main()
|
|||
int sum03 = 0, sum05 = 0, sum15 = 0;
|
||||
int i;
|
||||
|
||||
for(i = 0;i<1000; i++){
|
||||
for(i = 0; i < 1000 ; i++){
|
||||
if (i%3 == 0 )
|
||||
sum03 += i;
|
||||
if (i%5 == 0)
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
|
||||
|
||||
int fib(int m)
|
||||
{
|
||||
if (m == 1 || m == 0) return m;
|
||||
return (fib(m-1) + m);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
long l, ll;
|
||||
long long sum = 0;
|
||||
|
||||
l = 1;
|
||||
while ((ll = fib(l++)) < 4000000)
|
||||
if (ll % 2 == 0)
|
||||
sum += ll;
|
||||
printf("%lld\n", sum);
|
||||
printf("%i", fib(2800));
|
||||
return 0;
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
long FindMax(int*);
|
||||
|
||||
int main(void){
|
||||
|
||||
FILE *f;
|
||||
long ans;
|
||||
int *arr, i=0;
|
||||
char c;
|
||||
|
||||
arr = (int*) malloc (sizeof(int)*1000);
|
||||
f=fopen("inp", "r");
|
||||
|
||||
if(f)
|
||||
while(!feof(f)){
|
||||
fscanf(f, "%c", &c);
|
||||
arr[i++]=(int)c-38;
|
||||
}
|
||||
else
|
||||
printf("Error opening the file");
|
||||
|
||||
printf("Anwer: %ld", FindMax(arr));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
long FindMax(int *arr){
|
||||
int i,j;
|
||||
long max=0, product=1;
|
||||
for(i=0; i< 995; i++){
|
||||
product=1;
|
||||
for(j=0; j<5; i++)
|
||||
product*= arr[i+j];
|
||||
if(product>max)
|
||||
max=product;
|
||||
}
|
||||
return max;
|
||||
}
|
Binary file not shown.
|
@ -1,20 +0,0 @@
|
|||
73167176531330624919225119674426574742355349194934
|
||||
96983520312774506326239578318016984801869478851843
|
||||
85861560789112949495459501737958331952853208805511
|
||||
12540698747158523863050715693290963295227443043557
|
||||
66896648950445244523161731856403098711121722383113
|
||||
62229893423380308135336276614282806444486645238749
|
||||
30358907296290491560440772390713810515859307960866
|
||||
70172427121883998797908792274921901699720888093776
|
||||
65727333001053367881220235421809751254540594752243
|
||||
52584907711670556013604839586446706324415722155397
|
||||
53697817977846174064955149290862569321978468622482
|
||||
83972241375657056057490261407972968652414535100474
|
||||
82166370484403199890008895243450658541227588666881
|
||||
16427171479924442928230863465674813919123162824586
|
||||
17866458359124566529476545682848912883142607690042
|
||||
24219022671055626321111109370544217506941658960408
|
||||
07198403850962455444362981230987879927244284909188
|
||||
84580156166097919133875499200524063689912560717606
|
||||
05886116467109405077541002256983155200055935729725
|
||||
71636269561882670428252483600823257530420752963450
|
Loading…
Reference in New Issue