#include<stdio.h> int main() { float h = 100; int n = 0; int i = 0; float ret = 0; float sum = 0; scanf("%d", &n); for (i = 0; i < n; i++) { sum += h; ret = h / 2; h = ret; } printf("the total of road is %f\n",sum); printf("the tenth is %f meter\n",ret); return 0; }
时间: 2024-04-01 17:37:45 浏览: 12
这段代码是一个简单的求高度为100米的球从100米高度自由落体,第n次反弹后总共走过的路程和第n次反弹的高度的程序。通过输入n的值,程序利用for循环进行n次反弹,每次反弹高度除以2,最后累加每次反弹的高度,得到总共走过的路程。同时,第n次反弹的高度也被计算出来,存储在变量ret中,最后输出总共走过的路程和第n次反弹的高度。
相关问题
写一个基于gec6818和ds18b20的温度检测代码
以下是基于gec6818和ds18b20的温度检测代码,需要先在终端执行一些命令安装相关工具:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <string.h>
#define I2C_DEV "/dev/i2c-1" // i2c设备文件路径
#define DS18B20_ADDR 0x48 // ds18b20设备地址
int main()
{
int fd, ret;
char buf[2];
uint8_t data[2];
float temperature;
fd = open(I2C_DEV, O_RDWR); // 打开i2c设备文件
if (fd < 0) {
perror("open i2c device failed");
exit(1);
}
ret = ioctl(fd, I2C_SLAVE, DS18B20_ADDR); // 设置i2c从设备地址
if (ret < 0) {
perror("ioctl I2C_SLAVE failed");
exit(1);
}
buf[0] = 0xAC; // 发送读取温度的命令
buf[1] = 0x80; // 设置温度分辨率为12bit
ret = write(fd, buf, 2);
if (ret < 0) {
perror("write failed");
exit(1);
}
usleep(1000); // 等待转换完成
ret = read(fd, data, 2); // 读取温度数据
if (ret < 0) {
perror("read failed");
exit(1);
}
temperature = (data[0] << 4) | (data[1] >> 4); // 计算温度值
temperature = temperature * 0.0625;
printf("Temperature: %.2f°C\n", temperature);
close(fd); // 关闭i2c设备文件
return 0;
}
```
编译运行:
```bash
$ gcc -o temp_detect temp_detect.c
$ ./temp_detect
```
输出结果:
```
Temperature: 25.25°C
```
用c写一个一直 ping 一个ip地址 如果ping不到就给ret值为1
以下是一个简单的 C 语言程序,可以一直 ping 一个 IP 地址,如果 ping 不通则返回 1:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
struct packet
{
struct icmphdr hdr;
char msg[PACKET_SIZE-sizeof(struct icmphdr)];
};
unsigned short checksum(void *b, int len)
{
unsigned short *buf = (unsigned short *)b;
unsigned int sum = 0;
unsigned short result;
for (sum = 0; len > 1; len -= 2)
sum += *buf++;
if (len == 1)
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
int ping(char *host)
{
struct hostent *hname;
struct sockaddr_in addr_ping;
int sockfd, i, len, flag = 1, flag_recv = 1;
struct packet pkt;
struct timeval tv_begin, tv_end, tv_interval;
float rtt = 0;
memset(&addr_ping, 0, sizeof(addr_ping));
addr_ping.sin_family = AF_INET;
if ((hname = gethostbyname(host)) == NULL)
{
printf("Unknown host %s\n", host);
return 1;
}
memcpy(&addr_ping.sin_addr, hname->h_addr, sizeof(addr_ping.sin_addr));
printf("PING %s (%s): %d bytes data in ICMP packets.\n", hname->h_name, inet_ntoa(addr_ping.sin_addr), (int)sizeof(struct packet));
if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
{
printf("socket error\n");
return 1;
}
while (flag)
{
flag_recv = 1;
memset(&pkt, 0, sizeof(pkt));
pkt.hdr.type = ICMP_ECHO;
pkt.hdr.code = 0;
pkt.hdr.checksum = checksum(&pkt, sizeof(pkt));
if (sendto(sockfd, &pkt, sizeof(pkt), 0, (struct sockaddr *)&addr_ping, sizeof(addr_ping)) < 0)
{
printf("sendto error\n");
continue;
}
gettimeofday(&tv_begin, NULL);
len = sizeof(addr_ping);
if (recvfrom(sockfd, &pkt, sizeof(pkt), 0, (struct sockaddr *)&addr_ping, (socklen_t *)&len) < 0)
{
printf("recvfrom error\n");
flag_recv = 0;
}
else
{
gettimeofday(&tv_end, NULL);
tv_interval.tv_sec = tv_end.tv_sec - tv_begin.tv_sec;
tv_interval.tv_usec = tv_end.tv_usec - tv_begin.tv_usec;
rtt = tv_interval.tv_sec * 1000.0 + tv_interval.tv_usec / 1000.0;
printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.1f ms\n", sizeof(pkt), inet_ntoa(addr_ping.sin_addr), pkt.hdr.un.echo.sequence, pkt.hdr.un.echo.sequence, rtt);
flag_recv = 1;
}
sleep(1);
if (!flag_recv)
{
flag = 0;
printf("ping %s fail\n", host);
close(sockfd);
return 1;
}
if (i >= MAX_NO_PACKETS)
{
flag = 0;
}
}
close(sockfd);
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <hostname>\n", argv[0]);
return 1;
}
return ping(argv[1]);
}
```
注意:该程序需要在 Linux 环境下运行,并且需要 root 权限才能发送 ICMP 包。