我是C的新人,我无法在论坛中找到我的问题的答案。
关键是,我需要从用户那里获得一副牌卡值。所以它可以从2到10传播,也可以是' J' Q' K' K'或者' A'。这意味着它可以是整数或字符。
我试图把它放在一个名为" _val1"的整数变量中。这个工作适用于0到10之间的任何数字。我希望如果我输入一个字母,_val1将得到该字符的ASCII值(我稍后可以用于我的pourposes)。而是_val1 geta值' 0'并且该字母会自动传递给我的下一个变量调用(这是_naipe1)。
我该如何解决?
这意味着,我如何使用scanf获取整数值或字符的ASCII值?
short int _val1, _val2;
char _naipe1, _naipe2;
printf("Qual a 1ª carta?\n Valor:");
scanf(" %hd", &_val1);
printf("Valor 1 = %hd \n", _val1 );
printf(" Naipe:");
scanf(" %c", &_naipe1);
答案 0 :(得分:1)
首先,一张典型的扑克牌有52张牌,分为4种套装:心形,钻石,黑桃和棍棒。这种情况表明用户输入的内容类似于:10s,Ad,3c等(意思是10个黑桃,1个钻石和3个俱乐部)因此,你不仅要确定个人卡的价值,还必须确定诉讼。
这不能解决所有这些要求,但它至少会回答你最直接的问题,如何使用scanf()
读取int或char。
这将证明:
#include <stdio.h>
int main(int argc, char** argv)
{
int aNumber;
char aChar;
printf("\nEnter a number:");
scanf("%d", &aNumber);
printf("\nEnter a character:");
scanf("%c", &aChar);
printf("\nThe number entered is %d\n", aNumber);
printf("\nThe character entered is %c\n", aChar);
return 0;
}
您还可以简单地将所有值都包含在字符串中,例如
char cards[]={"Ad Js 10c 2c Qh"};
然后使用strtok()
解析它,然后使用isdigit()
或isalpha()
注意: 您必须将每张卡映射到一个值才能保持正确,这样缩写的枚举可能会起作用:
enum {
AD = 1, //start enum values at 1 for the diamonds suit
2D,
3D,
...//fill in rest of cards here
JC,
QC,
KC, // last card == 52, with the clubs suit
};
答案 1 :(得分:1)
好吧,如果我是你,我会尝试简化问题:
'2'
到'9'
和'J','Q','K','A'
获取卡片表示的ASCII值;在那里,您只需使用scanf("%c")
甚至更好的getchar()
操作。然后要么在整个算法中继续使用卡片的ASCII表示,要么可以使用映射函数进行翻译,例如:
int map(char card) {
switch (card) {
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return card-'0';
case 'A':
return 1;
case 'J':
return 10;
case 'Q':
return 11;
case 'K':
return 12;
}
}
答案 2 :(得分:0)
输入字母时_val1的输出为0的原因在于您已将_val1声明为short int。你应该使用char。然后你可以分配和比较它们的ascii值。
char card;
int value;
scanf("%c", card);
if(card < 58 && card > 49)
value = card - 48;
else {
switch(card) {
case 'a': value = 1;
case '0': value = 10;
case 'j': value = 11;
case 'q': value = 12;
case 'k': value = 13;
default: printf("Must enter 0-9 (0 for 10 card), or a, j, q, k\n");
}
}
答案 3 :(得分:0)
阅读&#34; A&#34;,&#34; 2&#34;,&#34; 3&#34;,...&#34; 10&#34;,&#34; J& #34;,...&#34; K&#34;,使用fgetc()
和strchr()
。
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
short GetCardRank(void) {
static const char rank[] = "A234567891JQK";
short val = -1;
int ch = fgetc(stdin);
while (isspace(ch)) ch = fgetc(stdin); // Skip leading white-space
char *p = strchr(rank, toupper(ch)); // Use toupper() to make case insensitive
if (ch != EOF && p != NULL && *p != '\0') {
short val = (short) (p - rank + 1);
if (val != 10) return val;
ch = fgetc(stdin);
if (ch == '0') return val;
val = 1; // Allow a lone '1' to act like an 'A'
}
ungetc(ch, stdin); // Put back unused char for next IO function
return val;
}
答案 4 :(得分:0)
我试图把它放在一个名为“_val1”的整数变量中。这个工作适用于0到10之间的任何数字。我希望如果我输入一个字母,_val1将得到该字符的ASCII值(我稍后可以用于我的pourposes)。但是_val1 geta值为'0'而且字母会自动传递给我的下一个变量调用(这是_naipe1)
问题是%d
转换说明符只识别十进制数字字符串(带有可选的前导+
或-
),并且会停止读取第一个非数字字符;如果您输入的数字不是数字,那么输入操作将失败,该字符将保留在输入流中。
您最好的选择是将输入作为文本读取,然后手动将其转换为数值,如下所示:
#include <ctype.h>
#include <stdlib.h>
/**
* Reads a card's face value (2-10,J,Q,K,A) from standard input
* Returns 0 on error
*/
short get_card_value( void )
{
char buf[4]; // large enough to hold a 2-digit string plus newline plus 0 terminator
short val = 0;
if ( fgets( buf, sizeof buf, stdin ) != NULL )
{
char *chk;
short tmp = (short) strtol( buf, &chk, 0 );
if ( isspace( *chk ) || *chk == 0 )
{
if ( tmp >= 2 && tmp <= 10 )
val = tmp;
}
else
{
switch( tolower( *chk ) )
{
case 'j': val = 11; break;
case 'q': val = 12; break;
case 'k': val = 13; break;
case 'a': val = 11; break;
default: break;
}
}
}
// else read error
return val;
}
你称之为
val1 = get_card_value();
if ( val1 == 0 )
// error on input
此代码不对输入进行任何长度检查,因此如果您输入的卡值为1234567890,则无法正常处理。
不要在变量名中使用前导下划线;带有前导下划线的名称保留用于实现。