悬赏100分做一道简单C语言题

来源:百度知道 编辑:UC知道 时间:2024/06/19 07:39:12
悬赏100分作一道简单的计算逻辑和的C程序。

要满足以下条件:(必须用点隔开各个数字)

1 必须要使用bite运算符
2 输入的范围必须是0到255
3 当输入的数字不符合要求的时候(即超过3个点或者范围超过255)输出Bye后结束程序。

执行结果要如下:

Z:¥>prog1009-2
Please input the first number: 192.168.1.3
Please input the second number: 255.255.255.0

Answer: 192.168.1.0

Z:¥>prog1009-2
Please input the first number: 192.168.1.3.
Please input the second number: 255.255.255.0
Bye!

就这些了,谢谢大家帮助……

#include <stdio.h>
#include <string.h>

int main(){
int a,b,c,d;
int aa,bb,cc,dd;
int err = 0,num=0;
char s[255]={0};
printf("Please input the first number: ");
if(scanf("%s",&s)==EOF)
err = 1;

char *p=s;

num=0;
while(p=strchr(p,'.')){
num++;p++;
}
if(num!=3)
err=1;
else{
p=s;
a=atoi(p);
p=strchr(p,'.');
b=atoi(++p);
p=strchr(p,'.');
c=atoi(++p);
p=strchr(p,'.');
d=atoi(++p);
if(strchr(p,'.'))
err = 1;
}
printf("Please input the second number: ");
if(scanf("%s",&s)==EOF)
err = 1;

p=s;
num=0;
while(p=strchr(p,'.')){
num++;p++;
}
if(num!=3)
err=1;
else{