Computer Games Forum
 

Go Back   Computer Games Forum > Tech > The Workshop > Limbaje de programare

Notices

Căutare produse PC Garage: Şterge căutarea

Reply
 
LinkBack Thread Tools
Old 01-06-2008, 02:10   #1 (permalink)
TLD
Registered User
 
TLD's Avatar
 
Join Date: Jan 2005
Location: Bucuresti
Problema c++

Mi-am propus sa calculez o suma de puteri, mai exact S5(n)=1/12*[n2*(n+1)2*(2*n2+2*n-1)]=1/12*(2*n6+6*n5+5*n4-n2). Bun, pana aici toate bune si frumoase, cand scriu programul da insa erori la compilare, iar sincer la ora asta chiar nu stiu ce sa-i mai fac. Daca exista cineva care are vreo solutie, solutia e binevenita.

[codebox]#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>


const MAX_NO=20;

using namespace std;
typedef pair<long, long> TFraction;
long cmmdc(long a, long b ){
long r;
while(b!=0){
r=a%b;
a=b;
b=r;
}
return a;
}

void simplifyFraction(TFraction &f){
long d;
d=cmmdc(f.first, f.second);
if(!d){
cout<<"Eroare!!! Impartire la 0!";
return;
}
f.first /=d; f.second /=d;
if(f.second<0){
f.second *=-1;
f.first *=-1;
}
}

TFraction sumFractions(TFraction& f1, TFraction& f2){
TFraction f;
simplifyFraction(f1); simplifyFraction(f2);
f.first=f1.first*f2.second+f1.second*f2.first;
f.second=f1.second*f2.second;
simplifyFraction(f);
return f;
}

long lcmDenomin(TFraction fr[MAX_NO], short n){
long m;
short isM;
vector<long> v;
for(int i=0; i<n; i++){
if(fr[i].first!=0)
v.push_back(fr[i].second);
}
sort(v.begin(), v.end());
if(v.size()<1) return 1;
m=v[v.size()-1]; isM=0;
while(!isM){
isM=1;
for(size_t i=1; i<v.size() && isM; i++)
if(m%v[i] !=0){isM=0; break;}
m++;
}
m--;
return m;
}

void genCombinations(long C[MAX_NO+2][MAX_NO+2], short n){
short i,j;
C[0][0]=1;
for(i=1; i<=n; i++){
C[i][0]=C[i][i]=1;
for(j=1; j<i; j++)
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}

int main(){
long C[MAX_NO+2][MAX_NO+2];
long A[MAX_NO+1][MAX_NO+2];
long M[MAX_NO+1], Q;
short k, p, t, r, sign;
TFraction fr[MAX_NO+2], W[MAX_NO+1], fAux, fAux2;
genCombinations(C, MAX_NO+1);
ifstream in("psum.in");
ofstream out("psum.out");
M[0]=1; A[0][1]=1; A[0][0]=0;
for(p=1; p<=MAX_NO; p++){
sign=1;
for(t=p-1; t>=0; t--){
W[t].first=sign*C[p+1][p+1-t];
W[t].second=M[t];
sign *=-1;
simplifyFraction(W[t]);
}
for(t=p; t>=0; t--){
fAux.first=0; fAux.second=1;
for(r=p-1; r>=t-1 && r>=0; r--){
fAux2.first=W[r].first*A[r][t];
fAux2.second=W[r].second;
simplifyFraction(fAux2);
fAux=sumFractions(fAux, fAux2);
}
fr[t]=fAux;
}
fAux2.first=W[0].first*A[0][0];
fAux2.second=W[0].second;
simplifyFraction(fAux2);
fr[0]=sumFractions(fr[0], fAux2);
fr[p+1].first=1; fr[p+1].second=1;
Q=lcmDenomin(fr, p+1);
M[p]=(p+1)*Q;
for(t=p+1; t>=0; t--){
fAux2.first=fr[t].first*Q;
fAux2.second=fr[t].second;
simplifyFraction(fAux2);
A[p][t]=fAux2.first;
}
}

while(in && !in.eof() && in>>k){
out<<M[k]<< " ";
for(t=k+1; t>=0; t--){
out<<A[k][t]<< " ";
}
out<< endl;
}
return 0;
}[/codebox]
 
TLD is offline    Reply With Quote
Old 01-06-2008, 02:18   #2 (permalink)
75% apa
 
Doctor Bonez's Avatar
 
Join Date: Feb 1999
Pai, scrii ditai codul si nu specifici si eroarea pe care o primesti?

La o privire fugara, vad ca n-ai declarat tipul constantei.

const int MAX_NO=20;
__________________
Nashpa...

"In the beginning the Universe was created. This has made a lot of people very angry
and been widely regarded as a bad move." (Douglas Adams)

 
Doctor Bonez is offline    Reply With Quote
Old 01-06-2008, 02:30   #3 (permalink)
TLD
Registered User
 
TLD's Avatar
 
Join Date: Jan 2005
Location: Bucuresti
Erorile sunt

Could not find a match for 'std:: pair<long,long>:: pair()'
Cannot find default constructor to initialize array element of type 'std:: pair<long,long>:: pair()'
Could not find a match for 'std:: pair<long,long>:: pair()'

Practic eroarea pare a fi in zona asta a programului, dar nu vad ce trebuie schimbat:

TFraction sumFractions(TFraction& f1, TFraction& f2){
TFraction f;
simplifyFraction(f1); simplifyFraction(f2);
f.first=f1.first*f2.second+f1.second*f2.first;
f.second=f1.second*f2.second;
simplifyFraction(f);
return f;
}
 
TLD is offline    Reply With Quote
Old 01-06-2008, 11:50   #4 (permalink)
75% apa
 
Doctor Bonez's Avatar
 
Join Date: Feb 1999
Mie azi-noapte mi l-a compilat in DevCpp dupa ce am facut modificarea de mai sus (const int..). N-am reusit sa-l testez, pentru ca nu stiam ce trebuie sa pun in psum.in si imi era foarte somn, dar de compilat il compila fara probleme.
__________________
Nashpa...

"In the beginning the Universe was created. This has made a lot of people very angry
and been widely regarded as a bad move." (Douglas Adams)

 
Doctor Bonez is offline    Reply With Quote
Old 01-06-2008, 13:32   #5 (permalink)
TLD
Registered User
 
TLD's Avatar
 
Join Date: Jan 2005
Location: Bucuresti
Da, destul de ciudat, eu l-am rulat cu Borland C++-ul si imi dadea erorile alea, chit ca adugam int la acea constanta. Insa cu dev-c++ vad ca il compileaza, deci era problema de compilator. Mersi mult pentru ajutor!
 
TLD is offline    Reply With Quote
Old 02-06-2008, 17:26   #6 (permalink)
Registered User
 
Nokturnis's Avatar
 
Join Date: Jun 2006
Location: Bucureşti
Încearcă să incluzi şi headerul <utility>

#include <utility>
__________________
Stat rosa pristina nomine, nomina nuda tenemus.
 
Nokturnis is offline    Reply With Quote
Advertisment
Reply

  Computer Games Forum > Tech > The Workshop > Limbaje de programare

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 07:51.


Advertisement System V2.5 By   Branden
This site is copyrighted ©1997 - 2008, Computer Games Online SRL