Can any body tell me what is the error in my code??
the code is C++:
Code: Select all
removed
Moderator: Board moderators
Code: Select all
removed
Code: Select all
3
U=120V and I=5.36A. What is the reason for calculating p? I cant understand ?
There was a p who has P=560W Which was destroyed by U=291.89V.
No chance if P=111.11W and I=3.004A.
Code: Select all
Problem #1
P=643.20W
Problem #2
I=1.92A
Problem #3
U=36.99V
Code: Select all
3
U=200VI=4.5A.
U=220VP=100.00W.
P=2.5MWI=2A.
Code: Select all
Problem #1
P=900.00W
Problem #2
I=0.45A
Problem #3
U=1250000.00V
Code: Select all
#include <iostream>
#include <vector>
#include <string>
#include <ctype.h>
#include <iomanip>
#include <stdio.h>
using namespace std;
int numCases = 0;
float P, U, I;
char s[10000];
bool pgiven = false, ugiven = false, igiven = false;
bool first = true;
int main() {
cin >> numCases;
vector<string> prob(numCases+1);
for(int i = 0; i < numCases; i++)
{
if(first)
{
getchar();
first = false;
}
gets(s);
prob[i] = s;
}
for(int i = 0; i < numCases; i++)
{
int ppos = prob[i].find("P=") + 2;
int upos = prob[i].find("U=") + 2;
int ipos = prob[i].find("I=") + 2;
if(ppos != 1) {
pgiven = true;
string p;
int num = 0;
while(isdigit(prob[i].operator[](ppos+num)) || prob[i].operator[](ppos+num) == '.' || prob[i].operator[](ppos+num) == '-')
{
p = p + prob[i].operator[](ppos+num);
num++;
}
char* pdata = (char*)p.data();
P=atof(pdata);
if(prob[i].operator[](ppos+num) == 'M')
{
P = P * 1000000;
} else if(prob[i].operator[](ppos+num) == 'm')
{
P = P / 1000;
} else if(prob[i].operator[](ppos+num) == 'k')
{
P = P * 1000;
}
}
if(upos != 1) {
ugiven = true;
string u;
int num = 0;
while(isdigit(prob[i].operator[](upos+num)) || prob[i].operator[](upos+num) == '.' || prob[i].operator[](upos+num) == '-')
{
u = u + prob[i].operator[](upos+num);
num++;
}
char* udata = (char*)u.data();
U=atof(udata);
if(prob[i].operator[](upos+num) == 'M')
{
U = U * 1000000;
} else if(prob[i].operator[](upos+num) == 'm')
{
U = U / 1000;
} else if(prob[i].operator[](upos+num) == 'k')
{
U = U * 1000;
}
}
if(ipos != 1) {
igiven = true;
string is;
int num = 0;
while(isdigit(prob[i].operator[](ipos+num)) || prob[i].operator[](ipos+num) == '.' || prob[i].operator[](ipos+num) == '-')
{
is = is + prob[i].operator[](ipos+num);
num++;
}
char* idata = (char*)is.data();
I=atof(idata);
if(prob[i].operator[](ipos+num) == 'M')
{
I = I * 1000000;
} else if(prob[i].operator[](ipos+num) == 'm')
{
I = I / 1000;
} else if(prob[i].operator[](ipos+num) == 'k')
{
I = I * 1000;
}
}
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
if(!igiven)
{
float ans = P / U;
cout << "Problem #" << i+1 << endl;
if(ans == -0)
cout << "I=" << 0.00 << "A" << endl << endl;
else
cout << "I=" << ans << "A" << endl << endl;
} else if(!ugiven)
{
float ans = P / I;
cout << "Problem #" << i+1 << endl;
if(ans == -0)
cout << "U=" << 0.00 << "V" << endl << endl;
else
cout << "U=" << ans << "V" << endl << endl;
} else if(!pgiven)
{
float ans = U * I;
cout << "Problem #" << i+1 << endl;
if(ans == -0)
cout << "P=" << 0.00 << "W" << endl << endl;
else
cout << "P=" << ans << "W" << endl << endl;
}
P = 0; U = 0; I = 0;
pgiven = false; ugiven = false; igiven = false;
ppos = 0; upos = 0; ipos = 0;
}
return 0;
}
Code: Select all
Code removed after AC.
Code: Select all
very silly mistake.. An easy problem..
Code: Select all
removed
Code: Select all
#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
int T, cas=0;
//freopen("in-537.txt","r",stdin);
//freopen("out-537.txt","w",stdout);
double P=0.0, U=0.0, I=0.0, tmp=0.0;
scanf("%d\n",&T);
while(T)
{
char s[10000], ch;
scanf("%s%c",s,&ch);
int ln=strlen(s), k=0, m=0, M=0, eq=0, PP=0, VV=0, II=0;
for(int i=0 ; i<ln ; i++)
{
if(s[i]=='=' && ( s[i-1]=='I' || s[i-1]=='P'|| s[i-1]=='U' ) && isdigit(s[i+1]) ) eq++;
//if(s[i]=='=') eq++;
}
float d=1.0;
if(eq>0)
{
long nm=0, dv=0, fag=0;
for(int i=0 ; i<ln ; i++)
{
if(s[i]=='m') m=1;
if(s[i]=='M') M=1;
if(s[i]=='k') k=1;
if(s[i]=='P') PP=1;
if(s[i]=='U') VV=1;
if(s[i]=='I') II=1;
if( s[i]>='0' && s[i]<='9' )
{
if(fag==1){dv=dv*10; d=(float)dv;}
nm=nm*10+(int)s[i]-48;
}
if( s[i]=='.' )
{
dv=1;
fag=1;
}
if(s[i]=='A' || s[i]=='W' || s[i]=='V')
{
tmp=(float)nm/d;
//printf("%lf m=%d M=%d k=%d\n",tmp,m,M,k);
if(PP==1){
P=tmp;
if(m==1) P=P/1000.0;
else if(M==1) P=P*1000000.0;
else if(k==1) P=P*1000.0;
}
else if(VV==1){
U=tmp;
if(m==1) U=U/1000.0;
else if(M==1) U=U*1000000.0;
else if(k==1) U=U*1000.0;
}
else if(II==1){
I=tmp;
if(m==1) I=I/1000.0;
else if(M==1) I=I*1000000.0;
else if(k==1) I=I*1000.0;
}
PP=0; VV=0; II=0; k=0; m=0; M=0;
nm=0; dv=0; fag=0; d=1.0;
}
}
}
if(ch=='\n'){
if(cas>0) printf("\n");
printf("Problem #%d\n",++cas);
if(P==0.0){
P=U*I;
printf("P=%.2lfW\n",P);
}
else if(U==0.0){
U=P/I;
printf("U=%.2lfV\n",U);
}
else if(I==0.0){
I=P/U;
printf("I=%.2lfA\n",I);
}
P=0.0; U=0.0; I=0.0; tmp=0.0;
T--;
}
}
return 0;
}