Sa se rezolve ecuatia de gradul I de forma ax+b=0, cu coeficienti numere reale.
Analiza Programului
In vederea realizari unei rezolvari riguroase, inainte de a afla solutia x=-b/a, trebuiesc facute niste teste asupra valorilor a si b. astfel daca a=0 si b=0 avem de a face cu o ecuatie nedeterminata, iar daca a=0 si b diferit de 0 avem de a face cu o ecuatie imposibila. Acestea sunt cazuri particulare care trebuiesc tratate.
In cazul in care a diferit de 0 se poate extrage solutia dupa formula cunoscuta x=-b/a, iar aceasta solutie este tiparita si pe ecran cu ajutorul functiei Console.WriteLine().
Pentru a avea instrumentele necesare calculului, in prima faza se vor citi de la tastature valorile reale a si b, dupa care se trece la gasirea solutiei. Se observa ca pentru rationamentul enuntat mai sus, au fost folosite in program doua instructiuni if-else cu ajutorul carora se testeaza valorile lui a si b.
Rezolvarea problemei in C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _2 { class Program { static void Main(string[] args) { float a, b, x; Console.Write("Introduceti a = "); a = float.Parse(Console.ReadLine()); Console.Write("Introduceti b = "); b = float.Parse(Console.ReadLine()); if (a == 0) if (b == 0) Console.WriteLine("Ecuatie nedet. "); else Console.WriteLine("Ecuatie imposibila"); else { x = -b / a; Console.WriteLine("Solutia este x = {0}", x); } Console.ReadKey(); } } }
Rezolvarea problemei in C++
//the header conio, which include library functions for performing "console input and output" #include <conio.h> //the header iostream, which supports the C++ I/O system #include <iostream> //tells the compiler to use the std namespace using namespace std; //begin program execution int main() { //declare variables float a, b, x; //display message on the screen cout<<"Introduceti a:"<<endl; //read from keyboard cin>>a; cout<<"Introduceti b:"<<endl; cin>>b; if(a == 0) if(b == 0) cout<<"Ecutie nedeterminata"<<endl; else cout<<"Ecuatie imposibila"<<endl; else { x = -b/a; cout<<"Solutia este "<<x; } //terminates main and return value to the calling process return 0; }
Dragosh says:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Ecuatia de gradul I
double a, b;
double x;
Console.Write(“Introduceti a = “);
a = int.Parse(Console.ReadLine());
Console.Write(“Introduceti b = “);
b = int.Parse(Console.ReadLine());
if (a == 0 && b == 0)
{
Console.WriteLine(“\nEcuatie nedeterminata”);
}
else if (a == 0)
{
Console.WriteLine(“\nEcuatie imposibilia”);
}
else
{
x = (-b) / a;
Console.WriteLine(“Solutia este x = {0:#.##}”, x);
}
Console.ReadKey();
}
}
}
Gheorghe says:
Salut….am realizat un program insa am nevoie de putin ajutor. Acest program are la baza Cifrul lui Cezar. Criptarea si decriptarea merge insa pot introduce doar cite o litera, nu-mi permite sa introduc spre exemplu o fraza. Va rog sa ma ajutati.
Cod sursa:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string encrypt = textBox1.Text;
encrypt.ToLower();
bool tbNull = textBox1.Text == “”;
if (tbNull)
MessageBox.Show(“There is nothing to encrypt.”);
else
{
char[] array = encrypt.ToCharArray();
for (int i = 0; i = ‘a’ && num ‘z’)
num = num – 26;
}
}
else if (num >= ‘A’ && num ‘Z’)
{
num = num – 26;
}
}
array[i] = (char)num;
}
label1.Text = “Encrypted Message”;
textBox1.Text = new string(array).ToLower();
}
textBox1.Copy();
}
private void button2_Click(object sender, EventArgs e)
{
string decrypt = textBox1.Text;
decrypt.ToLower();
bool tbNull = textBox1.Text == “”;
if (tbNull)
MessageBox.Show(“There is nothing to decrypt.”);
else
{
char[] array = decrypt.ToCharArray();
for (int i = 0; i = ‘a’ && num ‘z’)
num = num – 26;
if (num = ‘A’ && num ‘Z’)
num = num – 26;
if (num < 'A')
num = num + 26;
}
array[i] = (char)num;
}
label1.Text = "Decrypted Message";
textBox1.Text = new string(array).ToUpper();
}
textBox1.Copy();
}
private void textBox1_TextChanged(object sender, MouseEventArgs e)
{
this.Text = textBox1.Text;
}
private void textBox2_TextChanged(object sender, MouseEventArgs e)
{
textBox2.SelectAll();
}
}
}