Tutorialspoint

D Programming Examples

Compile and Execute D Online

d

import std.stdio;

/* Hello World Program in D Programming */
void main(string[ ] args)
{
   writeln("Hello World!");
}

2 - exemplo: if

d

import std.stdio;

void main() {
    bool existsCoffee = true;

if (existsCoffee) {
        writeln("Drink coffee");

    } else {
        writeln("Drink tea");
    }
}

6 - exemplo: Orientação a Objetos

d

import std.stdio;

class Box { 
   public: 
      double length;   // Tamanho da caixa 
      double breadth;  // largura da caixa 
      double height;   // Altura da caixa 
}
  
void main() { 
   Box box1 = new Box();    // Declara Box1 como tipo Box 
   Box box2 = new Box();    // Declara Box2 como tipo Box  
   double volume = 0.0;     // Armazena o volume da caixa aqui  
   
   // especificações box 1
   box1.height = 5.0; 
   box1.length = 6.0; 
   box1.breadth = 7.0; 
   
   // especificações  box 2
   box2.height = 10.0; 
   box2.length = 12.0; 
   box2.breadth = 13.0;
   
   // volume da box 1 
   volume = box1.height * box1.length * box1.breadth; 
   writeln("Volume da Box1 : ",volume);
   
   // volume da box 2 
   volume = box2.height * box2.length * box2.breadth; 
   writeln("Volume da Box2 : ", volume);
}

5 - exemplo: Orientação a Objetos

d

import std.stdio;
import std.string;

abstract class Pessoa {
   string nome; 
   
   string getNome() { 
      return nome;
   }

   abstract void print();
}

class Empregado : Pessoa {
   int empID;
   
   override void print() { 
      writeln("Detalhes do empregado:"); 
      writeln("ID: ", this.empID); 
      writeln("Nome: ", this.nome); 
   } 
}

void main() {
   Empregado emp = new Empregado(); 
   
   emp.empID = 101; 
   emp.nome = "Empregado101"; 
   emp.print();
}

1 - exemplo: HelloWorld

d

import std.stdio;

void main () {
     
     writeln( "Hello, World!" );

}

Compile and Execute D Online

d

import std.stdio;
import std.conv;

bool Task1(double a, double b, double c)
{
    bool res = (a==b && a == c);
    return res;
}

bool Task2(double a, double b, double c)
{
	
	bool res = (a == b || b == c || a == c);
	return res;
}

bool Task3(double a, double b, double c)
{
	
	bool res = (a < 0 && b < 0 && c < 0);
	return res;
}

bool Task4(double a, double b, double c)
{
	
	bool res = (a < 0 || b < 0 || c < 0);
	return res;
}

bool Task5(double a, double b, double c)
{
	int counter = 0;
	a < 0 ? ++counter : counter;
	b < 0 ? ++counter : counter;
	c < 0 ? ++counter : counter;
	bool res = (counter == 1);
	return res;
}

bool Task6(double a, double b, double c)
{
	int counter = 0;
	a < 0 ? ++counter : counter;
	b < 0 ? ++counter : counter;
	c < 0 ? ++counter : counter;
	bool res = (counter > 1);
	return res;
}

bool Task7(double a, double b, double c)
{
    a = to!int(a);
    b = to!int(b);
    c = to!int(c);
	bool res = (a % 2 == 0 && b % 2 == 0 && c % 2 == 0);
	return res;
}

bool Task8(double a, double b, double c)
{
    a = to!int(a);
    b = to!int(b);
    c = to!int(c);
	bool res = (a % 2 == 0 || b % 2 == 0 || c % 2 == 0);
	return res;
}

bool Task9(double a, double b, double c)
{
    a = to!int(a);
    b = to!int(b);
    c = to!int(c);
	int counter = 0;
	a % 2 == 0 ? ++counter : counter;
	b % 2 == 0 ? ++counter : counter;
	c % 2 == 0 ? ++counter : counter;
	bool res = (counter == 1);
	return res;
}

bool Task10(double a, double b, double c)
{
    a = to!int(a);
    b = to!int(b);
    c = to!int(c);
    bool res = ((a % 2 != 0 && b % 2 != 0 && c % 2 != 0) || (a % 2 == 0 && b % 2 == 0 && c % 2 == 0));
    return res;
}

bool Task11(double a, double b, double c)
{
    a = to!int(a);
    b = to!int(b);
    c = to!int(c);
	int counter = 0;
	a % 2 == 0 ? ++counter : counter;
	b % 2 == 0 ? ++counter : counter;
	c % 2 == 0 ? ++counter : counter;
	bool res = (counter > 1);
	return res;
}

bool Task12(int a)
{
	int v, b, c, d;
	v = a % 10;
	b = ((a - v) / 10) % 10;
	c = ((((a - v) / 10) - b) / 10) % 10;
	d = (((((a - v) / 10) - b) / 10) - c) / 10;
	bool res = (d < c && c < b && b < v);
	return res;
}

bool Task13(int a)
{
	int v, b, c, d;
	v = a % 10;
	b = ((a - v) / 10) % 10;
	c = ((((a - v) / 10) - b) / 10) % 10;
	d = (((((a - v) / 10) - b) / 10) - c) / 10;
	bool res = (v == d && b == c);
	return res;
}
	
int Task14(double x, double y)
{
	int chetvert = 0;
	(x > 0 && y > 0) ? chetvert = 1 : chetvert;
	(x < 0 && y>0) ? chetvert = 2 : chetvert;
	(x < 0 && y < 0) ? chetvert = 3 : chetvert;
	(x > 0 && y < 0) ? chetvert = 4 : chetvert;
	return chetvert;
}

bool Task15(double a, double b, double c)
{
	return (a + b > c && a + c > b && b + c > a) ? true : false;
}

bool Task16(double a, double b, double c)
{
	bool res = (Task15(a, b, c) == true && (a == b && b == c));
	return res;
}

bool Task17(double a, double b, double c)
{
	bool res = (Task15(a, b, c) == true && (a == b || b == c));
	return res;
}

bool Task18(double a, double b, double c)
{
	bool res = (Task15(a, b, c) == true && ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (c * c + b * b == a * a))
			);
	return res;
}

bool Task19(double a, double b, double c)
{
	bool res = (Task15(a, b, c) == true && ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (c * c + b * b == a * a)) && Task15(a, b, c) == true
			);
	return res;
}

bool Task20(double x, double y, double x1, double y1, double x2, double y2)
{
	bool res = (x > x1 && x < x2 && y<y1 && y>y2);
	return res;
}

void chooser(int v, double a, double b, double c)
{
    if     (v == 1) writeln("All values are the same? ", Task1(a,b,c));
    else if(v == 2) writeln("At least two values are the same? ", Task2(a,b,c));
    else if(v == 3) writeln("All values are positive? ", Task3(a,b,c));
    else if(v == 4) writeln("At least one value is negative? ", Task4(a,b,c));
    else if(v == 5) writeln("There's only one negative value? ", Task5(a,b,c));
    else if(v == 6) writeln("There are more negative values? ", Task6(a,b,c));
    else if(v == 7) writeln("All values are even? ", Task7(a,b,c));
    else if(v == 8) writeln("Is there at least one even value? ", Task8(a,b,c));
    else if(v == 9) writeln("There's only one even value? ", Task9(a,b,c));
    else if(v == 10) writeln("The values have the same parity? ", Task10(a,b,c));
    else if(v == 11) writeln("There are more even values? ", Task11(a,b,c));
    else if(v == 12)
    {
        int n;
        readf(" %d", &n);
        writeln("Digits of the value are increasing? ", Task12(n));
    }
    else if(v == 13) 
    {
        int n;
        readf(" %d", &n);
        writeln("The value is a palindrom? ", Task13(n));
    }
    else if(v == 14)
    {
        double x,y;
        readf(" %f %f", &x, &y);
        writeln("Point is located in quarter №", Task14(x,y));
    }
    else if(v == 15) writeln("A B C are really sides of a triangle? ", Task15(a,b,c));
    else if(v == 16) writeln("Triangle with sides A B C is equilateral? ", Task16(a,b,c));
    else if(v == 17) writeln("Triangle with sides A B C is isosceles? ", Task17(a,b,c));
    else if(v == 18) writeln("Triangle with sides A B C is right? ", Task18(a,b,c));
    else if(v == 19) writeln("Triangle with sides A B C is an equilateral right triangle? ", Task19(a,b,c));
    else if(v == 20)
    {
        double x,y,x1,y1,x2,y2;
        readf(" %f %f %f %f %f %f", &x, &y, &x1, &y1, &x2, &y2);
        writeln("Pount with coords (X,Y) is located inside the rectangle (X1,Y1)(X2,Y2)? ", Task20(x,y,x1,y1,x2,y2));
     }
}



/* Hello World Program in D Programming */
void main(string[ ] args)
{
   double a,b,c;
   int v;
   readf("%f %f %f", &a, &b, &c);
   readf(" %d", &v);
   chooser(v,a,b,c);
}

Compile and Execute D Online

d

module hotel.employee;
  
import std.stdio;  

class staff {
   public: 
      string FullName;
	
   void printName() {
      writeln("Hotel Employee Name: ",FullName); 
   } 
}

void main()

{
    
    staff m1= new staff();
    
    m1.FullName= "Daniel Jones";
    
    m1.printName();
    
}

D stuff

d

import std.stdio;
import std.datetime;

void arg_demo(string[] args) 
{
    int argc = 0, total = 0;
    foreach(arg; args) {
        printf("arg[%i] = \"%s\"\n", argc++, arg.ptr);
    }
    write("\nargs = ", argc, "\ntotal = ", total, "\n\n");
}

void ascii_demo()
{
    char c;
    for(c = ' '; c < 128; ) {
        write(c);
        if(++c % 32 == 0)
            write('\n');
    }
    write('\n');
}

void print_datetime() 
{
    SysTime now = Clock.currTime();
    writeln(now.toISOExtString());
}

void main(string[ ] args)
{
    arg_demo(args);
    ascii_demo();
    print_datetime();
}

Love

d

import std.stdio;

/* Hello World Program in D Programming */
void main(string[ ] args)
{
   writeln("Hello World!");
}

00000

d

#include <math.h>
#define PI 3.1416
#define MAX 180
main ( )
{
    int angle;
    float x,y;
    angle=0;
    printf(" Angle Cos(angle)\n\n");
    while(angle <= MAX)
    {
        x = (PI/MAX)*angle;
        y = Cos(x);
        printf("%15d%13.4f\n", angle, y);
        angle = angle + 10;
    }
}

1 2 3 4 5 6 7 ... 15 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.