wheat.c
/*wheat.c --指数增长*/
#include<stdio.h>
#define SQUARES 64 /*棋盘上的方格数*/
#define CROP 1E15 /* 以粒计的美国小麦产量*/
#include<float.h>
int main(void)
{
double current, total;
int count = 1;
printf("square grains total ");
printf("fraction of \n");
printf(" added grain");
printf("US total\n");
total = current = 1.0; // 开始时是1.0粒
printf("%4d %13.2e %12.2e %12.2e\n",count,current,
total,total/CROP);
while (count < SQUARES) {
count = count + 1;
current = 2.0*current; // 下个方格的粒数加倍
total = total + current; // 更新总数
printf("%4d %13.2e %12.2e %12.2e\n",count,current,
total,total/CROP);
}
printf("This is all\n");
getchar();
getchar();
return 0;
}
---
/*min_sec.c --把秒转换为分钟数*/
#include<stdio.h>
#define SEC_PER_MIN 60 /*每分钟的秒数*/
int main(void)
{
int sec = 0, min = 0, left = 0;
printf("Convert seconds to minutes and seconds!\n");
printf("Enter the number of seconds(<=0 to quit):\n");
scanf("%d", &sec);
while (sec > 0) {
min = sec / SEC_PER_MIN; // 截尾后的分钟数
left = sec%SEC_PER_MIN; // 剩下的秒数
printf("%d second is %d minutes,%d seconds.\n", sec, min,
left);
printf("Enter the number of seconds(<=0 to quit):\n");
scanf("%d", &sec);
}
printf("Done!\n");
getchar();
getchar();
return 0;
}
Convert seconds to minutes and seconds!
Enter the number of seconds(<=0 to quit):
12555
12555 second is 209 minutes,15 seconds.
Enter the number of seconds(<=0 to quit):
12366
12366 second is 206 minutes,6 seconds.
Enter the number of seconds(<=0 to quit):
4566
4566 second is 76 minutes,6 seconds.
Enter the number of seconds(<=0 to quit):
---
/*add_one.c --增量,前缀和后缀*/
#include<stdio.h>
int main(void)
{
int super = 0, ultra = 0;
while (super<5)
{
ultra++;
++super;
printf("super= %d, ultra = %d\n", super, ultra);
}
getchar();
getchar();
return 0;
}
super= 1, ultra = 1
super= 2, ultra = 2
super= 3, ultra = 3
super= 4, ultra = 4
super= 5, ultra = 5
----
/*pound.c --定义一个带参数*/
#include<stdio.h>
void pound(int n);
int main(void)
{
int times = 5;
char ch = '!'; //ASCII 码值为33
float f = 6.0;
pound(times); /* int参数*/
pound(ch);/* char 参数*/
pound((int)f); /* 指派运算符把f强制转换为int类型*/
getchar();
getchar();
return 0;
}
void pound(int n) { // int 类型参数
while (n-->0)
printf("#");
printf("\n");
}
#####
#################################
######
---
/*runing.c --一个对于长跑运动员有用的程序*/
#include<stdio.h>
const int S_PER_M = 60; /*每分钟的秒数*/
const int S_PER_H = 3600; /*每小时的秒数*/
const double M_PER_K = 0.62137; /*每公里的英里数*/
int main(void)
{
double distk=0, distm=0; // 分别以公里和英里计的跑过的距离
double rate=0; // 以英里/小时为单位的平均速度
int min=0, sec=0; // 跑步用时的分钟数和秒数
int time=0; // 用秒表示跑步的时间
double mtime=0; // 跑完一英里所用的时间,以秒计
int mmin=0, msec=0; //跑完1英里所用的时间,以分钟和秒计
printf("This program converts your time for a metric race\n");
printf("to a time for running a mile and to your average\n");
printf("speed in miles per hour.\n");
printf("Please enter, in kilometers, the distance run.\n");
scanf("%lf", &distk);
printf("Next enter the time in minutes and seconds.\n");
printf("Begin by entering the minutes\n");
scanf("%d", &min);
printf("Now enter the seconds.\n");
scanf("%d", &sec);
// 把时间全部转换为用秒表示
time = S_PER_M*min + sec;
// 把公里转换为英里
distm = M_PER_K*distk;
// 英里/秒*秒/小时=英里/小时
rate = distm / time*S_PER_H;
// 时间/距离=跑完每英里的用时
mtime = (double)time / distm;
mmin = (int)mtime / S_PER_M; // 求出分钟数
msec = (int)mtime / S_PER_M; // 求出剩余秒数
printf("You can %1.2f km(%1.2f miles) in %d min, %d sec.\n",
distk, distm, min, sec);
printf("That pace corresponds to running a mile in %d min,\n", mmin);
printf("%d sec.\nYour average speed was %1.2f mph.\n", msec, rate);
getchar();
getchar();
return 0;
}
---
/*runing.c --一个对于长跑运动员有用的程序*/
#include<stdio.h>
#define S_PER_H 60 // 每小时60分
int main(void)
{
int min = 0;
printf("Please input time by minutes:\n");
scanf("%d", &min);
while (min>0)
{
printf("%d minutes is %d hours and %d min\n",min,min/S_PER_H,min%S_PER_H);
printf("Enter a value<0 will exit\n");
scanf("%d", &min);
}
getchar();
getchar();
return 0;
}
---
#include<stdio.h>
int main(void)
{
int a= 0,i=0;
printf("Please input a integer number:\n");
scanf("%d", &a);
while (i++<11)
{
printf("%d ",a++);
}
getchar();
getchar();
return 0;
}
Please input a integer number:
5
5 6 7 8 9 10 11 12 13 14 15
---
#include<stdio.h>
#define WEKEND 7
int main(void)
{
int days= 0;
printf("Please input days:\n");
scanf("%d", &days);
while (days>0)
{
printf("%d days are %d weeks, %d days.\n",days,days/WEKEND,days%WEKEND);
printf("if you input negative number or 0 will be exit\n");
scanf("%d", &days);
}
getchar();
return 0;
}
Please input days:
18
18 days are 2 weeks, 4 days.
if you input negative number or 0 will be exit
---
#include<stdio.h>
#define INCH 2.54 // 1 inch = 2.54cm
int main(void)
{
float cm= 0;
printf("Please input a height in centimeter:");
scanf("%f", &cm);
while (cm>0)
{
printf("%.1f cm = %d feet, %.1f inches\n",cm,(int)(cm/INCH/12),cm/INCH-(int)(cm/INCH/12)*12);
printf("Enter a height in centimeters (<=0 or quit):");
scanf("%d", &cm);
printf("bye/n");
}
getchar();
return 0;
}
Please input a height in centimeter:182.0
182.0 cm = 5 feet, 11.7 inches
Enter a height in centimeters (<=0 or quit):
---
#include<stdio.h>
int main(void)
{
int sum = 0, count = 0, max=0;
printf("Please input the max\n");
scanf("%d", &max);
while (count++<max)
sum = sum + count;
printf("sum = %d\n",sum);
getchar();
getchar();
return 0;
}
Please input the max
20
sum = 210
---
#include<stdio.h>
int main(void)
{
int count = 0,max = 0, sum = 0;
printf("Please input the max");
scanf("%d", &max);
while (count++ < max)
sum = sum + count*count;
printf("sum = %d", sum);
getchar();
getchar();
return 0;
}
Please input the max3
sum = 14
---
#include<stdio.h>
float cube(float);
int main(void)
{
float m = 0;
printf("Please input a int number\n");
scanf("%f", &m);
printf("The sum = %f\n",cube(m));
getchar();
getchar();
return 0;
}
float cube(float n) {
return n*n*n;
}
Please input a int number
3.3
The sum = 35.936996
---
/* 温度与摄氏度转换*/
#include<stdio.h>
void Temperatures(double);
int main(void)
{
double Fahrenheit = 0;
printf("Please input the fahrenheit:\n");
while (scanf("%lf",&Fahrenheit) == 1)
{
Temperatures(Fahrenheit);
printf("Please input a Fahrenheit:\n");
}
printf("end\n");
getchar();
getchar();
return 0;
}
void Temperatures(double Fahrenheit) {
const a=1.8;
const b=32.0;
const c=273.16;
double Celsius = 0, Kelvin = 0;
printf("Fahrenheit = %lf, Celsius = %lf\n", Fahrenheit, a*Fahrenheit + b);
printf("Kelvin %lf\n",(a*Fahrenheit + b) + c);
}
参数传值
2016年8月20日第五章 运算符、表达式和语句
最新推荐文章于 2024-02-25 08:38:17 发布