1.
#include <stdio.h>
int main(void)
{
char ch;
int i=0;
while ((ch=getchar())!=EOF)
i++;
printf("%d\n",i);
return 0;
}
2.(没太看懂题)
#include <stdio.h>
int main (void)
{
char ch;
int i;
while ((ch = getchar()) != EOF)
{
if (ch<' ')
switch(ch)
{
case '\n':
printf("\\n %d ",ch);
break;
case '\t':
printf("\\t %d ",ch);
break;
default:
printf("^%c %d ",ch+64,ch);
break;
}
else
printf("%c %d ",ch,ch);
if (i++ == 9)
{
printf("\n");
i = 0;
}
}
return 0;
}
3.
#include <ctype.h>
int main(void)
{
char ch;
int n_lowercase,n_uppercase;
n_lowercase=n_uppercase=0;
while ((ch=getchar())!=EOF)
{
if (islower(ch)!=0) //islower 参数为小写字母,返回值为真
n_lowercase++;
else if (isupper(ch)!=0) //isupper 参数为大写字母,返回值为真
n_uppercase++;
else
continue;
}
printf("lowercase :%d uppercase :%d \n",n_lowercase,n_uppercase);
return 0;
}
4.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int flag,n,count;
flag=n=count=0; //ch在单词中,flag=0
while ((ch=getchar())!=EOF)
{
if(!isspace(ch)&&!ispunct(ch)&&!flag)
{
n++;
flag=1; //开始一个新的单词
}
if(isspace(ch)||ispunct(ch)&&flag) //标点符号或空白符号
{
flag=0; //单词的尾部
}
if (isalpha(ch))
count++;
}
printf(" average %lf \n",(double)count/(double)n);
return 0;
}
5.
#include <stdio.h>
int main(void)
{
int uppe_limit,lower_limit,guess;
char response;
guess=50;uppe_limit=101;lower_limit=0;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right.\n");
printf("Respond with l if my guess is too large,\n"
"Respond with s if my guess is too small\n");
printf("Uh...is your number %d?\n",guess);
while ((response=getchar()) !='y')
{
if (response=='l')
{
uppe_limit=guess;
guess=(uppe_limit+lower_limit)/2;
}
if (response=='s')
{
lower_limit=guess;
guess=(uppe_limit+lower_limit)/2;
}
while ((getchar())!='\n')
continue;
printf("Is the number %d correct or is it too large or too small? \n"
,guess);
}
printf("I knew I could do it!\n");
return 0;
}
6.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
ch=getchar();
while (isspace(ch))
ch=getchar();
while (getchar()!='\n')
continue;
putchar(ch);
return 0;
}
/前面行得通,不知道为什么作为调用函数运行不了?求大佬解释
#include <stdio.h>
#include <ctype.h>
char get_first (void);
int main(void)
{
char a;
a=get_first();
printf("%c\n",a);
return 0;
}
char get_fisrt (void)
{
int ch;
ch=getchar();
while (isspace(ch))
ch=getchar();
while (getchar()!='\n')
continue;
return ch;
}
7.
#include <stdio.h>
#define TAXRATE1 0.15 //税率15%
#define TAXRATE2 0.2
#define TAXRATE3 0.25
#define WAGE1 8.75 //工资等级
#define WAGE2 9.33
#define WAGE3 10.00
#define WAGE4 11.20
void wage_menu (void); //菜单
void star (void); //*****
int main(void)
{
int flag;
char ch;
double hours,salary,taxes,net_income,per_hour;
// 工作时间、工资总额、税金、净收入、工资等级
salary=taxes=net_income=0;
flag=1;
star();
wage_menu();
star();
ch=getchar();
while(flag)
{
switch (ch)
{
case '1':
per_hour = WAGE1;
printf("How many hours do you work a week?\n");
scanf("%lf", &hours);
flag=0;
break;
case '2':
per_hour = WAGE2;
printf("How many hours do you work a week?\n");
scanf("%lf", &hours);
flag=0;
break;
case '3':
per_hour = WAGE3;
printf("How many hours do you work a week?\n");
scanf("%lf", &hours);
flag=0;
break;
case '4':
per_hour = WAGE4;
printf("How many hours do you work a week?\n");
scanf("%lf", &hours);
flag=0;
break;
case 'q':
return 0;
default:
while ((ch=getchar())!='\n')
;
printf("please enter number from 1 to 4 (q to quit): \n");
ch=getchar();
}
}
if (hours<=40)
salary=hours*per_hour;
else
salary=40*per_hour+(hours-40)*per_hour*1.5;
if (salary<=300)
{
taxes=salary*TAXRATE1;
net_income=salary-taxes;
}
else if (salary<=450)
{
taxes=300*TAXRATE1+(salary-300)*TAXRATE2;
net_income=salary-taxes;
}
else
{
taxes=300*TAXRATE1+150*TAXRATE2+(salary-450)*TAXRATE3;
net_income=salary-taxes;
}
printf("salary: %lf taxes: %lf net income:%lf \n"
,salary,taxes,net_income);
return 0;
}
void wage_menu(void)
{
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
}
void star (void )
{
int i;
for (i=0;i<65;i++)
printf("*");
printf("\n");
}
8.
#include <stdio.h>
char get_choice (void );
char get_fisrt (void);
float get_float1(void);
float get_float2(void);
float Additon (void);
float Subtraction(void);
float Multiplication(void);
float Division(void);
int main(void)
{
int choice;
while ((choice=get_choice()) != 'q')
{
switch (choice)
{
case 'a':
Additon();
break;
case 's':
Subtraction();
break;
case 'm':
Multiplication();
break;
case 'd':
Division();
break;
case 'q':
printf("Bye.\n");
return 0;
default:
printf("Please enter a,s,m,d or q.\n");
break;
}
}
return 0;
}
char get_choice (void)
{
int ch;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
ch=get_fisrt();
return ch;
}
char get_fisrt (void)
{
int ch;
ch=getchar();
while (getchar() !='\n')
continue;
return ch;
}
float get_float1(void)
{
float input;
char ch;
printf("Enter first number: ");
while (scanf("%f",&input) != 1 )
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
return input;
}
float get_float2(void)
{
float input;
char ch;
printf("Enter second number: ");
while (scanf("%f",&input) != 1)
{
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not a number.\n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
return input;
}
float Additon (void)
{
float a,b,c;
a = get_float1();
b = get_float2();
c=a+b;
printf("%.1f + %.1f = %.1f\n",a,b,c);
return c;
}
float Subtraction(void)
{
{
float a,b,c;
a = get_float1();
b = get_float2();
c = a-b;
printf("%.1f - %.1f = %.1f\n",a,b,c);
return c;
}
}
float Multiplication(void)
{
float a,b,c;
a = get_float1();
b = get_float2();
c = a*b;
printf("%.1f * %.1f = %.1f\n",a,b,c);
return c;
}
float Division(void)
{
float a,b,c;
a = get_float1();
b = get_float2();
if (b== 0)
{
printf("Enter a number other than 0: \n");
b = get_float2();
}
c = a/b;
printf("%.1f / %.1f = %.1f\n",a,b,c);
return c;
}
为什么 Enter second number 前面会出现空格?? 请大佬解释解释