if
语法: if ( expression )
statement
如果expression为真则执行statement。否则跳过
if else语句
语法: if (expression)
statement1
else
statement2
如果expression为真则执行statement1。否则执行statement2
else if 多重选择
if (expression1)
statement1
else if (expression2)
statement2
else
statement3
如果expression1为真则执行,statement1.如果2为真则执行statement2否则执行statement3
逻辑运算符
含义
与:当两边都是真时才为真
或:当两边有一个是真,变为真
非:当都是假时,才是真
求值顺序:
从左往右。发现有使整个表达式为假的因素是,立即停止求值。
x++ < 10 && x+y < 20
&&相当于一个序列点,保证在对右侧表达式进行求值时,已经递增x
条件运算符
expression1 ? expression2 : expression3
expression1为真。则执行expression2 否则执行expression3
循环辅助: continue和break
continue:跳转剩余部分开始新循环
break:结束当前循环
多重选择: switch
goto语句
不推荐使用,但是可以用来跳出一组循环
while (fun > 0)
{
for (i = 0; i < count; i++)
{
for ( j = 0; j < count; j++)
{
其他语句
if (问题)
goto help;
其他语句
}
其他语句
}
其他语句
}
其他语句
help: 语句
编写一个程序读取输入,读到#字符停止,然后报告读取的空个数、换行符数和所有其他字符的数量
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int space_ =0, cc = 0, space__ = 0;
while ((ch = getchar()) != '#')
{
if (!isspace(ch))
cc++;
else if(ch == ' ')
{
space_++;
}
else
{
space__++;
}
}
printf("其他字符:%d个\n", cc);
printf("空格:%d个\n回车%d个", space_, space__);
}
编写一个程序读取输入,到#字符停止。程序要打印每个输入的字符以及对应的ASCII码。一行打印8个字符
#include <stdio.h>
int main(void)
{
char ch;
int count = 0;
while ((ch = getchar()) != '#')
{
if (ch == '\n')
{
count =0;
printf("\n");
continue;
}
count++;
printf(" %c %d", ch, ch);
if (count == 8)
{
count = 0;
printf("\n");
}
}
}
编写一个程序,读取整数直到用户输入0。结束输入后,程序应该报告用户输入的偶数(不包括0)个数、这些偶数的平均值、输入的奇书个数及其奇数的平均值
#include <stdio.h>
int main(void)
{
int num;
int count_1 = 0, count_2 = 0;
float avg_1 = 0, avg_2 = 0;
while ((scanf("%d", &num)) == 1 && num != 0)
{
if (num % 2 == 0)
{
count_2++;
avg_2 += num;
}
else
{
count_1++;
avg_1 += num;
}
}
printf("偶数个数是:%d,平均数是:%.2f", count_2, avg_2/count_2);
printf("奇数个数是:%d,平均数是:%.2f", count_1, avg_1/count_1);
return 0;
}
使用if else 语句编写一个程序读取输入。读到#停止。用感叹号替换句号。用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
#include <stdio.h>
int main(void)
{
char ch;
int num = 0;
while ((ch = getchar()) != '#')
{
if (ch == '.')
{
putchar('!');
num++;
}
else if(ch == '!')
{
putchar('!');
putchar('!');
num++;
}
else
{
putchar(ch);
}
}
printf("\n转换了%d次", num);
return 0;
}
编写程序读取输入,到#停止。报告ei出现的次数
#include <stdio.h>
int main(void)
{
char ch;
int flag = 0;
int num = 0;
while ((ch = getchar()) != '#')
{
if (ch == 'e')
{
flag = 1;
}
else if (flag == 1)
{
flag = 0;
if (ch == 'i')
{
num++;
}
}
}
printf("%d", num);
return 0;
}
编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。做如下假设;
#include <stdio.h>
#define MONEY_HOUR 10
#define AMT1 300
#define AMT2 150
#define OVERTIME 1.5
#define TIME 40
#define TAX15 0.15
#define TAX20 0.20
#define TAX25 0.25
int main(void)
{
double gross;
double taxs;
double hour;
double net;
printf("请输入工作时间:");
scanf("%lf", &hour);
if (hour <= TIME)
{
gross = TIME * MONEY_HOUR;
}
else
{
gross = TIME * MONEY_HOUR + (hour - TIME) * MONEY_HOUR * OVERTIME;
}
if (gross <= AMT1)
{
taxs = gross * TAX15;
}
else if ( gross <= (AMT1+AMT2) )
{
taxs = AMT1 * TAX15 + TAX20 * (gross - AMT1);
}
else
{
taxs = AMT1 * TAX15 + TAX20 * AMT2 + (gross - AMT1 - AMT2) * TAX25;
}
net = gross - taxs;
printf("gross:$%.2f; taxs:$%.2f; net: $%.2f\n", gross, taxs, net);
return 0;
}
修改练习7的假设a,让程序可以给出一个供选择的工资等级单。
#include <stdio.h>
#define AMT1 300
#define AMT2 150
#define OVERTIME 1.5
#define TIME 40
#define TAX15 0.15
#define TAX20 0.20
#define TAX25 0.25
#define SIGN "***************************"
#define QUIT 5
int main(void)
{
double taxes, gross, net, time, base;
int code = 0;
help:while (code != QUIT)
{
printf("%s",SIGN);
printf("%s\n", SIGN);
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("%s%s\n",SIGN, SIGN);
printf("请输入数字1~5:");
scanf("%d", &code);
switch (code)
{
case 1:
base = 8.75;
break;
case 2:
base = 9.33;
break;
case 3:
base = 10.00;
break;
case 4:
base = 11.20;
break;
default:
goto help;
}
printf("请输入工作时间:");
scanf("%lf", &time);
if (time <= TIME)
{
gross = time * base;
}
else
{
gross = TIME * base + (time - TIME) * base;
}
if (gross <= AMT1)
{
taxes = TAX15 * gross;
}
else if( gross <= AMT1 + AMT2)
{
taxes = TAX15 * AMT1 + (gross - AMT1) * TAX20;
}
else
{
taxes = TAX15 * AMT1 + TAX20 * AMT2 + (gross - AMT1 - AMT2) * TAX25;
}
net = gross - taxes;
printf("gross: $%.2lf; taxes: $%.2lf; net:$%.2lf\n\n", gross, taxes, net);
}
return 0;
}
编写一个整数只接受正整数输入,然后显示所有小于或等于该数的素数;
#include <stdio.h>
void main(void)
{
unsigned int num;
int div, flag;
printf("请输入正整数:");
scanf("%d", &num);
for ( ; num > 1; num--)
{
for ( div = 2, flag = 1; (div * div) <= num; div++)
{
if (num % div == 0)
{
flag = 0;
}
}
if (flag == 1)
{
printf("%u ", num);
}
}
}
1988年的美国联邦税收计划是近代最简单的税收方案。他分为4个类别。每个类别有两个等级。下面是该税后计划的摘要
#include <stdio.h>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
#define DISCOUNT 0.05
#define POUNDS0_5 6.5
#define POUNDS5_20 14
#define POUNDS20 0.5
#define SIGN "************************"
void main(void)
{
double A_pounds = 0.0, B_pounds = 0.0, C_pounds = 0.0;
double pounds;
char ch = 'x';
double gross, discount = 0, trans, A, B, C, Algross;
help: while (ch != 'q')
{
printf("%s%s\n", SIGN, SIGN);
printf("a) ARTICHOKE $2.05 b) BEET $1.15\n");
printf("c) CARROT $1.09 q) quit\n");
printf("%s%s\n", SIGN, SIGN);
printf("请输入选项(输错不管):");
scanf("%s", &ch);
switch (ch)
{
case 'a':
printf("Weight of input ARTICHOKE:");
scanf("%lf", £s);
A_pounds += pounds;
printf("ARTICHOKE:%.2lfP\n", A_pounds);
break;
case 'b':
printf("Weight of input BEET:");
scanf("%lf", £s);
B_pounds += pounds;
printf("BEET:%.2lfP\n", A_pounds);
break;
case 'c':
printf("Weight of input CARROT:");
scanf("%lf", £s);
C_pounds += pounds;
printf("CARROT:%.2lfP\n", A_pounds);
break;
default:
goto help;
}
}
A = ARTICHOKE * A_pounds;
B = BEET * B_pounds;
C = CARROT * C_pounds;
gross = A + B + C;
if (gross >= 100)
{
discount = gross * DISCOUNT;
}
pounds = A_pounds + B_pounds + C_pounds;
if (pounds <= 5)
trans = 6.5;
else if (pounds <= 20)
{
trans = 14;
}
else
{
trans = 14 + (pounds - 20) * 0.5;
}
Algross = gross - discount + trans;
printf("物品售价\t订购重量(磅)\t订购蔬菜费用\t折扣\t运费和包装\t需付金额\n");
printf("ARTICHOKE\t%12.2lf\t%12.2lf\n", A_pounds, A);
printf("BEET \t%12.2lf\t%12.2lf\n", B_pounds, B);
printf("ARTICHOKE\t%12.2lf\t%12.2lf\n", C_pounds, C);
printf("\t\t\t\t\t%12.2lf\t%8.2lf\t%8.2lf\n", discount, trans, Algross);
}