-
May 30th, 2022, 09:51 AM
#1
needs to convert C++ code to JAVA
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int NP, NE, *VO,*VTP, T, pop, stok,st, nonam, iter, it, nb_am;
struct et
{
int id, s, NM, *VT, *VA, **M, cmax;
};
struct sol
{
int *VSO, **aff,ValOrd, cmax;
};
et *FSH;
sol *SL;
void init();
void gener_sol();
void Aff();
void Matrice(et);
bool exist(int, int*);
int id(int, int[]);
int som(int **);
int MAX(sol);
void aff_sol(sol);
void permut(int&, int&);
int nbrNA(sol*, int);
sol best (sol*);
void genetic(sol*);
//---------------------------------------------------------------------------
int main()
{
int V[10], i;
srand(time(0));
init();
Aff();
cout << "\nSol initiale avant simulation : \n";
aff_sol(SL[0]);
genetic(SL);
cout << "\nSol optimale apres simulation : \n";
aff_sol(best(SL));
cout << "\nnombre des iterations : "<< it<<"\n";
cout << "\nnombre de non ameliores : "<< nb_am<<"\n";
return 0;
}
//----------------------------------------------------------------------------
void init()
{
int i,j,k,a;
cout << "Donner le NBR des Pieces : ";
cin >> NP;
cout << "Donner le NBR des Etages : ";
cin >> NE;
cout << "Donner la position du stockage \n(1-Par Machine, 2-Par Etage, 3-Par Systeme):";
cin >> stok;
cout << "Donner la taille de pop : ";
cin >> pop;
cout << "Donner le nombre d'iterations pour Genetic :";
cin >> iter;
cout << "Donner le nombre de non ameliores pour Genetic :";
cin >> nonam;
VO = new int[NP];
VTP = new int[NP];
for(a=0;a<NP;a++)for(i=0;i<NP;i++) {VO[i]=i+1; VTP[i] = rand()%30+10;}
FSH = new et[NE];
for(i=0;i<NE;i++)
{
FSH[i].id=i+1;
//cout << "Donner le Stockage de l'etage "<< i+1 << " :";
//cin >> FSH[i].s;
if(stok==1) st = 5;
else if(stok==2) st = 10;
else st = 30;
cout << "Donner le nbr des machines de l'etage "<< i+1 << " :";
cin >> FSH[i].NM;
FSH[i].VT = new int[FSH[i].NM];
for(j=0;j<FSH[i].NM;j++) for(a=0;a<NP;a++)FSH[i].VT[j] = rand()%5+1;
FSH[i].VA = new int[NP];
for(j=0;j<NP;j++)
for(k=0;k<3;k++) {FSH[i].VA[j] = rand()%(FSH[i].NM)+1;}
FSH[i].M = new int*[NP];
for(j=0;j<NP;j++)FSH[i].M[j] = new int[NP];
Matrice(FSH[i]);
FSH[i].cmax=0;
}
gener_sol();
}
void Aff()
{
int i,j,k;
cout << "le Nbr des pieces = " << NP << ".\n";
cout << "le Nbr des etages = " << NE << ".\n";
cout << "L'Ordonnancement des pieces : {";
for(i=0;i<NP;i++) cout << VO[i] <<" ";
cout <<"}.\n";
cout << "Temps de traitement de chaque pieces : {";
for(i=0;i<NP;i++) cout << VTP[i] <<" ";
cout <<"}.\n";
for(i=0;i<NE;i++)
{
cout << "\nL'etage "<< i+1 << " :\n";
//cout << " Capacite de stockage: "<< FSH[i].s<<".\n";
cout << " le nbr des machines de l'etage "<< FSH[i].NM<<".\n";
cout << " Affectation des pieces: {";
for(j=0;j<NP;j++) cout << FSH[i].VA[j] <<" ";
cout << "}.\n";
cout << " Vitesse de traitement des machines: {";
for(j=0;j<FSH[i].NM;j++) cout << FSH[i].VT[j] <<" ";
cout << "}.\n";
/*cout << " La Matrice de correspondance :\n";
for(j=0;j<NP;j++)
{
cout << " ";
for(k=0;k<NP;k++) cout << setw(3) << FSH[i].M[j][k] << setw(3);
cout << "\n";
}*/
}
}
void Matrice(et e)
{
int i,j;
for(i=0;i<NP;i++)
for(j=i;j<NP;j++)
{
if(i==j)
e.M[i][j]=0;
else
if(e.VA[i]==e.VA[j])
if(id(i+1,VO)<id(j+1,VO))
e.M[i][j]=1;
else
e.M[i][j]=-1;
else
if(id(i+1,VO)<id(j+1,VO))
e.M[i][j]=2;
else
e.M[i][j]=-2;
e.M[j][i]=-e.M[i][j];
}
}
int id(int v, int A[])
{
int i=0;
while(v!=A[i])i++;
return i;
}
int som(int **M)
{
int i,j,s=0;
for(i=0;i<NP;i++)
for(j=i;j<NP;j++)
s += abs(M[i][j]);
return s*2;
}
void permut (int &a, int &b)
{
int c = a; a = b; b = c;
}
void gener_sol()
{
int k,i,j,k1,k2;
SL = new sol[pop];
for (k=0;k<pop;k++)
{
SL[k].VSO= new int[NP];
SL[k].aff= new int*[NP];
for(j=0;j<NP;j++)
SL[k].aff[j]= new int[NE];
if(k==0)
{
for(i=0;i<NP;i++) SL[k].VSO[i] = VO[i];
SL[k].ValOrd = (rand()%20+50)*(NP+NE/2);
for(i=0;i<NP;i++)
for(j=0;j<NE;j++)
SL[k].aff[i][j]=FSH[j].VA[i];
}
else
{
for(i=0;i<NP;i++) SL[k].VSO[i] = VO[i];
k1 = rand()%NP; k2 = rand()%NP;
permut (SL[k].VSO[k1], SL[k].VSO[k2]);
SL[k].ValOrd = (rand()%20+50)*(NP+NE/2);
for(j=0;j<NE;j++)
for(i=0;i<NP;i++)
SL[k].aff[i][j]=rand()%(FSH[j].NM)+1;
}
SL[k].cmax = MAX(SL[k]);
}
}
void aff_sol(sol S)
{
int i,j;
cout <<"\nOrdonnancement : ";
for (i=0;i<NP;i++)cout << S.VSO[i] <<" ";
//cout << " \nLa valeur "<< S.ValOrd<<"\n";
cout << "\nAffectation : \n";
for (j=0;j<NE;j++)
{
for (i=0;i<NP;i++)cout << S.aff[i][j] <<" ";
cout << "\n";
}
cout << "\nCmax = " << S.cmax <<"\n";
}
int MAX(sol S)
{
int f=0,i,j,m=0;
for(j=0;j<NE;j++)
{
for(i=0;i<NP;i++)
m += st*0,01*VTP[S.VSO[i]-1]/FSH[j].VT[S.aff[i][j]-1];
m += S.ValOrd;
}
for(i=0;i<NE;i++) f+=FSH[i].NM;
f = f*NP;
return m+f;
}
int nbrNA(sol *S, int max)
{
int i, nb=0;
for(i=0;i<pop;i++)
if(MAX(S[i])==max)nb++;
return nb;
}
sol best (sol *S)
{
int i;
sol B = S[0];
for(i=1;i<pop;i++)
if(MAX(S[i])<MAX(B))B = S[i];
return B;
}
void genetic(sol *S)
{
it=0; nb_am=0; int i,j,k1,k2,v,t;
sol S1, S2;
while (it<iter && nb_am<nonam)
{
i = rand()%pop; j = rand()%pop;
S1 = S[i]; S2 = S[j];
k1 = rand()%NP; k2 = rand()%NP;
permut(S1.VSO[k1], S1.VSO[k2]);
permut(S2.VSO[k1], S2.VSO[k2]);
for(t=0;t<NE;t++)
{
v = rand()%NP;
S1.aff[v][t] = S2.aff[v][t];
v = rand()%NP;
S2.aff[v][t] = S1.aff[v][t];
}
if(MAX(S1)<MAX(S[i]))S[i]=S1;
if(MAX(S2)<MAX(S[j]))S[j]=S2;
it++;
nb_am = nbrNA(S, MAX(best(S)));
}
}#include <iostream>
using namespace std;
int main()
{
// variable declaration
int location,fine = 0,current_demerit = 0,months,demerit,total_demerit,license = 1;
char offence,iphone;
// get location input
cout << "--------****--------****--------****--------****--------****--------\n";
cout << "\t\tWelcome to the Fine and Demerit Point Evaluator!\n\t\t\tbased on Crazy Nancy's Criteria\n";
cout << "--------****--------****--------****--------****--------****--------\n";
cout << "Welcome Officer - I need some information before I tell you what the fine and demerit points are.\nHere are the possible locations\n" ;
cout << "\t\t1 - Driver was stopped on the highway\n\t\t2 - In a school zone\n\t\t3 - Car is stopped at a Stop sign or traffic light\n\t\t4 - None of the above\nPlease enter digit corresponding to your case: ";
cin >> location;
// get previous demerit points
cout << "Last question officer! How many demerit points does the driver have prior to being stopped? ";
cin >> demerit;
//switch location
switch(location)
{
// if in highway
case 1:
fine = 80;
cout << "Officer is this the 1st offense of the driver? n for No,y for yes : ";
cin >> offence;
// for first offense
if(offence == 'y')
current_demerit = 1;
// for multiple offense
else
current_demerit = 2;
break;
// if in school zone
case 2:
fine = 100;
// get months
cout << "Officer for how many months has the driver been driving for? ";
cin >> months;
// check months
if(months < 24)
license = 0;
else
current_demerit = 4;
break;
// if car is stopped
case 3:
cout << "Officer is the driver using an iPhone? n for no,y for yes : ";
cin >> iphone;
// if driver uses iphone
if(iphone == 'y')
{
fine = 100;
current_demerit = 2;
}
// for other phones
else
{
fine = 80;
current_demerit = 1;
}
break;
// in any other case
default:
fine = 90;
current_demerit = 3;
}
// print fine and demerit points
total_demerit = current_demerit + demerit;
cout << "--> Write a ticket for $" << fine;
cout << ". Also driver has " << total_demerit << " demerit points.\n";
// check if driver still has license
if(total_demerit > 12 || license == 0)
cout << "Please take away their driver's license and make arrangements to have the car towed right away.\n";
cout << "Good job officer! Crazy Nancy's tells you to keep up the good work!!!!";
return 0;
}
OUTPUT :
Last edited by 2kaud; May 31st, 2022 at 06:40 AM.
Reason: Added code tags
-
May 30th, 2022, 11:58 AM
#2
Re: needs to convert C++ code to JAVA
can anyone do the conversion
-
May 30th, 2022, 01:24 PM
#3
Re: needs to convert C++ code to JAVA
 Originally Posted by Sheria
can anyone do the conversion
No one will ever try it until you will post a properly indented and formatted code. Note that you have to use CODE tags while posting your code snippets!
Victor Nijegorodov
-
May 31st, 2022, 06:46 PM
#4
Re: needs to convert C++ code to JAVA
 Originally Posted by VictorN
No one will ever try it until you will post a properly indented and formatted code. Note that you have to use CODE tags while posting your code snippets!
Code in C++ :
#include <iostream>
using namespace std;
int main()
{
// variable declaration
int location,fine = 0,current_demerit = 0,months,demerit,total_demerit,license = 1;
char offence,iphone;
// get location input
cout << "--------****--------****--------****--------****--------****--------\n";
cout << "\t\tWelcome to the Fine and Demerit Point Evaluator!\n\t\t\tbased on Crazy Nancy's Criteria\n";
cout << "--------****--------****--------****--------****--------****--------\n";
cout << "Welcome Officer - I need some information before I tell you what the fine and demerit points are.\nHere are the possible locations\n" ;
cout << "\t\t1 - Driver was stopped on the highway\n\t\t2 - In a school zone\n\t\t3 - Car is stopped at a Stop sign or traffic light\n\t\t4 - None of the above\nPlease enter digit corresponding to your case: ";
cin >> location;
// get previous demerit points
cout << "Last question officer! How many demerit points does the driver have prior to being stopped? ";
cin >> demerit;
//switch location
switch(location)
{
// if in highway
case 1:
fine = 80;
cout << "Officer is this the 1st offense of the driver? n for No,y for yes : ";
cin >> offence;
// for first offense
if(offence == 'y')
current_demerit = 1;
// for multiple offense
else
current_demerit = 2;
break;
// if in school zone
case 2:
fine = 100;
// get months
cout << "Officer for how many months has the driver been driving for? ";
cin >> months;
// check months
if(months < 24)
license = 0;
else
current_demerit = 4;
break;
// if car is stopped
case 3:
cout << "Officer is the driver using an iPhone? n for no,y for yes : ";
cin >> iphone;
// if driver uses iphone
if(iphone == 'y')
{
fine = 100;
current_demerit = 2;
}
// for other phones
else
{
fine = 80;
current_demerit = 1;
}
break;
// in any other case
default:
fine = 90;
current_demerit = 3;
}
// print fine and demerit points
total_demerit = current_demerit + demerit;
cout << "--> Write a ticket for $" << fine;
cout << ". Also driver has " << total_demerit << " demerit points.\n";
// check if driver still has license
if(total_demerit > 12 || license == 0)
cout << "Please take away their driver's license and make arrangements to have the car towed right away.\n";
cout << "Good job officer! Crazy Nancy's tells you to keep up the good work!!!!";
return 0;
}
OUTPUT :
-
June 1st, 2022, 02:27 AM
#5
Re: needs to convert C++ code to JAVA
You didn't look at your post after you have posted it. Did you?
Well, just try it now! Can you understand something from this unformatted text that does not look like any programming code? I guess you cannot.
It is because the "code" was not properly formatted.
You have to use CODE tags around your code snippet to format it:
[CODE] yor code snippet [/CODE]
Note also that your code snippet that you put between the tags must have proper indentations!
Victor Nijegorodov
-
June 2nd, 2022, 07:51 AM
#6
Re: needs to convert C++ code to JAVA
that's the Code in C++ :
I have no idea why can't be converted to Java Code
#include <iostream>
using namespace std;
int main()
{
// variable declaration
int location,fine = 0,current_demerit = 0,months,demerit,total_demerit,license = 1;
char offence,iphone;
// get location input
cout << "--------****--------****--------****--------****--------****--------\n";
cout << "\t\tWelcome to the Fine and Demerit Point Evaluator!\n\t\t\tbased on Crazy Nancy's Criteria\n";
cout << "--------****--------****--------****--------****--------****--------\n";
cout << "Welcome Officer - I need some information before I tell you what the fine and demerit points are.\nHere are the possible locations\n" ;
cout << "\t\t1 - Driver was stopped on the highway\n\t\t2 - In a school zone\n\t\t3 - Car is stopped at a Stop sign or traffic light\n\t\t4 - None of the above\nPlease enter digit corresponding to your case: ";
cin >> location;
// get previous demerit points
cout << "Last question officer! How many demerit points does the driver have prior to being stopped? ";
cin >> demerit;
//switch location
switch(location)
{
// if in highway
case 1:
fine = 80;
cout << "Officer is this the 1st offense of the driver? n for No,y for yes : ";
cin >> offence;
// for first offense
if(offence == 'y')
current_demerit = 1;
// for multiple offense
else
current_demerit = 2;
break;
// if in school zone
case 2:
fine = 100;
// get months
cout << "Officer for how many months has the driver been driving for? ";
cin >> months;
// check months
if(months < 24)
license = 0;
else
current_demerit = 4;
break;
// if car is stopped
case 3:
cout << "Officer is the driver using an iPhone? n for no,y for yes : ";
cin >> iphone;
// if driver uses iphone
if(iphone == 'y')
{
fine = 100;
current_demerit = 2;
}
// for other phones
else
{
fine = 80;
current_demerit = 1;
}
break;
// in any other case
default:
fine = 90;
current_demerit = 3;
}
// print fine and demerit points
total_demerit = current_demerit + demerit;
cout << "--> Write a ticket for $" << fine;
cout << ". Also driver has " << total_demerit << " demerit points.\n";
// check if driver still has license
if(total_demerit > 12 || license == 0)
cout << "Please take away their driver's license and make arrangements to have the car towed right away.\n";
cout << "Good job officer! Crazy Nancy's tells you to keep up the good work!!!!";
return 0;
}
OUTPUT :
-
June 2nd, 2022, 09:33 AM
#7
Re: needs to convert C++ code to JAVA
that was the required solution
Write a complete java program (using a single System.out.println statement) that prints the
following output. Note that you should not declare any variable to implement this question.
Question 2 – Nested selection statements
The Quebec government is trying to crack down on texting and driving. “The law is very clear:
you must not hold a cell phone in your hand while driving. Failure to abide by this rule is an
offence subject to a fine and demerit points.” (https://saaq.gouv.qc.ca/en/road
safety/behaviours/distractions/cell-phones-texting/what-the-law-says/). Write a program that
determines the penalty for a driver holding a cell phone while driving based on the following
criteria.
If the driver is driving and holding a cell phone
On the highway the fine is $80. If it is the drivers 1st offence, s/he gets 1 demerit point
otherwise 2 demerit points.
In a school zone the fine is $100 (the maximum). If the driver has been driving for less
than 24 months s/he loose his/her license on the spot otherwise s/he get 4 demerit points
(the maximum).
If the car is stopped, for example at a stop sign or a traffic light, and the cellphone is an
iPhone, the fine is $100 and s/he gets 2 demerit point otherwise the fine is $80 and s/he
gets 1 demerit points.
All other cases the fine is $90 and the driver gets 3 demerit points.
After determining where the driver was stopped and what their fine if they have not yet lost
their license, ask the user how many demerit points the driver originally had. When a driver
has 12 demerit points they lose their license. Determine if the driver should lose their license
given where they were when they were holding their cell phone. Output how many demerit
points the driver now has.
-Assume you have a perfect user who enters valid input.
-Here are few sample outputs to illustrate the expected behavior of your program.
-
June 2nd, 2022, 09:37 AM
#8
Re: needs to convert C++ code to JAVA
-
June 2nd, 2022, 10:10 AM
#9
Re: needs to convert C++ code to JAVA
 Originally Posted by Sheria
that's the Code in C++ :
I have no idea why can't be converted to Java Code
I have to say you again: no one will ever try to convert your "code" either to Java or to something other!
Just because what you already (three times!) posted is absolutely unreadable/non-understandable.
Once more: see my post#6.
Victor Nijegorodov
-
June 2nd, 2022, 10:53 AM
#10
Re: needs to convert C++ code to JAVA
I have no idea why can't be converted to Java Code
It could be by someone who knows both C++ and Java. However I doubt that anyone looking at this thread with the requisite skills will actually do this 'ex gratia'. This forum isn't for getting work done for free but for asking/answering of c/c++ questions.
Even given the program requirements, it is highly unlikely anyone with Java skills would code the program without payment.
If you try the conversion yourself and then have specific C++/Java questions, then post them.
It also looks like this is a homework exercise. In this case if a complete Java program is provided then that would be considered as 'cheating' - and not allowed.
Why are you trying to convert an existing C++ program? Just start designing and coding one from scratch in Java!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 4th, 2022, 04:27 AM
#11
Re: needs to convert C++ code to JAVA
 Originally Posted by Sheria
can anyone do the conversion
There are C++ to Java conversion tools available on the internet. Some are for free.
-
June 5th, 2022, 08:42 AM
#12
Re: needs to convert C++ code to JAVA
 Originally Posted by wolle
There are C++ to Java conversion tools available on the internet. Some are for free.
could you kindly provide a link
-
June 5th, 2022, 08:46 AM
#13
Re: needs to convert C++ code to JAVA
 Originally Posted by Sheria
could you kindly provide a link
Are you banned by Google?
https://www.google.com/search?q=C%2B...client=gws-wiz
Victor Nijegorodov
-
June 5th, 2022, 10:46 AM
#14
Re: needs to convert C++ code to JAVA
... but that would require the OP to actually do some work as opposed to being spoon-fed!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 5th, 2022, 10:51 AM
#15
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|