有没有办法从黑色cmd屏幕(stdin)输入功能?

时间:2019-07-12 12:37:18

标签: c

我被要求编写一个使用黎曼和的程序来数值计算积分。我结束了该代码(感谢上帝),但我想进一步介绍一下:从黑色cmd屏幕添加功能。

但是我似乎找不到任何方法。我浏览了成千上万本书籍,但似乎找不到任何东西。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
double mulfun (double x) {
    /*I want to add function from the black screen here*/
    double q = (x*x+5*x+1); //but you can change q to any functions you want
    double s = exp(x)*q; //this is basically homework :)
    return s;
}
double polynomial(double x) {
    int u;
    printf("Insert degree of this polynomial?\t");
    scanf("%d", &u);
    u = u + 1;
    float z[u];
    int k;
    for (k = u - 1; k <= 0; k-- ) {
        printf("\nInsert term of x^%d\t",z[k]);
        scanf("%f%*c", &z[k]);
    }
    double s = z[0];
    for (k=1; k<(u); k++) {
        s = s + z[k]*pow(x,k);
    }
    return s;
}
double riemann (double (*f) (double), double a, double b, double n) {
    double delta, s = 0;
    int i;
    delta = (b - a)/(double)n;
    for (i = 0; i < n; i++) {
        s = s + delta*(f(a + i*delta + delta/2));
    };
    return s;
}
/*mulfun is the function where you add your functions in*/
void main() {
/* a is the lower-bound, b is the upper-bound, n is number of parts
inteval [a, b] is divided into*/
    float a, b, n;
    scanf("%f", &a);
    scanf("%f", &b);
    scanf("%f", &n);
    double e = riemann (mulfun, a, b, n);
    printf("%f", e);
    getch();
}

1 个答案:

答案 0 :(得分:0)

您要询问的内容在某些语言中称为eval()函数,或更普遍地称为evaluation。例如,在Perl中,您可以调用eval(STRING),其中STRING是任意字符串-例如,用户提供的输入,就像您想做的一样-和{{1} }将被解释为Perl代码,并照此执行。

这是一个摘要。有许多更详细的细节需要考虑,例如范围,退出状态,异常处理,安全性等。即使语言提供了STRING函数,它也非常棘手。试图实现自己的过程充满了陷阱和尖锐的边缘。

但是,C没有等效于eval()。我发布此答案的主要目的是给您一个进行进一步研究的常用术语。知道您正在谈论一类用通用术语eval()eval进行的编程任务,可以帮助您专注于研究。

例如,这是一个起点:https://en.wikipedia.org/wiki/Eval