Вы находитесь на странице: 1из 13

D. Estrin,Tutorial Wireless Sensor Networks -Part IV: Sensor Network Protocols, MobiCom,2002.M. Young, The Technical Writer's Handbook.

Mill Valley, CA: University Science, 1989.

article:l16633 size nsz 2599 sudo apt-get install ns2 sudo apt-get install nam sudo apt-get install xgraph nam /*#include <stdio.h> #include <conio.h> int main() { int fahr=0; int cels=0; int upper=300; int step=5; while(fahr<=upper) { cels=5*(fahr-32)/9; printf("%d \t %d\n",fahr,cels); fahr=fahr+step; } getch(); return 0; }* #include<stdio.h> #include<conio.h> void figure2(int x);//function prototype int main() { int n; printf("enter n value\n"); /*how many lines* scanf("%d",&n); figure2(n); /*function call* getch(); } void figure2(int x) /*function definition* { int i,j,l; for(j=i-1;j;j--) printf("%d",j); printf("\n"); /*print in next line*/ /*end of outer loop*/ /*end of function figure2*/

/*

/* #include<stdio.h> #include<conio.h> int max(int x); int main() { char i[5]=fjkl; printf("%d",max(i)); getch(); } int max(int x) { printf("%d",x++); } /*int fahr; for(fahr=0;fahr<=300; fahr=fahr+20) { printf("%3d %7.2f\n",fahr,(5.0/9.0)*(fahr-32.0)); } getch(); }*

#include <stdio.h> #include <conio.h> main() { int n, c, k; printf("Enter an integer in decimal number system\n"); scanf("%d",&n); printf("%d in binary number system is:\n", n); for ( c = 31 ; c >= 0 ; c-- ) { k = n >> c; if ( k & 1 ) printf("1"); else printf("0"); }

printf("\n"); getch(); return 0; } */

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> int main() { char str[80]; FILE *fp; if((fp=fopen("TEST","w"))==NULL){ printf("cannot open file.\n"); } do{ printf("enter a string(CR to quit):\n"); gets(str); strcat(str,"\n"); fputs(str,fp); }while(*str!='\n'); return 0; }

#include<stdio.h> #include<conio.h> #include<stdlib.h> char matrix[3][3]; char check(void); void init_matrix(void); void get_player_move(void); void get_computer_move(void); void disp_matrix(void); int main() { char done=' '; init_matrix(); do {

disp_matrix(); get_player_move(); done=check(); if(done!=' ')break; get_computer_move(); done=check(); }while(done=' '); if(done='X')printf("you won!\n"); else printf("I won!!!!!!!!\n"); disp_matrix(); getch(); return 0; } void init_matrix(void) { int i,j; for(i=0;i<3;i++) for(j=0;j<3;j++) matrix[i][j]=' '; } void get_player_move(void) { int x,y; printf("enter x and y co-ordinate for move\n"); scanf("%d %*c %d",&x,&y); x--;y--; if(matrix[x][y]!=' '){printf("invalid move"); get_player_move(); } else matrix[x][y]='X'; } void get_computer_move(void) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) if(matrix[i][j]==' ')break; if(matrix[i][j]==' ')break; } if(i*j==9){

printf("draw!"); exit(0); } else matrix[i][j]='Y'; } void disp_matrix(void) { int t; for(t=0;t<3;t++){ printf(" %c | %c | %c ",matrix[t][0],matrix[t][1],matrix[t][2]); if(t!=2)printf("\n---|---|---\n"); } printf("\n"); } char check(void) { int i; for(i=0;i<3;i++) if(matrix[i][0]==matrix[i][1]&& matrix[i][0]==matrix[i][2])return matrix[i][0]; for(i=0;i<3;i++) if(matrix[0][i]==matrix[1][i]&& matrix[0][i]==matrix[2][i])return matrix[0][i]; if(matrix[0][0]==matrix[1][1]&& matrix[1][1]==matrix[2][2])return matrix[0][0]; if(matrix[0][2]==matrix[1][1]&& matrix[0][2]==matrix[2][2])return matrix[2][0]; return ' '; }

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> void check(char *a,char *b,int (*cmp)(const char *,const char *)); int compvalues(const char *a,const char *b); int main() { char s1[80],s2[80]; printf("enter two values or two strings\n"); gets(s1); gets(s2); if(isdigit(*s1)){ printf("testing values for equality\n"); check(s1,s2,compvalues); }

else { printf("testing strings for equality\n"); check(s1,s2,strcmp); } getch(); return 0; } void check(char *a,char *b,int (*cmp)(const char *,const char *)) { if(!(*cmp)(a,b))printf("equal"); else printf("not equal"); } int compvalues(const char *a,const char *b) { if(atoi(a)==atoi(b))return 0; else return 1; }

#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 100 struct addr{ char name[20]; char street[20]; char city[20]; char state[20]; unsigned long int zip; }addr_list[MAX]; void init_list(),enter(),delet(),list(),load(),save(); int menu_select(),find_free(); int main() { char choice; init_list(); for(;;){ choice=menu_select(); switch(choice){ case 1:enter(); break;

case 2:delet(); break; case 3:list(); break; case 4:load(); break; case 5:save(); break; case 6:exit(0); } } return 0; } void init_list() { register int t; for(t=0;t<MAX;++t)addr_list[t].name[0]='\0'; } int menu_select() { char s[80]; int c; printf("1.enter a name\n"); printf("2.delete a name\n"); printf("3.list the file\n"); printf("4.save the file\n"); printf("5.load the file\n"); printf("6.exit\n"); do{ printf("enter your choice\n"); gets(s); c=atoi(s); }while(c<0||c>6); return c; } void enter() { int slot; char s[80]; slot=find_free(); if(slot==-1){ printf("\nlist full"); return;

} printf("enter name\n"); gets(addr_list[slot].name); printf("enter street\n"); gets(addr_list[slot].street); printf("enter city\n"); gets(addr_list[slot].city); printf("enter state\n"); gets(addr_list[slot].state); printf("enter name\n"); gets(s); addr_list[slot].zip=strtoul(s, '\0',10); } int find_free() { register int t; for(t=0; addr_list[t].name[0] && t<MAX; ++t); if(t==MAX)return -1; return t; } void delet() { register int slot; char s[80]; printf("enter your record#\n"); gets(s); slot=atoi(s); if(slot<=0&&slot<MAX) addr_list[slot].name[0]='\0'; } void list() { register int t; for(t=0; t<MAX; ++t) { if(addr_list[t].name[0]){ printf("%s\n",addr_list[t].name); printf("%s\n",addr_list[t].street); printf("%s\n",addr_list[t].city); printf("%s\n",addr_list[t].state);

printf("%lu\n\n",addr_list[t].zip); } } printf("\n\n"); } void save() { FILE *fp; register int i; if((fp=fopen("maillist","wb"))==NULL){ printf("cannot open file\n"); return; } for(i=0; i<MAX; i++) if(*addr_list[i].name) if(fwrite(&addr_list[i],sizeof(struct addr),1,fp)!=1) printf("file write error\n"); fclose(fp); } void load() { FILE *fp; register int i; if((fp=fopen("maillist","wb"))==NULL){ printf("cannot open file\n"); return; } init_list(); for(i=0; i<MAX; i++) if(fread(&addr_list[i],sizeof(struct addr),1,fp)!=1) { if(feof(fp)) break; printf("file write error\n"); } fclose(fp); }

#include <stdio.h> #include <conio.h>

#define DELAY 128000 struct my_time { int hours; int minutes; int seconds; }; void display(struct my_time *t); void update(struct my_time *t); void delay(); int main() { struct my_time systime; systime.hours=0; systime.minutes=0; systime.seconds=0; for(;;) { update(&systime); display(&systime); } getch(); return 0; } void update(struct my_time *t) { t->seconds++; if(t->seconds==60) { t->seconds=0; t->minutes++; } if(t->minutes==60) { t->minutes=0; t->hours++; } if(t->hours==24) t->hours=0; delay(); } void display(struct my_time *t) { printf("%02d:", t->hours);

printf("%02d:", t->minutes); printf("%02d\n", t->seconds); } void delay() { long int t; for(t=1; t<DELAY; ++t); } #include <stdio.h> #include <conio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *in, *out; char ch; if(argc!=3) printf("enter a filename\n"); exit(1); if((in=fopen(argv[1], "rb"))==NULL) { printf("cannot open source file\n"); exit(1); } if((out=fopen(argv[1], "rb"))==NULL) { printf("cannot open destination file\n"); exit(1); } while(!feof(in)) { ch=getc(in); if(!feof(in)) putc(ch, out); } fclose(in); fclose(out); getch(); return 0; }

#include <stdio.h> #include <conio.h>

#include <stdlib.h> #define TABSIZE 8 #define IN 0 #define OUT 1 void err(int e); int main(int argc, char *argv[]) { FILE *in, *out; int tab,i; char ch; if(argc!=3) printf("usage: detab<in><out>\n"); getch(); exit(1); if((in=fopen(argv[1], "rb"))==NULL) { printf("cannot open *s", argv[1]); getch(); exit(1); } if((in=fopen(argv[1], "wb"))==NULL) { printf("cannot open *s", argv[1]); exit(1); } tab=0; do{ ch=getc(in); if(ferror(in)) err(IN); if(ch=='\t') { for(i=tab; i<8; i++) { putc(' ',out); if(ferror(out)) err(OUT); } tab=0; } else { putc(ch,out); if(ferror(out)) err(OUT); tab++; if(tab==TABSIZE) tab=0; if(ch=='\n' || ch=='\r')tab=0;

} } while(!feof(in)); fclose(in); fclose(out); getch(); return 0; } void err(int e) { if(e==IN) printf("error on input\n"); else printf("errror on output\n"); exit(1); }

Вам также может понравиться