Open ITHelpself opened 3 years ago
bài 2
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string correctUsername = "nhom2";
string correctPasswword = "nhom2345";
string username, password;
int solannhap = 0;
do {
if (solannhap > 0)
{
Console.WriteLine("Nhap lai: ");
}
// nhap username va password
Console.WriteLine("nhap username");
username = Console.ReadLine();
Console.WriteLine("nhap passwword");
password = Console.ReadLine();
solannhap++;
} while (!(username == correctUsername && password == correctPasswword)&&(solannhap<3));
if (solannhap == 3)
{
Console.WriteLine("Error:nhap sai tai khoan");
}
else
{
Console.WriteLine("dang nhap thanh cong");
}
}
}
}
using System;
using System.Security.Cryptography;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
double a, b, c, x1, x2, x;
double delta;
// nhap vao a, b, c
do
{
Console.Write("a:");
a = double.Parse(Console.ReadLine());
}
while (a == 0);
Console.Write("b:");
b = double.Parse(Console.ReadLine());
Console.Write("c:");
c = double.Parse(Console.ReadLine());
// tinh delta
delta = Math.Pow(b, 2) - 4 * a * c;
// xét nghiem
if (delta ==0)
{
x = -b /( 2 * a);
Console.WriteLine("x1 = x2 ={0}",x );
}
else if (delta > 0)
{
x1 = (-b - Math.Sqrt(delta)) / (2 * a);
x2 = (-b + Math.Sqrt(delta)) / (2 * a);
Console.WriteLine("x1 = {0}, x2 = {1}", x1, x2);
}
else
{
Console.WriteLine("phuong trinh vo nghiem");
}
}
}
}
using System;
using System.ComponentModel.DataAnnotations;
namespace ConsoleApp1
{
class Program
{
static int giaithua(int n)
{
if (n == 0)
{
return 1;
}
else
return n * giaithua(n - 1);
}
static int tongLe(int n)
{
int tong = 0;
for(int i = 1; i <= n; i += 2)
{
tong += i;
}
return tong;
}
static void inBangNhan(int n)
{
for(int i = 1; i <= 10; i++)
{
Console.WriteLine("{0} * {1} = {2}", n, i, n * i);
}
}
static void Main(string[] args)
{
}
}
}
bài 4
using System;
namespace ConsoleApp1
{
class Program
{
static double tinhDienTichHinhTron(double r)
{
return Math.PI * Math.Pow(r, 2);
}
static double tinhDienTichHinhChuNhat(double dai,double rong)
{
return dai * rong;
}
static double tinhDienTichHinhTamGiac(double a , double b , double c)
{
double nuachuvi = (a + b + c) / 2;
double dientich = Math.Sqrt(nuachuvi * (nuachuvi - a) * (nuachuvi - b) * (nuachuvi - c));
return dientich;
}
static void menu ()
{
int luachon = 0;
double dientich = 0;
Console.WriteLine("---------------------Menu-------------");
Console.WriteLine("-----1:tinh dien tich hinh tron-------");
Console.WriteLine("-----2:tinh dien tich hinh chu nhat-------");
Console.WriteLine("-----3:tinh dien tich hinh tam giac-------");
Console.Write("nhap lua chon:");
luachon = int.Parse(Console.ReadLine());
switch (luachon)
{
case 1:
double r = 0;
//TODO: dien tich hinh tron
Console.WriteLine("nhap ban kinh r:");
r = double.Parse(Console.ReadLine());
dientich = tinhDienTichHinhTron(r);
break;
case 2:
double x = 0, y = 0;
// TODO:dien tich hinh chu nhat
Console.WriteLine("nhap chieu dai x:");
x = double.Parse(Console.ReadLine());
Console.WriteLine("nhap chieu rong y:");
y = double.Parse(Console.ReadLine());
dientich = tinhDienTichHinhChuNhat(x, y);
break;
case 3:
double a = 0, b = 0, c = 0;
Console.WriteLine("nhap chieu dai canh a:");
a = double.Parse(Console.ReadLine());
Console.WriteLine("nhap chieu dai canh b:");
b = double.Parse(Console.ReadLine());
Console.WriteLine("nhap chieu dai canh c:");
c = double.Parse(Console.ReadLine());
//TODO dien tich hinh tam giac
dientich = tinhDienTichHinhTamGiac(a, b, c);
break;
default:
Console.WriteLine("vui long chon phep tinh 1 hoac 2 hoac 3");
break;
}
Console.WriteLine("dientich: " + dientich);
}
static void Main(string[] args)
{
menu();
}
}
}
using System;
using System.Security.Cryptography;
namespace ConsoleApp1
{
class Program
{
static double tinhTong(double a, double b)
{
return a + b;
}
static double tinhHieu(double a, double b)
{
return a - b;
}
static double tinhTich(double a, double b)
{
return a * b;
}
static double tinhThuong(double a, double b)
{
return a / b;
}
static void menu()
{
int luachon;
double a, b;
Console.Write("a:");
a = double.Parse(Console.ReadLine());
Console.Write("b:");
b = double.Parse(Console.ReadLine());
Console.WriteLine("------------menu-----------");
Console.WriteLine("1 tong");
Console.WriteLine("2 hieu");
Console.WriteLine("3 tich");
Console.WriteLine("4 thuong");
// nhap lua chon
luachon = int.Parse(Console.ReadLine());
switch (luachon)
{
case 1:
//TODO: tong
double tong = tinhTong(a, b);
Console.WriteLine("tong = {0}",tong);
break;
case 2:
//TODO: hieu
double hieu = tinhHieu(a, b);
Console.WriteLine("hieu = {0}",hieu);
break;
case 3:
//TODO: tich
double tich = tinhTich(a, b);
Console.WriteLine("tich = {0}",tich);
break;
case 4:
//TODO: thuong
double thuong = tinhThuong(a, b);
Console.WriteLine("thuong = {0}",thuong);
break;
}
}
static void Main(string[] args)
{
menu();
}
}
}
slide trang 56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lop6
{
class Box
{
public double chieu_dai;
public double chieu_rong;
public double chieu_cao;
static void Main(string[] args)
{
Console.WriteLine("Class trong c#");
Console.WriteLine("---------------------------------\n");
Box box1 = new Box();
Box box2 = new Box();
double thetich = 0.0;
box1.chieu_cao = 5.0;
box1.chieu_dai =6.0;
box1.chieu_rong = 7.0;
box2.chieu_cao = 5.0;
box2.chieu_dai = 6.0;
box2.chieu_rong = 7.0;
thetich = box1.chieu_cao * box1.chieu_dai * box1.chieu_rong;
Console.WriteLine("The tich cua box 1: {0}", thetich);
thetich = box2.chieu_cao * box2.chieu_dai * box2.chieu_rong;
Console.WriteLine("The tich cua box 2: {0}", thetich);
Console.ReadKey();
}
}
}
slide 59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lop6
{
class Box
{
public double chieu_dai;
public double chieu_rong;
public double chieu_cao;
public void setChieudai(double len)
{
chieu_dai = len;
}
public void setChieurong(double bre)
{
chieu_rong = bre;
}
public void setChieucao(double hei)
{
chieu_cao= hei;
}
public double tinhtheTich()
{
return chieu_dai * chieu_rong * chieu_cao;
}
public static void main(string[] args)
{
Box box1 = new Box();
Box box2 = new Box();
double the_tich;
box1.setChieurong(6.0);
box1.setChieudai(7.0);
box1.setChieucao(5.0);
box2.setChieurong(12.0);
box2.setChieudai(13.0);
box2.setChieucao(10.0);
the_tich = box1.tinhtheTich();
Console.WriteLine("the tich box1: {0}", the_tich);
the_tich = box2.tinhtheTich();
Console.WriteLine("the tich box2: {0}", the_tich);
Console.ReadKey();
}
}
}