Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab 9
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <locale.h>
- void task1() {
- int sum = 0;
- for (int i = 100; i <= 990; i+= 10) sum += i;
- printf("\nSum is %d", sum);
- }
- void task2() {
- int A, B, C, r;
- int copyA, n = 0; // for the second part of the task
- printf("Введите сумму вклада: ");
- scanf("%d", &A);
- printf("Введите кол-во месяцев: ");
- scanf("%d", &B);
- printf("Введите сумму, которую превысит основной вклад через n месяц(а)(ев): ");
- scanf("%d", &C);
- if (A < 0 || B < 0 || C < 0) printf("Src data must be positive or zero"); return;
- if (C < A) printf("A must be less or equal C"); return;
- copyA = A;
- r = A/100;
- for (int i = 1; i <= B; i++) {
- A += r;
- }
- printf("Через %d месяцев на счету будет находиться следующая сумма: %d", B, A);
- while(copyA < C) {
- copyA += r;
- n++;
- }
- printf("Через %d месяцев основная сумма вклада превысит %d", n, C);
- }
- void printOnNewLine(char src[]) {
- printf("\n%s", src);
- }
- bool srcDataIsCorrected(int start, int end, int step) {
- bool correted = true;
- if (start > end) {
- printOnNewLine("Error! Start must be less or equal to end");
- correted = false;
- }
- if (step < 1) {
- printOnNewLine("Error! Step must be more or equal to 1");
- correted = false;
- }
- return correted;
- }
- int countOfDigitsInNumber(int number) {
- int count = 0;
- while (number % 10 != 0)
- {
- number /= 10;
- count++;
- }
- return count;
- }
- double deriveFirstNDigits(int n, double number) {
- double copyNumber = number;
- double pivot = (double) pow(10, n);
- if(pivot >= copyNumber) return copyNumber;
- while (copyNumber > pivot)
- {
- copyNumber /= 10;
- }
- return copyNumber;
- }
- void task3() {
- setlocale(LC_ALL, "Russian");
- int start, end, step;
- double f;
- printOnNewLine("Введите range");
- printOnNewLine("start: ");
- scanf("%d", &start);
- printOnNewLine("end: ");
- scanf("%d", &end);
- printOnNewLine("Введите step: ");
- scanf("%d", &step);
- if (srcDataIsCorrected(start, end, step))
- {
- printOnNewLine("|\tx\t|\tstep\t|\tf\t|");
- for (int x = start; x <= end; x += step)
- {
- if (x <= 5)
- {
- f = 2 * x;
- } else {
- if(x < 7) {
- f = 3 * log(2 * x);
- } else {
- f = exp(x);
- }
- }
- int derivedNumber = deriveFirstNDigits(5, f);
- if (countOfDigitsInNumber(derivedNumber) == 5)
- {
- printf("\n|\t%d\t|\t%d\t|\t%d...\t|", x, step, derivedNumber);
- } else {
- printf("\n|\t%d\t|\t%d\t|\t%d \t|", x, step, derivedNumber);
- }
- }
- }
- }
- int main() {
- int numberOfTask = -1;
- do
- {
- printf("\nВведите номер задачи(1, 2 или 3, 0 - для выхода): ");
- scanf("%d", &numberOfTask);
- switch(numberOfTask) {
- case 1: task1(); break;
- case 2: task2(); break;
- case 3: task3(); break;
- default: {
- if (numberOfTask !=0) {
- printf("\nNumber of task %d doesn't exist!", numberOfTask);
- }
- }
- }
- } while (numberOfTask != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement