986

无所有、不可得!

[置顶] 卖域名

c程序设计教程练习题-3.22-自减运算符

c程序设计教程练习题-3.22的答案:


    豆瓣老师chentienx版



/* Exercise 3.22 Solution 练习3.22解决方案 */
#include <stdio.h>
int main()
{
int c; /* define c to use decrement operator 定义c以使用递减运算符 */
c = 5;
printf( "%d\n", c );
printf( "%d\n", --c ); /* predecrement 前置自减 */
printf( "%d\n\n", c );


c = 5 ;
printf( "%d\n", c );
printf( "%d\n", c-- ); /* postdecrement 后置自减*/
printf( "%d\n\n", c );
return 0; /* indicate successful termination 表示成功终止 */
} /* end main */

c程序设计教程练习题-3.21-计算雇员收入

c程序设计教程练习题-3.21的答案:


    豆瓣老师chentienx版



/* Exercise 3.21 Solution 练习3.21解决方案 */
#include <stdio.h>
int main( void )
{
double hours; /* total hours worked 总工作小时数 */
double rate; /* hourly pay rate 小时工资率 */

double salary; /* gross pay 工资总额 */


/* get first employee's hours worked 让第一个员工的工作时间*/
printf( "Enter number of hours worked ( -1 to end ): " );
scanf( "%lf", &hours );




/* loop until sentinel value read from user 循环直到从用户读取sentinel值 */
while ( hours != -1.0 ) {
/* get hourly rate 获取小时费率 */
printf( "Enter hourly rate of the worker ( $00.00 ): ");
scanf( "%lf", &rate );

/* if employee worked less than 40 hours 如果员工工作时间少于40小时 */
if ( hours <= 40 ) {
salary = hours * rate;
} /* end if */
else { /* compute "time-and-a-half" pay 计算“半时间”工资 */
salary = 40.0 * rate + ( hours - 40.0 ) * rate * 1.5;
} /* end else */

/* display gross pay 显示总薪酬 */
printf( "Salary is $%.2lf\n\n", salary );


/* prompt for next employee's data 提示下一个员工的数据*/
printf( "Enter number of hours worked ( -1 to end ): ");
scanf( "%lf", &hours );
} /* end while */




return 0; /* indicate successful termination 表示成功终止 */

} /* end main */

c程序设计教程练习题-3.20-贷款的利息

c程序设计教程练习题-3.20的答案:


    豆瓣老师chentienx版



/* Exercise 3.20 Solution 练习3.20解决方案*/
#include <stdio.h>
int main()
{
double principal; /*loan principal 贷款本金 */
double rate; /* interest rate 利率  */
double interest; /*interest charge 利息支出 */
int term; /*length of loan in days 贷款期限(天) */


/* get loan principal 获取贷款本金*/
printf( "Enter loan principal ( -1 to end): " );
scanf( "%lf" , &principal );

/* loop until sentinel value is read from user 循环,直到从用户读取sentinel值 */
while ( principal != -1.0 ) {
printf( "Enter interest rate: " ); /* get rate 获取速率 */
scanf( "%lf", &rate );

printf( "Enter term of the loan in days: " ); /* get term 获取期限 */
scanf( "%d", &term );

/* calculate interest charge 计算利息 */
interest = principal * rate * term / 365.0;
printf( "The interest charge is $%.2f\n\n", interest );


/* get next loan principal 获取下一笔贷款本金 */
printf( "Enter loan principal ( -1 to end ): " );
scanf( "%lf", &principal );
} /* end while 结束 */

return 0; /* indicate successful termination 表示成功终止 */
} /* end main */

c程序设计教程练习题-3.19-化学公司薪水

c程序设计教程练习题-3.19的答案:


    豆瓣老师chentienx版


/* Exercise 3.19 Solution 练习3.19解决方案 */
#include <stdio.h>


int main()
{
double sales; /* gross weekly sales 每周总销售额 */
double wage; /* commissioned earnings 委托收益 */


/* get first sales 获得首次销售 */
printf( "Enter sales in dollars ( -1 to end): " );
scanf( "%lf", &sales );


/* loop until sentinel value read from user 循环直到从用户读取sentinel值 */
while ( sales != -1.0 ) {
wage = 200.0 + 0.09 * sales; /* calculate wage 计算工资 */


/* display salary 显示工资 */
printf( "Salary is: $%.2f\n\n", wage );


/* prompt for next sales 提示下次销售 */
printf( "Enter sales in dollars ( -1 to end ): " );
scanf( "%lf", &sales );
} /* end while */


return 0; /* indicate successful termination 表示成功终止*/

} /* end main */

c程序设计教程练习题-3.18-百货商店顾客的账单

c程序设计教程练习题-3.18的答案:


    豆瓣老师chentienx版


/* Exercise 3.18 Solution

练习3.18解决方案  */
#include <stdio.h>

int main()
{
int accountNumber; /*current account's number 往来帐户号码*/
double balance; /*current account's starting balance 经常账户期初余额*/
double charges; /*current account's total charges 经常账户费用总额 */
double credits; /*current account's total credits 经常账户总信贷 */
double limit; /*current account's credit limit 活期账户信用额度 */

/* get account number 获取账号 */
printf( "\nEnter account number ( -1 to end): " );
scanf( "%d", &accountNumber );

/* loop until sentinel value read from user 循环直到从用户读取sentinel值 */
while ( accountNumber != -1 ) {

printf( "Enter beginning balance: " );
scanf( "%lf", &balance );

printf( "Enter total charges: " );
scanf( "%lf", &charges );

printf( "Enter total credits: " );
scanf( "%lf", &credits );


printf( "Enter credit limit: " );
scanf( "%lf", &limit );


balance += charges - credits; /* calculate balance 计算余额 */


/* if balance is over limit, display account number
with credit limit and balance to two digits of precision 如果余额超过限额,显示账号

信用额度和余额精确到两位数 */
if ( balance > limit ) {
printf( "%s%d\n%s%.2f\n%s%.2f\n%s\n",
"Account: ", accountNumber, "Credit limit: ", limit,
"Balance: ", balance, "Credit Limit Exceeded." );
} /* end if */

/* prompt for next account 提示下一个帐户 */
printf( "\nEnter account number ( -1 to end ): " );
scanf( "%d", &accountNumber );
} /* end while */

return 0; /* indicate successful termination 表示成功终止 */
} /* end main */


c程序设计教程练习题-3.17-汽油的价格

c程序设计教程练习题-3.17的答案:


    豆瓣老师chentienx版



/* Exercise 3.17 Solution 练习3.17解决方案*/
#include <stdio.h>
int main()
{

 double gallons; /*gallons used for current tank 当前储罐使用的加仑数*/
 double miles; /*miles driven for current tank 当前油箱行驶里程*/
 double totalGallons = 0.0; /*total gallons used 使用的总加仑数 */
 double totalMiles = 0.0; /*total miles driven 行驶总里程 */
 double totalAverage; /*average miles/gallon 平均英里/加仑 */


 /* get gallons used for first tank 第一个水箱使用加仑数 */
printf( "Enter the gallons used ( -1 to end): " );
scanf( "%lf", &gallons );


/* loop until sentinel value read from user 循环直到从用户读取sentinel值 */
while ( gallons != -1.0 ) {
totalGallons += gallons; /* add current tank gallons to total 将当前储罐加仑数加至总加仑数 */

printf( "Enter the miles driven: " ); /* get miles driven 行驶里程 */
scanf( "%lf", &miles );
totalMiles += miles; /* add current tank miles to total 将当前油箱里程添加到总里程中 */


/* display miles per gallon for current tank 显示当前油箱的每加仑英里数 */
printf( "The Miles / Gallon for this tank was %f\n\n",
miles / gallons );

/* get next tank's gallons 获取下一个储罐的加仑数 */
printf( "Enter the gallons used ( -1 to end ): " );
scanf( "%lf", &gallons );
} /* end while

结束时  */

/* calculate average miles per gallon over all tanks 计算所有油箱的平均每加仑英里数 */
totalAverage = totalMiles / totalGallons;
printf( "\nThe overall average Miles/Gallon was %f\n", totalAverage );

return 0; /* indicate successful termination 表示成功终止 */

} /* end main */


                                                                986版

c程序设计教程练习题-2.31-计算0到10的平方和立方

c程序设计教程练习题-2.31的答案:


    豆瓣老师chentienx版


/* Exercise 2.31 Solution 练习2.31解决方案 */
#include <stdio.h>

int main()
{
int count = 0 ; /* initialize count to zero 将计数初始化为零 */


/* calculate the square and cube for the numbers 0 to 10 计算数字0到10的平方和立方体 */
printf( "\nnumber\tsquare\tcube\n" );
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1; /* increment count by 1 将计数增加1 */
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );


count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );

count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );

count = count + 1;
printf( "%d\t%d\t%d\n", count, count * count,
count * count * count );

return 0; /* indicate successful termination 表示成功终止 */
} /* end main */

                                                                     986版

c程序设计教程练习题-2.30-把5个数分解为单个数

c程序设计教程练习题-2.30的答案:


    豆瓣老师chentienx版



/* Exercise 2.30 Solution

练习2.30解决方案  */
#include <stdio.h>

int main()
{
int number; /* number input by user 用户输入的数字*/
int temp1; /* first temporary integer 第一个临时整数*/
int temp2; /* second temporary integer 第二个临时整数*/

printf( "Enter a five-digit number: " ); /* prompt user 提示用户*/
scanf( "%d", &number ); /* read integer 读取整数*/

printf( "%d ", number / 10000 ); /* print left-most digit 打印最左边的数字 */
temp2 = number % 10000;

printf( " %d ", temp2 / 1000 );
temp1 = temp2 % 1000;

printf( " %d ", temp1 / 100 );
temp2 = temp1 % 100;

printf( " %d ", temp2 / 10 );
temp1 = temp2 % 10;

printf( " %d\n", temp1 ); /* print right-most digit 打印最右边的数字*/

return 0; /*indicate successful termination 表示成功终止 */

} /* end main */

c程序设计教程练习题-2.29-打印字符对应的整数

c程序设计教程练习题-2.29的答案:


    豆瓣老师chentienx版


/* Exercise 2.29 Solution 练习2.29解决方案 */
#include <stdio.h>

int main()
{
char intEquivalent; /* letter, digit or character 字母、数字或字符 */

c程序设计教程练习题-2.27-8条printf打印星号

c程序设计教程练习题-2.27的答案:


    豆瓣老师chentienx版



/* Exercise 2.27 Solution */
#include <stdio.h>
int main()
{
printf( "With eight printf() statements: \n" );

printf("* * * * * * * *\n" );
printf(" * * * * * * * *\n" );
printf("* * * * * * * *\n" );
printf(" * * * * * * * *\n" );
printf("* * * * * * * *\n" );
printf(" * * * * * * * *\n" );
printf("* * * * * * * *\n" );
printf(" * * * * * * * *\n" );

printf( "\nNow with one printf() statement: \n" );

printf( "* * * * * * * *\n * * * * * * * *\n"
"* * * * * * * *\n * * * * * * * *\n"
"* * * * * * * *\n * * * * * * * *\n"
"* * * * * * * *\n * * * * * * * *\n" );

return 0; /* indicate successful termination */

} /* end main */

«12»
控制面板
您好,欢迎到访网站!
  [查看权限]
网站分类
搜索
最新留言
    网站收藏
    友情链接
    图标汇集
    • RainbowSoft Studio Z-Blog
    • 订阅本站的 RSS 2.0 新闻聚合

    Powered By Z-Blog 2.3 Avengers Build 180518

    Copyright Your WebSite. Some Rights Reserved.