Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- void findDerivative(char **dst, char *src, int deg) {
- int a = strtol(src, &src, 10);
- if (a != 0) {
- a *= deg;
- } else if (strlen(src) > 0) {
- a = deg;
- }
- *dst = malloc(1024);
- itoa(a, *dst, 10);
- if (strlen(src) > 0) {
- strcat(*dst, "*");
- strcat(*dst, src);
- }
- strcat(*dst, "*t^");
- char *c = malloc((deg - 1) / 10 + 2);
- itoa(deg - 1, c, 10);
- strcat(*dst, c);
- dst = realloc(*dst, strlen(*dst) + 1);
- }
- int main(void) {
- printf("podaj wspolczynniki rownania w kolejnosci od tego przy najnizszej "
- "potedze (czyli wyrazu wolnego) oddzielone spacja (jezeli ktorys "
- "jest rowny 0 to trzeba to 0 napisac): ");
- char *buf = malloc(1024);
- if (buf == NULL) {
- printf("Memory allocation error\n");
- exit(1);
- }
- fgets(buf, 1024, stdin);
- while (*buf == ' ')
- buf++;
- char **formula;
- char *ptr = buf;
- size_t degree = 0;
- while (*ptr != '\n') {
- while (*ptr != ' ' && *ptr != '\n')
- ptr++;
- degree++;
- while (*ptr == ' ')
- ptr++;
- }
- //printf("%d\n", degree);
- formula = malloc(degree * sizeof(char *));
- degree--;
- if (formula == NULL) {
- printf("Memory allocation error\n");
- exit(1);
- }
- ptr = buf;
- char *strBegin = buf;
- size_t length, i = 0;
- while (*ptr != '\n') {
- while (*ptr != ' ' && *ptr != '\n')
- ptr++;
- length = ptr - strBegin;
- //printf("***%d***\n", length);
- formula[i] = malloc(length + 1);
- strncpy(formula[i], strBegin, length);
- formula[i][length] = '\0';
- i++;
- while (*ptr == ' ')
- ptr++;
- strBegin = ptr;
- }
- free(buf);
- char **derivative = malloc(degree * sizeof(char *));
- for (size_t i = 1; i <= degree; i++) {
- findDerivative(derivative + i - 1, formula[i], i);
- }
- printf("v(t) = ");
- for (size_t i = 0; i < degree; i++) {
- printf("%s ", derivative[i]);
- if (i != degree - 1)
- printf("+ ");
- else
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement