// 第八章 字符输入,输出和输入验证.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int i = 0;
char ch = '\0';
void introduction();
void fun_1();
void fun_2();
void fun_3();
void fun_4();
void fun_5();
char fun_6();
void fun_8();
int main()
{
introduction();
while (scanf("%d", &i) == 1)
{
getchar();
switch (i)
{
case 1:system("cls"); fun_1(); system("cls"); break;
case 2:system("cls"); fun_2(); system("cls"); break;
case 3:system("cls"); fun_3(); system("cls"); break;
case 4:system("cls"); fun_4(); system("cls"); break;
case 5:system("cls"); fun_5(); system("cls"); break;
case 6:system("cls");
PRintf("%c",fun_6()); system("cls"); break;
case 8:system("cls"); fun_8(); system("cls"); break;
default:printf("erroe number,please reinput\n");
}
introduction();
}
}
void introduction()
{
printf("please input the number before the sentence to run the progress if you want\n""press any other key to quit\n");
printf(
"1:输入直到EOF结束\n"
"2:输入一段字符,输出字符数,每一个字符的ASCLL码,每行打印10字符\n"
"3:输入一段字符,输出其中大写字母与小写字母的输入\n"
"4:输入一段字符,输出平均每个单词的字符数\n"
"5:一个猜数字程序,使用二分法\n"
"6:输入一段字符,返回第一个非空白字符\n"
"7:略\n"
"8:支持加减乘除的计算机\n");
}
void fun_1()
{
int i = 0;
char str[100];
printf("please input \"ctrl + z\" at last new row\n");
for (; (str[i] = getchar()) != EOF; i++)
{
;
}
str[i + 1] = '\0';
puts(str);
system("pause");
}
void fun_2()
{
int i = 0;
char str[100];
printf("please input \"ctrl + z\" at last new row\n");
for (; (str[i] = getchar()) != EOF; i++)
{
;
}
str[i + 1] = '\0';
int i_count_char = strlen(str);
for (i = 0; i < i_count_char; i++)
{
printf("%c ",str[i]);
if ((i + 1) % 10 == 0) { printf("\n"); }
} printf("\n");
for (i = 0; i < i_count_char; i++)
{
if ((i + 1) % 10 == 0) { printf("\n"); }
printf("%d ", str[i]);
}
system("pause");
}
void fun_3()
{
int i_lower_alphabet = 0, i_upper_alphabet = 0;
char str[100]="\0";
for(i=0;(str[i]=getchar())!=EOF;i++)
{
if (str[i]>='A'&&str[i]<='Z') { i_upper_alphabet++; }
if (str[i] >= 'a'&&str[i] <= 'z'){ i_lower_alphabet++; }
}printf("lower aphabet:%d upper aphabet:%d", i_lower_alphabet, i_upper_alphabet);
system("pause");
}
void fun_4()
{
int i_count_
Wordnumber = 0;//单词数
int i_count_word = 0, i_count_eachword=0;//总单词字母数和单个单词字母数
char str[100]="\0";
int flag = 0;//单词内为1,其他为0,一开始为0
for (i = 0; (str[i] = getchar()) != EOF; i++)
{
if (str[i] >= 'A'&&str[i] <= 'Z'|| str[i] >= 'a'&&str[i] <= 'z')
{
if (flag == 0) { i_count_wordnumber++;}
flag = 1; i_count_eachword++; }
else { flag = 0;i_count_word+=i_count_eachword,i_count_eachword=0; }
}
printf("average alphabet in each word:%d\n", i_count_word / i_count_wordnumber);
system("pause");
}
void fun_5()
{
int* fun_5_select(int num[]);
int i_choose_num = 1000,up_limit=1000,low_limit=0;
int num[3]; num[0] = i_choose_num, num[1] = up_limit; num[2] = low_limit;
printf("welcome!please prpare a number and i will guess it\n""press any other key except 'l' and 'u'to quit\n""\nAre you ready?\n");
while(num[0]!=0)
{
fun_5_select(num);
}
}
int* fun_5_select(int num[])
{
printf("%d?\n",num[0]);
printf("if it is,input 'y\n'""if it lower,input alphabet 'l',if it upper,input 'u'\n");
scanf("%c", &ch); getchar();
if (ch == 'y')
{
printf("continue?input 'y',or press any other key\n");
if (scanf("%c", &ch) == 'y') { getchar(); num[0] = 1000; }
else { getchar(); num[0] = 0; return num; }
}
else if (ch == 'l')
{
num[1] = num[0];
num[0] = (num[1]+ num[2]) / 2;
printf("%d?\n", num[0]);
return num;
}
else if (ch == 'u')
{
num[2] = num[0];
num[0] = (num[1] + num[2]) / 2;
printf("%d?\n", num[0]);
return num;
}
else { num[0] = 0; return num; }
}
char fun_6()
{
while (1)
{
ch = getchar();
if (ch == '\0') { ; continue; }
else if (ch == ' ') { continue; }
else {
printf("%c", ch); system("pause"); return ch;
}
};
}
void fun_8()
{
int flag = 0;
int result = 0;
int num[100] = { '\0' };
char ch[100]="\0";
printf("press any other key with no relationsh
ip with calculation to quit\n");
scanf("%d", &result);
for(i=0; 1;i++)
{
if (scanf("%c", &ch[i]) != 1) { break; };
if (scanf("%d", &num[i]) != 1) {break;};
getchar();
if (ch[i] == '+') { result += num[i]; printf("%d\n", result); break;}
else if (ch[i] == '-') { result -= num[i]; printf("%d\n", result); break;}
else if (ch[i] == '*') { result *= num[i]; printf("%d\n", result);break;}
else if (ch[i] == '/') { if (num[i] <= 0) { printf("erroe\n"); system("pause"); break; }result /= num[i]; printf("%d\n", result); }
}
;
}