gpt4 book ai didi

html - CGI-通过 HTML 获取用户给出的 10 个输入的平均值

转载 作者:行者123 更新时间:2023-11-30 19:37:38 25 4
gpt4 key购买 nike

对于我们的作业,我们必须创建一个 HTML,使用表单从用户那里获取 10 个输入。然后,根据这 10 个输入,我们必须创建一个程序来获取 10 个输入的平均值,然后显示结果。这是我到目前为止所做的:我们必须首先将这 10 个输入存储在一个数组中

CGI代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char *data;
int i;
int nums[10];
double sum = 0.0;
double size=0.0;
printf("Content-type:text/html");
printf("<html><body>");

data=getenv("QUERY_STRING");
if(data)
{
for(i=0; i<10; i++)
{
sscanf(data,"nums[i]=%d", &nums[i]);
sum += nums[i];
}
size=sum/10;
printf("The average is %d\n",size);
}
return 0;
}

和 HTML:

<html>
<body>
<form action='/cgi-bin/trust.cgi'>
<a: input type=text value='' name=a />
<b: input type=text value='' name=b />
<c: input type=text value='' name=c />
<d: input type=text value='' name=d />
<e: input type=text value='' name=e />
<f: input type=text value='' name=f />
<g: input type=text value='' name=g />
<h: input type=text value='' name=h />
<i: input type=text value='' name=i />
<j: input type=text value='' name=j />
<input type=submit value='Compute'>
</form>
</body>
</html>

问题:我不断收到“错误 500:服务器错误!脚本 header 过早结束:trust.cgi”。这是我第一次编写此类代码,我将不胜感激。

编辑:已解决错误 500。但现在我的问题是,它为我提供了我需要的平均值的错误答案?有什么帮助吗?谢谢!

最佳答案

您的 HTML 并不是真正的 HTML,因此 tidy首先。

<!DOCTYPE html>
<html>
<head>
<title>Get The Average</title>
</head>
<body>
<form action='/cgi-bin/trust.cgi'>
<input type="text" value='' name="a"></br>
<input type="text" value='' name="b"></br>
<input type="text" value='' name="c"></br>
<input type="text" value='' name="d"></br>
<input type="text" value='' name="e"></br>
<input type="text" value='' name="f"></br>
<input type="text" value='' name="g"></br>
<input type="text" value='' name="h"></br>
<input type="text" value='' name="i"></br>
<input type="text" value='' name="j"></br>
<input type="submit" value='Compute'>
</form>
</body>
</html>

使用 sscanf() 解析查询字符串可能不是最好的主意,您的教授似乎也持有相同的观点,正如您所写的。尽管如此 scanf()

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *data;
int i;
int nums[10] = {0};
double sum = 0.0;
double size = 0.0;
printf("Content-type:text/html\r\n\r\n");
printf("<!DOCTYPE html><html><head><title>Here's The Average</title></head><body>");
// TODO: check if data is not NULL
data = getenv("QUERY_STRING");
printf("%s</br>\n", data);
if (data) {
// That's an abomination. Obviously.
sscanf(data, "a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=%d&h=%d&i=%d&j=%d",
&nums[0], &nums[1], &nums[2], &nums[3], &nums[4], &nums[5],
&nums[6], &nums[7], &nums[8], &nums[9]);
for (i = 0; i < 10; i++) {
sum += nums[i];
}
size = sum / 10;
printf("The average is %f\n", size);
}
printf("</body></html>");
exit(EXIT_SUCCESS);
}

使用 strtok 看起来确实好一点:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char *data;
char *token;
int i = 0;
int nums[10] = { 0 };
double sum = 0.0;
double size = 0.0;
printf("Content-type:text/html\r\n\r\n");
printf
("<!DOCTYPE html><html><head><title>Here's The Average with strtok</title></head><body>");
// TODO: check if data != NULL
data = getenv("QUERY_STRING");
printf("%s</br>\n", data);
if (data) {
token = strtok(data, "&");
while (token) {
// look for the '='
while (*token != '=') {
token++;
}
// and skip it
token++;
nums[i++] = atoi(token);
// check if we have enough
// the constant "10" should better be #define'd
if (i == 10) {
break;
}
token = strtok(NULL, "&");
}
for (i = 0; i < 10; i++) {
sum += nums[i];
}
size = sum / 10;
printf("The average is %f\n", size);
}
printf("</body></html>");
exit(EXIT_SUCCESS);
}

仅对字符串进行标记是不够的,如果您需要的话,您还必须转换/取消转义 HTTP 转义字符(以 % 开头的十六进制值)。

关于html - CGI-通过 HTML 获取用户给出的 10 个输入的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com