高级语言程序设计课程第五次个人作业
2024高级语言程序设计:https://edu.cnblogs.com/campus/fzu/2024C
高级语言程序设计课程第五次个人作业:https://edu.cnblogs.com/campus/fzu/2024C/homework/13298
学号:102400226
姓名:石华波
//8.11.1
#include <stdio.h>
int main() {
int n=0;
while(getchar()!=EOF) n++;
printf("%d",n);
return 0;
}
//8.11.2
#include <stdio.h>
#define SPACE_ASCII 32
int main(){
int i=0;
char ch;
while((ch=getchar())!=EOF) {
if(ch =='\n'||ch =='\t') printf("'\\%c'-%d ",ch=='\n'?(i=0,'n'):'t',ch);
else if(ch<SPACE_ASCII) printf("'^%c'-%d ",ch+64,ch);
else printf("'%c'-%d ",ch,ch);
if(ch!='\n'&& ++i%10==0) printf("\n");
}
return 0;
}
//8.11.3
#include <stdio.h>
#include <ctype.h>
int main(){
int Upper=0,Lower=0;
char ch;
while((ch=getchar())!=EOF) {
if(isupper(ch)) Upper++;
else if(islower(ch)) Lower++;
}
printf("%dUppers %dLowers\n",Upper,Lower);
return 0;
}
//8.11.4
#include<stdio.h>
#include<ctype.h>
int main(){
char ch,tmp;
int words=0,letters=0;
while((ch=getchar())!=EOF){
if(isalpha(ch)) letters++;
else if(ch!='\n') words++;
}
printf("%.2lf letters per word.",1.0*letters/words);
return 0;
}
//8.11.5
#include <stdio.h>
int main(){
int response;
int low=1,high=100, guess=(high+low)/2;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right, with");
printf("\na h if it is high, and with an l if it is low.\n");
printf("Uh...is your number %d?\n", guess);
while((response=getchar())!='Y'){
if(response=='\n') continue;
if(response=='H'){
high=guess - 1;
}
else if(response=='L'){
low=guess+1;
}
guess=(high+low)/2;
printf("Well, then, is it %d?\n", guess);
}
printf("I knew I could do it!\n");
return 0;
}
//8.11.6
#include <stdio.h>
char get_first(void){
int ch=getchar();
while(ch<=32) ch=getchar();
return ch;
}
int main(){
printf("%c",get_first());
return 0;
}
//8.11.7
#include<stdio.h>
#include<ctype.h>
#define EXTRA_HOUR 1.5
#define BASE_TAX 0.15
#define EXTRA_TAX 0.2
#define EXCEED_TAX 0.25
char menu();
void salary(double base_salary,double hours);
char choose();
int main(){
char ch;
double n;
while ((ch=menu())!='q'){
printf("Enter the working hours per week: ");
scanf("%lf",&n);
switch (ch){
case 'a':{
salary(8.75,n);
break;
}
case 'b':{
salary(9.33,n);
break;
}
case 'c':{
salary(10.00,n);
break;
}
case 'd':{
salary(11.20,n);
break;
}
}
putchar('\n');
}
printf("Done!\n");
return 0;
}
char choose(){
char ch;
do{
ch = getchar();
} while (isspace(ch));
return ch;
}
char menu(){
char ch;
printf("********************************************************\n");
printf("Enter an alpha to choose the desired pay rate or action:\n");
printf("a) $8.75/hr b) $9.33/hr\n");
printf("c) $10.00/hr d) $11.20/hr\n");
printf("q) quit\n");
printf("********************************************************\n");
printf("Enter an alpha(q to quit): ");
ch=choose();
while (ch!='a'&&ch!='b'&&ch!='c'&&ch!='d'&&ch!='q'){
printf("Please enter a suitable alpha:");
ch=choose();
}
return ch;
}
void salary(double base_salary,double hours)
{
double tax,taxed_salary;
double salary=(hours>40?(40+1.5*(hours-40)):hours)*base_salary;
if (salary<=300) tax=salary*BASE_TAX;
else if (salary<=450) tax=300*BASE_TAX+(salary-300)*EXTRA_TAX;
else tax=300*BASE_TAX+150*EXTRA_TAX+(salary-450)*EXCEED_TAX;
taxed_salary=salary-tax;
printf("Untaxed salary: $%g\n",salary);
printf("Tax: $%g\n", tax);
printf("Taxed salary): $%g\n",taxed_salary);
}
//8.11.8
#include <stdio.h>
void menu();
float numget();
int main(){
char ch;
float num1,num2;
menu();
ch=getchar();
while(ch!='q'){
while(ch!='a'&&ch!='s'&&ch!='m'&&ch!='d'&&ch!='q'){
printf("Enter a suitable choice.\n");
menu();
getchar();
ch=getchar();
}
if(ch=='q') break;
printf("Enter first number:");
num1=numget();
printf("Enter second number:");
num2=numget();
while(num2==0&&ch=='d'){
printf("Zero can't be used here,please enter a proper number:");
num2=numget();
}
if(ch=='a') printf("%f+%f=%f\n",num1,num2,num1+num2);
else if(ch=='s') printf("%f-%f=%f\n",num1,num2,num1-num2);
else if(ch=='m') printf("%f*%f=%f\n",num1,num2,num1*num2);
else printf("%f/%f=%f\n",num1,num2,num1/num2);
menu();
getchar();
ch=getchar();
}
printf("Bye.\n");
return 0;
}
void menu(){
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\nq. quit\n");
}
float numget(){
float num;
while(scanf("%f",&num)!=1){
printf("That's not a number,please enter a proper number:");
}
return num;
}
//9.11.1
#include <stdio.h>
double min(double x,double y);
int main(){
double x=0.26,y=0.02;
printf("%lf",min(x,y));
return 0;
}
double min(double x,double y){
return x<y?x:y;
}
//9.11.2
#include <stdio.h>
void chline(char ch,int i,int j);
int main(){
char ch='A';
int i=3,j=4;
chline(ch,i,j);
return 0;
}
void chline(char ch,int i,int j){
for(int m=0;m<i;m++){
for(int n=0;n<j;n++) putchar(ch);
putchar('\n');
}
}
//9.11.3
#include <stdio.h>
void chline(char ch,int i,int j);
int main(){
char ch='A';
int i=3,j=4;
chline(ch,i,j);
return 0;
}
void chline(char ch,int i,int j){
for(int m=0;m<j;m++){
for(int n=0;n<i;n++) putchar(ch);
putchar('\n');
}
}
//9.11.4
#include <stdio.h>
double harave(double x,double y);
int main(){
double x=2,y=3;
printf("%lf",harave(x,y));
return 0;
}
double harave(double x,double y){
return 1/(1/x+1/y);
}
//9.11.5
#include <stdio.h>
void large_of(double *x,double *y);
int main(){
double x=2,y=3;
large_of(&x,&y);
printf("%lf %lf",x,y);
return 0;
}
void large_of(double *x,double *y){
*x=*x>*y?*x:*y;
*y=*y>*x?*y:*x;
}
//9.11.6
#include <stdio.h>
void sort(double *x,double *y,double *z);
int main(){
double x=5,y=4,z=3;
sort(&x,&y,&z);
printf("%lf %lf %lf",x,y,z);
return 0;
}
void sort(double *x,double *y,double *z){
double temp;
if(*x>*y){
temp=*x;
*x=*y;
*y=temp;
}
if(*y>*z){
temp=*y;
*y=*z;
*z=temp;
}
if(*x>*y){
temp=*x;
*x=*y;
*y=temp;
}
}
//9.11.7
#include <stdio.h>
#include <ctype.h>
void read();
int alpha(char ch);
int main(){
read();
return 0;
}
void read(){
char ch;
while((ch=getchar())!=EOF){
if(ch=='\n') continue;
if(alpha(ch)==-1) printf("%c not an alpha.\n",ch);
else printf("%c is %d\n",ch,alpha(ch));
}
}
int alpha(char ch){
int poi=-1;
if(isalpha(ch)){
ch=toupper(ch);
poi=(ch-64)%26;
}
return poi;
}
//9.11.8
#include <stdio.h>
double power(double x,int n);
int main(){
double x1=3,x2=0;
int n1=0,n2=-3,n3=5;
printf("3^0=%lf 3^-3=%lf 3^5=%lf 0^0=%lf 0^-3=%lf\n",power(x1,n1),power(x1,n2),power(x1,n3),power(x2,n1),power(x2,n2));
}
double power(double x,int n){
double y=x;
if(x==0&&n==0) return 1;
else if(x==0) return 0;
else if(n==0) return 1;
else{
if(n>0) for(int i=1;i<n;i++) x*=y;
else for(int i=n;i<=0;i++) x/=y;
}
return x;
}
//9.11.9
#include <stdio.h>
double power(double x,int n);
int main(){
double x1=3,x2=0;
int n1=0,n2=-3,n3=5;
printf("3^0=%lf 3^-3=%lf 3^5=%lf 0^0=%lf 0^-3=%lf\n",power(x1,n1),power(x1,n2),power(x1,n3),power(x2,n1),power(x2,n2));
}
double power(double x,int n){
if(x==0&&n==0) return 1;
else if(x==0) return 0;
else if(n==0) return 1;
else{
if(n>0) return x*power(x,n-1);
else return power(x,n+1)/x;
}
return x;
}
//9.11.10
#include <stdio.h>
void to_base_n(unsigned long n,int base);
int main(){
to_base_n(129,8);
return 0;
}
void to_base_n(unsigned long n,int base){
int r;
r=n%base;
if(n>=base) to_base_n(n/base,base);
printf("%d",r);
}
//9.11.11
#include <stdio.h>
long long Fibionacii(int n);
int main(){
for(int i=1;i<=20;i++) printf("%lld\n",Fibionacii(i));
return 0;
}
long long Fibionacii(int n){
int a=1,b=1;
for(int i=2;i<n;i++){
b=a+b;
a=b-a;
}
return b;
}