537 - Artificial Intelligence?
Moderator: Board moderators
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
-
- New poster
- Posts: 31
- Joined: Sat Nov 17, 2001 2:00 am
- Contact:
537
It alway gets WA. But I don't know where the bugs are.
[c]
#include<stdio.h>
#include<string.h>
#define YES 1
#define NO 0
void main(void)
{
int amp,volt,power,ampcnt,voltcnt,powercnt,n,x,y,point;
double temp,v,p,a,b;
char buffer[10000];
scanf("%d",&n);
for(x=0;x<n;x++)
{
fflush(stdin);
fgets(buffer,10000,stdin);
buffer[strlen(buffer)-1]='\0';
amp=volt=power=ampcnt=voltcnt=powercnt=point=NO;
for(y=0,temp=0;buffer[y]!='\0';y++)
{
if(buffer[y]=='I' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
ampcnt=amp=YES;
if(buffer[y]=='U' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
voltcnt=volt=YES;
if(buffer[y]=='P' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
powercnt=power=YES;
if(ampcnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='A')
{
a=temp;
temp=0;
ampcnt=NO;
point=NO;
}
if(voltcnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='V')
{
v=temp;
temp=0;
voltcnt=NO;
point=NO;
}
if(powercnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='W')
{
p=temp;
temp=0;
powercnt=NO;
point=NO;
}
}
printf("Problem #%d\n",x+1);
if(amp && volt)
printf("P=%.2lfW\n\n",a*v);
if(amp && power)
printf("U=%.2lfV\n\n",p/a);
if(volt && power)
printf("I=%.2lfA\n\n",p/v);
}
}
[/c]
[c]
#include<stdio.h>
#include<string.h>
#define YES 1
#define NO 0
void main(void)
{
int amp,volt,power,ampcnt,voltcnt,powercnt,n,x,y,point;
double temp,v,p,a,b;
char buffer[10000];
scanf("%d",&n);
for(x=0;x<n;x++)
{
fflush(stdin);
fgets(buffer,10000,stdin);
buffer[strlen(buffer)-1]='\0';
amp=volt=power=ampcnt=voltcnt=powercnt=point=NO;
for(y=0,temp=0;buffer[y]!='\0';y++)
{
if(buffer[y]=='I' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
ampcnt=amp=YES;
if(buffer[y]=='U' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
voltcnt=volt=YES;
if(buffer[y]=='P' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
powercnt=power=YES;
if(ampcnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='A')
{
a=temp;
temp=0;
ampcnt=NO;
point=NO;
}
if(voltcnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='V')
{
v=temp;
temp=0;
voltcnt=NO;
point=NO;
}
if(powercnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='W')
{
p=temp;
temp=0;
powercnt=NO;
point=NO;
}
}
printf("Problem #%d\n",x+1);
if(amp && volt)
printf("P=%.2lfW\n\n",a*v);
if(amp && power)
printf("U=%.2lfV\n\n",p/a);
if(volt && power)
printf("I=%.2lfA\n\n",p/v);
}
}
[/c]
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
537 - Artificial Intelligence
why do i always get the compile error report when i send my source code..?
Here are the compiler error messages:
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function
`_start':
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18):
undefined reference to `main'
collect2: ld returned 1 exit status
does any one can help me plz? thx
Here are the compiler error messages:
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function
`_start':
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18):
undefined reference to `main'
collect2: ld returned 1 exit status
does any one can help me plz? thx
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
emm..
here are the source code..
plz help me thxxxx..
@begin_of_source_code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* @JUDGE_ID: 27057TE 537 C */
#define INPUTFILE "AI.IN"
#define OUTPUTFILE "AI.OUT"
FILE *finput;
FILE *foutput;
void main() {
char input[100];
char num[100];
char ch[2];
char f=0;
char finished=0;
int i,j;
double P,I,U;
char vp,vi,vu;
char *pv;
double *p;
int problem=0;
printf("Artificial Intelligence\n");
printf("=======================\n\n\n");
printf("Open input file %s...\n",INPUTFILE);
finput = fopen(INPUTFILE,"rt");
if (finput == NULL) {
printf("Open File Fail.\n");
exit(EXIT_FAILURE);
}
printf("Open File Success.\n\n");
printf("Open output file %s...\n",INPUTFILE);
foutput = fopen(OUTPUTFILE,"wt");
if (foutput == NULL) {
printf("Open File Fail.\n");
exit(EXIT_FAILURE);
}
printf("Open File Success.\n\n");
printf("Processing input file...\n");
input[0]='\0';
i=0;
f=0;
finished=0;
vi=vp=vi=0;
while (finished==0) {
if (fgets(ch,2,finput)==NULL) {
finished = 1;
}
if (ch[0] == ' ' || ch[0] == ',' || ch[0] == '\n' || (finished)) {
input = '\0';
if (f==1 && strlen(input) > 0) {
printf("%s\n",input);
if (input[0]=='P') {
pv = &vp;
p = &P;
}else if (input[0] == 'U') {
pv = &vu;
p = &U;
}else {
pv = &vi;
p = &I;
}
*pv = 1;
for (i=0,j=0; i<strlen(input); i++) {
if (input >= '0' && input <= '9') {
num[j] = input;
j++;
}
}
num[j]='\0';
if (strlen(num)>0) { *p = strtod(num,NULL);
printf("P = %f, U = %f, I = %f\n",P,U,I);
}
}
if (ch[0] == '\n' || (finished)) {
problem++;
fprintf(foutput,"Problem #%d\n",problem);
if (vp == 0) {
P = U * I;
fprintf(foutput,"P = %lfW\n\n",P);
}else if (vu == 0) {
U = P / I;
fprintf(foutput,"U = %lfA\n\n",U);
}else {
I = P / U;
fprintf(foutput,"I = %lfV\n\n",I);
}
P=U=I=0;
vp=vu=vi=0;
}
i=-1;
f=0;
} else {
if (ch[0]=='=') {f=1;}
input=ch[0];
}
i++;
}
printf("Processing completed\n");
fclose(finput);
fclose(foutput);
}
@end_of_source_code
![:-?](./images/smilies/icon_confused.gif)
![:D](./images/smilies/icon_biggrin.gif)
@begin_of_source_code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* @JUDGE_ID: 27057TE 537 C */
#define INPUTFILE "AI.IN"
#define OUTPUTFILE "AI.OUT"
FILE *finput;
FILE *foutput;
void main() {
char input[100];
char num[100];
char ch[2];
char f=0;
char finished=0;
int i,j;
double P,I,U;
char vp,vi,vu;
char *pv;
double *p;
int problem=0;
printf("Artificial Intelligence\n");
printf("=======================\n\n\n");
printf("Open input file %s...\n",INPUTFILE);
finput = fopen(INPUTFILE,"rt");
if (finput == NULL) {
printf("Open File Fail.\n");
exit(EXIT_FAILURE);
}
printf("Open File Success.\n\n");
printf("Open output file %s...\n",INPUTFILE);
foutput = fopen(OUTPUTFILE,"wt");
if (foutput == NULL) {
printf("Open File Fail.\n");
exit(EXIT_FAILURE);
}
printf("Open File Success.\n\n");
printf("Processing input file...\n");
input[0]='\0';
i=0;
f=0;
finished=0;
vi=vp=vi=0;
while (finished==0) {
if (fgets(ch,2,finput)==NULL) {
finished = 1;
}
if (ch[0] == ' ' || ch[0] == ',' || ch[0] == '\n' || (finished)) {
input = '\0';
if (f==1 && strlen(input) > 0) {
printf("%s\n",input);
if (input[0]=='P') {
pv = &vp;
p = &P;
}else if (input[0] == 'U') {
pv = &vu;
p = &U;
}else {
pv = &vi;
p = &I;
}
*pv = 1;
for (i=0,j=0; i<strlen(input); i++) {
if (input >= '0' && input <= '9') {
num[j] = input;
j++;
}
}
num[j]='\0';
if (strlen(num)>0) { *p = strtod(num,NULL);
printf("P = %f, U = %f, I = %f\n",P,U,I);
}
}
if (ch[0] == '\n' || (finished)) {
problem++;
fprintf(foutput,"Problem #%d\n",problem);
if (vp == 0) {
P = U * I;
fprintf(foutput,"P = %lfW\n\n",P);
}else if (vu == 0) {
U = P / I;
fprintf(foutput,"U = %lfA\n\n",U);
}else {
I = P / U;
fprintf(foutput,"I = %lfV\n\n",I);
}
P=U=I=0;
vp=vu=vi=0;
}
i=-1;
f=0;
} else {
if (ch[0]=='=') {f=1;}
input=ch[0];
}
i++;
}
printf("Processing completed\n");
fclose(finput);
fclose(foutput);
}
@end_of_source_code
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
I think, that your problem is using fopen, fclose and some other input/output functions ....
This function are not allowed on OJ![:(](./images/smilies/icon_frown.gif)
Try to avoid it - read from standard input and write to standard output
)
if you want to test your progra under Dos/Windows/Unix run it in this way:
program_name <data.in >data.out
data.in, data.out - your name of files which you want to use to test solution![:)](./images/smilies/icon_smile.gif)
program_name - executable file name (example: 537.exe, a.out )
Try this
Dominik
This function are not allowed on OJ
![:(](./images/smilies/icon_frown.gif)
Try to avoid it - read from standard input and write to standard output
![:)](./images/smilies/icon_smile.gif)
if you want to test your progra under Dos/Windows/Unix run it in this way:
program_name <data.in >data.out
data.in, data.out - your name of files which you want to use to test solution
![:)](./images/smilies/icon_smile.gif)
program_name - executable file name (example: 537.exe, a.out )
Try this
Dominik
-
- New poster
- Posts: 16
- Joined: Tue Dec 03, 2002 9:44 pm
-
- New poster
- Posts: 16
- Joined: Tue Dec 03, 2002 9:44 pm
AC
I got AC now :
* input may be like: I=-1e-2A U=2e3V
* donot print minus zero ie, -0.00 : print 0.00
![:o](./images/smilies/icon_eek.gif)
* input may be like: I=-1e-2A U=2e3V
* donot print minus zero ie, -0.00 : print 0.00
![:o](./images/smilies/icon_eek.gif)
537 A.I pls help , I'm getting WA
someone pls give me input test case for this problem
one more question ==> is P,U,and I always be in uppercase
or it can be written in lowercase too ????
one more question ==> is P,U,and I always be in uppercase
or it can be written in lowercase too ????