使用printf for char时,仅输出点
我目前正在做作业,无法从下面的代码中得到想要的结果使用printf for char时,仅输出点,c,C,我目前正在做作业,无法从下面的代码中得到想要的结果 #include <stdio.h> int main() { int base, num, x, y, z, t, choice; char hexa = 0, a, b, c, d, k, l, m, n, h, g, f, v; switch (hexa) { case 'A': num = 10; break; case 'B': n
#include <stdio.h>
int main() {
int base, num, x, y, z, t, choice;
char hexa = 0, a, b, c, d, k, l, m, n, h, g, f, v;
switch (hexa) {
case 'A':
num = 10;
break;
case 'B':
num = 11;
break;
case 'C':
num = 12;
break;
case 'D':
num = 13;
break;
case 'E':
num = 14;
break;
case 'F':
num = 15;
break;
}
printf("Please enter the base:\n");
scanf("%d", &base);
if (base == 10) {
printf("Please enter your ip address:\n");
scanf("%c.%c.%c.%c", &a, &b, &c, &d);
//if((a<0 || a>255) && (b<0 || b>255) && (c<0 || c>255) && (d<0 || d>255)){ //FIX THIS
// printf("Sorry, that is not a valid address!");}
printf("Please enter the sub-net mask\n"); //Skips this line
scanf("%d.%d.%d.%d", &x, &y, &z, &t);
}
else if (base == 16) {
printf("Enter your ip address\n");
scanf("%c.%c.%c.%c", &k, &l, &m, &n);
printf("Enter the subnet mask\n");
scanf("%c.%c.%c.%c", &h, &g, &f, &v);
}
scanf("%d", &choice);
if (choice == 1) { //there is a part that asks which
printf("%c.%c.%c.%c", a, b, c, d); //prints only dots
}
else if (choice == 2) {}
else if (choice == 3) {}
else if (choice == 4) {}
else if (choice == 5) {}
else if (choice == 6) {}
return 0;
}
#包括
int main(){
int base,num,x,y,z,t,choice;
charhexa=0,a,b,c,d,k,l,m,n,h,g,f,v;
开关(六边形){
案例“A”:
num=10;
打破
案例“B”:
num=11;
打破
案例“C”:
num=12;
打破
案例“D”:
num=13;
打破
案例“E”:
num=14;
打破
案例“F”:
num=15;
打破
}
printf(“请输入基数:\n”);
scanf(“%d”、&base);
如果(基数=10){
printf(“请输入您的ip地址:\n”);
scanf(“%c.%c.%c.%c”、&a、&b、&c、&d);
//如果((a255)&(b255)&&(c255)&&(d255)){//修复此问题
//printf(“对不起,这不是一个有效的地址!”;}
printf(“请输入子网掩码”;//跳过此行
scanf(“%d.%d.%d.%d”、&x、&y、&z、&t);
}
否则如果(基数==16){
printf(“输入您的ip地址\n”);
scanf(“%c.%c.%c.%c”、&k、&l、&m、&n);
printf(“输入子网掩码\n”);
scanf(“%c.%c.%c.%c”、&h、&g、&f、&v);
}
scanf(“%d”,选择(&C);
如果(choice==1){//有一个部件询问
printf(“%c.%c.%c.%c”,a,b,c,d);//只打印点
}
如果(choice==2){}
如果(choice==3){}
如果(choice==4){}
如果(choice==5){}
如果(choice==6){}
返回0;
}
因此,基本上我正在尝试获取IP和子网,并将其转换为二进制,但按照说明,我不能使用按位移位和%x
来获取IP地址作为基16。问题是当我试图打印给定的IP地址时,printf
只打印点,其他什么都不打印。并且其中一个跳过子网掩码scanf
。我真的很高兴能得到你们的帮助,因为我没有很多朋友可以问问题。谢谢
编辑:我不知道是否有人会面临这个问题,但我的解决方案相当愚蠢,但它是有效的。基本上,您将输入作为
scanf(“%c%c.%c%c.%c.%c.%c%c”)
,最后一个%c
用于输入字符,因为字符以enter作为输入。您好,欢迎来到stackoverflow
我的回答只会对你基本的16部分有所帮助,因为你可以根据你已经取得的成绩和我给你的一些提示轻松地完成基本的10部分
使用base 16解析
正如您提到的,您不能使用%x检索IP地址,因此另一种可能是使用字符串。
IP地址是0到255之间的数字,因此您可以期望使用0x00到0xFF之间的十六进制字符
对我来说,实现这一点的最佳方法是使用带有scanf功能的:
scanf("%2[0-9A-F]", str);
模式%2[0-9A-F]表示scanf最多需要两个字符,它们必须是A和F之间的任何数字或任何大写字母(所以是任何十六进制字符)。
注意,str长度必须为3才能存储2个字符+空终止符\0
然后,str缓冲区将填充ASCII字符,作为IP地址的十六进制表示。可以使用该函数将十六进制表示字符串转换为整数
完整代码如下:
#include <stdio.h>
#include <stdlib.h> // strol
#define DIGIT_SIZE 3 // 2 hex digit + null character
int main(void) {
char d1[DIGIT_SIZE];
char d2[DIGIT_SIZE];
char d3[DIGIT_SIZE];
char d4[DIGIT_SIZE];
scanf("%2[0-9A-F].%2[0-9A-F].%2[0-9A-F].%2[0-9A-F]", d1, d2, d3, d4);
printf("hex ip:[%s.%s.%s.%s]\n", d1, d2, d3, d4);
int i1 = (int)strtol(d1, NULL, 16);
int i2 = (int)strtol(d2, NULL, 16);
int i3 = (int)strtol(d3, NULL, 16);
int i4 = (int)strtol(d4, NULL, 16);
printf("dec ip:[%d.%d.%d.%d]\n", i1, i2, i3, i4);
return 0;
}
嗨,欢迎来到stackoverflow 我的回答只会对你基本的16部分有所帮助,因为你可以根据你已经取得的成绩和我给你的一些提示轻松地完成基本的10部分 使用base 16解析 正如您提到的,您不能使用%x检索IP地址,因此另一种可能是使用字符串。 IP地址是0到255之间的数字,因此您可以期望使用0x00到0xFF之间的十六进制字符 对我来说,实现这一点的最佳方法是使用带有scanf功能的:
scanf("%2[0-9A-F]", str);
模式%2[0-9A-F]表示scanf最多需要两个字符,它们必须是A和F之间的任何数字或任何大写字母(所以是任何十六进制字符)。
注意,str长度必须为3才能存储2个字符+空终止符\0
然后,str缓冲区将填充ASCII字符,作为IP地址的十六进制表示。可以使用该函数将十六进制表示字符串转换为整数
完整代码如下:
#include <stdio.h>
#include <stdlib.h> // strol
#define DIGIT_SIZE 3 // 2 hex digit + null character
int main(void) {
char d1[DIGIT_SIZE];
char d2[DIGIT_SIZE];
char d3[DIGIT_SIZE];
char d4[DIGIT_SIZE];
scanf("%2[0-9A-F].%2[0-9A-F].%2[0-9A-F].%2[0-9A-F]", d1, d2, d3, d4);
printf("hex ip:[%s.%s.%s.%s]\n", d1, d2, d3, d4);
int i1 = (int)strtol(d1, NULL, 16);
int i2 = (int)strtol(d2, NULL, 16);
int i3 = (int)strtol(d3, NULL, 16);
int i4 = (int)strtol(d4, NULL, 16);
printf("dec ip:[%d.%d.%d.%d]\n", i1, i2, i3, i4);
return 0;
}
您的代码中存在多个问题:
语句是无用的,因为switch
有一个常量值,并且无论如何都不使用hexa
num
- 您应该用
、%d
或%u
解析十进制数,用%i
解析十六进制数%x
- 如果坚持将le IP选择器存储为字节,则应使用
而不是普通的无符号字符
,并分别使用字符
和%hhu
%hhx
- 数字有效性测试不正确,任何数字超出范围
都是错误的,因此所有测试之间都需要0..255
|
- 您应该使用
而不是%d
打印IP组件:%c
输出字符,而不是数字。这些字符可能无法打印,因此只显示点%c
#include <stdio.h>
int main() {
int base, a, b, c, d, x, y, z, t, n, choice;
printf("Please enter the base: ");
if (scanf("%d", &base) != 1)
return 1;
if (base != 10 && base != 16) {
printf("invalid base\n");
return 1;
}
printf("Please enter your IP address: ");
if (base == 10) {
n = scanf("%d.%d.%d.%d", &a, &b, &c, &d);
} else {
n = scanf("%x.%x.%x.%x", &a, &b, &c, &d);
}
if (n != 4) {
printf("invalid input\n");
return 1;
}
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255) {
printf("invalid IP address\n");
return 1;
}
printf("Please enter the sub-net mask: ");
if (base == 10) {
n = scanf("%d.%d.%d.%d", &x, &y, &z, &t);
} else {
n = scanf("%x.%x.%x.%x", &x, &y, &z, &t);
}
if (n != 4) {
printf("invalid input\n");
return 1;
}
if (x < 0 || x > 255 || y < 0 || y > 255 || z < 0 || z > 255 || t < 0 || t > 255) {
printf("invalid sub-net mask\n");
return 1;
}
if (scanf("%d", &choice) != 1) {
printf("invalid input\n");
return 1;
}
switch (choice) {
case 1:
printf("%d.%d.%d.%d\n", a, b, c, d);
break;
case 2:
case 3:
case 4:
case 5:
case 6:
break;
}
return 0;
}
您的代码中存在多个问题:
语句是无用的,因为switch
有一个常量值,并且无论如何都不使用hexa
num
- 您应该用
、%d
或%u
解析十进制数,用%i
解析十六进制数%x
- 如果坚持将le IP选择器存储为字节,则应使用
而不是普通的无符号字符
,并分别使用字符
和%hhu
%hhx
- 数字有效性测试不正确,任何数字超出范围都是错误的
,因此所有数字之间需要0..255
|