نکات توابع سی و بعضی مهارتها

saalek110

Well-Known Member
توابع ریاضی:
PHP:
double acos(double x) -- Compute arc cosine of x.
double asin(double x) -- Compute arc sine of x.
double atan(double x) -- Compute arc tangent of x.
double atan2(double y, double x) -- Compute arc tangent of y/x, using the signs of both arguments to determine the quadrant of the return value.
double ceil(double x) -- Get smallest integral value that exceeds x.
double cos(double x) -- Compute cosine of angle in radians.
double cosh(double x) -- Compute the hyperbolic cosine of x.
div_t div(int number, int denom) -- Divide one integer by another.
double exp(double x -- Compute exponential of x
double fabs (double x ) -- Compute absolute value of x.
double floor(double x) -- Get largest integral value less than x.
double fmod(double x, double y) -- Divide x by y with integral quotient and return remainder.
double frexp(double x, int *expptr) -- Breaks down x into mantissa and exponent of no.
labs(long n) -- Find absolute value of long integer n.
double ldexp(double x, int exp) -- Reconstructs x out of mantissa and exponent of two.
ldiv_t ldiv(long number, long denom) -- Divide one long integer by another.
double log(double x) -- Compute log(x).
double log10 (double x ) -- Compute log to the base 10 of x.
double modf(double x, double *intptr) -- Breaks x into fractional and integer parts.
double pow (double x, double y) -- Compute x raised to the power y.
double sin(double x) -- Compute sine of angle in radians.
double sinh(double x) - Compute the hyperbolic sine of x.
double sqrt(double x) -- Compute the square root of x.
void srand(unsigned seed) -- Set a new seed for the random number generator (rand).
double tan(double x) -- Compute tangent of angle in radians.
double tanh(double x) -- Compute the hyperbolic tangent of x.
منبع
 
آخرین ویرایش:

saalek110

Well-Known Member
ceil
PHP:
/* Example using ceil by TechOnTheNet.com */

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the ceil of */
    value = 1.6;

    /* Calculate the ceil of value */
    result = ceil(value);

    /* Display the result of the calculation */
    printf("The ceil of %f is %f\n", value, result);

    return 0;
}
PHP:
The ceil of 1.600000 is 2.000000
منبع
 

saalek110

Well-Known Member
floor
PHP:
/* Example using floor by TechOnTheNet.com */

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the floor of */
    value = 1.6;

    /* Calculate the floor of value */
    result = floor(value);

    /* Display the result of the calculation */
    printf("The floor of %f is %f\n", value, result);

    return 0;
}
کد:
The floor of 1.600000 is 1.000000
منبع
 

saalek110

Well-Known Member
sin
PHP:
/* sin example */

#include <stdio.h>      /* printf */
#include <math.h>       /* sin */

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 30.0;
  result = sin (param*PI/180);
  printf ("The sine of %f degrees is %f.\n", param, result );
  return 0;
}
کد:
The sine of 30.000000 degrees is 0.500000.
منبع
 

saalek110

Well-Known Member
cos
PHP:
/* cos example */
#include <stdio.h>      /* printf */
#include <math.h>       /* cos */

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 60.0;
  result = cos ( param * PI / 180.0 );
  printf ("The cosine of %f degrees is %f.\n", param, result );
  return 0;
}
کد:
The cosine of 60.000000 degrees is 0.500000.
منبع
 

saalek110

Well-Known Member
round floor ceil trunc

PHP:
/* round vs floor vs ceil vs trunc */
#include <stdio.h>      /* printf */
#include <math.h>       /* round, floor, ceil, trunc */

int main ()
{
  const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n";
  printf ("value\tround\tfloor\tceil\ttrunc\n");
  printf ("-----\t-----\t-----\t----\t-----\n");
  printf (format, 2.3,round( 2.3),floor( 2.3),ceil( 2.3),trunc( 2.3));
  printf (format, 3.8,round( 3.8),floor( 3.8),ceil( 3.8),trunc( 3.8));
  printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5));
  printf (format,-2.3,round(-2.3),floor(-2.3),ceil(-2.3),trunc(-2.3));
  printf (format,-3.8,round(-3.8),floor(-3.8),ceil(-3.8),trunc(-3.8));
  printf (format,-5.5,round(-5.5),floor(-5.5),ceil(-5.5),trunc(-5.5));
  return 0;
}
کد:
value   round   floor   ceil    trunc
-----   -----   -----   ----    -----
2.3     2.0     2.0     3.0     2.0
3.8     4.0     3.0     4.0     3.0
5.5     6.0     5.0     6.0     5.0
-2.3    -2.0    -3.0    -2.0    -2.0
-3.8    -4.0    -4.0    -3.0    -3.0
-5.5    -6.0    -6.0    -5.0    -5.0
منبع
 

saalek110

Well-Known Member
PHP:
// Using the goto statement
    #include <stdio.h>
    #include <math.h>
    // function main begins program execution
    int main( void )  {
    // calculates and outputs the square root
   printf( "sqrt(%.1f) = %.1f\n", 900.0, sqrt( 900.0 ) );
   printf( "sqrt(%.1f) = %.1f\n", 9.0, sqrt( 9.0 ) );
  
    // calculates and outputs the exponential function e to the x
   printf( "exp(%.1f) = %f\n", 1.0, exp( 1.0 ) );
   printf( "exp(%.1f) = %f\n", 2.0, exp( 2.0 ) );
  
    // calculates and outputs the logarithm (base e)
   printf( "log(%f) = %.1f\n", 2.718282, log( 2.718282 ) );
   printf( "log(%f) = %.1f\n", 7.389056, log( 7.389056 ) );
  
    // calculates and outputs the logarithm (base 10)
   printf( "log10(%.1f) = %.1f\n", 1.0, log10( 1.0 ) );
   printf( "log10(%.1f) = %.1f\n", 10.0, log10( 10.0 ) );
   printf( "log10(%.1f) = %.1f\n", 100.0, log10( 100.0 ) );
  
    // calculates and outputs the absolute value
   printf( "fabs(%.1f) = %.1f\n", 13.5, fabs( 13.5 ) );
   printf( "fabs(%.1f) = %.1f\n", 0.0, fabs( 0.0 ) );
   printf( "fabs(%.1f) = %.1f\n", -13.5, fabs( -13.5 ) );
  
    // calculates and outputs ceil( x )
   printf( "ceil(%.1f) = %.1f\n", 9.2, ceil( 9.2 ) );
   printf( "ceil(%.1f) = %.1f\n", -9.8, ceil( -9.8 ) );
    // calculates and outputs floor( x )
   printf( "floor(%.1f) = %.1f\n", 9.2, floor( 9.2 ) );
   printf( "floor(%.1f) = %.1f\n", -9.8, floor( -9.8 ) );
  
    // calculates and outputs pow( x, y )
   printf( "pow(%.1f, %.1f) = %.1f\n", 2.0, 7.0, pow( 2.0, 7.0 ) );
   printf( "pow(%.1f, %.1f) = %.1f\n", 9.0, 0.5, pow( 9.0, 0.5 ) );
  
    // calculates and outputs fmod( x, y )
   printf( "fmod(%.3f/%.3f) = %.3f\n", 13.657, 2.333,
      fmod( 13.657, 2.333 ) );
    // calculates and outputs sin( x )
   printf( "sin(%.1f) = %.1f\n", 0.0, sin( 0.0 ) );
  
    // calculates and outputs cos( x )
   printf( "cos(%.1f) = %.1f\n", 0.0, cos( 0.0 ) );
  
    // calculates and outputs tan( x )
   printf( "tan(%.1f) = %.1f\n", 0.0, tan( 0.0 ) );
    } // end main
کد:
Output:
    sqrt(900.0) = 30.0
    sqrt(9.0) = 3.0
    exp(1.0) = 2.718282
    exp(2.0) = 7.389056
    log(2.718282) = 1.0
    log(7.389056) = 2.0
    log10(1.0) = 0.0
    log10(10.0) = 1.0
    log10(100.0) = 2.0
    fabs(13.5) = 13.5
    fabs(0.0) = 0.0
    fabs(-13.5) = 13.5
    ceil(9.2) = 10.0
    ceil(-9.8) = -9.0
    floor(9.2) = 9.0
    floor(-9.8) = -10.0
    pow(2.0, 7.0) = 128.0
    pow(9.0, 0.5) = 3.0
    fmod(13.657/2.333) = 1.992
    sin(0.0) = 0.0
    cos(0.0) = 1.0
    tan(0.0) = 0.0
منبع
 

saalek110

Well-Known Member
دنبال یک رسم بودم این سورس را پیدا کردم. نظری در مورد کیفیت کارش ندارم. خودتان بررسی کنید.
من خودم اجرا کردم و عکس گرفتم در ادامه گذاشتم.
منبع

PHP:
#include <stdio.h>
#include <math.h>

#define WIDTH 60
#define HEIGHT 20
#define X WIDTH/2
#define Y HEIGHT/2
#define XMAX WIDTH-X-1
#define XMIN -(WIDTH-X)
#define YMAX HEIGHT-Y
#define YMIN -(HEIGHT-Y)+1

char grid[HEIGHT][WIDTH];

int plot(int x, int y);
void init_grid(void);
void show_grid(void);

int main()
{
    float x,y;

    init_grid();
    for(x=-3.14159;x<=3.14159;x+=0.1)
    {
        y = sin(x);
        plot(rintf(x*10),rintf(y*8));
    }
    show_grid();

    return(0);
}

/* Set "pixel" at specific coordinates */
int plot(int x, int y)
{
    if( x > XMAX || x < XMIN || y > YMAX || y < YMIN )
        return(-1);

    grid[Y-y][X+x] = '*';
    return(1);
}

/* Initialize grid */
void init_grid(void)
{
    int x,y;

    for(y=0;y<HEIGHT;y++)
        for(x=0;x<WIDTH;x++)
            grid[y][x] = ' ';
    /* draw the axis */
    for(y=0;y<HEIGHT;y++)
        grid[y][X] = '|';
    for(x=0;x<WIDTH;x++)
        grid[Y][x] = '-';
    grid[Y][X] = '+';
}

/* display grid */
void show_grid(void)
{
    int x,y;

    for(y=0;y<HEIGHT;y++)
    {
        for(x=0;x<WIDTH;x++)
            putchar(grid[y][x]);
        putchar('\n');
    }
}

a3.jpg
 

saalek110

Well-Known Member
قرار بود توابع خود سی در این تاپیک گفته شود.
ولی من SDL را مناسب دیدم که کار شود.

در این پست دانلود و تنظیمات برای Code::Blocks معرفی شده.
برای کامپایلرهای دیگر هم می توانید سرچ کنید. مثل ویژوال سی و Dev c.


Setting up SDL 2 on Code::Blocks 12.11
Lazy Foo' Productions - Setting up SDL 2 on Code::Blocks 12.11
 
آخرین ویرایش:

جدیدترین ارسال ها

بالا