View Full Version : ช่วย Assignment ผมทีครับ T^T
Black_Phoenix
24-08-2008, 11:49 AM
เป็นโจทย์เกี่ยวกับ OOP (Object Oriented Program) นะครับ
ส่วนโจทย์ก็...แปลง Code จากภาษา Java (ที่ผมได้อัพโหลดไว้แล้ว link อยู่ด้านล่างครับ) ให้เป็นภาษา C+, Perl และ Visual Basic ให้สามารถแสดงผลได้ใกล้เคียงกับของภาษา Java ครับ
งงมากเลยตอนนี้ ไอ้การจะมาจับในส่วน Object ของแต่ละภาษาภายใน 3 วันเลยก็ดูจะยากไปหน่อยสำหรับผม เลยอยากจะรบกวนผู้รู้ช่วยชี้แนะด้วยครับ
Link : http://file.citec.us/download.php?id=9174BA59
PS.ไม่รู้ว่าผมโพสท์ได้ตรงหมวดรึเปล่า ที่เอาไว้นี่เพราะว่าภาษาตั้งต้นเป็น Java ครับ (Mod ท่านใดเห็นสมควรไปอยู่ที่อื่นรบกวนย้ายให้ด้วยนะครับ)
ขอบคุณล่วงหน้าครับ
Tummy
26-08-2008, 09:31 AM
- -a
เท่าที่ดูๆนะครับ (เอาแค่ C นะอย่างอื่นผมเขียนไม่เป็น)
ก็เปลี่ยนจาก import class ของ java ให้เป็น include .h ของ C น่ะครับ
ส่วนการใช้ รูปแบบการใช้งานก็ยึดเอาตาม assignment ที่เค้าให้มาเลย
Edkung_
26-08-2008, 10:21 AM
Assign1 - NameInStars.java -> เอา extends java.applet.Applet ออกเพราะ java ไม่มี ^^'
[code]public class NameInStars extends java.applet.Applet {
Black_Phoenix
26-08-2008, 03:28 PM
กลับมาพร้อม 24 Errors...
Compiling...
ShowNameInStars.cpp
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(1) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(1) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(2) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(3) : error C3381: 'NameInStars' : assembly access specifiers are only available in code compiled with a /clr option
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(6) : error C2146: syntax error : missing ':' before identifier 'String'
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(6) : error C2146: syntax error : missing ';' before identifier 'name'
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(9) : error C2144: syntax error : 'NameInStars' should be preceded by ':'
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(9) : error C2061: syntax error : identifier 'String'
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(14) : error C2146: syntax error : missing ':' before identifier 'String'
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(14) : error C2146: syntax error : missing ';' before identifier 'surroundNameInStars'
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(18) : warning C4183: 'surroundNameInStars': missing return type; assumed to be a member function returning 'int'
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(10) : error C2065: 'name' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(10) : error C2065: 'n' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(15) : error C2065: 'name' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(16) : error C2065: 'name' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(17) : error C2065: 'name' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(4) : error C2065: 'String' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(4) : error C2146: syntax error : missing ';' before identifier 'userName'
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(4) : error C2065: 'userName' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(5) : error C2065: 'cout' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(5) : error C2065: 'cin' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(5) : error C2065: 'userName' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(7) : error C2065: 'userName' : undeclared identifier
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(9) : error C2065: 'cout' : undeclared identifier
ประมาณนี้ครับ
Environment : Compile with Microsoft Visual Studio 2008 in Windows Vista 64 Bit Edition
Black_Phoenix
26-08-2008, 03:54 PM
อันนี้ NameInStars.h
public class NameInStars {
//data fieldss
public :
char* name;
char* n;
//Constructor methoddss
public : NameInStars(char* n){
name=n;
}
public : char* surroundNameInStars() {
return "*****"+name+"*****\n"+
"*****"+name+"*****\n"+
"*****"+name+"*****";
}
};
ส่วนอันนี้เป็น ShowNameInStars.cpp ครับ
#include "NameInStars.h";
#include <iostream>
#include <string>
//using std::string;
using namespace std;
int main(int argc, char* argv[]) {
//get the user's name
char* userName;
cout << "Enter your name"; cin >> userName;
//create a NameInStars object that stores the user's name
NameInStars you = new NameInStars(userName);
//display your name in star three times
cout << you.surroundNameInStars();
}
Error ที่เกิดขึ้นทั้งหมดครับ
Compiling...
ShowNameInStars.cpp
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(1) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(1) : error C3381: 'NameInStars' : assembly access specifiers are only available in code compiled with a /clr option
d:\bkpx\oop_cpp1\oop_cpp1\nameinstars.h(14) : error C2110: '+' : cannot add two pointers
d:\bkpx\oop_cpp1\oop_cpp1\shownameinstars.cpp(11) : error C2440: 'initializing' : cannot convert from 'NameInStars *' to 'NameInStars'
No constructor could take the source type, or constructor overload resolution was ambiguous
Edkung_
26-08-2008, 04:22 PM
อัพเดตแก้ error
ShowNameInStars.cpp
[code]#include <iostream>
#include "NameInStars.h";
using namespace std;
int main(int argc, char* argv[]) {
chidkido
26-08-2008, 05:29 PM
visual basic นี่ 6.0
หรือว่า .net หละครับ :unsure:
Black_Phoenix
26-08-2008, 06:29 PM
visual basic นี่ 6.0
หรือว่า .net หละครับ :unsure:
[/b]
ผมใช้ Visual Studio 2008 อ่ะครับ (ซึ่งผมก็ไม่แน่ใจว่ามันเป็นอันไหนเหมือนกัน ^^")
asylu3
26-08-2008, 06:38 PM
ตกลงว่าแก้ได้ยังครับ ล่าสุดเหลือ error อะไรบ้าง Post update ด้วยครับ
Black_Phoenix
26-08-2008, 07:49 PM
ตอนนี้แก้ได้แล้วครับ ตอนนี้ก็เหลือลุยข้ออื่นต่อ T^T
asylu3
26-08-2008, 11:23 PM
ผมขอกล่่าวขอบคุณแทน Black Phoenix ให้กับผู้ช่วยเหลือทุกๆท่านนะครับ (โดยเฉพาะ Edkung ที่ช่วยเหลือเยอะเป็นพิเศษ )
และอยากให้ผู้รับการช่วยเหลือไป จับจุดให้ได้ว่า
1. เหตุใดตัวเองจึงหลุดหรือผิดพลาดไปใน จุดนั้นๆ
2. ผู้ช่วยเหลือทำอย่างไรในการแก้ปัญหาเหล่านั้นได้
ซึ่งหากทำได้เช่นนี้แล้วรับรองว่า สามารถช่วยเหลือตัวเองในโอกาสหน้าแน่นอนครับ เพราะ Assignment หาก
สามารถได้ "แนวทาง" แก้ปัญหาไปแทนที่จะได้ Solution ไปจะได้ประโยชน์มากกว่า
เป็นกำลังใจให้ครับ
pspn.n
27-08-2008, 05:26 AM
ผมใช้ Visual Studio 2008 อ่ะครับ (ซึ่งผมก็ไม่แน่ใจว่ามันเป็นอันไหนเหมือนกัน ^^")[/b]
อันนี้เป็น vb.net แล้วครับ - -'
ความสุขอย่างหนึงของการเขียนโปรแกรมคือ การแก้บักได้สำเร็จ
และโปรแกรมที่เราเขียนทำงานได้ มีมีคนใช้งาน :-)
ผมลองทำ Assign1 เป็น VB.Net ให้ดูก็แล้วกัน --- ไม่รู้สายไปหรือป่าว (C++ Ed ทำไปแล้ว)
และอ่านข้อมูลเพิ่มเติมได้ที่นี Object Oriented VB.net (http://www.codeproject.com/KB/vb/OOPS_In_VBNET.aspx) และที่นี่ ONDotnet (http://www.ondotnet.com/pub/a/dotnet/2001/09/25/oop_vb.html)
จริงๆ ยังมีอีกเพียบ
NameInStars.vb
[code]
Public Class NameInStars
Black_Phoenix
27-08-2008, 08:11 AM
ทีนี้เหลือแต่ภาษา Perl แล้ว ซึ่งผมยังไม่เข้าใจว่ามันจะจับเป็น Object ได้ยังไง - -a
สงสัยงานนี้ต้องพึ่งเซียน Perl ด้วยล่ะมั้งเนี่ย (หนักใจความสามารถของตัวเองจริงๆ T^T)
Edit : เลื่อนเส้นตายไปเป็นวันเสาร์นี้ครับ (รอดไป ^^")
Edit 2: ขอขอบคุณทุกท่านที่ช่วยแก้ปัญหา Assignment ให้ผมนะครับ (ได้ความรู้ขึ้นอีกโขเลยงานนี้ ^^v)
pspn.n
27-08-2008, 08:36 AM
ทีนี้เหลือแต่ภาษา Perl แล้ว ซึ่งผมยังไม่เข้าใจว่ามันจะจับเป็น Object ได้ยังไง - -a[/b]
ผมไม่มีเวลาที่จะทำตัวอย่าง Assignment ให้ ลองดูตัวอย่างที่นี่ครับน่าจะช่วยท่านได้ ที่เหลือท่านต้องออกแรงเอง
Perl Object Oriented Programming (http://www.codeproject.com/KB/perl/camel_poop.aspx)
Hope this help
Black_Phoenix
29-08-2008, 12:34 AM
ตอนนี้ติดปัญหาอยู่ที่ Assignment 3 ครับ แก้ยังไงก็ไม่ได้ซักที เริ่มงงๆ+ ง่วง
Code ที่ผมทำมีดังนี้ครับ
Employee.h
[src]#include <cstddef>
#include <cassert>
#include <cstring>
#include <cstdlib>
#define restrict
static const double MAX_NO_OVERTIME = 40; //hours in work week
static const double OVERTIME_RATE = 1.5; //hours in work week
static const double MAX_NO_DUES = 100; //max earnings before dues
static const double DUES = 25; //dues amount
class Employee {
//class constants
public:
char *empID;
double hours;
double rate;
//method with decision steps
//postcondition - returns net pay
double computeGross(){
if (hours <= MAX_NO_OVERTIME){
return hours * rate;
}
else {
double regularPay = MAX_NO_OVERTIME * rate;
double overtimePay = (hours - MAX_NO_OVERTIME)*
OVERTIME_RATE * rate;
return regularPay + overtimePay;
}
}
//method with decision steps
//postcondition - returns net pay
double computeNet(double gross){
if (gross<=MAX_NO_DUES)
return gross;
else
return gross - DUES; //deduct dues amount
}
//Other methods
Employee(){ }
Employee(char *id,double ho,double ra){
empID = id;
hours = ho;
rate = ra;
}
void setEmpID(char *id){
empID = id;
}
char *getEmpID(){
return empID;
}
char *const Con4Strs(char const *const restrict a,
char const *const restrict b,
char const *const restrict c,
char const *const restrict d,
char const *const restrict e,
char const *const restrict f,
char const *const restrict g) {
assert(a); assert(B); assert
Edkung_
29-08-2008, 09:33 PM
d:\bkpx\oop_cpp3\employee.h(95) : error C2664: 'Employee::Con4Strs' : cannot convert parameter 4 from 'double' to 'const char *const '[/b]
parameter ที่ 4 ผิดครับ ต้องเป็น pointer of char ไม่ใช่ double -> แปลงข้อมูลของ double ให้อยู่ในรูปข้อความก่อนค่อย pass ลงไป
d:\bkpx\oop_cpp3\companypayroll.h(22) : error C2143: syntax error : missing ')' before ';'
d:\bkpx\oop_cpp3\companypayroll.h(22) : error C2664: 'Employee::Employee(const Employee &)' : cannot convert parameter 1 from 'std::basic_ostream<_Elem,_Traits>' to 'const Employee &'
with
[
_Elem=char,
_Traits=std::char_traits
]
Reason: cannot convert from 'std::basic_ostream<_Elem,_Traits>' to 'const Employee'
with
[
_Elem=char,
_Traits=std::char_traits
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called[/b]
ผิดเยอะครับ เอาแบบคร่าวๆ ลองเปลี่ยนเป็น
char *empID, *hours, *rate;
cout << "Enter Employee id : "; cin >> empID;
cout << "Enter hours worked : "; cin >> hours;
cout << "How many employees? : "; cin >> rate;
Employee programmer = new Employee (empID, hours, rate);
แต่ว่า hours, rate ต้องแปลงเป็น double ก่อนนะครับ ถึงจะใช้ได้เพราะ constructer ของท่านมันเป็น Employee(char*, double, double)
ส่วนข้างล่างกว่านี้ยังไม่ต้องไปดูครับ แก้ตรงนี้ให้หมดก่อน บางที พอแก้แล้ว จุดอื่นอาจจะหายตามไปด้วย
Black_Phoenix
30-08-2008, 01:00 AM
ขอถามอะไรโง่ๆนิดนึงนะครับ
แปลงข้อมูลยังไงอ่า
ลองใช้ทั้ง _gcvt(hours) กะ _gcvt(rate) แล้วมันก็ไม่ได้อ่ะครับ (วางไว้ก่อนถึงประโยค Employee programmer = new Employee (empID, hours, rate); อ่ะครับ)
รบกวนด้วยนะครับ ขอบคุณล่วงหน้าครับผม
Powered by vBulletin® Version 4.2.5 Copyright © 2026 vBulletin Solutions Inc. All rights reserved.