Difference between revisions of "2016-17 Programmi C"
		
		
		
		
		
		Jump to navigation
		Jump to search
		
				
		
		
	
m  | 
				m  | 
				||
| Line 1: | Line 1: | ||
| + | == char by char copy ==  | ||
<syntaxhighlight lang=C>  | <syntaxhighlight lang=C>  | ||
#include <stdio.h>  | #include <stdio.h>  | ||
| Line 6: | Line 7: | ||
   while ((c = getchar()) != EOF)  |    while ((c = getchar()) != EOF)  | ||
     putchar(c);  |      putchar(c);  | ||
| + | }  | ||
| + | </syntaxhighlight>  | ||
| + | |||
| + | == arrays, pointers and structs ==  | ||
| + | <syntaxhighlight lang=C>  | ||
| + | #include <stdio.h>  | ||
| + | |||
| + | char *spoint="hello";  | ||
| + | char sarr[]="hello";  | ||
| + | struct strs {  | ||
| + |   char s[6];  | ||
| + | } sstruct = {"hello"};  | ||
| + | |||
| + | void foo(char *s) {  | ||
| + |   s[4]=0;  | ||
| + | }  | ||
| + | |||
| + | void bar(struct strs s) {  | ||
| + |   s.s[4]=0;  | ||
| + |   printf("from bar %s\n", s.s);  | ||
| + | }  | ||
| + | |||
| + | int main(int argc, char *argv[]) {  | ||
| + |   printf("%s %s %s\n", spoint, sarr, sstruct.s);  | ||
| + |   foo(sarr);  | ||
| + |   printf("%s %s %s\n", spoint, sarr, sstruct.s);  | ||
| + |   bar(sstruct);  | ||
| + |   printf("%s %s %s\n", spoint, sarr, sstruct.s);  | ||
| + |   // test the following statements, one at a time  | ||
| + |   //spoint = sarr;  | ||
| + |   //sarr = spoint;  | ||
| + |   foo(spoint);  | ||
| + |   printf("%s %s %s\n", spoint, sarr, sstruct.s);  | ||
| + | }  | ||
| + | </syntaxhighlight>  | ||
| + | |||
| + | == iteration and recursion ==  | ||
| + | |||
| + | <syntaxhighlight lang=C>  | ||
| + | #include <stdio.h>  | ||
| + | |||
| + | struct elem {  | ||
| + |   int val;  | ||
| + |   struct elem *next;  | ||
| + | };  | ||
| + | |||
| + | struct elem *head = NULL;  | ||
| + | |||
| + | struct elem *rinsert(struct elem *new, struct elem *head) {  | ||
| + |   if (head == NULL || new->val < head->val) {  | ||
| + |     new->next = head;  | ||
| + |     return new;  | ||
| + |   } else {  | ||
| + |     head->next = rinsert(new, head->next);  | ||
| + |     return head;  | ||
| + |   }  | ||
| + | }  | ||
| + | |||
| + | struct elem *iinsert(struct elem *new, struct elem *head) {  | ||
| + |   struct elem **pnext;  | ||
| + |   for (pnext = &head;  | ||
| + |       *pnext != NULL && new->val > (*pnext)->val;  | ||
| + |       pnext = &((*pnext)->next))  | ||
| + |     ;  | ||
| + |   new->next = *pnext;  | ||
| + |   *pnext = new;  | ||
| + |   return head;  | ||
| + | }  | ||
| + | |||
| + | void rprint(struct elem *this) {  | ||
| + |   if (this) {  | ||
| + |     printf("%d ",this->val);  | ||
| + |     rprint(this->next);  | ||
| + |   }  | ||
| + | }  | ||
| + | |||
| + | void iprint(struct elem *this) {  | ||
| + |   for ( ; this != NULL; this = this->next)  | ||
| + |     printf("%d ",this->val);  | ||
| + | }  | ||
| + | |||
| + | struct elem test[]={{5},{3},{9},{1},{7}};  | ||
| + | #define NELEM (sizeof(test) / sizeof(struct elem))  | ||
| + | |||
| + | int main(int argc, char *argv[]) {  | ||
| + |   int i;  | ||
| + |   for (i = 0; i < NELEM; i++)  | ||
| + |     head = rinsert(&test[i], head);  | ||
| + |   rprint(head);  | ||
| + |   printf("\n");  | ||
| + |   iprint(head);  | ||
| + |   printf("\n");  | ||
| + |   head = NULL;  | ||
| + |   for (i = 0; i < NELEM; i++)  | ||
| + |     head = iinsert(&test[i], head);  | ||
| + |   rprint(head);  | ||
| + |   printf("\n");  | ||
| + |   iprint(head);  | ||
| + |   printf("\n");  | ||
}  | }  | ||
</syntaxhighlight>  | </syntaxhighlight>  | ||
Revision as of 07:35, 2 October 2016
char by char copy
#include <stdio.h>
int main(int argc, char *argv[]) {
  int c;
  while ((c = getchar()) != EOF)
    putchar(c);
}
arrays, pointers and structs
#include <stdio.h>
char *spoint="hello";
char sarr[]="hello";
struct strs {
  char s[6];
} sstruct = {"hello"};
void foo(char *s) {
  s[4]=0;
}
void bar(struct strs s) {
  s.s[4]=0;
  printf("from bar %s\n", s.s);
}
int main(int argc, char *argv[]) {
  printf("%s %s %s\n", spoint, sarr, sstruct.s);
  foo(sarr);
  printf("%s %s %s\n", spoint, sarr, sstruct.s);
  bar(sstruct);
  printf("%s %s %s\n", spoint, sarr, sstruct.s);
  // test the following statements, one at a time
  //spoint = sarr;
  //sarr = spoint;
  foo(spoint);
  printf("%s %s %s\n", spoint, sarr, sstruct.s);
}
iteration and recursion
#include <stdio.h>
struct elem {
  int val;
  struct elem *next;
};
struct elem *head = NULL;
struct elem *rinsert(struct elem *new, struct elem *head) {
  if (head == NULL || new->val < head->val) {
    new->next = head;
    return new;
  } else {
    head->next = rinsert(new, head->next);
    return head;
  }
}
struct elem *iinsert(struct elem *new, struct elem *head) {
  struct elem **pnext;
  for (pnext = &head;
      *pnext != NULL && new->val > (*pnext)->val;
      pnext = &((*pnext)->next))
    ;
  new->next = *pnext;
  *pnext = new;
  return head;
}
void rprint(struct elem *this) {
  if (this) {
    printf("%d ",this->val);
    rprint(this->next);
  }
}
void iprint(struct elem *this) {
  for ( ; this != NULL; this = this->next)
    printf("%d ",this->val);
}
struct elem test[]={{5},{3},{9},{1},{7}};
#define NELEM (sizeof(test) / sizeof(struct elem))
int main(int argc, char *argv[]) {
  int i;
  for (i = 0; i < NELEM; i++)
    head = rinsert(&test[i], head);
  rprint(head);
  printf("\n");
  iprint(head);
  printf("\n");
  head = NULL;
  for (i = 0; i < NELEM; i++)
    head = iinsert(&test[i], head);
  rprint(head);
  printf("\n");
  iprint(head);
  printf("\n");
}