这是我的第一篇文章,我不得不承认,我的编程很糟糕。我是类那个努力工作的人,但似乎永远无法像其他同学一样掌握编程。所以请保持友善,我将在下面尝试解释我的问题。
我有以下代码(删除了注释),但是当我运行它时,我收到类似于下面列出的警告。此外,当我运行程序时,允许第一个用户输入值,但突然间,它跳到程序末尾,不允许我输入其他变量的值(例如变量“beta” ).我有一个输出图像 ( http://i.stack.imgur.com/yc3jq.jpg ),您可以看到我输入了 alpha,但程序运行到最后。有什么想法吗?
非常感谢您的帮助! -斯宾塞
----------------------------代码----------------
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float alpha, beta, h;
float slope_k (float, float, float, float);
float slope_q (float, float, float, float);
float slope_p (float, float, float, float);
int main (void)
{
float t0=0, tf, h, S0, I0, R0, k1, k2, k3, k4, q1, q2, q3, q4, p1, p2, p3, p4;
int N;
char sim_file[1000];
FILE *out_file;
float *time_out, *s_out, *i_out, *r_out;
printf("Enter the value of the rate constant for infection (alpha) \n");
scanf("&f", &alpha);
printf("Enter the value of the rate constant for recovery or death (beta) \n");
scanf("&f", &beta);
printf("Enter the value of number of persons susceptible to the given contagion [S] at the initial time zero [i.e. S(t)=S(0) = ? ] \n");
scanf("&f", &S0);
printf("Enter the value of the number of persons infected [I] at the intial time zero [i.e. I(t) = I(0) = ?] \n");
scanf("&f", &I0);
printf("Enter the value of the number of persons that have already been infected but have recovered [or died] [R] at the initial time zero [i.e. R(t) = R(0) = ?] \n");
scanf("&f", &R0);
printf("Enter the final time for solution \n");
scanf("&f", &tf);
printf("Enter the solution step size (H) \n");
scanf("&f", &h);
N = (int)(tf/h);
printf("Enter file solution to store solution to simulation \n");
scanf("&s", sim_file);
out_file = fopen(sim_file, "w");
time_out = (float *)calloc(sizeof(float), N);
s_out = (float *)calloc(sizeof(float), N);
i_out = (float *)calloc(sizeof(float), N);
r_out = (float *)calloc(sizeof(float), N);
time_out[0]= 0;
s_out[0] = S0;
i_out[0] = I0;
r_out[0] = R0;
for(int i = 0; i < N; ++i);
{
int i = 0;
time_out[i+1] = (i+1)*h;
k1 = h*slope_k(time_out[i], s_out[i], i_out[i], r_out[i]);
q1 = h*slope_q(time_out[i], s_out[i], i_out[i], r_out[i]);
p1 = h*slope_p(time_out[i], s_out[i], i_out[i], r_out[i]);
k2 = h*slope_k(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
q2 = h*slope_q(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
p2 = h*slope_p(time_out[i]+(h/2), s_out[i]+(k1/2), i_out[i]+(q1/2), r_out[i]+(p1/2));
k3 = h*slope_k(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
q3 = h*slope_q(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
p3 = h*slope_p(time_out[i]+(h/2), s_out[i]+(k2/2), i_out[i]+(q2/2), r_out[i]+(p2/2));
k4 = h*slope_k((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
q4 = h*slope_q((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
p4 = h*slope_p((time_out[i] + h), (s_out[i]+k3), (i_out[i]+q3), (r_out[i]+p3));
s_out[i+1] = s_out[i] + (1.0/6)*(k1 + (2*k2) + (2*k3) + k4);
i_out[i+1] = i_out[i] + (1.0/6)*(q1 + (2*q2) + (2*q3) + q4);
r_out[i+1] = r_out[i] + (1.0/6)*(p1 + (2*p2) + (2*p3) + p4);
}
return 0;
}
float slope_k(float t, float s, float i, float r)
{
float slope_k_out;
slope_k_out = -alpha*s*i;
return slope_k_out;
}
float slope_q(float t, float s, float i, float r)
{
float slope_q_out;
slope_q_out = (alpha*s*i)-(beta*i);
return slope_q_out;
}
float slope_p(float t, float s, float i, float r)
{
float slope_p_out;
slope_p_out = beta*i;
return slope_p_out;
}
示例警告:
warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
最佳答案
编译器告诉您的是,Microsoft 认为 scanf
不安全。
scanf
函数可以如果您小心使用是安全的。 scanf
确实存在数字输入问题(溢出具有未定义的行为),但 scanf_s
没有解决这些问题。
scanf_s
最初是 Microsoft 特定的扩展;它作为可选功能添加到 2011 ISO C 标准(N1570 附件 K)。许多 C 实现仍然不提供 scanf_s
。
引用C草案标准:
K.3.5.3.4p4:
The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s.
K.3.5.3.2p4:
The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a *). The first of these arguments is the same as for fscanf. That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair. If the first argument points to a scalar object, it is considered to be an array of one element.
对这个特定程序使用 scanf_s
而不是 scanf
不会使它更安全,但会导致它的可移植性较差。
使用 _CRT_SECURE_NO_WARNINGS
并忽略警告。
关于input - Scanf_s 警告?跳过用户输入(主题 : Runge-Kutta, 流行病模拟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986776/