我写了一个简单的程序来查找3个数字的Sum,average,maximum和最小数.它允许用户输入三个(整数)数字并返回总和,平均值,最大值和最小值.它没有错误,只有警告.这是我的源代码:
main.c中:
#include <stdio.h>
int main()
{
int num1, num2, num3, sum, max, min, avg;
printf("Enter Three \"Integer\" Numbers:");
scanf("%i%i%i", &num1, &num2, &num3);
sum = summation(&num1, &num2, &num3);
avg = average(&sum);
max = max_val(&num1, &num2, &num3);
min = min_val(&num1, &num2, &num3);
printf("Sum: %i Avg: %i MAX: %i MIN: %i", sum, avg, max, min);
return 0;
}
int summation(int *n1, int *n2, int *n3)
{
int s;
s = *n1 + *n2 + *n3;
return s;
}
int average(int *s)
{ …Run Code Online (Sandbox Code Playgroud) 我正在阅读K&R的"The C Programming Language"一书.在"结构"一章中,在"表查找"(页144)的子主题下,我找到了此哈希生成函数
#define HASHSIZE 101
struct nlist {
struct nlist *next;
char *name;
char *defn;
}
static struct nlist *hashtab[HASHSIZE];
unsigned hash(char *s)
{
unsigned hashval;
for (hashval = 0; *s != '\0'; s++)
hashval = *s + 31 * hashval;
return hashval % HASHSIZE;
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是这个功能实际上是做什么的.
我认为它为给定的字符串(char*s)生成一个唯一的地址(作为hashtab的索引).
但我认为两个不同的字符串可以被赋予相同的索引,因为(hashval%HASHSIZE)是给定的地址(203%101 = 405%101 = 1).
为什么HASHSIZE 101和hashval乘以31?为什么不是100或32?
我最近写了一个简单的程序来反转一个字符串,我的程序只是接受来自用户的输入字符串然后反转它.这一切都是用2个指针完成的.关于程序中的指针安全性,我编写了一个复制给定字符串的函数,该函数为新的(相同长度)字符串分配内存,然后逐个复制字符.
我的问题是当我运行这个程序时,虽然它做了我需要的东西,它打印出一些神秘的额外输出.在接受用户的输入之前,它每次都这样做.这是我运行程序时发生的情况.
C:\Users\0xEDD1E\Desktop\revstr>revstr.exe
[C]
[.]
[revstr.exe]
hello
[hello]
olleh
Run Code Online (Sandbox Code Playgroud)
这里最后三行是输入和输出,没关系,问题出在前3行
[C]
[.]
[revstr]
Run Code Online (Sandbox Code Playgroud)
那些是什么?无论如何,这是我的计划
#include <stdio.h>
#include <stdlib.h>
#define swap(a, b) (((a) ^ (b)) && ((a) ^= (b), (b) ^= (a), (a) ^= (b)))
unsigned long strlen_(char *);
char *strdup(char *s);
char *reverseString(char *, int);
int main(void)
{
//fflush(stdout);
char *str = (char *) malloc(1024 * sizeof (char));
scanf("%[^\n]s", str);
int slen = strlen_(str);
printf("%s\n", reverseString(str, slen));
return 0;
}
unsigned long strlen_(char *s)
{
char *p = s;
while …Run Code Online (Sandbox Code Playgroud) 我正在阅读Bjarne Stroustrup的"The C++ Programming Language"第4版.在本书的第一部分中,我发现了using如下所示的用法:
// Error is in these 2 lines
template<typename T>
using Iterator<T> = typename T::iterator;
Run Code Online (Sandbox Code Playgroud)
*有关完整程序和错误消息,请参阅[**]*
这正是我在页面105中找到的.当我把它变成一个完整的程序并试图编译它时,g++给了我这个错误masseage:
> g++ -std=c++14 -o fnd find_all.cpp
find_all.cpp:13:15: error: expected '=' before '<' token
using Iterator<T> = typename T::iterator;
^
find_all.cpp:13:15: error: expected type-specifier before '<' token
Run Code Online (Sandbox Code Playgroud)
我在这段代码中找不到任何问题,(我是C++的新手,我用我的小知识找不到问题)(更混乱的是我在Bjarne的书上发现了这个)
有人能告诉我为什么那段代码会出错?
注:不过如果我换成
Iterator<C>用typename C::iterator(见下文),它工作正常,没有错误!
[**]完整程序和错误消息:
// Error is in these 2 lines
template<typename T>
using Iterator<T> = typename T::iterator;
// -------------------------------------------
// For …Run Code Online (Sandbox Code Playgroud) 我在一本书上看到这个源代码:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *delivery = "";
int thick = 0;
int count = 0;
char ch;
while ((ch = getopt(argc, argv, "d: t")) != EOF)
switch(ch)
{
case 'd':
delivery = optarg;
break;
case 't':
thick = 1;
break;
default:
fprintf(stderr, "Unknown option: '%s'\n", optarg);
return 1;
}
argc -= optind;
argv += optind;
if (thick)
puts("Thick Crust.");
if (delivery[0])
printf("To be deliverd %s\n", delivery);
puts("Ingredients: ");
for (count = 0; …Run Code Online (Sandbox Code Playgroud) 我对这个程序很困惑,我将在这里说明.我写了两个简单的程序来打印字符串列表.首先,我创建了一个指向字符串的指针数组.这就是我尝试这样做的方式
#include <stdio.h>
int main()
{
int i = 2;
char *a[] = {"Hello", "World"};
while (--i >= 0) {
printf("%s\n", *a++); // error is here.
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要它打印
Hello
World
Run Code Online (Sandbox Code Playgroud)
但有编译错误,它说,
lvalue required as increment operand.
Run Code Online (Sandbox Code Playgroud)
然后我将程序更改为以下内容
#include <stdio.h>
void printout(char *a[], int n)
{
while (n-- > 0)
printf("%s\n", *a++);
}
int main()
{
int i = 2;
char *a[] = {"Hello", "World"};
printout(a,i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后它按预期工作.
我的问题是,当我将数组名称传递给函数时会发生什么不同?为什么它不能第一次工作(我怀疑"数组名称不能修改"是原因但是为什么在第二个程序中,它允许我增加)?
我写了一个程序来通过XOR加密给定的消息.它有效,但它没有结束.这是代码.(我创建了3个文件):
encrypt.h:
void encrypt(char *message);
Run Code Online (Sandbox Code Playgroud)
message_hider.c:
#include <stdio.h>
#include "encrypt.h"
int main() {
char msg[80];
while (fgets(msg, 80, stdin)){
encrypt(msg);
printf("%s", msg);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
encrypt.c:
#include "encrypt.h"
void encrypt(char *message) {
while (*message) {
*message++ ^= 0x1f;
}
}
Run Code Online (Sandbox Code Playgroud)
正如我上面提到的,它有效.但我无法阻止它.当我按下Ctrl + D来停止它(在cmd中)它也加密它.(我需要在加密消息后停止此代码).请解释一下这个案子.
我正在编写一个程序来计算信息熵,这是熵(H)的函数
这里使用base 2 log
然后这是我的计划
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <signal.h>
typedef struct vars {
char *var;
float prob;
} CHOISES;
float infocont(float x);
float entropy(CHOISES *, int);
void onsig(int);
int main(int argc, char *argv[])
{
int i = 0;
int siz = 0;
float H = 0.0;
printf("input the number of vars: ");
scanf("%d", &siz);
//printf("echo: %d\n", siz);
CHOISES chs[siz];
signal(SIGSEGV, onsig);
for (i = 0; i < siz; i++) {
printf("%d: ", i + 1);
scanf("%s …Run Code Online (Sandbox Code Playgroud)