537 - Artificial Intelligence未通过

//未通过,重点掌握:istringstream的用法。
//整形和getline()或者(fgets())结合使用时要用getchar()来做区别

/*#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX 105
int main()
{
	struct {
		char Concept;
		char Prefix;
		double data;
	}s[2];
	int n;
	scanf("%d",&n);
	getchar();
	for(int cas=1;cas<=n;cas++)
	{
		char buf[MAX];
		char str[20];
		int slen=0;
		fgets(buf,sizeof(buf),stdin);
		int len=strlen(buf);
		int flag=0;
		int j=0;
		for(int i=0;i<len;i++)
		{
			if((buf[i]=='I'||buf[i]=='U'||buf[i]=='P') && buf[i+1]=='=')
			{
				s[j].Concept=buf[i];
				flag=1;
				i++;
				continue;
			}
			if((!isdigit(buf[i])&&buf[i]!='.') && flag==1)
			{
				flag=0;
				str[slen]='\0';
				s[j].data=atof(str);
				s[j].Prefix=buf[i];
				slen=0;
				j++;
				continue;
			}
			if(flag==1)
			{
				str[slen++]=buf[i];
			}
		}
		for(int k=0;k<2;k++)
		{
			switch(s[k].Prefix)
			{
			case 'm':
				s[k].data*=1e-3;
				break;
			case 'k':
				s[k].data*=1e3;
				break;
			case 'M':
				s[k].data*=1e6;
				break;
			default:
				break;
			}
		}
		printf("Problem #%d\n",cas);
		if((s[0].Concept=='U' && s[1].Concept=='I') || (s[0].Concept=='I' && s[1].Concept=='U')) 
		{
			printf("P=%.2lfW\n",s[0].data*s[1].data);
		}
		else if(s[0].Concept=='P' && s[1].Concept=='U')
		{
			printf("I=%.2lfA\n",s[0].data/s[1].data);
		}
		else if(s[0].Concept=='P' && s[1].Concept=='I')
		{
			printf("U=%.2lfV\n",s[0].data/s[1].data);
		}
		else if(s[1].Concept=='P' && s[0].Concept=='U')
		{
			printf("I=%.2lfA\n",s[1].data/s[0].data);
		}
		else if(s[1].Concept=='P' && s[0].Concept=='I')
		{
			printf("U=%.2lfV\n",s[1].data/s[0].data);
		}
			printf("\n");
	}
	return 0;
}*/

//c++ 心如止水

#include<iostream>
#include<sstream>
#include<string>
#include<cstdio>

using namespace std;

int main()
{
	int n;
	cin>>n;
	getchar();
	for(int cas=1;cas<=n;cas++)
	{
		string line;
		getline(cin,line);
		istringstream sin(line);
		double P(-1.0),U(-1.0),I(-1.0);
		char ch;
		while(sin>>ch)
		{
			char t;
			if(ch=='P')
			{
				sin>>t;
				if(t!='=')
					continue;
				sin>>P;
				sin>>t;
				if(t=='m')
					P*=1e-3;
				else if(t=='k')
					P*=1e3;
				else if(t=='M')
					P*=1e6;
			}
			else if(ch=='U')
			{
				sin>>t;
				if(t!='=')
					continue;
				sin>>U;
				sin>>t;
				if(t=='m')
					U*=1e-3;
				else if(t=='k')
					U*=1e3;
				else if(t=='M')
					U*=1e6;
			}
			else if(ch=='I')
			{
				sin>>t;
				if(t!='=')
					continue;
				sin>>I;
				sin>>t;
				if(t=='m')
					I*=1e-3;
				else if(t=='k')
					I*=1e3;
				else if(t=='M')
					I*=1e6;
			}
		}
		printf("Problem #%d\n",cas);
		if(P==-1.0)
			printf("P=%.2lfW\n",U*I);
		else if(U==-1.0)
			printf("U=%.2lfV\n",P/I);
		else if(I==-1.0)
			printf("I=%.2lfA\n",P/U);
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(struct,String)