Jump to content
Fórum Script Brasil
  • 0

Como reconhecer e calcular o seno em uma string?


tahinf

Question

Olá pessoal!

Tenho um trabalho para fazer, a prof quer que calcule o sen também, só que if para verificar se o usuario digitou sen num tá dando certo, podem me ajudar?

por favor

<style type="text/css">
    div .saida
    {
        padding-left: 7cm;
        margin-top: -6cm;
    }
    
    h1
    {
        color: #f66000
    }
    
    span
    {
        font-family: Arial;
        font-size: 12px;
    }
    
    p
    {
        font-family: Helvetica;
    }
</style>

&lt;script>
    var T_u = new Array();
    var T_v = new Array();
    var T_u_T_v = new Array();
    var T_uv = new Array();
    var T_u_e = new Array();
    var T_v_e = new Array();

    function clear()
    {
        T_u = new Array();
        T_v = new Array();
        T_u_T_v = new Array();
        T_uv = new Array();
        T_u_e = new Array();
        T_v_e = new Array();
    }

    function verifica()
    {
        clear();

        var count = 0;
        var count2 = 0;
        var e = new Array();

        for(i=0; i<3; i++)
        {
            e[i] = document.getElementById('e'+i).value != '' ?  document.getElementById('e'+i).value : '0';
            
            if(e[i]!='0')
                count2++;
        }

        var dominio = "T: R";

        for(i=0; i<3; i++)
        {
            if(document.getElementById('v1'+i).value != '' && document.getElementById('v2'+i).value != '')
                count++;
        }

        if(count == 2)
            dominio += "²";

        if(count == 3)
            dominio += "³";
            
        dominio += "-> R";

        if(count2 == 2)
            dominio += "²";

        if(count2 == 3)
            dominio += "³";

        if(!TZero(e))
        {
            dominio +="<br /><br /><span class=\"error\">T(0) &eacute; diferente de zero</span>";
        }
        else
        {
            dominio +="<br /><br /><span>T(0) &eacute;  igual a  zero</span>";
        }

        if(!Testa(e, 'soma'))
        {
            dominio +="<br /><br /><span class=\"error\">T(u+v) &eacute; diferente de T(u) + T(v)</span>";
        }
        else
        {
            dominio +="<br /><br /><span>T(u+v) &eacute;  igual a   T(u) + T(v)</span>";
        }

        clear();

        if(!Testa(e, 'alfa'))
        {
            dominio += "<br /><br /><span class=\"error\">T(u*R) &eacute; diferente de T(u)*R</span>";
        }
        else
        {
            dominio +="<br /><br /><span>T(R*u) &eacute;  igual a   T(u)*R</span>";
        }

        document.getElementById('saida').innerHTML = dominio;

        document.getElementById('saida').style.display = 'block';

    }

    

    function TZero(Teq)
    {
        for(i=0;i<3;i++)
        {
            var regExp = /x|y|z/gi;
            var T = Teq[i].replace(regExp, "0");

        

            if(eval(T) != 0)
                return false;

        }
        
        return true;

    }

    

    function substitui(T, vetor)
    { 
      
        regExp = /x/gi;
        if(T == "sen")
        {    document.write("entrou if ");
            T = T.replace("sen", " ");
            T = T.replace(regExp, vetor[0]);
            T = Math.sin(T);
return T;
        }
        else
{



        T = T.replace(regExp, vetor[0]);
        
        regExp = /y/gi;

        T = T.replace(regExp, vetor[1]);

        regExp = /z/gi;

        T = T.replace(regExp, vetor[2]);

        return T;
}
    }

    

    function Testa(Teq, tipo)
    {    
        var flag = true;

        for(j=0;j<3;j++)
        {
            var u = new Array();

            for(i=0; i<3; i++)
                u[i] = document.getElementById('v1'+i).value != '' ?  document.getElementById('v1'+i).value : '0';

            var v = new Array();

            for(i=0; i<3; i++)
                v[i] = document.getElementById('v2'+i).value != '' ?  document.getElementById('v2'+i).value : '0';

            

            var s = new Array();

            for(i=0; i<3; i++)
                s[i] = parseFloat(v[i])+parseFloat(u[i]);

            

            var Tu = substitui(Teq[j], u);
            var Tv = substitui(Teq[j], v);


            if(tipo == 'soma')
            {
                Tu = eval(Tu);

                T_u.push(Tu);

                Tv = eval(Tv);

                T_v.push(Tv);

                T_u_T_v.push(Tv+Tu);

                var Ts = substitui(Teq[j], s);

                Ts = eval(Ts);
                T_uv.push(Ts);

                if(!((Tu + Tv) == Ts))

                    flag = false;

            } 
            
            else 
            {
                Tu = eval(Tu)*parseFloat(document.getElementById('alfa').value);

                T_u.push(Tu);

                Tv = eval(Tv)*parseFloat(document.getElementById('alfa').value);

                T_v.push(Tv);

                var u_alfa = new Array();

                for(i=0; i<3; i++)
                    u_alfa[i] = parseFloat(u[i])*parseFloat(document.getElementById('alfa').value);

                var Tue = eval(substitui(Teq[j], u_alfa));

                T_u_e.push(Tue);


                if(Tue != Tu)
                    flag = false;

                var v_alfa = new Array();

                for(i=0; i<3; i++)
                    v_alfa[i] = parseFloat(v[i])*parseFloat(document.getElementById('alfa').value);


                var Tve = eval(substitui(Teq[j], v_alfa));

                T_v_e.push(Tve);

                if(Tve != Tv)
                    flag = false;

            }
        }

        return flag;

    }

</script>



<html>

<head>
    <title>Verifica se a Trans</title>
    <h1>Verifica se a Trans</h1>
</head>

<body>
    <div class="main">
    
        <div class="info" id="info">
            
            
            <p>Ao inserir valores no T:(x, y, z), favor inserir desta forma (Assim como no exemplo já preenchido): 
            <br />Se 2x, insira 2*x.
            <br />Se x², insira x*x.
            <br />Utilize apenas números no valor do Alfa.<br /><br /></p>
            
        </div>
        
        
        <div class="entrada" id="entrada">
            <div class="entrada_data">
                
                <label>T:</label>

                (<input type="text" id="e0" name="e0" size="10" value="x*x"/>, 

                <input type="text" id="e1" name="e1" size="10" value="3*y"/>,

                <input type="text" id="e2" name="e2" size="10"/>)



                <br /><br /><br />

            

                <label>u:</label>
                (<input type="text" id="v10" name="v10" size="2" value="3"/>,
                <input type="text" id="v11" name="v11" size="2" value="2"/>,
                <input type="text" id="v12" name="v12" size="2"/>)

                <br /><br />

                <label>v:</label>
                (<input type="text" id="v20" name="v20" size="2" value="1" />,
                <input type="text" id="v21" name="v21" size="2" value="4"/>,
                <input type="text" id="v22" name="v22" size="2"/>)

                <br /><br /><br />

                <label>Alfa:</label><br />

                <input type="text" id="alfa" name="alfa" size="3" value="4"/>


                <br /><br /><br />

            </div>

        </div>
        
        <button class="botao" onclick="verifica();">Verificar</button>
<input type="text" id="v22" name="testr" size="2"/>
        
        <div class="saida" id="saida">

        </div>

        

    </div>

</body>

</html>

Edited by tahinf
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Só para esclarecer, o trabalho é sobre transformações lineares.

Tentamos fazer if na função substitui.

function substitui(T, vetor)
    { 
      
        regExp = /x/gi;
        if(T == "sen")
        {    document.write("entrou if ");
            T = T.replace("sen", " ");
            T = T.replace(regExp, vetor[0]);
            T = Math.sin(T);
return T;
        }

Mas não deu certo.

Obrigada.

Link to comment
Share on other sites

  • 0

Pro caso mais simples:

var textoAntes = "sen 30";
var textoDepois = "";
var ps = textoAntes.split(" ");

if (ps[0] == "sen") {
    textoDepois = Math.sin(ps[1]);
}

alert('Antes: '+textoAntes+'\n\nDepois: '+textoDepois);

Edited by fiote
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...