POJ总结(3299)

POJ总结(3299)

POJ3299 Humidex

题目思路:

1.题目中涉及到三个变量,要求给出其中两个,计算出第三个,然后将三个内容都输出。其中涉及到的主要问题为:

判断需要计算哪个参数

根据上述判断执行计算过程

2.首先解决第一个问题,我是通过将三个变量构造在一个结构体中,并且设置flag标志。如果输入该值,则将对应变量flag设为true,否则设为false。那么通过判断三个变量的flag标志就可以判断出需要执行什么运算了。

3.其次解决计算问题。计算过程的公式都已经给出了,剩下的就是对这个公式进行变换,然后用代码表示出来就好了。这里使用到了标准库中的乘方函数以及对数函数(log),直接代公式就可以了。

问题与解惑:

在编写代码的过程中遇到了一些细节问题,这里总结一下。

1.Q:结构体指针定义的过程?

typedef struct input_struct{
    double numT;
    bool flagT;
    double numH;
    bool flagH;
    double numD;
    bool flagD;
}input,*input_p;

A:已经定义了结构体类型的指针*input_p;那么在下边定义结构体指针的时候就可以直接把这个input_p作为一个指针类型了

input_p test_p=(input_p)malloc(sizeof(input));

而不是input_p * test_p;这就变成了两层指针了

2.Q:scanf可以这么用?

scanf("%c",&fir);
        if(fir=='E')
        {
            break;
        }
        scanf("%lf %c %lf",&fir_num,&sec,&sec_num);

A:scanf是以空格分割的

3.Q:这里需要一个getchar()?

scanf("%lf %c %lf",&fir_num,&sec,&sec_num);
getchar();

A:这里有一个while(1)循环,如果不用这个getchar()接收那个回车字符的话,那么下一个scanf接收到的就是”\n”,于是会出现输入与错位的现象

4.Q:double类型如何输入?

A:%lf;long float;

5.Q:输入两位小数?

A:%.2f 或者%.2lf

6.Q:结构体定义的时候不能赋初始值吗????

代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define const1 5417.7530
#define conste 2.718281828
typedef struct input_struct{
    double numT;
    bool flagT;
    double numH;
    bool flagH;
    double numD;
    bool flagD;
}input,*input_p;
void copy(char a,double a_num,input_p p);
void compute_H(input_p input); 
void compute_T(input_p input);
void compute_D(input_p input);
void init(input_p p);

int main(){
    input_p test_p=(input_p)malloc(sizeof(input));
    while(1){
        init(test_p);
        char fir,sec;
        double fir_num,sec_num;
        scanf("%c",&fir);
        if(fir=='E')
        {
            break;
        }
        scanf("%lf %c %lf",&fir_num,&sec,&sec_num);
        getchar();

        copy(fir,fir_num,test_p);
        copy(sec,sec_num,test_p);

        if(test_p->flagH==true&&test_p->flagT==true)
        {   
            compute_D(test_p);
        }
        else if(test_p->flagD==true&&test_p->flagT==true)
        {
            compute_H(test_p);
        }
        else if(test_p->flagH==true&&test_p->flagD==true)
        {
            compute_T(test_p);
        }
        else
        {
            return -1;
        }
    }
    return 0;
}

void init(input_p p)
{
    p->numT=0;
    p->flagT=false;
    p->numD=0;
    p->flagD=false;
    p->numH=0;
    p->flagH=false;
}

void compute_H(input_p input)
{
    double e=(6.11)*(pow(conste,const1*((1.0/273.16)-(1.0/(input->numD+273.16)))));
    double h=0.5555*(e-10.0);
    input->numH=(input->numT)+h;
    printf("%c %.1lf %c %.1lf %c %.1lf\n",'T',input->numT,'D',input->numD,'H',input->numH);
}
void compute_T(input_p input)
{
    double e=(6.11)*(pow(conste,const1*((1.0/273.16)-(1.0/(input->numD+273.16)))));
    double h=0.5555*(e-10.0);
    input->numT=input->numH-h;
    printf("%c %.1lf %c %.1lf %c %.1lf\n",'T',input->numT,'D',input->numD,'H',input->numH);
}
void compute_D(input_p input)
{
    double mid1=((input->numH-input->numT)/0.5555+10.0)/6.11;
    double mid2=log(mid1)/const1;
    double mid3=(1.0)/((1.0)/273.16-mid2);
    input->numD=mid3-273.16;
    printf("%c %.1lf %c %.1lf %c %.1lf\n",'T',input->numT,'D',input->numD,'H',input->numH);
}

void copy(char a,double a_num,input_p p)
{
    if(a=='T')
    {
        p->numT=a_num;
        p->flagT=true;
    }
    if(a=='H')
    {
        p->numH=a_num;
        p->flagH=true;
    }
    if(a=='D')
    {
        p->numD=a_num;
        p->flagD=true;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值