Hỏi & đáp lập trình

Thảo luận trong 'Lập trình & Đồ hoạ' bắt đầu bởi canh en buon, 23/5/07.

  1. BadBoy199x

    BadBoy199x T.E.T.Я.I.S

    Tham gia ngày:
    12/6/12
    Bài viết:
    528
    Nơi ở:
    Hà Giang Cỉty
    Cơ mà bài bảo dùng đoạn main kia, và phải dùng dynamic mới củ bựa @@
     
  2. INTP

    INTP T.E.T.Я.I.S

    Tham gia ngày:
    16/5/11
    Bài viết:
    639
    chắc ổng/bả tập cho làm quen cách xài new. Mốt học tới danh sách liên kết linked list thì xài cái new nhiều lắm.

    có 2 cái new 1 để xài cấp phát cho 1 vật, 1 để cấp phát cho mảng gồm nhiều vật thì mới xài new []. Ở đây con trỏ a chỉ trỏ tới tối đa 1 object Student vậy thì xài new là đủ rồi.

    ứng với new [] thì xài delete [], ứng với new thì xài delete.


    Và viết class thì thường phải có default constructor (ctor), ctor ko nhận 1 biến nào. Nếu class có dữ liệu là mảng C-array thì phải overload thêm copy ctor (ctor nhận 1 biến cùng class), assignment operator ( operator= ), và operator==. Nếu có xài cấp phát động cho các dữ liệu trong class thì phải viết thêm destructor (dtor) (và copy ctor, operator=, operator== ).

    Vd với 3 class:
    Mã:
    class A {
    private:
        int id;
        std::string name;
        std::vector<double> grades;
    };
    
    class B {
    private:
        int id;
        char name[16];
        double grades[3];
    };
    
    class C {
    private:
        int id;
        char *name;  //sẽ cấp phát động và copy toàn bộ ký tự vào, ko chỉ đơn thuần trỏ vào 1 vùng nhớ nào đó
        double *grades;  //sẽ cấp phát động và copy toàn bộ điểm mới vào, ko chỉ đơn thuần trỏ vào 1 vùng nhớ nào đó
    };
    class A ko cần default ctor, copy ctor, dtor, operator=, operator==
    class B nên có default ctor (nếu ko muốn output dữ liệu rác); phải có copy ctor, operator=, opeartor== ; ko cần dtor
    class C phải có default ctor, copy ctor, dtor (tối trọng), operator=, opeartor==
     
    Chỉnh sửa cuối: 27/9/13
  3. BadBoy199x

    BadBoy199x T.E.T.Я.I.S

    Tham gia ngày:
    12/6/12
    Bài viết:
    528
    Nơi ở:
    Hà Giang Cỉty
    E có đoạn class như này:

    Mã:
    class Author{
    	char name[30];
    	char email[30];
    	char gender;
    	
    	public:
    		Author (char name[], char email[], char gender);
    		
    };
    
    Author::Author(char n[], char e[], char g)
    {
    	strcpy(name, name);
    	strcpy(email, e);
    	gender=g;
    }
    Và dùng câu lệnh này: Author anAuthor = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm');

    Nhưng cứ bị báo lỗi [Error] conversion from 'Author*' to non-scalar type 'Author' requested hoài là sao?

    Ah tiện hỏi thêm: mở 1 file dulieu.inp, lấy dữ liệu trong đó nhập vào các biến trong class thì làm kiểu gì ? E mới mò ra cách ghi vào file chứ chưa tìm ra cách lấy dữ liệu từ trong file =.=
     
  4. INTP

    INTP T.E.T.Я.I.S

    Tham gia ngày:
    16/5/11
    Bài viết:
    639
    trong C++ ko có
    Author anAuthor = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm');

    mà phải ghi là
    Author anAuthor("Tan Ah Teck", "ahteck@somewhere.com", 'm');

    gọi new Author(...) là cấp phát 1 object của class Author trên heap, rồi trả về con trỏ trỏ tới object đó chứ ko phải tạo object đó vô anAuthor.



    còn mở file dữ liệu thì
    ifstream fin("file_name");
    if (!fin) { cout << "Cannot open file\n"; return 1; }
    rồi sử dụng fin như cin thôi.
     
  5. BadBoy199x

    BadBoy199x T.E.T.Я.I.S

    Tham gia ngày:
    12/6/12
    Bài viết:
    528
    Nơi ở:
    Hà Giang Cỉty
    đệch, thế mà trong đề nó ghi rõ ràng Author anAuthor = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm');

    Phang luon vào hỏi cho tiện

    Mã:
     #include<iostream>
    #include<string.h>;
    using namespace std;
    
    class Author{
    	char name[30];
    	char email[30];
    	char gender;
    	
    	public:
    		Author (char name[], char email[], char gender);
    		char getName();
    		char getEmail();
    		void setEmail(char e[]);
    		char getGender();
    		char toString();
    };
    
    Author::Author(char n[], char e[], char g)
    {
    	strcpy(name, name);
    	strcpy(email, e);
    	gender=g;
    }
    
    char Author::getName()
    {
    	return name[30];
    }
    char Author::getEmail()
    {
    	return email[30];
    }
    char Author::getGender()
    {
    	return gender;
    }
    void Author::setEmail(char e[])
    {
    	strcpy(email, e);
    }
    char Author::toString()
    {	
       ??????????????????????
    }
    
    main()
    {
    	Author anAuthor("Tan Ah Teck", "ahteck@somewhere.com", 'm');
    //cout<< anAuthor.toString()<< endl;
    anAuthor.setEmail("paul@nowhere.com");
    //cout<< anAuthor.toString()<< endl;
    cout<< "Author information:";
    cout<< "Name: "<< anAuthor.getName()<< endl;
    cout<< "Email: "<< anAuthor.getEmail()<< endl;
    cout<< "Sex: "<< anAuthor.getGender()<< endl;
    } 
    Sao chạy toàn ko như ý T_T, ko biết sai chỗ nào.

    chỗ toString là phải ra: A toString() method that returns "author-name (gender) at email", e.g., "Tan Ah Teck (m) at ahTeck@somewhere.com" mà e chả biết làm thế nào luôn, dùng strcat toàn báo lỗi =.=
     
  6. INTP

    INTP T.E.T.Я.I.S

    Tham gia ngày:
    16/5/11
    Bài viết:
    639
    mấy cái method này là đề nó ghi vậy hay mi tự chế ra đó O_o

    getName() là trả về chuỗi name, tức là return type của nó phải là const char* (const để ko ghi bậy bạ vào mảng name được)
    getEmail() là trả về chuỗi email, tức là return type của nó phải là const char* (const để ko ghi bậy bạ vào mảng email được)

    sửa lại là
    const char* getName()const { return name; } //cái const thứ 2 sau dấu () là để bảo đảm method này sẽ ko thay đổi các dữ liệu trong class.
    email tương tự

    cái toString thì cũng tương tự phải trả về const char* và có const phía sau để bảo đảm method ko thay đổi thuộc tính class. Cái này ko hay cho lắm vì phải cấp phát động, nếu xài mà ko dọn dẹp thì gây ra mem leak dữ lắm.
    const char* toString()const
    {
    char* ret = new char[68]; //<<<< 30 + 2 + 1 + 5 + 30
    //strcat vào thằng ret này...
    return ret;
    }

    khi xài phải xài theo kiểu:
    const char* str = NULL;
    if (str) delete [] str;
    str = author1.toString();
    cout << str << "\n";

    if (str) delete [] str;
    str = author2.toString();
    cout << str << "\n";

    //...

    delete [] str;
    return 0;
    }

    mà sao ko xài std::string??? 100% ko có mem leak nếu ko cấp phát động.
     
  7. BadBoy199x

    BadBoy199x T.E.T.Я.I.S

    Tham gia ngày:
    12/6/12
    Bài viết:
    528
    Nơi ở:
    Hà Giang Cỉty
    Bây h e có đoạn main thế này
    Mã:
    int main( ) {
         int  i, n;
         bool keepreading = true;
         Employee e, employee[20];
    
         cout << "Employee List" << endl;
         cout << "=============" << endl;
         n = 0;
         do {
             cin >> e;
             if (e.valid())
                 employee[n++] = e;
             else
                 keepreading = false;
         } while (keepreading && n < 20);
    
         cout << endl;
         for (i = 0; i < n; i++) {
             employee[i].display();
             cout << endl;
         }
     }
    
    Yêu cầu là nhập lần lượt name, no, salary, cho 20 biến dạng Employee
    Hàm bool valid() sẽ trả về fail nếu nhập Name là dấu * và biến đó sẽ đưa về mặc định (có hàm mặc định rồi)
    Vậy viết hàm bool valid() này dư thế lào ?

    E chồng hàm cin thế này
    Mã:
    istream& operator>>(istream &is, Employee &a)
     {cout<<"Employee Name: "; is>>a.name;
      cout<<"Employee No: "; is>>a.no;
      cout<<"Salary: "; is>>a.salary;
      return(is);
     }
    Nghĩa là phải nhập lần lượt hết Name, No, Salary thì mới gọi đc valid() để xem NAME nó có phải dấu * hay ko.
    Nhưng output nó yêu cầu thế này:
    Mã:
    Employee List
     =============
     Employee Name : Frank
     Employee No   : 1001
     Salary        : 25000
     Employee Name : Alice
     Employee No   : 1005
     Salary        : 35000
     Employee Name : Jane
     Employee No   : 1004
     Salary        : 30000
     Employee Name : *
    
     Frank               (1001)-  25000.00
     Alice               (1005)-  35000.00
     Jane                (1004)-  30000.00
    
    Đoạn nhập * kia, nó bỏ qua luôn No và Salary, naảy luôn xuống phần hiển thị :|, vậy làm thế nào đây?
     
  8. INTP

    INTP T.E.T.Я.I.S

    Tham gia ngày:
    16/5/11
    Bài viết:
    639
    cho thêm dòng if trong operator>> nữa thôi.

    Mã:
    cout<<"Employee Name: "; is>>a.name;
    if (a.name == "*") //nếu a.name là std::string, còn char[] thì xài strcmp == 0
    {
        //đưa về mặc định... vd a.no = 0;
        return is;
    }
    //... ở dưới bình thường
    còn hàm bool valid()const { return a.no != /*giá trị mặc định*/; } là xong.

    còn nếu check a.no, a.salary có valid ko nữa thì thêm mấy dòng check nữa


    Mã:
    cout<<"Employee Name: "; is>>a.name;
    if (a.name == "*") {/*cho về mặc định*/ return is; }
    cout<<"Employee No: "; is>>a.no;
    if (a.no > Employee::MIN_EMPLOYEE_NO) {/*cho về mặc định*/ return is; } //MIN_EMPLOYEE_NO có thể là static const int 1000, hay 999 hay 99 hay 100... declare nó trong class Employee rồi khởi tạo nó ở ngoài const int Employee::MIN_EMPLOYEE_NO = 1000;
    cout<<"Salary: "; is>>a.salary;
    if (a.salary < 0) {/*cho về mặc định*/ return is; }
    return is;
    bool valid()const như cũ.
     
  9. BadBoy199x

    BadBoy199x T.E.T.Я.I.S

    Tham gia ngày:
    12/6/12
    Bài viết:
    528
    Nơi ở:
    Hà Giang Cỉty
    ah, đơn giản thế mà ko nghĩ ra =.=
    Á còn cái này nữa, trong class Employee e có viết chồng cin để nhập 3 biến rồi,
    H lập 1 cái class nữa dạng class Manager: public Employee
    Và yêu cầu bài là Your Manager class is supported by an extraction operator that accepts input in the form shown in the example below.
    Mã:
    Manager List
     ============
     Employee Name : Frank
     Employee No   : 1001
     Salary        : 25000
     Manages       : 25
     Employee Name : Alice
     Employee No   : 1005
     Salary        : 35000
     Manages       : 65
     Employee Name : Jane
     Employee No   : 1004
     Salary        : 30000
     Manages       : 20
     Employee Name : *
    
     Frank               (1001)-  25000.00-(25)
     Alice               (1005)-  35000.00-(65)
     Jane                (1004)-  30000.00-(20)
    
    Đó, làm sao cho nó chèn đoạn nhập Manager vào nữa, mà ko động chạm gì tới cái cin trong Employee ?

    Và Trong Manager e muốn sử dụng 1 biến trong Employee thì làm thế nào ?
     
    Chỉnh sửa cuối: 25/10/13
  10. gamevn211088

    gamevn211088 Youtube Master Race

    Tham gia ngày:
    30/7/13
    Bài viết:
    5
    có bác nào có tài liệu về PHP không ạ? nếu có cho em xin với, em mới bắt đầu học PHP nên cần tài liệu. em xin cảm ơn ạ
     
  11. pjicoshop

    pjicoshop Youtube Master Race

    Tham gia ngày:
    2/12/13
    Bài viết:
    5
    giáo trình C# , php nào cách lập 1 trang web
     
  12. huydaybn3000

    huydaybn3000 Fire in the hole! ⚜ Duel Master ⚜ Lão Làng GVN

    Tham gia ngày:
    26/3/07
    Bài viết:
    2,960
    Nơi ở:
    Hà Nội
    Cho mình hỏi,hiện tại mình làm trong 1 nhóm (mình mới nên gà lắm) leader bảo sử dụng struts 1.3 để lập trình web do struts 2 có vấn đề về bảo mật,
    nên 1 số attri của HTML 5 k sử dụng đc trong taglib <html:text >của struts 1 như placeholder="".

    Vậy giờ mình muốn tạo 1 css thành 1 placeholder mới thì làm ntn ?,ty nhiều.
     
  13. zero 7090

    zero 7090 Mayor of SimCity

    Tham gia ngày:
    20/5/05
    Bài viết:
    4,281
    Cho mình hỏi là dùng unikey mà đánh chữ có dấu trong photoshop thì thỉnh thoảng bị nhảy font khi dùng 1 số font nhất định. Mình dùng arial hay calibri thì không sao nhưng mà qua mấy cái font khác thì những chữ như ấ ạ ở ử toàn bị nhảy về font myriad pro. Thế có cách nào chữa không hay là tại font thế thì phải chịu?
     
  14. INTP

    INTP T.E.T.Я.I.S

    Tham gia ngày:
    16/5/11
    Bài viết:
    639
    tại font nó ko có ký tự đó nên nó phải chuyển qua font khác. Chịu thôi.

    hỏi 1 câu 1 tháng sau trả lời :">
     
  15. pardini4416

    pardini4416 Youtube Master Race

    Tham gia ngày:
    4/7/07
    Bài viết:
    87
    Có ai biết về Webform giải giúp mình mấy câu trắc nghiệm này với ôn tập để thi mà tìm mãi ko đc

    1.Which of the following are true about semantic errors:

    a. It occurs when the application is running
    b.They are also knows as exceptions
    c.These errors are very hard to locate and fix
    d.None of the above

    2.Which of the following methods belong to the Debug class:

    a.Debug.Write
    b.Debug.Print
    c.Debug.Insert
    d.Debug.Assest
    e. None of the above

    3.We cannot create an instance of the Debug class because it has no constructors but the class can be inherited

    a.True
    b.False

    4.____________is the process of receiving information messages about the execution of a Web application at runtime.

    a.Debuging
    b.Tracing
    c.Retrieving

    5.The connected layer of ADO.NET manages the traffic of data to and from the data source and is not specific to any data source.

    a.True
    b.False

    6.Which of the following are implementations of LINQ

    a.LING to DataSet
    b.LINQ to Objects
    c.LINQ to SQL
    d.LINQ to Entries
    e.LINQ to XML
    f. None of the above

    7.____________allows to create a data-driven Web application with little or no code

    a.DataModel
    b.Scaffolding
    c.Templates

    8.The _________class is used by the GridView and detailsView controls to display a field value.

    a.DynamicGridControl
    b.DynamicField
    c.DynamicListView

    9.The ScriptManager control is used to manage server script for AJAX-enabled ASP.NET Web.

    a.True
    b.False

    10.Which following statements about Common Language runtime(CLR) is not true.

    a.It is the virtual machine component of the .NET framework.
    b.The CLR component functions as an agent that manages code at run time.
    c.The CLR component enforces security
    d.It promotes robustness by implementing a strict type-and-code-verification infrastructure called the common system.
    e. None of the above

    11.The web forms have an _____ extension

    a. .aspx
    b. .aspxv
    c. .aspx.vb
    d. .aspx.cs

    12.The attributes of a form element are class and title. The id and method attributes belong to the body element

    a.True
    b.False

    13.______controls are used to display data from a data source.

    a.Data-bound
    b.Data source
    c. Data list
    d. Data view

    14.__________are group of code statements that have no user interface.

    a.Structures
    b.Class
    c.Types

    15.In ASP.NET both master pages and content pages can use the same set of events such as the init or Load events.

    a. True
    b.False

    16.Which of the following are not true about user controls

    a.User controls are self-contained
    b.User controls can be used only once
    c.User controls can be written in a different language from the language used for the main hosting page
    d.User controls can be shared in the web application

    17.Which of the following are the validation controls that are included in the ASP.NET page framework.

    a.RequiredValidator
    b.CompareValidator
    c.RangeValidator
    d.CustomValidator
    e.None of the above

    18.The RangeValidator control has which of the following properties.

    a.The property that specifies the minimum value for numeric values or minimum characters length of the string for string variables.
    b.The property that specifies the maximum value for numeric values or maximum characters length of the string for string variables.
    c.The property that is used to specify the data type of the value
    d. None of the above

    19.The ASP.NET validation controls perform validation automatically when a Web Form is posted back to the server.

    a.True
    b.False

    20.In ASP.NET the validation controls perform validation before the page initialization, but after control event-hanlding.

    a.True
    b.False

    21.__________is the process of identifying and resolving the logical errors in a web application.

    a.Tracing
    b.Error removing
    c.Debugging
    d.Error handling.
     
    Chỉnh sửa cuối: 13/9/14
  16. nhinhanglam

    nhinhanglam Youtube Master Race

    Tham gia ngày:
    27/8/14
    Bài viết:
    2
    Nơi ở:
    Hà Nội
    Không có sql à hả bạn. bạn có phần mềm slq nào tốt không cho mình
     
  17. ken89

    ken89 Mr & Ms Pac-Man

    Tham gia ngày:
    9/12/04
    Bài viết:
    232
    Có ai rành về G++ cho mình hỏi là trong g++ có lệnh nào giống cin.getline hay ko và làm sao để debug trong g++
     
  18. donkist

    donkist The Warrior of Light

    Tham gia ngày:
    15/11/09
    Bài viết:
    2,235
    Nơi ở:
    Svitk06.com
    lên tải sql sever của ms đó
     
  19. Clover 4 Lá

    Clover 4 Lá Donkey Kong GameOver

    Tham gia ngày:
    28/5/08
    Bài viết:
    332
    Cho mình hiểu phần mền Adobe Premiere Pro chuyên dùng dựng phim và adobe effect chuyên dừng làm kỷ xảo có đúng vậy ko ạ !

    và Adobe Premiere pro có thể làm kỷ xảo đc ko , kiểu như lồng ghép các hiệu ứng phức tạp vào 1 đoạn phim hay chỉ riêng Adobe Effect thì dùng tốt hơn !!

    Cám ơn
     
  20. Nhatcuong2014

    Nhatcuong2014 Youtube Master Race GameOver

    Tham gia ngày:
    11/2/15
    Bài viết:
    33
    Lập trình là kỹ thuật cài đặt một hoặc nhiều thuật toán trừu tượng có liên quan với nhau bằng một hoặc nhiều ngôn ngữ lập trình để tạo ra một chương trình máy tính.
     

Chia sẻ trang này