C 将固定整数附加到字符串
我想找到一种方法,这样当用户输入2种颜色时,程序会自动给每个颜色一个我选择的整数值 例如:C 将固定整数附加到字符串,c,C,我想找到一种方法,这样当用户输入2种颜色时,程序会自动给每个颜色一个我选择的整数值 例如: #include <stdio.h> #include <math.h> enum color { black = 0, brown = 1, red = 2, orange = 3, yellow = 4, green = 5, blue = 6, purple = 7, grey = 8, whi
#include <stdio.h>
#include <math.h>
enum color {
black = 0,
brown = 1,
red = 2,
orange = 3,
yellow = 4,
green = 5,
blue = 6,
purple = 7,
grey = 8,
white = 9
} colors;
int main(){
int sum;
char first, second;
printf("enter the first 2 colors");
scanf("%s %s", &first, &second);
sum = first + second
printf("%d", colors);
getch();
return 0;
}
#包括
#包括
枚举颜色{
黑色=0,
布朗=1,
红色=2,
橙色=3,
黄色=4,
绿色=5,
蓝色=6,
紫色=7,
灰色=8,
白色=9
}色彩;
int main(){
整数和;
第一,第二;
printf(“输入前2种颜色”);
scanf(“%s%s”、&first和&second);
总和=第一+第二
printf(“%d”,颜色);
getch();
返回0;
}
第一件事首先,您需要用分号结束每个语句代码>,在sum=…
行之后缺少一个
只是一个旁注:当您像那样声明枚举类型时,我的意思是,从0
开始并增加1,您不必进行任何赋值。您可以只编写enumcolor{黑色、棕色、红色,}颜色代码>和它将是相同的
根据您在评论中的回答,您似乎希望打印出总和
,而不是颜色
。为此,请更改printf
行,如下所示:
printf( "%d", sum );
char first[15];
另一件事;不能在单个字符内存储字符序列。要存储字符序列,您需要一个可以容纳字符序列的内存空间。一种方法是声明一个字符数组,如下所示:
printf( "%d", sum );
char first[15];
当您这样做时,计算机会自动为您分配15*sizeof(char)
大内存,其中第一个是first[0]
,最后一个是first[14]
。第一个的地址将是first+0
或只是first
,最后一个的地址将是first+14
另一种方法是手动分配内存,我不想讨论这个问题
因此,要获得两个字符串,代码应如下所示:
char first[15];
char second[15];
scanf( "%s %s", first, second );
// notice that I omitted the ampersands (&)
// since first and second are already the addresses
int main( ){
int sum;
char first[15], second[15];
printf( "enter the first 2 colors" );
scanf( "%14s %14s", &first, &second );
// 14's between % and s are to limit the amount of characters
// that will be obtained and written
sum = colorID( first ) + colorID( second );
printf( "%d", sum );
getch( );
return 0;
}
可悲的是:你不能那样做你想做的事。变量名、枚举类型标签。。。它们都只是标签,对计算机没有任何意义
然而,你可以用很多很多方式做你想做的事。我会给你一个最简单的:
// I didn't say it will look beautiful...
#include <string.h>
...
int firstID;
if ( strcmp( "black", first ) == 0 )
firstID = 0;
else if ( strcmp( "brown", first ) == 0 )
firstID = 1;
else if ( strcmp( "red", first ) == 0 )
firstID = 2;
else if ( strcmp( "orange", first ) == 0 )
firstID = 3;
else if ( strcmp( "yellow", first ) == 0 )
firstID = 4;
else if ( strcmp( "green", first ) == 0 )
firstID = 5;
else if ( strcmp( "blue", first ) == 0 )
firstID = 6;
else if ( strcmp( "purple", first ) == 0 )
firstID = 7;
else if ( strcmp( "grey", first ) == 0 )
firstID = 8;
else if ( strcmp( "white", first ) == 0 )
firstID = 9;
else
firstID = -1;
现在,将其称为colorID(第一)代码>和颜色ID(秒)代码>应该为您提供希望它们对应的单个值
我删除了您的enum
,将string.h
与#include
(对于strcmp
),添加了上面的函数colorID
,然后将main
更改为如下:
char first[15];
char second[15];
scanf( "%s %s", first, second );
// notice that I omitted the ampersands (&)
// since first and second are already the addresses
int main( ){
int sum;
char first[15], second[15];
printf( "enter the first 2 colors" );
scanf( "%14s %14s", &first, &second );
// 14's between % and s are to limit the amount of characters
// that will be obtained and written
sum = colorID( first ) + colorID( second );
printf( "%d", sum );
getch( );
return 0;
}
现在,我认为它能满足您的需求。请澄清您的具体问题或添加其他详细信息,以突出您的需求。正如目前所写的,很难准确说出你的要求。基本上,我正在尝试制作一个电阻色码计算器,我希望程序能够识别用户输入的任何颜色作为数字。例如,如果输入是红色和橙色,我想让计算机识别为2和3,这样我就可以用它来做一个等式,您能告诉我当输入颜色白色和红色时,您希望看到的输出是什么吗?我希望输出是11。也许我还应该注意,strcmp
区分大小写:'A'
小于'B'
,但是'A'
大于'B'
。这都是关于它们的整数代表值。