#include<string.h> int main() { char x[10001]; int y[10000]; int a,b; while(scanf("%s",x)!=EOF) { int g,i=0; int j=1; a=strlen(x); for(g=0;g<a-1;g++) x[g]-='0'; for(b=a-2;b>=0;b--) { i+=x[b]*j; j*=2; } int m=0; while(i>0) { y[m]=i%16; m++; i/=16; } for(j=m-1;j>=0;j--) { if(y[j]>=10) printf("%c",(y[j]-10+'A')); else printf("%d",y[j]); } printf("\n"); } }
时间: 2024-03-31 19:32:34 浏览: 18
这是一个将二进制数转换为十六进制数的程序,可以接受多组输入。程序的流程如下:
1. 定义字符数组 x 和整型数组 y,表示输入的二进制数和转换后的十六进制数。
2. 不断读入二进制数 x,直到读到文件结束符(EOF)为止。
3. 将字符数组 x 中的每个字符转换为数字,并存入数组中。
4. 将二进制数转换为十进制数 i。
5. 将十进制数 i 转换为十六进制数,存入数组 y 中。
6. 输出数组 y 中存储的十六进制数。
7. 重复步骤 2-6,直到读完所有输入。
需要注意的是,程序中的十六进制数输出部分,使用了 ASCII 码将数字 10~15 转换为字母 A~F 进行输出。
相关问题
#include<stdio.h> #include<string.h> int main() { char str[100]; char *p; p=&str; scanf("%s",&str); while(*p!='\0') {
代码中缺少了结尾的 `}`,补充代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
char *p;
p = str;
scanf("%s", str);
while (*p != '\0') {
/*******************************
请在该部分补充缺少的代码
********************************/
printf("%c", *p); // 示例代码:输出每个字符
p++;
}
return 0;
}
```
在缺失的部分中,可以根据题目要求进行相应的处理。例如,题目要求是删除字符串中的非英文字母字符,可以使用判断条件来判断当前字符是否为英文字母,如果是,则进行相应的处理,否则跳过该字符,具体代码如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
char *p;
p = str;
scanf("%s", str);
while (*p != '\0') {
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) {
// 如果是英文字母,则输出
printf("%c", *p);
}
p++;
}
return 0;
}
```
注意,`scanf("%s", &str);` 应该修改为 `scanf("%s", str);`,因为 `str` 已经是指向字符数组的指针,不需要再加取地址符 `&`。
C++编制一个读单词过程,以#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #define NULL 0为开头
C++编写一个读取单词的过程可以以下代码:
```cpp
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
void readWord(char* word) {
int index = 0;
char ch;
// 跳过空白字符
while ((ch = getchar()) != EOF && isspace(ch));
// 读取单词字符
while (ch != EOF && !isspace(ch)) {
word[index++] = ch;
ch = getchar();
}
// 添加字符串结束符
word[index] = '\0';
}
int main() {
char word[100];
readWord(word);
printf("读取的单词是:%s\n", word);
return 0;
}
```
上述代码中,`readWord`函数用于读取一个单词,它会跳过输入中的空白字符,然后读取连续的非空白字符作为一个单词。`main`函数中调用了`readWord`函数,并打印出读取到的单词。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)