一些例子我遇到一个处理菜单的程序..
他在main函数之前声明了所有函数,因为我理解应该是,然后在main中也提到了一个函数,它是一个void函数:
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
void count(void);
while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : printf("Buy low, sell high.\n");
break;
case 'b' : putchar('\a'); /* ANSI */
break;
case 'c' : count();
break;
default : printf("Program error!\n");
break;
}
}
printf("Bye.\n");
Run Code Online (Sandbox Code Playgroud)
......(功能实现)
你能告诉我为什么吗?TNX
我正在阅读"C语言程序设计语言"这本书,并且有一个练习要求验证表达式getchar() != EOF是返回1还是0.现在,我被要求做的原始代码是:
int main()
{
int c;
c = getchar();
while (c != EOF)
{
putchar(c);
c = getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
所以我想把它改成:
int main()
{
int c;
c = getchar();
while (c != EOF)
{
printf("the value of EOF is: %d", c);
printf(", and the char you typed was: ");
putchar(c);
c = getchar();
}
}
Run Code Online (Sandbox Code Playgroud)
书中的答案是:
int main()
{
printf("Press a key\n\n");
printf("The expression getchar() != EOF evaluates to %d\n", getchar() != EOF);
}
Run Code Online (Sandbox Code Playgroud)
能告诉我为什么我的方式不起作用吗?
是否可以使用函数来检测非打印字符,isctrl()并使用带有%C说明符的printf将它们打印为'\n'?
或者我应该if为每个控制角色写一个printf("\\n"),例如..?
好的,感谢下面的所有人 - 这是不可能的,你必须指定每种情况.例:
if (isctrl(char))// WRONG
printf("%c", char);
if (char == '\n')//RIGHT, or using switch.
printf("\\n");
Run Code Online (Sandbox Code Playgroud) 我正试图将华氏温度改为开尔文,而公式是 K = 5/9 (° F - 32) + 273
我的代码是:
#include <stdio.h>
double const changeToC = 32.0;
double const changeToK = 273.16;
void temperatures(double n);
int main(void)
{
int q = 'q';
double userNumber;
printf("please enter fahrenheit number: \n");
scanf("%f", &userNumber);
while (userNumber != q)
{
temperatures(userNumber);
printf("\n");
printf("please enter fahrenheit number: \n");
scanf("%f", &userNumber);
}
}
void temperatures(double n)
{
double celsius, kelvin;
celsius = 5.0 / 9.0 * (n - changeToC);
kelvin = 5.0 / 9.0 (n …Run Code Online (Sandbox Code Playgroud) 我是C的初学者,所以请原谅我这个问题是愚蠢的还是怪问.
我正在阅读C primer plus,第8章中的一个例子是测试用户是否输入的一些循环 - a newline character or not我无法理解.
代码很短,所以我会告诉你:
int main(void)
{
int ch; /* character to be printed */
int rows, cols; /* number of rows and columns */
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
if (scanf("%d %d",&rows, &cols) != 2)
break;
display(ch, rows, cols);
while (getchar() != '\n')
continue;
printf("Enter another character and two integers;\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0; …Run Code Online (Sandbox Code Playgroud) 我写了一种方法,可以通过执行api调用来丰富个人数据并添加丰富的数据。
所以我有这个案例类:
case class Person(personData: PersonData, dataEnrichment: Option[DataEnrichment])
Run Code Online (Sandbox Code Playgroud)
我的方法假设要返回此case类。
但我之前没有几个过滤器
如果人身高不是,"1.8 m"或者如果使用正则表达式未在生物中找到personId,我想返回PersondataEnrichment = None
我的问题是,人身高和personId本身就是选项。
所以看起来像这样:
def enrichPersonObjWithApiCall(person: Person) = {
person.personData.height.map(_.equals("1.8 m")) match {
case Some(true) =>
val personId = person.personData.bio flatMap { comment =>
extractPersonIdIfExists(comment)
}
personId match {
case Some(perId) =>
apiCall(perId) map { apiRes =>
Person(
person.personData,
dataEnrichment = apiRes)
}
case _ =>
Future successful Person(
person.personData,
dataEnrichment = None)
}
case _ =>
Future successful Person(
person.personData,
dataEnrichment = None) …Run Code Online (Sandbox Code Playgroud) 我有一个练习来覆盖init方法,所以我需要创建一个init方法来设置一些属性.
我的问题是:为什么我还需要定义原始init方法?如果新init方法不起作用?
这是我的.h档案:
#import <Foundation/Foundation.h>
#import "XYPoint.h"
@interface Rectangle: NSObject
@property float width, height, tx, ty;
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) translate: (XYPoint *) point;
-(id) initWithWidth:(int) w andHeight:(int) h;
-(id) init;
@end
Run Code Online (Sandbox Code Playgroud)
而且.m(只有init方法):
-(id) initWithWidth:(int)w andHeight:(int)h
{
self = [super init];
if (self)
{
[self setWidth:w andHeight:h];
}
return self;
}
-(id) init
{
return [self initWithWidth:0 andHeight:0];
}
Run Code Online (Sandbox Code Playgroud)
我知道这样做很好,但如果有人能解释我为什么会受到赞赏.
我有一个API,允许用户创建一个人对象,我在mysql数据库中创建它,当前当mysql返回a时,MySQLException if t.errorMessage.errorCode == 1062我返回用户400,并显示具有此id的人已经存在的消息,我应该返回409吗?
这是我的代码:
int main(void)
{
int i, j, k, n;
char userLatter, space;
printf("please enter an uppercase letter:\n");
scanf("%c", &userLatter);
n = 9;
for (i = 0; i < 5; i++)
{
space = ' ';
for (j = 5; j > i; j--)
{
++space;
}
for (k = 0; k <= i ; k += 1)
{
printf("%c%c%c", space, userLatter, space);
}
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我我应该怎么做才能打印空间角色吗?
谢谢!
我正在尝试在下一个程序中打印变量'nc'中的值:
int main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
Run Code Online (Sandbox Code Playgroud)
请告诉我为什么不打印?