我是靠谱客的博主 现实火,最近开发中收集的这篇文章主要介绍C Primer Plus 第八章 编程练习 1-8题,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

第一题
#include<stdio.h>
int main(void)
{
int Counts = 0;
char Letters;
printf("Please Input:n");
while((Letters = getchar()) != EOF)
++Counts;
printf("You Had Inputed %d Letters.",Counts);
return 0;
}


第二题
#include<stdio.h>
const int LenColumn = 10;
const int MaxLen = 100;
int main(void)
{
char Letters;
char Words[MaxLen];
int Counts = 0;
printf("PLease Input Words To Test:n");
while((Letters = getchar()) != EOF)
{
Words[Counts] = Letters;
++Counts;
}
Words[Counts] = '';
for(int i = 0 ; i < Counts ; ++i)
{
if(Words[i] == 'n')
printf("\n/%d ",Words[i]);
else if(Words[i] == 't')
printf("\t/%d ",Words[i]);
else if(Words[i] == ' ')
printf("\b/%d ",Words[i]);
else printf("%c/%d ",Words[i],Words[i]);
if((i + LenColumn) % LenColumn == 0 && i != 0)
printf("n");
}
return 0;
}


第三题
#include<stdio.h>
#include<ctype.h>
int main(void)
{
int UpperCounts = 0;
int LowerCounts = 0;
char Letters;
printf("Please Input Word To Test:n");
while((Letters = getchar()) != EOF)
{
if(isupper(Letters))
++UpperCounts;
if(islower(Letters))
++LowerCounts;
}
printf("%d Lower And %d Upper.n",LowerCounts,UpperCounts);
return 0;
}


第四题
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
int main(void)
{
int WordsCounts = 0;
int LettersCounts = 0;
bool isWord = false;
char Letters;
printf("PLease Input Words To Test:n");
while((Letters = getchar()) != EOF)
{
if(isalpha(Letters))
{
isWord = true;
++LettersCounts;
}
if(!isalpha(Letters) && isWord == true)
{
++WordsCounts;
isWord = false;
}
}
printf("We Get %d Words And %d Letters.n",WordsCounts,LettersCounts);
printf("Every Word Have %0.2lf Letters.n",(double)LettersCounts/WordsCounts);
return 0;
}


第五题
#include<stdio.h>
int main(void)
{
printf("I Guess 50n");
int realResult = 50;
char ch;
int MAX = 100;
int MIN = 0;
while((ch = getchar()) != 'y')
{
if(ch == 'u')
//to up
MIN = realResult;
else if(ch == 'l')
//to low
MAX = realResult;
else
printf("Sorry ,I Can't UnderStand %c",ch);
printf("I Guess %d",(MAX+MIN)/2);
realResult = (MAX + MIN) / 2;
getchar();
}
printf("I Get Real Result %d.",realResult);
return 0;
}


第六题
#include<stdio.h>
const char Space = ' ';
int get_first(void);
int main(void)
{
char showChar;
showChar = get_first();
printf("%c",showChar);
return 0;
}
int get_first(void)
{
char ch;
while((ch = getchar()) == Space)
continue;
return ch;
}


第七题
#include<stdio.h>
#include<stdbool.h>
#include<iso646.h>
#define PayHour 40.0
#define PayLevel_1 300.0
#define PayLevel_2 450.0
int main(void)
{
bool on_off = true;
bool Choose = true;
char HR;
double WageHour;
while(on_off)
{
printf("**********************************************n");
printf("A)8.75/hr
B)9.33/hrn");
printf("C)10.00/hr
D)11.20/hrn");
printf("Q)Quitn");
printf("**********************************************n");
//Create menu
scanf("%c",&HR);
getchar();
switch(HR)
{
case 'A':WageHour = 8.75;
break;
case 'B':WageHour = 9.33;
break;
case 'C':WageHour = 10.00;
break;
case 'D':WageHour = 11.20;
break;
case 'Q':on_off = false;
printf("Done!");
break;
default:printf("There.s A Wrong,Please Try Input Again.n");
Choose = false;
}
if(Choose == false or on_off == false)
//choose 5 or 4
continue;
double WorkTime;
printf("Please Input Your Hours:n");
scanf("%lf",&WorkTime);
getchar();
if(WorkTime > PayHour)
WorkTime = (WorkTime-PayHour) * 1.5 + PayHour;
double Wages = WorkTime * WageHour;
double Level_1 = PayLevel_1 * (1-0.15);
double Level_2 = (PayLevel_2 - PayLevel_1) * (1-0.2);
if(Wages < PayLevel_1)
Wages = Wages * (1-0.15);
else
if(Wages >= PayLevel_1 && Wages < PayLevel_2)
Wages = Level_1 + (Wages-PayLevel_1) * (1-0.2);
else
if(Wages >= PayLevel_2)
Wages = Level_1 + Level_2 +(Wages-PayLevel_2) * (1-0.25);
printf("Your Wages Are %.2lfn",Wages);
}
return 0;
}


第八题
#include<stdio.h>
#include<stdbool.h>
void Counts(char types);
int main(void)
{
bool on_off = true;
while(on_off)
{
char Choice;
printf("Please Input Your Choice:n");
printf("a.add s.subtractn");
printf("m.multiply d.dividen");
printf("q.quitn");
scanf("%c",&Choice);
getchar();
switch(Choice)
{
case 'a':Counts(Choice);
break;
case 's':Counts(Choice);
break;
case 'm':Counts(Choice);
break;
case 'd':Counts(Choice);
break;
case 'q':printf("Bye.n");
on_off = false;
break;
default:printf("Bye!n");
on_off = false;
}
}
return 0;
}
void Counts(char types)
{
double FirstNumber;
double SecondNumber;
printf("Input First Number:n");
while(!scanf("%lf",&FirstNumber))
{
printf("Input First Number:n");
getchar();
}
printf("Input Second Number:n");
while(!scanf("%lf",&SecondNumber) || (SecondNumber == 0 && types == 'd'))
{
printf("Input Second Number:n");
getchar();
}
switch(types)
{
case 'a':printf("%.2lf + %.2lf = %.2lfn",FirstNumber,SecondNumber,FirstNumber+SecondNumber);
break;
case 's':printf("%.2lf - %.2lf = %.2lfn",FirstNumber,SecondNumber,FirstNumber-SecondNumber);
break;
case 'm':printf("%.2lf * %.2lf = %.2lfn",FirstNumber,SecondNumber,FirstNumber*SecondNumber);
break;
case 'd':printf("%.2lf / %.2lf = %.2lfn",FirstNumber,SecondNumber,FirstNumber/SecondNumber);
break;
}
}


最后

以上就是现实火为你收集整理的C Primer Plus 第八章 编程练习 1-8题的全部内容,希望文章能够帮你解决C Primer Plus 第八章 编程练习 1-8题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部