华氏温度转换摄氏温度,函数解决版
#include <stdio.h>
float celsius(float fahr);
/* 华氏温度转换摄氏温度,函数解决版 */
main()
{
float fahr;
int lower, upper, step;
lower = 0; /* 温度下限 */
upper = 300; /* 温度上限 */
step = 20; /* 步长 */
fahr = lower;
while(fahr <= upper){
printf("%3.0f %6.1f\n", fahr, celsius(fahr));
fahr = fahr + step;
}
}
/* celsius函数:华氏温度转换成摄氏温度 */
float celsius(float fahr)
{
return (5.0/9.0) * (fahr-32.0);
}
求幕函数
#include <stdio.h>
int power(int base, int n);
/* 求幕函数 */
main()
{
int i;
for(i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2, i), power(-3, i));
}
/* power函数:求底数的n次幕,其中n >= 0 */
int power(int base, int n)
{
int i, p;
p = 1;
for(i = 1; i <= n; ++i)
p = p * base;
return p;
}
打印最长的输入行
#include <stdio.h>
#define MAXLINE 1000 /* 允许的输入行的最大长度 */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* 打印最长的输入行 */
main()
{
int len; /* 当前行长度 */
int max; /* 目前为止发现的最长行的长度 */
char line[MAXLINE]; /* 当前的输入行 */
char longest[MAXLINE]; /* 用于保存最长的行 */
max = 0;
while((len = getline(line, MAXLINE)) > 0)
if(len > max){
max = len;
copy(longest, line);
}
if(max > 0) /* 存在这样的行 */
printf("%s", longest);
return 0;
}
/* getline函数:将一行读入到s中并返回其长度 */
int getline(char s[], int lim)
{
int c, i;
for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy函数:将from复制到to,这里假定to足够大 */
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i] != '\0'))
++i;
}
551

被折叠的 条评论
为什么被折叠?



