#include <stdio.h>include <stdio.h>void main ( void ) int num; char stringNum[20];int i,j,Num [20]; puts ("请输入十进制整数num: ");scanf ( ""%d" , &num) ; for (i=0 ; num !=0; ++i){ Num [ i]= ; num/=2; ) for(j=0;j<i;++j) stringNum[ j]=_ ? '1' : "0'; stringNum[ j]= puts(""转换成二进制字符串:"");puts (stringNum) ;将这段代码补充完整
时间: 2024-03-03 18:51:43 浏览: 17
好的,以下是补充完整后的代码:
```c
#include <stdio.h>
void main(void)
{
int num;
char stringNum[20];
int i, j, Num[20];
puts("请输入十进制整数num:");
scanf("%d", &num);
for (i = 0; num != 0; ++i)
{
Num[i] = num % 2;
num /= 2;
}
for (j = 0; j < i; ++j)
{
stringNum[j] = Num[i - j - 1] ? '1' : '0';
}
stringNum[j] = '\0';
puts("转换成二进制字符串:");
puts(stringNum);
}
```
输入一个十进制整数,程序会将其转换为二进制字符串并输出。该程序与上一个程序的不同之处在于,它使用了数组 `Num` 存储二进制数的每一位,使用字符数组 `stringNum` 存储二进制字符串,并且在输出字符串时使用了 `puts` 函数。
相关问题
#include <stdio.h> #include <string.h> void fun(char *s,int num) { } void main() { char s[10]; gets(s); fun(s,7); printf("%s", s); }
这是一道需要填空的题目,题目思路是将字符串 `s` 中的前 `num` 个字符反转。可以使用两个指针分别指向字符串的首位和末位,依次交换两个指针所指向的字符,直到两个指针相遇为止。具体实现可以这样写:
```c
#include <stdio.h>
#include <string.h>
void fun(char *s, int num) {
int i = 0, j = num - 1;
while (i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
void main() {
char s[10];
gets(s);
fun(s, 7);
printf("%s", s);
}
```
其中,`i` 和 `j` 分别表示指向字符串的首位和末位的指针,`temp` 是一个临时变量,用于交换两个指针所指向的字符。在循环中,每次将指针向中间移动一位,并交换两个指针所指向的字符,直到两个指针相遇为止。最终输出交换后的字符串即可。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include "添加药品.cpp" struct medicine { char name[20]; char number[50]; char price[50]; char type[10]; char inventory[50]; char brand[20]; char time[60]; char remark[30]; }medicines[100]; int num; int main(void) { int selectnum;add_medicine(); }int add_medicine() { printf("输入要保存的药品信息\n"); printf("输入药品名称\n"); scanf("%s",medicines[num].name); printf("输入药品编号\n"); scanf("%s",medicines[num].number); printf("输入药品单价\n"); return 0; } 帮我将这串代码分成两个cpp文件,一个主文件,一个子文件
主文件(main.cpp):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "medicine.h"
struct medicine medicines[100];
int num;
int main(void) {
int selectnum;
add_medicine();
return 0;
}
子文件(medicine.cpp):
#include "medicine.h"
#include <stdio.h>
int add_medicine() {
printf("输入要保存的药品信息\n");
printf("输入药品名称\n");
scanf("%s",medicines[num].name);
printf("输入药品编号\n");
scanf("%s",medicines[num].number);
printf("输入药品单价\n");
return 0;
}
头文件(medicine.h):
#ifndef MEDICINE_H
#define MEDICINE_H
struct medicine {
char name[20];
char number[50];
char price[50];
char type[10];
char inventory[50];
char brand[20];
char time[60];
char remark[30];
};
int add_medicine();
#endif
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)