C言語メモ5


華氏温度は摂氏温度を転換して、関数は版を解決します
#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
", 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
", 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 != '
'; ++i) s[i] = c; if(c == '
'){ 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; }