Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 7 của 7
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Lỗi dùng chuột trong C

    Mình làm bài về chu trình Hamilton,máy báo lỗi"Variable 'lbutton'(với rbutton,xmouse,ymouse) is initialized more than once"mà ko biết sửa sao nữa.Các bạn giúp mình với.Code đây:
    Mã nguồn PHP:
    #include <dos.h>#include <graphics.h>#include <alloc.h>#include "mouse.inc"#include <stdio.h>#include <conio.h>#include <ctype.h>#include <stdlib.h>#pragma warn -sus/* Toi da 100 nut */#define MAX 100int gr_drive=DETECT, gr_mode;unsigned char lbutton, rbutton;int xmouse, ymouse;int sonut = 0, socanh = 0;typedef struct tagnode {int x, y; /* Vi tri tren man hinh */} NODE;NODE nut[MAX];int themduoc = 1;int weight[MAX][MAX];void initialize(){char s1[] = "Nhap nut phai chuot de them nut";char s2[] = "Nhap nut trai chuot va re de them duong noi";char s3[] = "Nhan phim Q de thoat - S de bat dau tim chu trinh Hamilton";int i, j;initgraph(&gr_drive, &gr_mode, "");reset_mouse();setcolor(YELLOW);rectangle(0, 0, getmaxx(), getmaxy());outtextxy((getmaxx()-textwidth(s1))/2, 5, s1);outtextxy((getmaxx()-textwidth(s2))/2, 15, s2);outtextxy((getmaxx()-textwidth(s3))/2, 25, s3);line(0, 35, getmaxx(), 35);line(51, 35, 51, getmaxy()-1);set_mouse_hlimits(56, getmaxx()-6);set_mouse_vlimits(40, getmaxy()-6);for (i=0; i<MAX; i++)for (j=0; j<MAX; j++)weight[i][j] = -1;show_mouse();}int index(int x, int y, int heso){int i, OK = 0;for (i=0; i<sonut; i++)if (abs(nut[i].x - x) < 4*heso && abs(nut[i].y - y) < 4*heso){OK = 1;break;}if (OK)return i;elsereturn -1;}void get_mouse(){do {get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);} while (lbutton == 0 && rbutton == 0 && !kbhit());}void clear_mouse(){do {get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);} while (lbutton == 1 || rbutton == 1);}void get_line(){int x, y, oldx, oldy, i, OK = 0, start, end;setwritemode(XOR_PUT);setcolor(GREEN);for (i=0; i<sonut; i++)if (abs(nut[i].x - xmouse) < 4 && abs(nut[i].y - ymouse) < 4){start = i;oldx = x = nut[i].x;oldy = y = nut[i].y;OK = 1;break;}if (!OK)return;hide_mouse();line(x, y, oldx, oldy);clear_mouse();show_mouse();do {get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);if (oldx != xmouse || oldy != ymouse){hide_mouse();line(x, y, oldx, oldy);oldx = xmouse;oldy = ymouse;line(x, y, oldx, oldy);show_mouse();}} while (lbutton == 0);OK = 0;hide_mouse();line(x, y, oldx, oldy);for (i=0; i<sonut; i++)if (abs(nut[i].x - xmouse) < 4 && abs(nut[i].y - ymouse) < 4 ){end = i;OK = 1;break;}if (OK && end != start){setcolor(GREEN);line(x, y, nut[end].x, nut[end].y);weight[start][end] = 1;weight[end][start] = 1;socanh++;}setwritemode(COPY_PUT);show_mouse();}void main(){int done = 0;char c, s[4];void Hamilton();initialize();do {get_mouse();if (kbhit()){c = toupper(getch());switch(c){case 'Q' : done = 1; break;case 'S' : Hamilton(); break;}}if (rbutton == 1 && index(xmouse, ymouse, 10) == -1){hide_mouse();if (themduoc){setcolor(WHITE);circle(xmouse, ymouse, 4);itoa(sonut, s, 10);setcolor(CYAN);outtextxy(xmouse+6, ymouse-6, s);nut[sonut].x = xmouse;nut[sonut].y = ymouse;sonut++;if (sonut>=MAX)themduoc = 0;}clear_mouse();show_mouse();}if (lbutton == 1){get_line();clear_mouse();}} while (!done);closegraph();}int top;int stack[MAX];void push(int v){stack[++top] = v;}void pop(int *v){*v = stack[top--];}int isempty(void){return (top == -1);}int dinh[MAX];void Hamilton(){int size, visited = 0, v1 = 0, v2, i, j, iseuler = 0;int y, OK = 1, oldv, n;char s[10], *kq[2] = { "Do thi co chu trinh HAMILTON","Do thi khong co chu trinh HAMILTON"};hide_mouse();setcolor(BLUE);setfillstyle(SOLID_FILL, BLUE);bar(getmaxx()/2 - 150, 36, getmaxx()/2 + 150, 48);setcolor(YELLOW);for (n=0; n<sonut; n++){setcolor(WHITE);for (i=0; i<sonut; i++)for (j=0; j<sonut; j++)if (weight[i][j] != -1)line(nut[i].x, nut[i].y, nut[j].x, nut[j].y);y = 40;top = -1;for (i=0; i<sonut; i++)dinh[i] = 0; // Dinh chua duoc thamv1 = n;setcolor(YELLOW);push(v1);dinh[v1] = 1;visited = 1;sprintf(s, "%d", v1);outtextxy(5, y, s);y+=10;do {oldv = v1;pop(&v1);if (dinh[v1] == 0 && weight[oldv][v1] != -1){dinh[v1] = 1;visited++;sprintf(s, "%d", v1);outtextxy(5, y, s);line(nut[oldv].x, nut[oldv].y, nut[v1].x, nut[v1].y);y += 10;}for (i = 0; i<sonut; i++)if (dinh[i] == 0 && weight[v1][i] != -1)push(i);} while ((!isempty() || v1 != n) && visited != sonut);outtextxy(5, getmaxy()-40, "HAMIL");setcolor(RED);if (weight[n][v1] == -1)line(5, getmaxy()-36, 45, getmaxy()-36);else{setcolor(YELLOW);line(nut[n].x, nut[n].y, nut[v1].x, nut[v1].y);iseuler = 1;setcolor(BLUE);setfillstyle(SOLID_FILL, BLUE);bar(getmaxx()/2 - 150, 36, getmaxx()/2 + 150, 48);setcolor(YELLOW);outtextxy((getmaxx()-textwidth(kq[0]))/2, 38, kq[0]);getch();break;}setcolor(BLACK);setfillstyle(SOLID_FILL, BLACK);getch();bar(1, 36, 50, getmaxy()-1);}if (!iseuler){setcolor(BLUE);setfillstyle(SOLID_FILL, BLUE);bar(getmaxx()/2 - 150, 36, getmaxx()/2 + 150, 48);setcolor(YELLOW);outtextxy((getmaxx()-textwidth(kq[1]))/2, 38, kq[1]);}setcolor(BLACK);setfillstyle(SOLID_FILL, BLACK);bar(1, 36, 50, getmaxy()-1);show_mouse();}  

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    nhìn code này thì chịu thật rồi, chả biết đọc kiểu gì bây giờ, chả thấy đâu với đâu cả, sao bạn trình bày nó thằng tưng thế

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    lần đầu post bài lên mà thông cảm[IMG]images/smilies/tongue.png[/IMG]

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    thôi được rồi pare code hộ bạn nè
    Mã nguồn PHP:
    #include <dos.h>#include <graphics.h>#include <alloc.h>#include "mouse.inc"#include <stdio.h>#include <conio.h>#include <ctype.h>#include <stdlib.h>#pragma warn -sus/* Toi da 100 nut */#define MAX 100int gr_drive=DETECT, gr_mode;unsigned char lbutton, rbutton;int xmouse, ymouse;int sonut = 0, socanh = 0;typedef struct tagnode { int x, y; /* Vi tri tren man hinh */} NODE;NODE nut[MAX];int themduoc = 1;int weight[MAX][MAX];void initialize(){ char s1[] = "Nhap nut phai chuot de them nut"; char s2[] = "Nhap nut trai chuot va re de them duong noi"; char s3[] = "Nhan phim Q de thoat - S de bat dau tim chu trinh Hamilton"; int i, j; initgraph(&gr_drive, &gr_mode, ""); reset_mouse(); setcolor(YELLOW); rectangle(0, 0, getmaxx(), getmaxy()); outtextxy((getmaxx()-textwidth(s1))/2, 5, s1); outtextxy((getmaxx()-textwidth(s2))/2, 15, s2); outtextxy((getmaxx()-textwidth(s3))/2, 25, s3); line(0, 35, getmaxx(), 35); line(51, 35, 51, getmaxy()-1); set_mouse_hlimits(56, getmaxx()-6); set_mouse_vlimits(40, getmaxy()-6); for (i=0; i<MAX; i++) for (j=0; j<MAX; j++) weight[i][j] = -1; show_mouse();}int index(int x, int y, int heso){ int i, OK = 0; for (i=0; i<sonut; i++) if (abs(nut[i].x - x) < 4*heso && abs(nut[i].y - y) < 4*heso) { OK = 1; break; } if (OK) return i; else return -1;}void get_mouse(){ do { get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse); } while (lbutton == 0 && rbutton == 0 && !kbhit());}void clear_mouse(){ do { get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse); } while (lbutton == 1 || rbutton == 1);}void get_line(){ int x, y, oldx, oldy, i, OK = 0, start, end; setwritemode(XOR_PUT); setcolor(GREEN); for (i=0; i<sonut; i++) if (abs(nut[i].x - xmouse) < 4 && abs(nut[i].y - ymouse) < 4) { start = i; oldx = x = nut[i].x; oldy = y = nut[i].y; OK = 1; break; } if (!OK) return; hide_mouse(); line(x, y, oldx, oldy); clear_mouse(); show_mouse(); do { get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse); if (oldx != xmouse || oldy != ymouse) { hide_mouse(); line(x, y, oldx, oldy); oldx = xmouse; oldy = ymouse; line(x, y, oldx, oldy); show_mouse(); } } while (lbutton == 0); OK = 0; hide_mouse(); line(x, y, oldx, oldy); for (i=0; i<sonut; i++) if (abs(nut[i].x - xmouse) < 4 && abs(nut[i].y - ymouse) < 4 ) { end = i; OK = 1; break; } if (OK && end != start) { setcolor(GREEN); line(x, y, nut[end].x, nut[end].y); weight[start][end] = 1; weight[end][start] = 1; socanh++; } setwritemode(COPY_PUT); show_mouse();}void main(){ int done = 0; char c, s[4]; void Hamilton(); initialize(); do { get_mouse(); if (kbhit()) { c = toupper(getch()); switch(c) { case 'Q' : done = 1; break; case 'S' : Hamilton(); break; } } if (rbutton == 1 && index(xmouse, ymouse, 10) == -1) { hide_mouse(); if (themduoc) { setcolor(WHITE); circle(xmouse, ymouse, 4); itoa(sonut, s, 10); setcolor(CYAN); outtextxy(xmouse+6, ymouse-6, s); nut[sonut].x = xmouse; nut[sonut].y = ymouse; sonut++; if (sonut>=MAX) themduoc = 0; } clear_mouse(); show_mouse(); } if (lbutton == 1) { get_line(); clear_mouse(); } } while (!done); closegraph();}int top;int stack[MAX];void push(int v){ stack[++top] = v;}void pop(int *v){ *v = stack[top--];}int isempty(void){ return (top == -1);}int dinh[MAX];void Hamilton(){ int size, visited = 0, v1 = 0, v2, i, j, iseuler = 0; int y, OK = 1, oldv, n; char s[10], *kq[2] = { "Do thi co chu trinh HAMILTON","Do thi khong co chu trinh HAMILTON"}; hide_mouse(); setcolor(BLUE); setfillstyle(SOLID_FILL, BLUE); bar(getmaxx()/2 - 150, 36, getmaxx()/2 + 150, 48); setcolor(YELLOW); for (n=0; n<sonut; n++) { setcolor(WHITE); for (i=0; i<sonut; i++) for (j=0; j<sonut; j++) if (weight[i][j] != -1) line(nut[i].x, nut[i].y, nut[j].x, nut[j].y); y = 40; top = -1; for (i=0; i<sonut; i++) dinh[i] = 0; // Dinh chua duoc tham v1 = n; setcolor(YELLOW); push(v1); dinh[v1] = 1; visited = 1; sprintf(s, "%d", v1); outtextxy(5, y, s); y+=10; do { oldv = v1; pop(&v1); if (dinh[v1] == 0 && weight[oldv][v1] != -1) { dinh[v1] = 1; visited++; sprintf(s, "%d", v1); outtextxy(5, y, s); line(nut[oldv].x, nut[oldv].y, nut[v1].x, nut[v1].y); y += 10; } for (i = 0; i<sonut; i++) if (dinh[i] == 0 && weight[v1][i] != -1) push(i); } while ((!isempty() || v1 != n) && visited != sonut); outtextxy(5, getmaxy()-40, "HAMIL"); setcolor(RED); if (weight[n][v1] == -1) line(5, getmaxy()-36, 45, getmaxy()-36); else { setcolor(YELLOW); line(nut[n].x, nut[n].y, nut[v1].x, nut[v1].y); iseuler = 1; setcolor(BLUE); setfillstyle(SOLID_FILL, BLUE); bar(getmaxx()/2 - 150, 36, getmaxx()/2 + 150, 48); setcolor(YELLOW); outtextxy((getmaxx()-textwidth(kq[0]))/2, 38, kq[0]); getch(); break; } setcolor(BLACK); setfillstyle(SOLID_FILL, BLACK); getch(); bar(1, 36, 50, getmaxy()-1); } if (!iseuler) { setcolor(BLUE); setfillstyle(SOLID_FILL, BLUE); bar(getmaxx()/2 - 150, 36, getmaxx()/2 + 150, 48); setcolor(YELLOW); outtextxy((getmaxx()-textwidth(kq[1]))/2, 38, kq[1]); } setcolor(BLACK); setfillstyle(SOLID_FILL, BLACK); bar(1, 36, 50, getmaxy()-1); show_mouse();}  
    và chờ tí đọc xem lỗi ở đâu

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    có cái gì trong file
    mouse.inc
    có file trong file này đã reg các biến rbutton,xmouse,ymouse 1 lần rồi ko


    nếu vậy tròng file cpp này phải đổi lại là

    extern int xmouse, ymouse;

  6. #6
    Ngày tham gia
    Sep 2015
    Đang ở
    Hà Nội
    Bài viết
    0
    thanks bạn,hôm wa cúp điện ko lên mạng đc,nhân tiện mình muốn hỏi ai co thuật toán quay lui để tìm chu trình Hamilton ko vậy,nếu có thì cho mình xin luôn

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Procedure Hamilton(k);

    (* liet ke cac chu trinh Hamilton thu duoc bang viec phat trien day dinh (X[1],. . . , X[k-1]) cua do thi G=(V,E) cho boi danh sach ke: Ke(v), vÎ V *)

    begin

    for y Î Ke(X[k-1]) do

    if (k =N+1) and (y=v0) then Ghinhan(X[1],. . . , X[n], v0)

    else

    if Chuaxet[y] then

    begin

    X[k]:=y;

    Chuaxet[y]:=false;

    Hamilton(k+1);

    Chuaxet[y]:=true;

    end;

    end;

    (* Main program*)

    begin

    for v Î V do Chuaxet[v]:=true;

    X[1]:=0; (* v0 la mot dinh nao do cua do thi *)

    Chuaxet[v0]:=false;

    Hamilton(2);

    end.
    Đây là gợi ý của thuật toán mà mình ko biết phải viết thành bài C ra sao,hic

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •