برنامه های محاسبات

پیشرو پسرو

تقسیم و تفاضل

تکین

لانگ لانژ

وتری

تصنیف

نیوتن

نابجایی

برنامه ای که رشته هایی را از ورودی می خواند و در یک فایل متنی قرار می دهد ..

و سپس محتویات فایل را خوانده و در صفحه نمایش نشان می دهد .

#include
#include
#include
#include
#define  TAB_SIZE   8
#define  OUT  1
#define  IN   1
void err(int) ;
int main(int argc , char *argv[])
{
    FILE *in , *out;
    int tab, i ;
    char ch ;
    clrscr();
    if(argc != 3){
 printf("\n incorrect number of parameters") ;
 getch() ;
 exit(1) ;
    }
    in = fopen(argv[1] , "r");
    out = fopen(argv[2] , "wb") ;
    if (!out) {
 printf("\n cannot open output file.") ;
 getch() ;
 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) ;
  } //end of for
  tab = 0;
       } //end of if
      else {
    putc(ch , out) ;
    if (ferror(out))
       err(OUT) ;
    tab ++ ;
    if (tab == TAB_SIZE || ch == '\n' || ch == '\r')
       tab = 0;
      }//end of else
    } while (!feof(in)) ;
    fcloseall() ;
    printf("Successfuly changed!");
    getch();
    return 0;
}
//************************
void err(int eror)
{
     if (eror == IN)
       printf("\n error on input file. press a key ...");
     getch() ;
     exit(1);
}

برنامه زیر رشته هایی از صفحه کلید می خواند در یک فایل می ریزد .

 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
  {
    FILE *fp ;
    char str[80];
    clrscr();
    if(!(fp = fopen("test","w+"))) {
       printf("cannot open file.") ;
       getch();
       exit(1);
    }
    printf("enter strings(Enter to quit):\n");
    while (1)  {
       gets(str);
       if(!str[0])
   break ;
       strcat(str,"\n");
       fputs(str, fp);
    }
    printf("the content of file is:\n\n");
      rewind(fp);
      fgets(str, 79, fp);
      while (!feof(fp)) {
  printf("%s", str);
  fgets(str, 79, fp) ;
      }
      fclose(fp);
      getch();
      return 0;
}

 

مثال

int main()
{
    FILE *in, *out ;

  in = fopen("test.dat", "w");

   out = fopen("output.dat", "w");

  in = fopen("test.dat","r");

  fclose(out);

شیوه پردازش فایل

w نوشتن پرونده متنی یا سطری

r  خواندن پرونده متنی

a  پیوست کردن به پرونده متنی

wb نوشتن پرونده دوگانی (رکوردی - نویسه ای)

rb  خواندن پرونده دوگانی

+w  نوشتن و روزآمد کردن پرونده متنی

+r  خواندن و روزآمد کردن پرونده متنی

+a  پیوست کردن به پرونده متنی و روزآمد کردن آن

w+b نوشتن و روزآمد کردن پرونده دوگانی

r+b  خواندن و روزآمد کردن پرونده دوگانری

a+b پیوست کردن به پرونده دوگانی و روزآمد کردن آن

 

توابع فایلها

باز کردن فایل fopen

چاپ پرونده fprintf

پیمایش پرونده fseek

پایان پرونده fseek

بستن پرونده fclose

توضیح

دوستان از من راجع به برنامه ها توضیح خواستند . من در اسرع وقت واستون توضیح برنامه ها رو می نویسم !

 

موفق باشید .               باران

فایلها

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
    FILE *in, *out ;
    char ch;
    clrscr();
    in = fopen("test.dat", "w");
    if (!in) {
       printf("cannot open file\n");
       getch();
       exit(1);
    }
    printf("\n enter characters($ for end):");
    do {
  ch = getchar();
  putc(ch, in);
    } while(ch != '$');
    fclose(in);
    out = fopen("output.dat", "w");
    if(!out) {
  printf("cannot open out file");
  getch();
  exit(1);
    }
    in = fopen("test.dat","r");
    if(!in) {
       printf("can not open input file");
       getch();
       exit(1);
    }
    ch = getc(in);
    while(ch != EOF) {
    putc(ch, out);
    ch = getc(in);
    }//end of while
    fclose(in);
    fclose(out);
    printf("\n file test.dat transfered to output.dat.");
    getch();
    return 0;
}

برنامه

برنامه ای که مشخصات افرادی را گرفته در آرایه ای از ساختمانها قرار می دهد :

عمل درج و حذف نیز انجام می دهد .

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 100
struct  addr{
    char name[10] ;
    char street[15] ;
    char city[10] ;
    char state[3] ;
    unsigned long int zip;
} addr_info[MAX] ;
void init_list_all(void), enter(void) ;
void delete_e(void);
void list_all(void) ;
int menu_select(void), find_free(void);
int main()
{
   char choice , ch ;
   clrscr();
   init_list_all() ;
   for(;;){
      choice = menu_select() ;
      switch(choice) {
        case 1:  enter();  break ;
        case 2:  delete_e(); break ;
        case 3:  list_all();   break ;
        case 4:  exit(0) ;
      } //end of switch
   }//end of for
}
//****************************
void init_list_all(void)
{
     register int t ;
     for(t = 0 ; t < MAX; ++t)
  addr_info[t].name[0]='\0';
}
//****************************
int menu_select(void)
{
      char s[10] ;
      int c ;
      clrscr() ;
      gotoxy(29, 6);
      printf("1) << enter a name >>");
      gotoxy(29, 8);
      printf("2) << delete_e a name >>");
      gotoxy(29, 10);
      printf("3) << list_all the file >>");
      gotoxy(29, 12);
      printf("4) <<      Quit     >>");
      do {
   gotoxy(24, 15) ;
   printf(" Please enter your choice(1-4):");
   gets(s) ;
   c = atoi(s) ;
      } while(c < 0 || c > 4) ;
      return (c);
}

//****************************
void enter(void)
{
  int slot ;
  char s[80] ;
  slot = find_free() ;
  if(slot == -1) {
 printf("\n list full");
 return ;
  }
  gotoxy(5, 17) ;
  printf("enter name:") ;
  gets(addr_info[slot].name);
  gotoxy(40, 17) ;
  printf("enter street:") ;
  gets(addr_info[slot].street);
  gotoxy(5, 19) ;
  printf("enter city:") ;
  gets(addr_info[slot].city) ;
  gotoxy(40, 19) ;
  printf("enter state code:") ;
  gets(addr_info[slot].state) ;
  gotoxy(22, 21) ;
  printf("enter zip:") ;
  gets(s) ;
  addr_info[slot].zip = strtoul(s,'\0',10) ;
}
//****************************
find_free(void)
{
      register int t ;
      for(t = 0 ; addr_info[t].name[0]
   && t < MAX ; ++t) ;
      if(t == MAX) return -1 ;
     return t ;
}
//****************************
void delete_e(void)
{
      int slot ;
      gotoxy(28, 19) ;
      printf("enter record #(0-99):");
      scanf("%d",&slot) ;
      if(slot >= 0 && slot < MAX)
  addr_info[slot].name[0] = '\0';
}
//****************************
void list_all(void)
{
     register  int t ;
     int r = 0 ;
     char ch ;
     clrscr() ;
     gotoxy(25, 2) ;
     printf(" << all information in list are:>>") ;
     gotoxy(13,3) ;
     printf("**********************");
     printf("*******************") ;
     printf("********************") ;
     gotoxy(10, 4);
     printf("     name      street         city      ");
     printf("  state            zip");
     gotoxy(10, 5);
     printf("   ________    _______      ______     ") ;
     printf("  ______          ____");
     for(t = 0; t < MAX ; ++t) {
 if(addr_info[t].name[0]) {
      gotoxy(14, 6+r) ;
      printf("%s ",addr_info[t].name);
      gotoxy(26, 6+r) ;
      printf("%s ",addr_info[t].street);
      gotoxy(40, 6+r) ;
      printf("%s ",addr_info[t].city);
      gotoxy(54, 6+r) ;
      printf("%s ",addr_info[t].state);
      gotoxy(70, 6+r) ;
      printf("%lu ",addr_info[t].zip);
      r++ ;
 }
     }
     gotoxy(13, 6+r) ;
     printf("********************");
     printf("*********************");
     printf("********************");
     gotoxy(27, 7+r) ;
     printf("press any key to continue");
     getch() ;
}

 

نیوتن


    #include
    #include
    #include
    #include
    #include

    #define retf(x)  sin(x)
    #define FALSE 0
    #define TRUE 1

     unsigned char chk_error=FALSE;

     void main(void)
     {
     double nioton_solve(double,double);
     double accuracy,p0,answer;

     clrscr();
     printf("Enter Accuracy For Solving Sin(x)=0: \0");
     cin>>accuracy;
     printf("Enter Start Point P0:\0");
     cin>>p0;

     answer=nioton_solve(accuracy,p0);

     if(chk_error)
     printf("Error In calculation.\a\n\0");
     else
     {
     printf("Answer Of sin(x)=0 With Nioton Method And start Point= \0");
     printf("&f\n\0",answer);
     }
     }
     double d1x(double xi)
     {
     double h=0.00001;
     double yi_1=retf(xi);
     return((retf(xi+h)-yi_1)/h);
     }

     double d2x(double xi)
     {
     double h=0.00001;
     double yi_1=retf(xi);
     return((retf(xi-h)-2*yi_1+retf(xi+h))/(h*h));
     }
     double nioton_solve(double accuracy,double p0)
     {
     double y0=retf(p0),df,d2f,dp,p1,y1,relerror;
     unsigned int max =99,cmax=1;
     do
     {
     df=d1x(p0);
     if(df==0)
     {
     chk_error=TRUE;
     return(-1);
     }
     else
     dp=y0/df;
     p1=p0-dp;
     y1=retf(p1);
     relerror=2*fabs(dp)/(fabs(p1)+accuracy);
     if ((relerror     return(p1);
     p0=p1;
     y0=y1;
     cmax++;
     }while (cmax<=max);
     return (p0);
     }

مثال ها ...

یافتن گراف مینیمم

برنامه یافتن فایل با پسوند jpg

برنامه ضرب ماتریس 3×3

کراسکال

دتر مینان ماتریس ان در ان