本文仅为记录自己手打编程题答案所用,不能保证正确性,欢迎大家学习交流,有问题请随时指出~
1、原版:
#include<stdio.h>
int main(void)
{
int i = 0;
while (getchar() != '#')
{
i++
}
printf("the number of the chars is:%d", i);
return 0;
}
理解错题意了QAQ,应该是各求空格数,换行符数,和其他字符的数目,而不是合在一起的总数。懒得修改了,用c=getchar(),c==' ' and c=='\n'来做。
2、
#include<stdio.h>
int main(void)
{
int i=0;
char c;
while ((c = getchar() )!= '#') //简便getchar算法
{
i++;
printf("%c-%d,", c, c);
if (i % 8 ==0)
printf("\n");
}
return 0;
}
3、
#include<stdio.h>
int main(void)
{
int c[50], db = 0, sg = 0;
float db_av=0, sg_av=0;
printf("Please enter ints(0 to quit):");
for(int i=0;i<=50;i++)
{
scanf_s("%d", &c[i]);
if (c[i]==0)
{
printf("Done!\n");
break;
}
else if (c[i]%2==0)
{
db++;
db_av += c[i];
}
else
{
sg++;
sg_av += c[i];
}
}
if (db != 0)
{
db_av /= db;
printf("There are %d even numbers, and their average number is :%.2f\n", db, db_av);
}
else
printf("There is no even number.\n");
if (sg != 0)
{
sg_av /= sg;
printf("There are %d odd numbers, and their average number is %.2f\n", sg, sg_av);
}
else
printf("There is no odd number.\n");
return 0;
}
我不知道为啥当初这里用了一个数组来装。。直接用while(scanf_s(“%d”,&i))就好了,可能是为了这样不用每次输入都回车,直接用空格空开每个数就好了?
改版:
#include<stdio.h>
int main(void)
{
int i, db = 0, sg = 0;
float db_sum = 0, sg_sum = 0;
printf("Please enter ints(0 to quit):");
while (scanf_s("%d", &i))
{
if (i==0)
{
break;
}
else if (0 == (i % 2))
{
db++;
db_sum += i;
}
else
{
sg++;
sg_sum += i;
}
}
printf("the average value of %d singular number is: %f; the average value of %d double number is: %f\n",sg,,sg_sum/sg , db,db_sum/db);
return 0;
}
4、遇到问题:不会将字符替换为‘!!’
原版:
#include<stdio.h>
int main(void)
{
printf("输入“#”来结束程序。\n");
char c ;
int times=0;
while ((c = getchar())!= '#')
{
if (c == '。')
{
c = '!';
times++;
}
else if (c == '!')
{
c = '!!';//‘!!’不能算是字符,有两个字符
times++;
}
}
printf("替换次数:%d",times);
return 0;
}
问题解决:这里的替代并不是指真正地将字符替代,而是指在输出的时候打印“!!”
改版:
#include<stdio.h>
int main(void)
{
printf("输入“#”来结束程序。\n");
char c;
int times = 0;
while ((c = getchar()) != '#')
{
if (c == '.')
{
putchar('!');
times++;
}
else if (c == '!')
{
printf("!!");//‘!!’不能算是字符,有两个字符,所以不能用putchar,且要用双引号
times++;
}
else
putchar(c);
}
printf("替换次数:%d", times);
return 0;
}
6、 错版:这样e后面的那个字符都会被吃掉,如果e后面的字符是e或者#的话程序就会出错,所以只能用一次getchar,也就是说我们要找一个变量来装c前一个值,检测c==‘i’,pre_c=='e'
#include<stdio.h>
int main(void)
{
char c;
int times = 0;
printf("输入 '#'结束程序.\n");
while ((c = getchar()) != '#')
{
if (c == 'e')
{
c = getchar();
if (c == 'i')
times++;
}
}
printf("ei出现次数:%d",times);
return 0;
}
改版:
#include<stdio.h>
int main(void)
{
char c,c_pre=0;
int times = 0;
printf("输入 '#'结束程序.\n");
while ((c = getchar()) != '#')
{
if (c == 'i')
{
if (c_pre == 'e')
times++;
}
c_pre=c;
}
printf("ei出现次数:%d",times);
return 0;
}
7、
#include<stdio.h>
#define basic_sal 10
#define ext_work 1.5
#define taxrate_300 0.15
#define taxrate_450 0.20
#define taxrate_ext 0.25
int main(void)
{
int work_hour = 0;
float total_sal=0, tax=0;
printf("请输入您一周工作的小时数:\n");
scanf_s("%d", &work_hour);
if (work_hour > 40)
{
work_hour = 40 + ext_work * (work_hour - 40);
}
total_sal = basic_sal * work_hour;
if (total_sal <= 300)
tax = total_sal * taxrate_300;
else if (total_sal <= 450)
tax = total_sal * taxrate_450;
else
tax = total_sal * taxrate_450;
float actual_income = total_sal - tax;
printf("工资总额:%.2f\n税金:%.2f\n净收入:%.2f\n", total_sal, tax, actual_income);
return 0;
}
8、遇到问题:菜单边框算法待优化,菜单程序要不要循环?不循环quit啥?
#include<stdio.h>
#define ext_work 1.5
#define taxrate_300 0.15
#define taxrate_450 0.20
#define taxrate_ext 0.25
int main(void)
{
int work_hour,choice,flag;
float total_sal=0, tax=0, pay_rate;
char c;
do
{
for (int i = 0; i <= 65; i++)
printf("*");
printf("\nEnter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n3) $10.00/hr 4) $11.20/hr\n5) quit\n");
for (int i = 0; i <= 65; i++)
printf("*");
printf("\n");
flag = scanf_s("%d", &choice);
if (choice < 1 || choice>5 || flag != 1)//这里的1和5是数字,不能加‘’。
{
printf("Please choose correct answers.\n");
flag--;
}
while ((c=getchar()) != '\n')
continue;//清空缓存区,免得scanf死循环。
} while (flag!=1);
switch (choice)
{
case(1):
pay_rate = 8.75;
break;
case(2):
pay_rate = 9.33;
break;
case(3):
pay_rate = 10.00;
break;
case(4):
pay_rate = 11.20;
break;
case(5):
printf("That's the end, bye!");
flag++;
}
if (flag != 2)
{
printf("Please enter your working hours everyday:");
scanf_s("%d", &work_hour);
if (work_hour > 40)
{
work_hour = 40 + ext_work * (work_hour - 40);
}
total_sal = pay_rate * work_hour;
if (total_sal <= 300)
tax = total_sal * taxrate_300;
else if (total_sal <= 450)
tax = total_sal * taxrate_450;
else
tax = total_sal * taxrate_450;
float actual_income = total_sal - tax;
printf("Total salary:%.2f\nTax:%.2f\nActual income:%.2f\n", total_sal, tax, actual_income);
}
return 0;
}
解决问题:
·边框直接用strlen函数就不用自己一个个点了
·这里的菜单要循环,和前面do while 里的内容放在一起,quit指的是跳出整个while函数,用return来实现,其次可以用default来排除1-5以外的数字情况
·可以优化检验输入,不用flag那么麻烦
改版:
#include<stdio.h>
#define ext_work 1.5
#define taxrate_300 0.15
#define taxrate_450 0.20
#define taxrate_ext 0.25
int main(void)
{
int work_hour, choice, flag;
float total_sal = 0, tax = 0, pay_rate;
char c;
while (1)
{
for (int i = 0; i <= 65; i++)
printf("*");
printf("\nEnter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $9.33/hr\n3) $10.00/hr 4) $11.20/hr\n5) quit\n");
for (int i = 0; i <= 65; i++)
printf("*");
printf("\n");
if (!scanf_s("%d", &choice)|| (c = getchar()) != '\n')//检验输入
{
printf("Please choose correct answers.\n");
while ((c = getchar()) != '\n')
continue;//清空缓存区,免得scanf死循环。
continue;
}
switch (choice)
{
case(1):
pay_rate = 8.75;
break;
case(2):
pay_rate = 9.33;
break;
case(3):
pay_rate = 10.00;
break;
case(4):
pay_rate = 11.20;
break;
case(5):
printf("That's the end, bye!");
return;//退出while循环
default:
printf("Please enterr the choice between 1 to 5\n");
continue;
}
printf("Please enter your working hours everyday:");
scanf_s("%d", &work_hour);
if (work_hour > 40)
{
work_hour = 40 + ext_work * (work_hour - 40);
}
total_sal = pay_rate * work_hour;
if (total_sal <= 300)
tax = total_sal * taxrate_300;
else if (total_sal <= 450)
tax = total_sal * taxrate_450;
else
tax = total_sal * taxrate_450;
float actual_income = total_sal - tax;
printf("Total salary:%.2f\nTax:%.2f\nActual income:%.2f\n", total_sal, tax, actual_income);
}
return 0;
}
9、检验正整数花了好长时间写的QAQ
#include<stdio.h>
int main(void)
{
int pos_i, i,j;
char c;
printf("Please enter a positive int:");
i = scanf_s("%d", &pos_i);
//检验输入是否为正整数
while (i != 1 || pos_i <= 0 || (c = getchar()) != '\n')
{
while ((c = getchar()) != '\n')
{
continue;
}
printf("This isn't a positive int,please enter again:\n");
i = scanf_s("%d", &pos_i);
}
//打印pos_i以内的素数
for (i = 2; i <= pos_i; i++)
{
for ( j = 2; j < i; j++)
{
if (i % j == 0)
{
break;
}
}
if (j >= i)//没有除1和本身以外的因数即没有break
printf("%d ", i);
}
return 0;
}
对输出素数优化:判断i是不是素数,只要看根号i及以内的数是不是i的因数即可,因为两边是对称的
优化如下:
#include<stdio.h>
#include<math.h>
int main(void)
{
int pos_i, i,j;
char c;
printf("Please enter a positive int:");
i = scanf_s("%d", &pos_i);
//检验输入是否为正整数
while (i != 1 || pos_i <= 0 || (c = getchar()) != '\n')
{
while ((c = getchar()) != '\n')
{
continue;
}
printf("This isn't a positive int,please enter again:\n");
i = scanf_s("%d", &pos_i);
}
//打印pos_i以内的素数
for (i = 2; i <= pos_i; i++)
{
for ( j = 2; j <= sqrt(i); j++)//注意这里中间是等号
{
if (i % j == 0)
{
break;
}
}
if (j >sqrt(i))//没有除1和本身以外的因数即没有break
printf("%d ", i);
}
return 0;
}
10、依旧是检验遇到问题
#include<stdio.h>
#define basic_tax 0.15
#define extra_tax 0.28
int main(void)
{
float tax_earn,tax;
char choice,c;
int i;
do
{
int basic = 0;
do {
i = 0;
printf("Please enter your taxable earnings:(enter 0 to quit)\n");
scanf_s("%f", &tax_earn);
while ((c = getchar()) != '\n')
{
c = getchar();
i++;
}
} while (i!=0);
if (tax_earn == 0)
break;
printf("Here are the types of taxes:\n");
printf("A:single\nB:householder\nC:married\nD:married but divorced\n");
printf("Please choose the type of tax by entering the alpha:\n");
while(basic==0)
{
getchar();
scanf_s("%c", &choice);
while ((c = getchar()) != '\n')
{
basic = 0;
continue;
}
switch (choice)
{
case('A'):
{
basic = 17850;
break;//case里的break 只能跳出switch语句
}
case('B'):
{
basic = 23900;
break;
}
case('C'):
{
basic = 29750;
break;
}
case('D'):
{
basic = 14875;
break;
}
default:
{
printf("Please enter A,B,C,orD:\n");
}
}
}
if (tax_earn > basic)
tax = basic_tax * basic + extra_tax * (tax_earn - basic);
else
tax = tax_earn * basic_tax;
printf("The tax will be:.2%f dollars.\n",tax);
}while (tax_earn != 0);
printf("That's the end, bye!\n");
return 0;
}
解决:跟第八题一个模子,不改了
11、
#include<stdio.h>
#define ari 2.05
#define beet 1.15
#define car 1.09
#define discount 0.15
int main(void)
{
float ari_weight=0,bee_weight=0,car_weight=0,add,veg_fee,total_fee,ord,dis,dp,weight;
char choice,c;
float sale;
printf("a.arichoke\nb.beet\nc.carrot\nq.quit\n");
printf("Please choose the vegetable you want to buy by entering a, b, c or q:\n");
while (scanf_s("%c", &choice))
{
switch (choice)
{
case('a')://字符要用单引号括起来
printf("How many pounds of arichoke would you like to buy?\n");
scanf_s("%f", &add);
ari_weight += add;
while ((c = getchar()) != '\n')
continue;
printf("What else?:\n");
continue;
case('b'):
printf("How many pounds of beets would you like to buy?\n");
scanf_s("%f", &add);
bee_weight += add;
while ((c = getchar()) != '\n')
continue;
printf("What else?:\n");
continue;
case('c'):
printf("How many pounds of carrots would you like to buy?\n");
scanf_s("%f", &add);
car_weight += add;
while ((c = getchar()) != '\n')
continue;
printf("What else?:\n");
continue;
case('q'):
printf("That's the end.\n");
break;
default:
printf("Only a,b,c or d is accepted,please enter again:\n");
while ((c = getchar()) != '\n')
continue;
continue;
}
break;
}
veg_fee = ari * ari_weight + car * car_weight + bee_weight * beet;
dis = (int)veg_fee / 100 * discount; //浮点数强制转化,每满一百95折
weight = ari_weight + bee_weight + car_weight;
if (weight <= 5)
dp = 6.5;
else if (weight > 20)
dp = 14 + 0.5 * (weight - 20);
else
dp = 14;
ord = veg_fee - dis;
total_fee = ord + dp;
printf("Arichoke is %.2f dollars/pound, beet is %.2f dollars/pound and carrot is %.2f dollars/pound.\n",ari,beet,car);
printf("You want to buy %.2f pounds of arichoke, %.2f pounds of beets amd %.2f pounds of carrots.\n",ari_weight,bee_weight,car_weight);
printf("The fees for vegetables will be:%.2f\n",veg_fee);
printf("The order costs %.2f in total\n",ord);//什么叫订单的总费用?
if(dis!=0)
printf("Discounts:%.2f\n", dis);
printf("The fees for delivery and packing will be:%.2f\n",dp);
printf("The total fees will be:%.2f\n", total_fee);
return 0;
}