Fanidan 1-laboratoriya ishi


-variant Ixtiyoriy n ta sonning o’rta geometrigini hisoblash



Yüklə 134,03 Kb.
səhifə2/2
tarix07.01.2024
ölçüsü134,03 Kb.
#203003
1   2
tarmoq dasturlash

4-variant
Ixtiyoriy n ta sonning o’rta geometrigini hisoblash
Server kodi (TCP):
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class TCPClient {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345);

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));


BufferedReader serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

System.out.println("Raqamlarni kiriting (har bir satrda bittadan, tugatish uchun bo'sh qator):");


String numStr;
while (!(numStr = userInput.readLine()).isEmpty()) {
double num = Double.parseDouble(numStr);
writer.println(num);
}
// Send an empty line to indicate the end of input.
writer.println();

String result = serverInput.readLine();


System.out.println("Server javobi: " + result);

socket.close();


} catch (IOException e) {
e.printStackTrace();
}
}
}
Client kodi (TCP):
import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class TCPServer {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server 12345 portda kutyabdi...");

while (true) {


Socket clientSocket = serverSocket.accept();
System.out.println("Client ulandi.");

BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));


PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);

ArrayList numbers = new ArrayList<>();


String numStr;


while ((numStr = reader.readLine()) != null && !numStr.isEmpty()) {
double num = Double.parseDouble(numStr);
numbers.add(num);
}

if (numbers.isEmpty()) {


writer.println("Hech qanday raqamlar berilmagan.");
} else {
double product = 1.0;
for (double num : numbers) {
product *= num;
}

double geometricMean = Math.pow(product, 1.0 / numbers.size());


writer.println("O'rtacha geometrik: " + geometricMean);
}

clientSocket.close();


System.out.println("Client uzildi.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}






Yüklə 134,03 Kb.

Dostları ilə paylaş:
1   2




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin