Готовая 1 лаб. работа.

This commit is contained in:
Anastasia 2023-04-14 19:33:09 +04:00
parent 8090e4fbfa
commit 3549c765e3
2 changed files with 101 additions and 3 deletions

View File

@ -2,12 +2,98 @@ package com.lab.springbapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import static java.lang.Math.*;
@SpringBootApplication
@RestController
public class SpringbappApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbappApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(SpringbappApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/calculateSquare")
public String calculateSquare(@RequestParam(defaultValue = "1") int num,
@RequestParam(defaultValue = "1") int step) {
double res;
res = Math.pow(num, step);
return Double.toString(res);
}
@GetMapping("/calculateFactorial")
public String calculateFactorial(@RequestParam(required = true) int num) {
long res = 1;
for (int i = 1; i <= num; i++) {
res = res * i;
}
return Long.toString(res);
}
public int getFibonacciValue(int n) {
if (n <= 1) {
return 0;
} else if (n == 2) {
return 1;
} else {
return getFibonacciValue(n - 1) + getFibonacciValue(n - 2);
}
}
@GetMapping("/calculateFibbN")
public String calculateFibbN(@RequestParam(required = true) int num) {
int result = 0;
result = getFibonacciValue(num);
return Integer.toString(result);
}
public static boolean checkSimple(int i) {
if (i <= 1)
return false;
else if (i <= 3)
return true;
else if (i % 2 == 0 || i % 3 == 0)
return false;
int n = 5;
while (n * n <= i) {
if (i % n == 0 || i % (n + 2) == 0)
return false;
n = n + 6;
}
return true;
}
@GetMapping("/calculateLastSimple")
public String calculateSimple(@RequestParam(required = true) int num) {
int result = 0;
for (int i = 2; i <= num; i++) {
if (checkSimple(i))
return ("число простое");
}
return ("число непростое");
}
@GetMapping("/addDays")
public String addDays(@RequestParam(required = true) int num) {
Date currentDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(currentDate);
cal.add(Calendar.DATE, num);
Date newDate = cal.getTime();
return newDate.toString();
}
}

View File

@ -0,0 +1,12 @@
package com.lab.springbapp;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}