解析如何利用switch语句进行字符统计

#include <stdio.h>
void cotTime();
main()
{
cotTime();
}
void cotTime()
{
int c, i, nwhite, nother, ndigit[10];
nwhite = nother = 0;
for(i=0;i<10;i++)
{
ndigit[i] = 0;
}
while((c = getchar()) != EOF)
{
switch(c)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
/*一种哈希的思想,从观察数组元素值为多少的方法中确定某一数字出现的次数
可以演变为一种较快地去重算法,不用先排序后去重,牺牲了空间性能,但是提高了时间性能*/
ndigit[c-'0'] += 1;
break;

case ' ': case '/t': case '/n':
nwhite++; break;

default:
nother++;
break;
}
}
printf("digits = ");
for(i=0;i<10;i++)
{
printf(" %d",ndigit[i]);
}
printf(", white space = %d, other = %d/n", nwhite, nother);
return 0;
}

时间: 2024-08-17 08:10:25

解析如何利用switch语句进行字符统计的相关文章

解析如何利用switch语句进行字符统计_C 语言

复制代码 代码如下: #include <stdio.h>void cotTime();main(){   cotTime();}void cotTime(){  int c, i, nwhite, nother, ndigit[10];  nwhite = nother = 0;  for(i=0;i<10;i++)  {    ndigit[i] = 0;  }  while((c = getchar()) != EOF)  {    switch(c) {    case '0':

利用switch语句进行多选一判断的实例代码_php实例

实例如下: <!doctype html> <meta http-equiv="content-type" content="text/html" charset="utf-8"/> switch语句,switch语句用于根据多个不同条件执行不同动作.<br/> 如果你希望有选择地执行若干代码块之一,还请使用switch语句. <br/> 语法结构如下: <pre> switch(n)

深入解析Swift中switch语句对case的数据类型匹配的支持_Swift

Swift可以对switch中不同数据类型的值作匹配判断: var things = Any[]() things.append(0) things.append(0.0) things.append(42) things.append(3.14159) things.append("hello") things.append((3.0, 5.0)) things.append(Movie(name:"Ghostbusters", director:"Iv

《C语言及程序设计》实践项目——利用switch语句解决问题

返回:贺老师课程教学链接  [项目1:投票表决器]设计一个投票表决器,其功能是: 输入Y.y,打印agree 输入N.n,打印disagree 输入其他,打印lose 请在下面代码的基础上,将程序补充完整 #include <stdio.h> int main( ) { char c; scanf("%c",&c); ____(1)____ { case 'Y': case 'y': printf("agree"); ____(2)____; c

C语言及程序设计初步例程-26 利用switch语句解决问题

贺老师教学链接  C语言及程序设计初步 本课讲解 应用:计算运费每公里每吨货物的基本运费为p(price),货物重为w(weight),距离为s,折扣为d(discount),则总运费f(freight)的计算公式为 freight=price*weight*s*(1-discount) #include <stdio.h> int main() { int c,s; float p,w,d,f; printf("please enter p,w,s: "); scanf(

data modeling-BI 用DAX SWITCH 语句判断是否包含某个字符,怎么表示

问题描述 BI 用DAX SWITCH 语句判断是否包含某个字符,怎么表示 我有A列几万行,在data model中,我想用dax的Switch语句. 假设A列该单元格包含字母"T"或者包含"X"或者包含"P",则在B列显示"Internal sale",其他则显示"Exter sale". 重点是这个包含应该用什么表示?我用通配符*,%,都试过都不行. =switch(true(), Table1[Sol

解析C++编程中的选择结构和switch语句的用法_C 语言

C++编写选择结构的程序下面,通过两个实例来说明如何编写较为复杂的C++程序. [例]编写程序,判断某一年是否为闰年. #include <iostream> using namespace std; int main( ) { int year; bool leap; cout<<"please enter year:";//输出提示 cin>>year; //输入年份 if (year%4==0) //年份能被4整除 { if(year%100=

解析c语言switch中break语句的具体作用_C 语言

问题:break在for循环.while循环等循环流程控制中起的作用是停止执行break后面的语句,跳出本次循环,并跳出该循环控制体:在switch条件选择中,没有了循环控制,break又起什么作用呢? 解决办法:1. switch语句的执行流程是:首先计算switch后面圆括号中表达式的值,然后用此值依次与各个case的常量表达式比较,若圆括号中表达式的值与某个case后面的常量表达式的值相等,就执行此case后面的语句,执行后遇到break语句就退出switch语句,程序流程转向开关语句的下

asp.net switch语句用法(C,C#)

asp教程.net switch语句用法(C,C#) switch 语句是一个控制语句,它通过将控制传递给其体内的一个 case 语句来处理多个选择和枚举 int caseSwitch = 1; switch (caseSwitch) {     case 1:         Console.WriteLine("Case 1");         break;     case 2:         Console.WriteLine("Case 2");