仅使用按位运算符(|,&,〜,^,>>,<<)和其他基本运算符(如+, - 和!),是否可以替换下面的"=="?
int equal(int x, int y) {
return x == y;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个程序,将一个字符串作为输入,并用*符号替换所有元音.因此,对于"hello world",star_vowels应返回"h*ll*w*rld".
到目前为止我的代码是:
int star_vowels(char s[]){
int j;
j = 0;
while (s[j] != '0'){
j++;
if (s[j] = 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u'){
putchar('*');
} else {
putchar(j);
}
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种编码程序的方法,该程序只使用递归循环将整数乘以指数.我对递归的理解非常有限,但是能够编写一些东西来给出一个阶乘:
int fac2(int n)
{
if (n == 1){
return 1;
} else {
return n*fac2(n-1);
}
}
Run Code Online (Sandbox Code Playgroud)
我有办法找到一个力量,但它使用了一个for循环:
int my_power(int x, int e)
{
int i, total;
total = 1;
for (i = 1; i <= e; i++){
total *= x;
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
如何使用递归替换此for循环?
我正在寻找一种从用户那里获得浮点输入的方法.
我的方法是使用自制的getstrn函数并将其插入到另一个将字符串转换为double的函数中.
我的安全获取字符串:
void safeGetString(char arr[], int limit){
int c, i;
i = 0;
c = getchar();
while (c != '\n'){
if (i < limit -1){
arr[i] = c;
i++;
}
c = getchar();
}
arr[i] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
编写这个get_double函数的最佳方法是什么?
在C中,我需要创建一个函数,对于输入,它将计算并显示每个字母出现的次数.
对于"Lorem ipsum dolor sit amet"的输入,该函数应返回类似于:
a: 0
b: 0
c: 0
d: 1
e: 2
f: 0
...
Run Code Online (Sandbox Code Playgroud) 好的,所以我已经尝试了我能想到的一切,并且无法弄清楚如何使这个程序正常工作.我已经测试了main中使用的所有函数,但无论如何都包含它们,以防它们中存在一些错误.不仅如此,我相信我的错误主要在于.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265359
double int_power(double x, int e);
int main()
{
int my_factorial(int n);
double my_sine_taylor(double x);
double my_sine(double x);
double mod_two_pi(double x);
double get_double(void);
void safeGetString(char arr[], int limit)
char arr[255];
double x,y,ans;
printf("Enter a number: ");
safeGetString(arr[255],255);
my_sine(mod_two_pi(get_double()));
printf("The sine is %f \n", ans);
return 0;
}
/*
int_power should compute x^e, where x is a double and e is an integer.
*/
double int_power(double x, int e)
{
int i …Run Code Online (Sandbox Code Playgroud)