Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Программирование и базы данных (http://forum.oszone.net/forumdisplay.php?f=21)
-   -   [решено] Построение геом. фигуры (http://forum.oszone.net/showthread.php?t=288220)

kadett46 20-09-2014 22:04 2405253

Построение геом. фигуры
 
Нужно построить данную фигуру (картинка).
Желательно несколькими способами. Заранее благодарен.

kadett46 20-09-2014 22:48 2405263

1 способом сделал.
Подскажите еще.

HTML код:

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"

using namespace std;

void main()
{
        int w=40,h=21;
        cout << endl;
        for (int i=0; i<h; i++)
        {
                cout << setw(21);
                for (int j=0; j<w; j++)
                        if ( i == 0 || i == h-1 || j==0 || j==w-1)
                        {
                                cout << "*";
                        }
                        else if (j==(w-i*2))

                        {
                                cout << "*";
                        }
                        else
                                cout << " ";
                        cout << endl;
        }
_getch();
}


mrcnn 21-09-2014 16:19 2405437

Код:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#include <string.h>
#include <windows.h>
#pragma comment(lib,"user32.lib")
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib,"gdi32.lib")
HRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
struct c_program
{
WNDCLASSEX wc;
MSG msg;
HWND hwnd;
HDC hdc;

void draw_gdi(){  hdc = GetDC(hwnd);

MoveToEx(hdc, 40, 40, RGB(255, 0, 0)); LineTo(hdc, 140, 40);
MoveToEx(hdc, 140, 40, RGB(255, 0, 0)); LineTo(hdc, 140, 80);
MoveToEx(hdc, 40, 40, RGB(255, 0, 0)); LineTo(hdc, 40, 80);
MoveToEx(hdc, 40, 80,RGB(255, 0, 0)); LineTo(hdc, 140, 80);
MoveToEx(hdc, 40, 80, RGB(255, 0,0)); LineTo(hdc, 140, 40);


c_program(){ set_locale(LC_ALL,"Russian"); };
~c_program(){}
int main(){ HINSTANCE hInstance=GetModuleHandle(0); LPSTR CommandLine = GetCommandLine(); return WinMain(hInstance, NULL, CommandLine, SW_SHOWDEFAULT);};
void create_wndclass(HINSTANCE hInst){ wc.cbSize=sizeof(WNDCLASSEX); wc.style=CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc=(WNDPROC)WndProc; wc.cbClsExtra=NULL; wc.cbWndExtra=NULL;wc.hInstance=hInst;wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName="MainMenu"; wc.lpszClassName="SimpleWinClass"; wc.hIcon=LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL, IDC_ARROW);RegisterClassEx(&wc);}
void create_windows(HINSTANCE hInst){ hwnd=CreateWindowEx(NULL, "SimpleWinClass","host",WS_OVERLAPPEDWINDOW, 100,100,1000,1000,NULL,(HMENU) NULL,hInst, NULL); ShowWindow(hwnd, SW_SHOWNORMAL)! UpdateWindow(hwnd);}
int message_cycle(){while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg); DispatchMessage(&msg);} return msg.wParam;}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow){create_wndclass(hInst); create_windows(hInst);  return message_cycle();}
};

c_program z;
int main(int argc, char* argv[]){ return z.main();}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY: PostQuitMessage(NULL); return 0;
case WM_PAINT: z.draw_gdi(); return 0;
default: return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return DefWindowProc(hwnd,uMsg, wParam,lParam);
}

на GDI

pva 23-09-2014 17:35 2406208

Код:

#include <stdio.h>
int main() {
        int x1=0, x2=200, y1=0, y2=100;
        printf("<svg width=\"%d\" height=\"%d\">\n"
                "<polyline points=\"%d,%d %d,%d %d,%d %d,%d %d,%d %d,%d\" style=\"fill:none;stroke:black;stroke-width:3\" />\n"
                "</svg>\n", x2, y2, x2,y1, x1,y2, x2,y2, x2,y1, x1,y1, x1,y2);
}

использовать так:
Код:

gcc -o test.exe test.c
test.exe > test.html
firefox test.html


pva 24-09-2014 17:32 2406611

Код:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
        int const x2=78, y2=30, pitch=x2+1, area_size=pitch*y2;
       
        int x=1, normal=(x2-y2)/2;
        char *img = malloc(area_size), *last=img+area_size-pitch, *row=last;
       
        memset(img, ' ', area_size);
       
        for(;;) {
                if (0<normal) {
                        normal-=y2;
                        img[x]=last[x]=row[x]='x';
                        if (x<x2) { ++x; }
                }
                else {
                        normal+=x2;
                        row[0]=row[pitch-2]='x';
                        row[pitch-1]='\n';
                        if ((row-=pitch)<img) break;
                }
        }
       
        fwrite(img, y2, pitch, stdout);       
        free(img);
}

Код:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
        int const x2=78, y2=30, pitch=x2+1, area_size=pitch*y2;
       
        char *img = malloc(area_size);
        unsigned const dx=x2-2, dy=y2-2;
        int const width = (x2+y2)/2;
        unsigned x, y;
        for(y=0; y<y2; ++y) {
                img[(y+1)*pitch-1] = '\n';
                for(x=0; x<x2; ++x) {
                        img[y*pitch+x] =
                                x - 1 < dx && y - 1 < dy &&
                                width < (x-dx)*dy + y*dx ? ' ' : 'x';
                }
        }
       
        fwrite(img, y2, pitch, stdout);       
        free(img);
}



Время: 04:33.

Время: 04:33.
© OSzone.net 2001-