实现碰撞检测

我写了一个程序,模拟一个50米的建筑物被抛出的球。 我通过在球撞击地面时反转y方向的速度(y <0),保持水平速度相同,并将两个速度乘以某个最小值,从而使球最终变为休息。 #include #include #include int main() { FILE *fp; FILE *fr; float ax = 0, ay = 0, x = 0, y = 0, vx = 0, vy = 0; float time = 0, deltaTime = .001; float min = -.00000000001; int numBounces = 0; fr = fopen(“input_data.txt”, “rt”); fp = fopen( “output_data.txt”, […]

警告:从不兼容的指针类型分配

#include #include #include int main() { double x,y; printf(“Enter a double: “); scanf(“%lf”, &x); printf(“Enter another double:”); scanf(“%lf”, &y); int *ptr_one; int *ptr_two; ptr_one = &x; ptr_two = &x; int *ptr_three; ptr_three = &y; printf(“The Address of ptr_one: %p \n”, ptr_one); printf(“The Address of ptr_two: %p \n”, ptr_two); printf(“The Address of ptr_three: %p”, ptr_three); return […]

转换为int时浮点数为何在C下面舍入?

在一次采访中,有人问我对以下代码有何看法: #include int main() { float f = 10.7; int a; a = f; printf (“%d\n”, a); } 我回答了: 当您将float更改为没有float转换的int ,编译器将发出警告。 由于您没有使用强制转换,因此int将具有垃圾值。 然后,他们允许我在在线编译器上运行程序。 我不堪重负。 我的假设都是错误的。 编译器没有发出任何警告,并且int的值为10.即使我改变了, float值为10.9或10.3,答案也是一样的。 即使投了一个cast并没有改变结果。 有人能告诉我为什么会发生这种情况,结果会在什么情况下有所不同。 注意:在编译时,面试官告诉我不添加gcc标志。 编辑:现在我已经明白,浮动不会四舍五入,答案将是10.但有人可以解释我为什么这样设计? 转换为int时为什么浮动,在下面四舍五入? 有具体原因吗?

在c 中交换任何类型的两个变量

c中是否有任何逻辑可以交换任何类型的两个变量。 即int,float,字符序列。 我可以想到将每种类型的变量存储为characte序列并将其交换为普通字符串的逻辑,但我不是它的好主意。

如何检查字符串是否在文件中

我是C的新手,我正在试图弄清楚如何在一个包含多个数字和单词的文件中搜索特定字符串及其后面的数字。 我正在使用的输入文件如下所示: TREES 2 BENCHES 5 ROCKS 10 PLANTS 8 我知道如何使函数读取字符串,我知道如何比较两个字符串但我不知道如何将它们放在一起或如何设置数组以读取整个文件。 我一直在使用strcmp ,但我只是不知道如何初始化我想要寻找的这个词。

注册Windows类Win32

注册win32程序的窗口类的最佳做法是什么? 您是否在WinMainfunction中注册每个课程或者是否需要?

计算C中的行,单词和字符

下面是我的计算行数,单词和字符的函数 – void count(char* file) { int fd; long end=0; char c; long words=0; long lines=0; if((fd=open(file, O_RDONLY))>0){ end=lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); while(read(fd, &c, 1)==1){ if(c == ‘ ‘) words++; if(c == ‘\n’) { lines++; words++; } } printf(“The Number of characters in file is: %ld\n”,end); printf(“The Number of lines in file is: […]

使用未分配的空间calloc

我只是想知道当我使用非分配空间时编译器不会抛出exception,这里是一个代码例如: #include #include int main() { int i, n; int *a; printf(“Number of elements to be entered:”); scanf(“%d”,&n); a = (int*)calloc(n, sizeof(int)); printf(“Enter %d numbers:\n”,n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf("The numbers entered are: "); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } […]

使用C ++导入注册表文件

我有一些问题,让以下简单的代码正确运行: #include int main( void ) { system(“foo.reg”); //why does this NOT WORK?! //system(“reg import foo.reg”); //why does this NOT WORK?! //system(“regedit \”foo.reg\””); //why does this NOT WORK?! return 0; } 注册表文件位于已编译的可执行文件所在的目录中。 当我从命令行运行它时, foo.reg运行成功,但运行上面的程序只显示regedit确认窗口,并且在注册表本身没有相应的更改。 我使用Dev-C ++编写程序,我运行的是Windows XP x64 SP2。 任何帮助将不胜感激。

用C ++执行脚本

我想通过c ++程序执行脚本并获取其输出。 现在我在做 system(“./script.sh > out.txt”); 但是我需要一个命令来将输出转换为字符串,例如: out = system(“./script.sh”); printf(out); 执行脚本后我无法读取文件out.txt,因为我没有权限。 我在其他框架(boinc)上部署了我的c ++程序,但没有给我这个权限。 有人有提示吗? 提前致谢! 费利佩