Quick actions

cmd+k|ctrl+k

Navigation

Languages

lab2.task2

Snippet info

Language

C

Visibility

public

Author

danylo-zaychenko-525i

Created

2023-03-12T12:05:21.971944Z

Updated

2023-03-15T11:30:42.444122Z

/**
* @file main.c
* @author Зайченко Д.І., гр. 515
* @date 14 березня 2023
* @brief Лабораторна робота No 2
*
* Модульне тестування. Завдання 2
*/

#include <stdio.h>
#include<stdlib.h>  //заголовний файл в якому міститься операнд очищення консолі
#include "header.h"

int main() {

	double number;
	double second_number;
	char operation;


reset:         //мітка для переходу після введення команди скидання

	//виведення тексту
	printf("List of aviable operations and their 'commands': \n\n'+'\t  '-'\t\t '/'\t '*'\t\t'e'");
	printf("\nadd\tsubstract\tdivide\tmultiply\texit");
	printf("\n\n's'\t  'm'\t\t 'r'\t 'c'");
	printf("\nsin(x)\t  |x|\t\tsqrt(x)\tclear");
	printf("\n\n(for sine the number must be entered in degrees, for squre root the number must be positive)\n\n");
	 char c;	
	
	//введення першого числа і запобігання введення невірних даних
	do {
		printf("Enter first number: ");
	}while
		(((scanf("%lf%c", &number, &c) != 2 || c != '\n') && clean_stdin()));


operation:   //мітка для переходу до наступної операції

	//введення команди операції
	printf("Enter operation command:"); 
	scanf(" %c", &operation);

	//перевірка на коректність команди операції
	if (correct_check(operation) == -1) {
		printf("\nEntered incorrect operation command\n");
		return -1;
	}
	//перевірка на потребу введення другого числа 
	if (operation != 's' && operation != 'm' && operation != 'r' && operation != 'c' && operation != 'e') {
		//введення другого числа і запобігання введення невірних даних
		do {
			printf("Enter second number: ");
		} while
			(((scanf("%lf%c", &second_number, &c) != 2 || c != '\n') && clean_stdin()));
	}

	//розгалудження на операції
	switch (operation)
	{
	case '+':
		number = number + second_number;
		break;
	case '-':
		number = number - second_number;
		break;
	case '/':
		number = number / second_number;
		break;
	case '*':
		number = number * second_number;
		break;
	case 's':
		number = sinx(number);
		break;
	case 'm':
		number = modulex(number);
		break;
	case 'r':
		if ((sqrtx(number)) == -1) {
			printf("impossible to find the square root of a negative number\n");
			return -1;
		}
	    else number = sqrtx(number);
		break;
	case 'c':
		//операція скидання, переход на початок коду
		//system("cls"); - операнд, який не працює в glot.io
		goto reset;
	case 'e':
		return 0;
	}
	//виведення	проміжного результату
	printf("\nYour result is %lf", number);   
	printf("\nIf you want to continue - enter next operation command\n");
	//перехід до введення наступнох операції
	goto operation;
}
INFO