C语言题目描述 本题要求实现符号函数sign(x)。 主函数参考代码如下: #include <stdio.h> int sign( int x ); int main() { int x; scanf("%d", &x); printf("sign(%d) = %d\n", x, sign(x)); return 0; } /* 你的代码将被嵌在这里 */ 输入 函数接口定义: int sign( int x ); 其中x是用户传入的整型参数。符号函数的定义为:若x大于0,sign(x) = 1;若x等于0,sign(x) = 0;否则,sign(x) = −1。
时间: 2023-09-20 16:10:00 浏览: 105
下面是符号函数的C语言实现:
```c
#include <stdio.h>
int sign(int x)
{
if (x > 0) {
return 1;
} else if (x < 0) {
return -1;
} else {
return 0;
}
}
int main()
{
int x;
scanf("%d", &x);
printf("sign(%d) = %d\n", x, sign(x));
return 0;
}
```
在函数 `sign` 中,首先判断参数 `x` 是否大于 0,若是,则返回 1;否则再判断是否小于 0,若是,则返回 -1;否则即为 `x` 等于 0,返回 0。
在主函数中,读入一个整数 `x`,调用 `sign` 函数并输出结果。
可以根据需要进行测试。
相关问题
c语言 检查一下下面的代码 为什么函数中获取不到键值#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/hmac.h> #include <jansson.h> #include <time.h> #include <errno.h> #include <resolv.h> #include <netdb.h> char* calculate_signature(char* json_str, char* key) { json_t *root; json_error_t error; /* 从文件中读取 JSON 数据 */ root = json_load_file(json_str, 0, &error); /* 遍历 JSON 对象中的所有键值对,并获取键的名称 */ int key_count = json_object_size(root); printf("key_names %d\n", key_count); const char *key_name; json_t *value; const char **key_names = (const char **)malloc(key_count * sizeof(char *)); int i = 0; json_object_foreach(root, key_name, value) { key_name = json_object_iter_key(value); key_names[i] = key_name; i++; } printf("key_names %s\n", key_names[2]); //int str_num = i; // 计算字符串数组中的字符串数量 /* char **sorted_names = sort_strings(key_names, key_count); char* stringA = (char*)malloc(1); // 初始化为一个空字符串 stringA[0] = '\0'; size_t len = 0; for (int i = 0; i < str_num; i++) { char* key = sorted_names[i]; json_t* value = json_object_get(root, key); char* str = json_dumps(value, JSON_ENCODE_ANY | JSON_COMPACT); len += strlen(key) + strlen(str) + 2; // 2 是键值对之间的字符 stringA = (char*)realloc(stringA, len); strcat(stringA, key); strcat(stringA, "="); strcat(stringA, str); strcat(stringA, "&"); free(str); } free(sorted_names); stringA[strlen(stringA) - 1] = '\0'; // 去掉最后一个"&" printf("stringA%s\n", stringA); unsigned char* sign = (unsigned char*)malloc(EVP_MAX_MD_SIZE); unsigned int sign_len = 0; HMAC(EVP_sha256(), key, strlen(key), (unsigned char*)stringA, strlen(stringA), sign, &sign_len); // 计算HMAC-SHA256签名 char* signature = (char*)malloc(sign_len * 2 + 1); // 签名的十六进制表示 signature[0] = '\0'; // 初始化为一个空字符串 for (int i = 0; i < sign_len; i++) { sprintf(signature + i * 2, "%02x", sign[i]); } json_object_set_new(root, "sign", json_string(signature)); // 在json中添加"sign"参数 json_dumpf(root, stdout, JSON_ENCODE_ANY | JSON_COMPACT); // 输出带有"sign"参数的json字符串 json_decref(root); free(key_names); free(stringA); free(sign); printf("signature%s\n", signature); */ return("A"); } int main() { char *key="39cabdfaab8c4da09bd6e9823c527836"; char *sss="{\"timestamp\":1685509898,\"sdkVersion\":\"1.0.30_1\",\"vin\":\"LJUBMSA24PKFFF198\"}"; calculate_signature(sss, key) ; }
代码中的问题在于 `json_load_file` 函数的第一个参数应该是一个文件名字符串,而不是 JSON 字符串。因此,需要将 `calculate_signature` 函数中的第一个参数 `json_str` 修改为文件名。
修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/hmac.h>
#include <jansson.h>
#include <time.h>
#include <errno.h>
#include <resolv.h>
#include <netdb.h>
char* calculate_signature(char* filename, char* key) {
json_t *root;
json_error_t error;
/* 从文件中读取 JSON 数据 */
root = json_load_file(filename, 0, &error);
if (!root) {
printf("Error in %s line %d: %s\n", error.source, error.line, error.text);
return NULL;
}
/* 遍历 JSON 对象中的所有键值对,并获取键的名称 */
int key_count = json_object_size(root);
printf("key_names %d\n", key_count);
const char *key_name;
json_t *value;
const char **key_names = (const char **)malloc(key_count * sizeof(char *));
int i = 0;
json_object_foreach(root, key_name, value) {
key_name = json_object_iter_key(value);
key_names[i] = key_name;
i++;
}
printf("key_names %s\n", key_names[2]);
//int str_num = i; // 计算字符串数组中的字符串数量
/*
char **sorted_names = sort_strings(key_names, key_count);
char* stringA = (char*)malloc(1); // 初始化为一个空字符串
stringA[0] = '\0';
size_t len = 0;
for (int i = 0; i < str_num; i++) {
char* key = sorted_names[i];
json_t* value = json_object_get(root, key);
char* str = json_dumps(value, JSON_ENCODE_ANY | JSON_COMPACT);
len += strlen(key) + strlen(str) + 2; // 2 是键值对之间的字符
stringA = (char*)realloc(stringA, len);
strcat(stringA, key);
strcat(stringA, "=");
strcat(stringA, str);
strcat(stringA, "&");
free(str);
}
free(sorted_names);
stringA[strlen(stringA) - 1] = '\0'; // 去掉最后一个"&"
printf("stringA%s\n", stringA);
unsigned char* sign = (unsigned char*)malloc(EVP_MAX_MD_SIZE);
unsigned int sign_len = 0;
HMAC(EVP_sha256(), key, strlen(key), (unsigned char*)stringA, strlen(stringA), sign, &sign_len); // 计算HMAC-SHA256签名
char* signature = (char*)malloc(sign_len * 2 + 1); // 签名的十六进制表示
signature[0] = '\0'; // 初始化为一个空字符串
for (int i = 0; i < sign_len; i++) {
sprintf(signature + i * 2, "%02x", sign[i]);
}
json_object_set_new(root, "sign", json_string(signature)); // 在json中添加"sign"参数
json_dumpf(root, stdout, JSON_ENCODE_ANY | JSON_COMPACT); // 输出带有"sign"参数的json字符串
json_decref(root);
free(key_names);
free(stringA);
free(sign);
printf("signature%s\n", signature);
*/
return("A");
}
int main() {
char *key="39cabdfaab8c4da09bd6e9823c527836";
char *filename="test.json";
calculate_signature(filename, key) ;
return 0;
}
```
另外,代码中还有一些注释掉的部分,需要先实现对应的函数才能使用。
6-4 字符数组与字符串:求带符号的数字串对应的整数。 分数 10 作者 苑丽红 单位 长春理工大学 请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。例如,若输入字符串"-1234",则函数把它转换为整数值 -1234。 (请勿在答题框编写主函数main, 仅需完成函数fun的begin和end之间应该填写 的代码。) 函数接口定义: long fun ( char *p); 其中 p指向主函数传入的带符号的数字字符串。 裁判测试程序样例: #include <stdio.h> #include <string.h> long fun ( char *p) ; int main() /* 主函数 */ { char s[6]; long n; scanf("%s", s) ; n=fun(s); printf("%ld",n); return 0; } /* 答题框所填写的代码将被嵌入到这里 */ 输入样例: -4567 输出样例: -4567 输入样例: 456 输出样例: 456
#include <stdio.h>
#include <string.h>
long fun(char *p) {
int len = strlen(p);
int sign = 1; // 符号位,默认为正数
int i = 0;
long result = 0;
// 判断是否有符号位
if (p[0] == '-') {
sign = -1;
i = 1;
}
// 遍历字符串并转换为整数
for (; i < len; i++) {
if (p[i] >= '0' && p[i] <= '9') {
result = result * 10 + (p[i] - '0');
}
}
return result * sign;
}