C/Data Type/Double

Материал из C\C++ эксперт
Версия от 13:22, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A function to output double values in a given width

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>
  3. include <ctype.h>
  4. define MAX_COUNT 100

void show(double array[], size_t array_size, unsigned int field_width); int main() {

 double array[MAX_COUNT] = {0.0};
 int count = 6;
 char answer = "n";
 array[0] = 123456.123456;
 array[1] = 12345.12345;
 array[2] = 1234.1234;
 array[3] = 123.123;
 array[4] = 12.12;
 array[5] = 1.1;
 show(array, count, 12);
 printf("\n\n\n");
 show(array, count, 2);
 printf("\n\n\n");
 show(array, count, 6);

} void show(double array[], size_t array_size, unsigned int field_width) {

 char format[10] = "%";
 char width_str[4];
 int i = 0;
 int j = 1;
 do
 {
   width_str[i++] = "0"+field_width%10;
 }while((field_width /= 10) != 0);
 do
 {
   format[j++] = width_str[--i];
 }while(i>0);
 format[j] = "\0";
 strcat(format, "lf");
 for(j = 0 ; j<array_size ; j++)
 {
   printf("\n");
   printf(format, array[j]);
 }

}


      </source>


Calculating average hourly pay rate

<source lang="cpp">

  1. include <stdio.h>

int main() {

 double pay = 0.0;
 double hours = 0.0;
 int dollars = 0;
 int cents = 0;
  pay = 10;
  hours = 11;
  dollars = (int)(pay/hours);
  cents = (int)(100.0*(pay/hours - dollars) +0.5);
  printf("Your average hourly pay rate is %d dollars and %d cents.\n",
    dollars, cents);

}


      </source>


Calculating the area of a room

<source lang="cpp">

  1. include <stdio.h>

int main() {

 double length = 0.0;
 double width = 0.0;
 long feet = 0L;
 long inches = 0L;
 const long inches_per_foot = 12L;
 const double inches_per_yard = 36L;
  feet = 9;
  inches = 11;
  length = (feet*inches_per_foot+inches)/inches_per_yard;
  feet = 20;
  inches = 3;
  width = (feet*inches_per_foot+inches)/inches_per_yard;
  /* Output the area */
  printf("The area of the room is %.2f square yards.\n",length*width);

}


      </source>


Calculating volume price of alternative products

<source lang="cpp">

  1. include <stdio.h>

int main() {

 double total_price = 0.0;
 int type = 0;
 int quantity = 0;
 const double type1_price = 3.50;
 const double type2_price = 5.50;
  type = 9;
  quantity = 12.12;
  total_price = quantity*(type1_price + (type-1)*(type2_price-type1_price));
  printf("The price for %d of type %d is $%.2f\n",quantity, type, total_price);

}


      </source>


Convert double to int

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 int i;
 long double ld;
 ld = 10.0;
 i = ld; /*convert double to int*/
 printf("%d", i);

}


      </source>


Double calculation: square

<source lang="cpp">

  1. include <stdio.h>
  2. include <math.h>

int main(void) {

 double i;
 for(i = 1.0; i < 101.0; i++) {
   printf("square root of %lf = %lf\n", i, sqrt(i));
   
   printf("Whole number part = %d ", (int)sqrt(i));
   
   printf("Fractional part = %lf\n", sqrt( i ) - (int) sqrt( i ));
   
   printf("\n");
 }
 return 0;

}


      </source>


Table of reciprocals, squares, cubes, and fourth powers

<source lang="cpp">

  1. include <stdio.h>

int main() {

 double num[11][5];
 double value = 0.0;
 int y = 0;
 int x = 0;
 for(y = 0, value = 3.0 ; y<11 ; y++, value += 0.3)
   num[y][0] = value;
 for(y = 0 ; y<11 ; y++)
 {
   num[y][1] = 1.0/num[y][0];
   num[y][2] = num[y][0]*num[y][0];
   num[y][3] = num[y][0]*num[y][0]*num[y][0];
   num[y][4] = num[y][0]*num[y][0]*num[y][0]*num[y][0];
 }
 printf("\n         x");
 printf("         1/x");
 printf("        x*x");
 printf("         x*x*x");
 printf("      x*x*x*x");
 for(y = 0 ; y<11 ; y++)
 {
   printf("\n");
   for(x = 0 ; x<5 ; x++)
     printf("%15.4lf", num[y][x]);
 }

}


      </source>