C控制语句:分支和跳转

小技巧:程序return前加个getchar();可以让程序停住。%%可以打印使printf()中打印出%号


#include<stdio.h>
#define SPACE ''
int main(void)
{
char ch;

ch = getchar();
while(ch != ' ')
{
if(ch == SPACE)
putchar(ch);
else
putchar(ch+1);
ch = getcahr();

}
putchar(ch); //打印换行符
return 0;

}


//ctype.h系列字符函数

#include<stdio.h>
#include<ctype.h>
int main(void)
{
char ch ;

while((ch = getchar()) != ' ')
{
if(isalpha(ch)) //如果是字母
putchar(ch+1);
else
putchar(ch);

}
putchar(ch);
return 0;


}

//使用嵌套if显示一个数的约数
/*
这段代码的可取之处在于用了_Bool类型,定义了一个判断变量isprime
*/

#include<stdio.h>
#include<stdbool.h>
#include<math.h>
int main(void)
{
unsigned long num;
unsigned long div;
bool isprime;

printf("Please enter an integer for analysis: ");
printf("Enter q to quit. ");
while(scanf("%lu",&num)==1)
{
for(div = 2,isprime = true;div <= sqrt(num);div++)
{
if(num % div == 0)
{
if(div*div != num)
printf("%lu is divisible by %lu and %lu. ",
num,div,num/div);
else
printf("%lu is divisible by %lu. ",num,div);
isprime = false;
}

}

if(isprime)
printf("%lu is prime. ",num);
printf("Please enter another integer for analysis: ");
printf("Enter q to quit. ");

}
printf("Bye. ");
return 0;


}

//使用逻辑与运算符
#include<stdio.h>
#define PERIOD '.'
int main(void)
{
int ch;
int charcount = 0;

while((ch = getchar()) != PERIOD)
{
if(ch !='"' && ch != ' ') // "是不要号的啊!
charcount++;


}
printf("There are %d non-quote characters. ",charcount);

return 0;

}

&&,||,!的说明
/*
1. !的优先级高于乘法运算,和增量运算符的优先级相同,仅次于圆括号。&&的优先级高于|| ,这两者的优先级都低于关系运算符而高于赋值运算。
2. &&和||是序列的分界点,因此在程序从一个操作数进入下一个操作数前,所有的副作用都会生效
3. C保证一旦发现某个元素使表达式整体无效,求值将立刻停止
4. if(90<range<100)这样写是错的!
*/

//统计字符、单词和行
#include<stdio.h>
#include<ctype.h>
//#include<stdbool.h>
#define STOP '|'
int main(void)
{
char c; //读入的字符
char prev; //前一个读入字符
long n_char = 0L; //字符数
int n_lines = 0; //行数
int n_words = 0; //单词数
int p_lines =0; //不完整的行数
bool inward =false; //如果c在一个单词中,则inward等于true

printf("Enter text to be analyzed(| to terminate): ");
prev = ' ';

while((c = getcahr())!=STOP)
{
n_char++;

if(c == ' ')
n_lines++;

if(!isspace(c) && !inword) //不是空格,并且c不处于一个单词里
{
inword = true;
n_words++;
}

if(isspace(c) && inword)
inword = false;

prev = c;

}
if(prev !=' ')
p_line = 1;

printf("Characters = %ld,words = %d,lines = %d,",n_char,n_words,n_lines);
printf("partial lines = %d ",p_lines);

return 0;


}


条件运算符的使用
/*
1.用于表达式中赋值
2.用于printf()中的参数:printf("%d",cans == 1?"can":"cans");
*/

switch:
/*
1.程序按照expression值跳转到相应的case标签处,然后程序流程继续通过所有剩余的语句,直到再次由break语句重定向。
expression和case标签必须都是整数型(包括类型char),并且标签必须是常量或者完全由常量组成的表达。
*/




continue和break:
1.break语句用于循环和switch中,而continue仅用于循环(switch在循环中可以用,但效果与在其他循环中相同)

跳过输入行的剩余部分:
while(getchar() != ' ')
continue;

goto:
1.出现故障时从一组嵌套的循环中跳出

//使用多重标签
#include<stdio.h>
int main(void)
{
char ch;
int a_ct,e_ct,i_ct,o_ct,u_ct;

a_ct = e_ct = i_ct = o_ct = u_ct = 0;

printf("Enter some text: enter # to quit. ");
while((ch = getchar()) != '#')
{
switch(ch)
{
case 'a':
case 'A': a_ct++;
break;
case 'e':
case 'E': e_ct++;
break;
case 'i':
case 'I': i_ct++;
break;
case 'o':
case 'O': o_ct++;
break;
case 'u':
case 'U': u_ct++;
break;

default: break;
}
}

printf("number of vowels:A E I O U ");
printf(" %-4d %-4d %-4d %-4d %-4d ",
a_ct,e_ct,i_ct,o_ct,u_ct);

getchar();

return 0;

}


---------------------------------------------------------字符输入/输出和输入确认----------------------------------------------------------------------------

缓冲区:
/*
1.延迟回显是缓冲(buffered)输入的实例,输入的字符被收集并存储在一个被称作缓冲区(buffer)的临时存储区域内,按下回车键可使您键入的字符块对程序变得有用。
2.交互式程序:如游戏,使用非缓冲输入,
3.缓冲分为:完全缓冲和行缓冲,完全缓冲在缓冲区满时进行清空,行缓冲在遇到一个换行符时就清空缓冲区:如键盘输入;
4.#include<conio.h>中getch()可用于不回显,getche()用于回显

5.低级I/O包--宿主操作系统的基本文件工具
标准I/O包--包括创建用于处理文件的I/O函数的标准模型和标准集

6.使用标准I/O包,就屏蔽了系统差异,I/O函数自动进行转化


7.系统识别文件结尾的方法不同,C让getchar()函数在到达文件结尾时返回一个特殊值,而不去管操作系统如何检测文件结尾的

*/

/*
从键盘模仿文件结尾的条件,不同系统发送文件尾信号的方式不同。
在cmd下cl 命令编译文件,指定时要说明文件的类型,exe文件不需要
文件的结束符号不同
*/

#include<stdio.h>
int main(void)
{
int ch;

while((ch = getchar()) != EOF)
putchar();
return 0;

}


//打开一个文件并显示它的内容
#include<stdio.h>
#include<stdlib.h> //为了使用exit()
int main(void)
{
int ch;
FILE * fp;
char fname[50];

printf("Enter the name of the file: ");
scanf("%s",fname);
fp = fopen(fname,"r");
if(fp == NULL)
{
printf("Failed to open file.Bye ");
exit(1);
}

while((ch = getc(fp)) != EOF) //getc读取文件的字符
putchar(ch);
fclose(fp);

return 0;
}

//确认输入指的是确认用户的输入在掌握中,比如用scanf的返回值,if语句等

//菜单浏览的程序--主要练规范
#include<stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);

int main(void)
{
int choice;

while((choice = get_choice()) != 'q')
{
switch(choice)
{
case 'a':printf("Bye low,sell high. ");
break;
case 'b':putchar('a');
break;
case 'c':count();
break;
default:printf("Program error! ");
break;
}
}
printf("Bye. ");
getchar();
return 0;
}


char get_choice(void)
{
int ch;

printf("Enter the letter of your choice: ");
printf("a.advice b.bell ");
printf("c.count d.quit ");
ch = get_first();
while((ch < 'a' || ch > 'c') && ch !='q')
{
printf("Please respond with a,b,c,or q. ");
ch = get_first();
}
return ch;
}


//选取第一个输入的字符并且清空缓冲区
char get_first(void)
{

int ch;

ch = getchar();
while(getchar() != ' ')
continue;
return ch;
}

//打印选项c结果
void count(void)
{
int n, i;

printf("Count how far? Enter an integer: ");
n = get_int();
for(i = 1;i <= n; i++)
printf("%d ",i);
while(getchar()!=' ') //防止返回时影响后面函数--缓存区每清零
continue;
}

//防止随便输入
int get_int(void)
{
int input;
char ch;

while(scanf("%d",&input) != 1) //不会就一直执行
{
while((ch = getchar()) != ' ')
putchar(ch);
printf(" is not an integer. Please enter an ");
printf("integer value,such as 25,-175,or 3: ");

}
return input;

}

原文地址:https://www.cnblogs.com/zxj-262410/p/6691238.html