C大学基础教程课后习题答案人民邮电出版社
C语言程序设计答案(人邮出版)

本文由Sjs反思贡献 doc文档可能在WAP端浏览体验不佳。
建议您优先选择TXT,或下载源文件到本机查看。
C 语言程序设计(人民邮电出品) 第1章 1.选择题 (1) C (2) B (3) D 2.填空题 (1)main (2) main (3) 有穷性、确定性、有零个或多个输入、有一个或多个输出、有效性 (4) 顺序、分支和循环(5) 自顶向下,逐步细化、模块化设计、结构化编码 第2章 1.选择题 BBCDA DDBBA BBDCB 2、填空题 (1) 数字、字母、下划线 (2)0 (3) 4、8 (4) (a+b)*c/(a-b) (5) -60 (6)-16 (7)3 (8)6、4、2 (9)0 (10)10、6 (11)5.500000 (12) 12、4 (13)double (14) 0 (15)16 (16)6.6 3、编程题 (1) main() { printf("int:%5d\n" "float:%5d\n" "char:%5d\n" "double:%5d\n" "long:%5d\n", sizeof(int),+sizeof(float), sizeof(char), sizeof(double), sizeof(long)); } (2) #define RAT 1.60934 main() { float k=0.0; printf("input the km:"); scanf("%f",&k); printf("\nmile:%f",k*RAT); } 第3章 1.选择题 (1) ~(10):DDCDCDCDBC 2.解析题 (1) x=170,x=ㄩㄩㄩ 170,x=ㄩㄩㄩ 252, x=ㄩㄩㄩ 170 x=170,x=170 ㄩㄩㄩ,x=ㄩㄩㄩ 170,x=%6d a=513.789185,a=ㄩㄩ 513.79,a=513.78918457,a=513.78918457 (2) a=3 ㄩ b=7x=8.5 ㄩ y=71.82c1=A ㄩ c2=a 3.编程题 (1) main() { int x,y; scanf("%d%d",&x,&y); printf("商数=%d,余数=%d",x/y,x%y); system("pause"); } (2) main() { double x,y,z,avg; scanf("%lf%lf%lf",&x,&y,&z); avg=(x+y+z)/3; printf("%.1lf",avg,avg); system("pause"); } 第4章 1.选择题 (1)~(10) CCAADCCABD 2.填空题 (1) ch>='A' && ch<='Z' ch=ch-32 (2) x<=10 && x>2 x<=2 && x>-1 y=-1 (3) a+b>c && a+c>b && b+c>a a==b&&a==c a==b || a==c || b==c (4) mark/10 k=1 case 9 (5) x<0 c=x/10 y!=-2 3.编程题 (1) #include main() { int x; printf("please input a number:"); scanf("%d",&x); if(x%2==0) printf("x is a even number"); else printf("x is a odd number"); } (2) #include main() { int x,y; printf("please input a number£o"); scanf("%d",&x); if(x<=-5) printf("the number is error"); else if(x<0) {y=x; printf("%d",y);} else if(x==0) {y=x-1;printf("%d",y);} else if(x<10) {y=x+1;printf("%d",y);} else printf("the number is error"); } (3) #include main() { int a,m; printf("please input a number:"); scanf("%d",&a); switch(a/10) { case 0: case 1: case 2:m=1;break; case 3:m=2;break; case 4:m=3;break; case 5:m=4;break; default:m=5;break; } printf("%d",m); } (4) #include main() { float price,tax; printf("please input the price of product:"); scanf("%f",&price); if(price>10000) tax=price*0.05; else if(price>5000) tax=price*0.03; else if(price>1000) tax=price*0.02; else tax=0; printf("%f",tax); } (5) #include main() { float score; printf("please input the score of student:"); scanf("%f",&score); if(score>=85) printf("VERY GOOD"); else if(score>=60) printf("GOOD"); else printf("BAD"); } 第5章 1.选择题 (1)d (2) c (3)a (4)d (5)a (6)d (7)d (8)b (9)d (10)b (11)c (12)b (13)d (14)a (15)c 2.填空题 (1) ==0 m=m/k k++ (2) 5 4 6 (3) 3*i-2 (4) -= *= (5) 8 5 2 (6) j++ i%j==0 j>=i (7)sum<k sum==k j-2 (8) s=0 p=1 j<=i 3.改错题 (1) 第一处改正: For 改为 for 第二处改正: ave=sum/4 改为 ave=sum/4.0 (2) 第一处改正: j<=9 第二处改正: m=100*i+10*j+k 3.编程题 (1) #include <math.h> main() { int s; float n,t,sum; t=1; sum=0; n=1; s=1.0; while(n<=100) { sum=sum+t; n=n+1; s=-s; t=s/n; } printf("sum=%10.6f\n",sum); } (2) 利用辗除法,直到 b 为 0 为止 main() { int p,r,n,m,temp; printf("input two integer n,m:"); scanf("%d,%d",&n,&m); if(n<m) { temp=n; n=m; m=temp; } p=n*m; while(m!=0) { r=n%m; n=m; m=r; } printf("greatest common divisor is:%d\n",n); //最大公约数 printf("lease common multiple is:%d\n",p/n); //最小公倍数 } (3) 采取逆向思维的方法,从后往前推断。
C语言程序设计基础教程课后习题答案(清华大学出版社于延编)自己做欢迎校正

四、编程题 1.从键盘上输入 5 个整数, 输出最大值(要求至少用 两种方法编程)。 ① #include<stdio.h> #include<stdlib.h> int max(int *a){ int b,i; b=a[0]; for(i=1;i<5;i++) if(b<a[i]) b=a[i]; return b; } int main(){ int i,a[5]; for(i=0;i<5;i++) scanf("%d",&a[i]); printf("最大值为:%d\n",max(a)); system("pause"); }
flag=1; n+=i; //将约数累加到 n 中 } } return n; //是这个数本身时返回值为 n } int main() { int m; scanf("%d",&m); printf("真约数之和=%d\n",f(m)); system("pause");}
4.设计算法求出 10000 以内所有孪生素数对。
if(i==j) printf("%5d",i);
Visual Basic程序设计教程 杨培添 人民邮电出版社 课后答案

Caption Caption
计算 退出
课 后 答 案 网
.khd图1 界面布局效果
然后,在代码编辑器窗口中编辑以下事件: Private Sub Command1_Click()
wConst pi = 3.1416
Dim r As Single
wr = Text1.Text Text2.Text = 4 * pi * r * r
课 后 答 案 网
2.点击按钮,根据按钮上的标签提示,完成对文本框的设置,程序运行界面如图 2.75 所示。
.com 图2.75 上机操作题运行界面(2) w Private Sub Command1_Click()
Text1.FontName = "黑体"
a End Sub
Private Sub Command10_Click()
d Form1.FontName = "幼圆" h Form1.ForeColor = RGB(0, 0, 255)
Form1.Print "Visual Basic 程序设计"
k End Sub www. 第 2 章【思考与练习】答案
一、选择题
1.B
2.B
3.A
4.B
5.C
6.A
7.C
8.B
9.D
c 三、简答题
1.简述 Visual Basic 6.0 有哪些特点。
. Visual Basic 6.0 是一种可视化的、面向对象和采用事件驱动方式的结构化高级程序设计
语言,可用于开发 Windows 环境下的各类应用程序。主要有以下主要特点:(1)可视化编 程;(2)面向对象的程序设计;(3)结构化程序设计;(4)事件驱动编程机制等。
C大学教程第五版课后复习题答案 (作者 DEITEL)

C++ 大学基础教程课后答案(DEITEL)版3.11GradeBook类定义:#include<string>// program uses C++ standard string classusing std::string;class GradeBook{public:// constructor initializes course name and instructor nameGradeBook( string, string );void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void setInstructorName( string ); // function to set instructor name string getInstructorName(); // function to retrieve instructor name void displayMessage(); // display welcome message and instructor name private:string courseName; // course name for this GradeBookstring instructorName; // instructor name for this GradeBook}; // end class GradeBook类成员函数:#include<iostream>using std::cout;using std::endl;#include"GradeBook.h"// constructor initializes courseName and instructorName// with strings supplied as argumentsGradeBook::GradeBook( string course, string instructor ){setCourseName( course ); // initializes courseNamesetInstructorName( instructor ); // initialiZes instructorName} // end GradeBook constructor// function to set the course namevoid GradeBook::setCourseName( string name ){courseName = name; // store the course name} // end function setCourseName// function to retrieve the course namestring GradeBook::getCourseName(){return courseName;} // end function getCourseName// function to set the instructor namevoid GradeBook::setInstructorName( string name ){instructorName = name; // store the instructor name} // end function setInstructorName// function to retrieve the instructor namestring GradeBook::getInstructorName(){return instructorName;} // end function getInstructorName// display a welcome message and the instructor's namevoid GradeBook::displayMessage(){// display a welcome message containing the course namecout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;// display the instructor's namecout << "This course is presented by: "<< getInstructorName() << endl; } // end function displayMessage测试文件:#include<iostream>using std::cout;using std::endl;// include definition of class GradeBook from GradeBook.h#include"GradeBook.h"// function main begins program executionint main(){// create a GradeBook object; pass a course name and instructor name GradeBook gradeBook("CS101 Introduction to C++ Programming", "Professor Smith" );// display initial value of instructorName of GradeBook objectcout << "gradeBook instructor name is: "<< gradeBook.getInstructorName() << "\n\n";// modify the instructorName using set functiongradeBook.setInstructorName( "Assistant Professor Bates" );// display new value of instructorNamecout << "new gradeBook instructor name is: "<< gradeBook.getInstructorName() << "\n\n";// display welcome message and instructor's namegradeBook.displayMessage();return 0; // indicate successful termination} // end main3.12类定义:class Account{public:Account( int ); // constructor initializes balancevoid credit( int ); // add an amount to the account balancevoid debit( int ); // subtract an amount from the account balance int getBalance(); // return the account balanceprivate:int balance; // data member that stores the balance}; // end class Account类成员函数:#include<iostream>using std::cout;using std::endl;#include"Account.h"// include definition of class Account// Account constructor initializes data member balanceAccount::Account( int initialBalance ){balance = 0; // assume that the balance begins at 0// if initialBalance is greater than 0, set this value as the// balance of the Account; otherwise, balance remains 0if ( initialBalance > 0 )balance = initialBalance;// if initialBalance is negative, print error messageif ( initialBalance < 0 )cout << "Error: Initial balance cannot be negative.\n" << endl; } // end Account constructor// credit (add) an amount to the account balancevoid Account::credit( int amount ){balance = balance + amount; // add amount to balance} // end function credit// debit (subtract) an amount from the account balancevoid Account::debit( int amount ){if ( amount > balance ) // debit amount exceeds balancecout << "Debit amount exceeded account balance.\n" << endl;if ( amount <= balance ) // debit amount does not exceed balance balance = balance - amount;} // end function debit// return the account balanceint Account::getBalance(){return balance; // gives the value of balance to the calling function } // end function getBalance测试函数:#include<iostream>using std::cout;using std::cin;using std::endl;// include definition of class Account from Account.h#include"Account.h"// function main begins program executionint main(){Account account1( 50 ); // create Account objectAccount account2( 25 ); // create Account object// display initial balance of each objectcout << "account1 balance: $" << account1.getBalance() << endl;cout << "account2 balance: $" << account2.getBalance() << endl;int withdrawalAmount; // stores withdrawal amount read from usercout << "\nEnter withdrawal amount for account1: "; // promptcin >> withdrawalAmount; // obtain user inputcout << "\nattempting to subtract " << withdrawalAmount<< " from account1 balance\n\n";account1.debit( withdrawalAmount ); // try to subtract from account1// display balancescout << "account1 balance: $" << account1.getBalance() << endl;cout << "account2 balance: $" << account2.getBalance() << endl;cout << "\nEnter withdrawal amount for account2: "; // promptcin >> withdrawalAmount; // obtain user inputcout << "\nattempting to subtract " << withdrawalAmount<< " from account2 balance\n\n";account2.debit( withdrawalAmount ); // try to subtract from account2// display balancescout << "account1 balance: $" << account1.getBalance() << endl;cout << "account2 balance: $" << account2.getBalance() << endl;return 0; // indicate successful termination} // end main3.13类定义:#include<string>// program uses C++ standard string classusing std::string;// Invoice class definitionclass Invoice{public:// constructor initializes the four data membersInvoice( string, string, int, int );// set and get functions for the four data membersvoid setPartNumber( string ); // part numberstring getPartNumber();void setPartDescription( string ); // part descriptionstring getPartDescription();void setQuantity( int ); // quantityint getQuantity();void setPricePerItem( int ); // price per itemint getPricePerItem();// calculates invoice amount by multiplying quantity x price per item int getInvoiceAmount();private:string partNumber; // the number of the part being soldstring partDescription; // description of the part being sold int quantity; // how many of the items are being soldint pricePerItem; // price per item}; // end class Invoice类成员函数:#include<iostream>using std::cout;using std::endl;// include definition of class Invoice from Invoice.h#include"Invoice.h"// Invoice constructor initializes the class's four data members Invoice::Invoice( string number, string description, int count, int price ){setPartNumber( number ); // store partNumbersetPartDescription( description ); // store partDescriptionsetQuantity( count ); // validate and store quantitysetPricePerItem( price ); // validate and store pricePerItem} // end Invoice constructor// set part numbervoid Invoice::setPartNumber( string number ){partNumber = number; // no validation needed} // end function setPartNumber// get part numberstring Invoice::getPartNumber(){return partNumber;} // end function getPartNumber// set part descriptionvoid Invoice::setPartDescription( string description ){partDescription = description; // no validation needed} // end function setPartDescription// get part descriptionstring Invoice::getPartDescription(){return partDescription;} // end function getPartDescription// set quantity; if not positive, set to 0void Invoice::setQuantity( int count ){if ( count > 0 ) // if quantity is positivequantity = count; // set quantity to countif ( count <= 0 ) // if quantity is not positive{quantity = 0; // set quantity to 0cout << "\nquantity cannot be negative. quantity set to 0.\n"; } // end if} // end function setQuantity// get quantityint Invoice::getQuantity(){return quantity;} // end function getQuantity// set price per item; if not positive, set to 0void Invoice::setPricePerItem( int price ){if ( price > 0 ) // if price is positivepricePerItem = price; // set pricePerItem to price if ( price <= 0 ) // if price is not positive{pricePerItem = 0; // set pricePerItem to 0cout << "\npricePerItem cannot be negative. "<< "pricePerItem set to 0.\n";} // end if} // end function setPricePerItem// get price per itemint Invoice::getPricePerItem(){return pricePerItem;} // end function getPricePerItem// calulates invoice amount by multiplying quantity x price per item int Invoice::getInvoiceAmount(){return getQuantity() * getPricePerItem();} // end function getInvoiceAmount测试函数:#include<iostream>using std::cout;using std::cin;using std::endl;// include definition of class Invoice from Invoice.h#include"Invoice.h"// function main begins program executionint main(){// create an Invoice objectInvoice invoice( "12345", "Hammer", 100, 5 );// display the invoice data members and calculate the amountcout << "Part number: " << invoice.getPartNumber() << endl;cout << "Part description: "<< invoice.getPartDescription() << endl; cout << "Quantity: " << invoice.getQuantity() << endl;cout << "Price per item: $" << invoice.getPricePerItem() << endl; cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;// modify the invoice data membersinvoice.setPartNumber( "123456" );invoice.setPartDescription( "Saw" );invoice.setQuantity( -5 ); //negative quantity,so quantity set to 0 invoice.setPricePerItem( 10 );cout << "\nInvoice data members modified.\n\n";// display the modified invoice data members and calculate new amount cout << "Part number: " << invoice.getPartNumber() << endl;cout << "Part description: "<< invoice.getPartDescription() << endl; cout << "Quantity: " << invoice.getQuantity() << endl;cout << "Price per item: $" << invoice.getPricePerItem() << endl; cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;return 0; // indicate successful termination} // end main3.14类定义:#include<string>// program uses C++ standard string classusing std::string;// Employee class definitionclass Employee{public:Employee( string, string, int ); // constructor sets data members void setFirstName( string ); // set first namestring getFirstName(); // return first namevoid setLastName( string ); // set last namestring getLastName(); // return last namevoid setMonthlySalary( int ); // set weekly salaryint getMonthlySalary(); // return weekly salaryprivate:string firstName; // Employee's first namestring lastName; // Employee's last nameint monthlySalary; // Employee's salary per month}; // end class Employee类成员函数:#include<iostream>using std::cout;#include"Employee.h"// Employee class definition// Employee constructor initializes the three data members Employee::Employee( string first, string last, int salary ){setFirstName( first ); // store first namesetLastName( last ); // store last namesetMonthlySalary( salary ); // validate and store monthly salary } // end Employee constructor// set first namevoid Employee::setFirstName( string name ){firstName = name; // no validation needed} // end function setFirstName// return first namestring Employee::getFirstName(){return firstName;} // end function getFirstName// set last namevoid Employee::setLastName( string name ){lastName = name; // no validation needed} // end function setLastName// return last namestring Employee::getLastName(){return lastName;} // end function getLastName// set monthly salary; if not positive, set to 0.0void Employee::setMonthlySalary( int salary ){if ( salary > 0 ) // if salary is positivemonthlySalary = salary; // set monthlySalary to salary if ( salary <= 0 ) // if salary is not positivemonthlySalary = 0; // set monthlySalary to 0.0} // end function setMonthlySalary// return monthly salaryint Employee::getMonthlySalary(){return monthlySalary;} // end function getMonthlySalary测试函数:#include<iostream>using std::cout;using std::endl;#include"Employee.h"// include definition of class Employee// function main begins program executionint main(){// create two Employee objectsEmployee employee1( "Lisa", "Roberts", 4500 );Employee employee2( "Mark", "Stein", 4000 );// display each Employee's yearly salarycout << "Employees' yearly salaries: " << endl;// retrieve and display employee1's monthly salary multiplied by 12 int monthlySalary1 = employee1.getMonthlySalary();cout << employee1.getFirstName() << " " << employee1.getLastName() << ": $" << monthlySalary1 * 12 << endl;// retrieve and display employee2's monthly salary multiplied by 12 int monthlySalary2 = employee2.getMonthlySalary();cout << employee2.getFirstName() << " " << employee2.getLastName() << ": $" << monthlySalary2 * 12 << endl;// give each Employee a 10% raiseemployee1.setMonthlySalary( monthlySalary1 * 1.1 );employee2.setMonthlySalary( monthlySalary2 * 1.1 );// display each Employee's yearly salary againcout << "\nEmployees' yearly salaries after 10% raise: " << endl;// retrieve and display employee1's monthly salary multiplied by 12 monthlySalary1 = employee1.getMonthlySalary();cout << employee1.getFirstName() << " " << employee1.getLastName() << ": $" << monthlySalary1 * 12 << endl;monthlySalary2 = employee2.getMonthlySalary();cout << employee2.getFirstName() << " " << employee2.getLastName() << ": $" << monthlySalary2 * 12 << endl;return 0; // indicate successful termination} // end main3.15类定义:class Date{public:Date( int, int, int ); // constructor initializes data members void setMonth( int ); // set monthint getMonth(); // return monthvoid setDay( int ); // set dayint getDay(); // return dayvoid setYear( int ); // set yearint getYear(); // return yearvoid displayDate(); // displays date in mm/dd/yyyy formatprivate:int month; // the month of the dateint day; // the day of the dateint year; // the year of the date}; // end class Date类成员函数:#include<iostream>using std::cout;using std::endl;#include"Date.h"// include definition of class Date from Date.h// Date constructor that initializes the three data members;// assume values provided are correct (really should validate) Date::Date( int m, int d, int y ){setMonth( m );setDay( d );setYear( y );} // end Date constructor// set monthvoid Date::setMonth( int m ){month = m;if ( month < 1 )month = 1;if ( month > 12 )month = 1;} // end function setMonth// return monthint Date::getMonth(){return month;} // end function getMonth// set dayvoid Date::setDay( int d ){day = d;} // end function setDay// return dayint Date::getDay(){return day;} // end function getDay// set yearvoid Date::setYear( int y ){year = y;} // end function setYear// return yearint Date::getYear(){return year;} // end function getYear// print Date in the format mm/dd/yyyyvoid Date::displayDate(){cout << month << '/' << day << '/' << year << endl;} // end function displayDate测试函数:#include<iostream>using std::cout;using std::endl;#include"Date.h"// include definition of class Date from Date.h // function main begins program executionint main(){Date date( 5, 6, 1981 ); // create a Date object for May 6, 1981 // display the values of the three Date data memberscout << "Month: " << date.getMonth() << endl;cout << "Day: " << date.getDay() << endl;cout << "Year: " << date.getYear() << endl;cout << "\nOriginal date:" << endl;date.displayDate(); // output the Date as 5/6/1981// modify the Datedate.setMonth( 13 ); // invalid monthdate.setDay( 1 );date.setYear( 2005 );cout << "\nNew date:" << endl;date.displayDate(); // output the modified date (1/1/2005) return 0; // indicate successful termination} // end main9.05类定义:#ifndef COMPLEX_H#define COMPLEX_Hclass Complex{public:Complex( double = 0.0, double = 0.0 ); // default constructorComplex add( const Complex & ); // function addComplex subtract( const Complex & ); // function subtract void printComplex(); // print complex number formatvoid setComplexNumber( double, double ); // set complex number private:double realPart;double imaginaryPart;}; // end class Complex#endif类成员函数:#include<iostream>using std::cout;#include"Complex.h"Complex::Complex( double real, double imaginary ){setComplexNumber( real, imaginary );} // end Complex constructorComplex Complex::add( const Complex &right ){return Complex(realPart + right.realPart, imaginaryPart + right.imaginaryPart ); } // end function addComplex Complex::subtract( const Complex &right ){return Complex(realPart - right.realPart, imaginaryPart - right.imaginaryPart ); } // end function subtractvoid Complex::printComplex(){cout << '(' << realPart << ", " << imaginaryPart << ')';} // end function printComplexvoid Complex::setComplexNumber( double rp, double ip ){realPart = rp;imaginaryPart = ip;} // end function setComplexNumber测试函数:#include<iostream>using std::cout;using std::endl;#include"Complex.h"int main(){Complex a( 1, 7 ), b( 9, 2 ), c; // create three Complex objects a.printComplex(); // output object acout << " + ";b.printComplex(); // output object bcout << " = ";c = a.add( b ); // invoke add function and assign to object cc.printComplex(); // output object ccout << '\n';a.setComplexNumber( 10, 1 ); // reset realPart andb.setComplexNumber( 11, 5 ); // and imaginaryParta.printComplex(); // output object acout << " - ";b.printComplex(); // output object bcout << " = ";c = a.subtract( b ); // invoke add function and assign to object c c.printComplex(); // output object ccout << endl;return 0;} // end main9.06类定义:#ifndef RATIONAL_H#define RATIONAL_Hclass Rational{public:Rational( int = 0, int = 1 ); // default constructorRational addition( const Rational & ); // function additionRational subtraction( const Rational & ); // function subtraction Rational multiplication( const Rational & ); // function multi. Rational division( const Rational & ); // function division void printRational (); // print rational formatvoid printRationalAsDouble(); // print rational as double format private:int numerator; // integer numeratorint denominator; // integer denominatorvoid reduction(); // utility function}; // end class Rational#endif类成员函数:#include<iostream>using std::cout;#include"Rational.h"// include definition of class Rational Rational::Rational( int n, int d ){numerator = n; // sets numeratordenominator = d; // sets denominatorreduction(); // store the fraction in reduced form} // end Rational constructorRational Rational::addition( const Rational &a ){Rational t; // creates Rational objectt.numerator = a.numerator * denominator;t.numerator += a.denominator * numerator;t.denominator = a.denominator * denominator;t.reduction(); // store the fraction in reduced form return t;} // end function additionRational Rational::subtraction( const Rational &s ){Rational t; // creates Rational objectt.numerator = s.denominator * numerator;t.numerator -= denominator * s.numerator;t.denominator = s.denominator * denominator;t.reduction(); // store the fraction in reduced form return t;} // end function subtractionRational Rational::multiplication( const Rational &m ){Rational t; // creates Rational objectt.numerator = m.numerator * numerator;t.denominator = m.denominator * denominator;t.reduction(); // store the fraction in reduced form return t;} // end function multiplicationRational Rational::division( const Rational &v ){Rational t; // creates Rational objectt.numerator = v.denominator * numerator;t.denominator = denominator * v.numerator;t.reduction(); // store the fraction in reduced form return t;} // end function divisionvoid Rational::printRational (){if ( denominator == 0 ) // validates denominatorcout << "\nDIVIDE BY ZERO ERROR!!!" << '\n';else if ( numerator == 0 ) // validates numeratorcout << 0;elsecout << numerator << '/' << denominator;} // end function printRationalvoid Rational::printRationalAsDouble(){cout << static_cast< double >( numerator ) / denominator; } // end function printRationalAsDoublevoid Rational::reduction(){int largest;largest = numerator > denominator ? numerator : denominator;int gcd = 0; // greatest common divisorfor ( int loop = 2; loop <= largest; loop++ )if ( numerator % loop == 0 && denominator % loop == 0 ) gcd = loop;if (gcd != 0){numerator /= gcd;denominator /= gcd;} // end if} // end function reduction测试函数:#include<iostream>using std::cout;using std::endl;#include"Rational.h"// include definition of class Rationalint main(){Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects c.printRational(); // prints rational object ccout << " + ";d.printRational(); // prints rational object dx = c.addition( d ); // adds object c and d; sets the value to xcout << " = ";x.printRational(); // prints rational object xcout << '\n';x.printRational(); // prints rational object xcout << " = ";x.printRationalAsDouble(); // prints rational object x as doublecout << "\n\n";c.printRational(); // prints rational object ccout << " - ";d.printRational(); // prints rational object dx = c.subtraction( d ); // subtracts object c and dcout << " = ";x.printRational(); // prints rational object xcout << '\n';x.printRational(); // prints rational object xcout << " = ";x.printRationalAsDouble(); // prints rational object x as doublecout << "\n\n";c.printRational(); // prints rational object ccout << " x ";d.printRational(); // prints rational object dx = c.multiplication( d ); // multiplies object c and dcout << " = ";x.printRational(); // prints rational object xcout << '\n';x.printRational(); // prints rational object xcout << " = ";x.printRationalAsDouble(); // prints rational object x as double cout << "\n\n";c.printRational(); // prints rational object ccout << " / ";d.printRational(); // prints rational object dx = c.division( d ); // divides object c and dcout << " = ";x.printRational(); // prints rational object xcout << '\n';x.printRational(); // prints rational object xcout << " = ";x.printRationalAsDouble(); // prints rational object x as double cout << endl;return 0;} // end main9.07类定义:#ifndef TIME_H#define TIME_Hclass Time{public:public:Time( int = 0, int = 0, int = 0 ); // default constructor // set functionsvoid setTime( int, int, int ); // set hour, minute, secondvoid setHour( int ); // set hour (after validation)void setMinute( int ); // set minute (after validation)void setSecond( int ); // set second (after validation)// get functionsint getHour(); // return hourint getMinute(); // return minuteint getSecond(); // return secondvoid tick(); // increment one secondvoid printUniversal(); // output time in universal-time format void printStandard(); // output time in standard-time format private:int hour; // 0 - 23 (24-hour clock format)int minute; // 0 - 59int second; // 0 - 59}; // end class Time#endif类成员函数:#include<iostream>using std::cout;#include<iomanip>using std::setfill;using std::setw;#include"Time.h"// include definition of class Time from Time.h // Time constructor initializes each data member to zero;// ensures that Time objects start in a consistent state Time::Time( int hr, int min, int sec ){setTime( hr, min, sec ); // validate and set time} // end Time constructor// set new Time value using universal time; ensure that// the data remains consistent by setting invalid values to zero void Time::setTime( int h, int m, int s ){setHour( h ); // set private field hoursetMinute( m ); // set private field minutesetSecond( s ); // set private field second} // end function setTime// set hour valuevoid Time::setHour( int h ){hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour} // end function setHour// set minute valuevoid Time::setMinute( int m ){minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute} // end function setMinute// set second valuevoid Time::setSecond( int s ){second = ( s >= 0 && s < 60 ) ? s : 0; // validate second} // end function setSecond// return hour valueint Time::getHour(){return hour;} // end function getHour// return minute valueint Time::getMinute(){return minute;。
C语言程序设计教程 杨路明 课后习题答案 北京邮电大学出版社

2、c语言程序的结构如下: 1、c语言程序由函数组成,每个程序必须具有一个main函数作为程序的主控函数。 2、"/*"与"*/"之间的内容构成c语言程序的注释部分。 3、用预处理命令#include可以包含有关文件的信息。 4、大小写字母在c语言中是有区别的。 5、除main函数和标准库函数以外,用户可以自己编写函数,程序一般由多个函数组成,这些函数制定实际 所需要做的工作。 例如: void main() { int a,b,c,s; a=8;b=12;c=6; s=a+b*c; printf("s=%d\n",s); }
1、符合C语法规定的常数为:0x1e、"ab\n"、1.e5 2、(1): 错误如下:int x,y=5,z=5,aver; x=7; aver = (x+y+x)/3; 结果如下:AVER=5 (2): 错误如下:char c1='a',c2='b',c3='c'; printf("a=%db=\'%c\'\"end\"\n",a,b); 结果如下:a=3b='A'"end" aabcc abc
scanf("%d,%d",&x,&y); while((i*y)<=x) { if(x==(i*y)) {temp1=1;break;} temp2=i; i++; } if(temp1) printf("%d / %d = %d",x,y,i); else printf("%d / %d---> shang=%d,yushu=%d",x,y,temp2,x-y*temp2); getch(); } 9、 #include<stdio.h> void main() { float x,y,m=0,n=0; scanf("%f,%f",&x,&y); n=(x-2)*(x-2); m=(y-2)*(y-2); if((m+n)<=1) printf("(%.3f,%.3f)In the yuan",x,y); else printf("(%.3f,%.3f)out of the yuan",x,y); getch(); } 10、 #include<stdio.h> void main() { int temp=0,month,year;
C语言程序设计基础教程课后习题答案(清华大学出版社于延编)自己做的欢迎校正

C语言程序设计基础教程课后习题答案(清华大学出版社于延编)自己做的欢迎校正第一章计算机程序设计导论一、简答题1.请简述计算机程序设计语言的发展历程。
答:迄今为止计算机程序设计语言的发展经历了机器语言、汇编语言、高级语言等阶段,C 语言是一种面向对象的编程语言,也属于高级语言。
2.什么是算法,请举例设计一个算法。
答:算法可以理解为有基本运算及规定的运算顺序所构成的完整的解题步骤。
或者看成按照要求设计好的有限的确切的计算序列,并且这样的步骤和序列可以解决一类问题。
求两个数的最大公约数设两个变量M和N1.如果M<n,则交换m和n< p="">2.M被N除,得到余数R3.判断R=0,正确则N为最大公约数否则进行下一步4.将N赋值给M,将R赋值给N,重复第一步3.请叙述算法都有哪些特性。
答:①有穷性②群定性③有效性④有零个或多个输入⑤有一个或多个输入4.请叙述什么是结构化程序设计以及结构答:①只要有几种简单类型的借口,就可以构成任意复杂的程序。
这样可以使程序设计规范化,便于用工程的方法来机型软件生产,由顺序结构,选择结构,循环结构这三种基本结构组成的程序就是结构化程序。
二、算法设计题1.设计算法求1+2+3+…+100的和。
#include#includemain(){int I,S = 0;for(I = 1;I<=100;I++) //求1-100的和S=S+I;printf("%d\n",S);system("pause");}2.已知两个自然数M和N,请设计算法输出它们的最小公倍数。
#include#include#includeint fmax(int m,int n) //求最大公约数{int r;r=m%n;while (r!=0){m=n;n=r;r=m%n;}return n;}int fmin(int m,int n) //最小公倍数=两个数的积除两个数的最大公约数{ return m*n/fmax(m,n);}main(){ int a,b;scanf("%d%d",&a,&b);printf("fmin is:%d\n",fmin(a,b));system("pause");}3.已知一个自然数N,请设计算法输出它所有真约数的和。
大学_C语言程序设计教程课后习题答案下载

C语言程序设计教程课后习题答案下载C语言程序设计教程内容简介第1章C语言概述11.1简单的C语言程序11.2C语言的发展历史与特点31.2.1C语言的发展历史31.2.2C语言的'特点41.3C程序的开发步骤和上机调试流程51.3.1C程序的开发步骤51.3.2C程序的上机开发过程6习题112第2章基本数据类型的输入/输出132.1C语言的输入/输出132.2整型数据的输入/输出132.3浮点型数据的输入/输出142.4字符数据的输入/输出152.5字符串数据的输入/输出152.6格式化输入/输出举例172.7阅读材料182.7.1格式化输出函数printf的格式说明和使用182.7.2格式化输入函数scanf的格式说明和使用19习题221第3章流程控制223.1算法223.1.1算法的概念223.1.2算法的表达方式233.1.3基本流程控制结构253.1.4案例3.1 求1+1/2+…+1/100的和263.2选择语句263.2.1案例3.2 求三个整数的最大值(if语句)263.2.2案例3.3 百分制成绩转换成五分制成绩(多分支if语句)293.2.3案例3.4 判断所输入的一个字符是数字、空白符还是其他字符(switch语句)313.2.4案例3.5 百分制成绩转换成五分制成绩(switch语句)333.3循环语句343.3.1案例3.6 求1+1/2+…+1/100的和(while语句)343.3.2案例3.7 求1+1/2+…+1/100的和(do...while语句)353.3.3案例3.8 求1+1/2+…+1/100的和(for语句)373.4转向语句393.4.1案例3.9 判断所输入的一个大于1的正整数是否是素数(break语句)393.4.2案例3.10 输出100~200之间能被3整除的数(continue语句)413.5应用举例423.5.1案例3.11 计算1! + 2! + … + 10!(并讨论溢出问题)423.5.2案例3.12 计算级数1-1/3+1/5-1/7+…的和443.5.3案例3.13 统计输入的数字字符、字母字符和其他字符的个数453.5.4案例3.14 求两个正整数的最大公约数和最小公倍数453.5.5案例3.15 将一个正整数逆序输出463.5.6案例3.16 输入日期并检查其合理性,直到输入合理为止473.6阅读材料483.6.1C语言的语句483.6.2goto语句简介493.6.3exit()函数493.6.4程序调试简介50习题351C语言程序设计教程目录本书定位于将C语言作为计算机编程入门语言,以帮助读者树立计算机程序设计的思想,培养学生程序设计基本能力为目标的教材。
c程序设计教程课后习题答案

c程序设计教程课后习题答案在编写C语言程序时,理解并完成课后习题是掌握编程知识的重要步骤。
以下是一些典型的C语言程序设计教程课后习题答案的示例,这些答案涵盖了基础语法、数据结构、函数、指针等概念。
习题1:变量声明和初始化编写一个C程序,声明一个整型变量`x`和一个浮点型变量`y`,并将它们分别初始化为10和3.14。
```c#include <stdio.h>int main() {int x = 10;double y = 3.14;printf("x = %d\n", x);printf("y = %f\n", y);return 0;}```习题2:条件语句编写一个程序,判断一个整数变量`a`的值,如果`a`大于10则输出"Greater than 10",如果小于10则输出"Less than 10",如果等于10则输出"Equal to 10"。
```c#include <stdio.h>int main() {int a;printf("Enter an integer: ");scanf("%d", &a);if (a > 10) {printf("Greater than 10\n");} else if (a < 10) {printf("Less than 10\n");} else {printf("Equal to 10\n");}return 0;}```习题3:循环编写一个程序,使用`for`循环打印从1到10的整数。
```c#include <stdio.h>int main() {for (int i = 1; i <= 10; ++i) {printf("%d ", i);}printf("\n");return 0;}```习题4:数组编写一个程序,声明一个整数数组`arr`,包含5个元素,初始化为1, 2, 3, 4, 5,并打印数组中的所有元素。
C语言程序设计教程 杨路明 课后习题答案 北京邮电大学出版社

{ int n; printf("please input the number:"); scanf("%d",&n); if(n>=100 && n <= 999) printf("%d%d%d",n%10,(n/10)%10,n/100); else printf("you input number is error!"); } 10、void main() { int i,j,k; scanf("%d,%d,%d",&i,&j,&k); ((i%2 != 0?1:0) + (j%2 != 0?1:0)+(k%2 != 0?1:0)) == 2?printf("YES\n"):printf("NO\n"); } 11、void main() { char a; scanf("%c",&a); printf("%c,%c,%c",a-1,a,a+1); printf("%d,%d,%d",a-1,a,a+1); } 12、void main() { float a,b,c,s,Area; scanf("%f,%f,%f",&a,&b,&c); if(a+b > c || a+c > b || b+c >a) { s = (a+b+c)/2;
3、 4、(1):9,11,9,10 (2):3,1,0,0 (3):11,19,31,1 5、(1):0 (2):0 (3):9.500000 (4):90 (5):10 (6):10 (7):65 (8):4 (9):4.500000 (10):1 (11):0 (12):20 (13):0
大学计算机基础教程课后习题答案习题1答案

习题1答案
1、单项选择题
(1)C (2)A (3)D (4)D (5)A (6)A (7)B (8)B (9)A (10)A
2、简答题
(1)计算机的发展按照它所使用的基本逻辑元件的不同,划分为四代。
第一代:电子管计算机时代
第二代:晶体管计算机时代
第三代:中小规模集成电路计算机时代
第四代:大规模和超大规模集成电路计算机时代
(2)通常,将运算器、控制器、内存储器合称为计算机的主机。
也可以说,主机由中央处理器和内存组成。
(3)将运算器、控制器集成在一个芯片上,称为中央处理器(CPU),它是计算机系统最重要的核心部件,CPU 控制着计算机的全部操作,它的品质的高低直接决定了计算机系统的档次。
在微机系统中,CPU 插接在主板的专用插槽中。
(4) 冯.诺依曼关于计算机提出的基本设想是什么?
针对 ENIAC 计算机的不足,冯.诺依曼提出了关于计算机的三个基本设想:
①关于计算机构成模式的设想:计算机硬件系统由运算器、控制器、存储器、输入设备和输出设备五大部分组成。
②关于计算机内部数据表示方式的设想:计算机内部,程序和数据都用二进制数表示,这是因为二进制数只有二个数码,电路简单且运行稳定,计算法则简单易于实现。
③计算机采用“存储程序”原理工作:即将程序预先输入到计算机中保存(存储程序原理),根据程序的指令的规定依次逐条取出指令,加以分析并执行,直至完成所有的指令(程序控制原理)。
现代计算机均为冯.诺依曼结构模型的计算机。
冯.诺依曼因此被尊称为现代计算机之父。
大学生C语言课后习题全部答案详解

#include〈stdio.h>main(){int a,b,he,cha,ji;double shang;a=8;b=3;he=a+b;cha=a—b;ji=a*b;shang=(double)a/b;printf(”和=%d\n”,he);printf("差=%d\n”,cha);printf(”积=%d\n”,ji);printf("商=%f\n",shang);}#include<stdio。
h>main(){double r,h,v,pi;pi=3。
14;r=2。
5;h=3.5;v=pi*r*r*h;printf(”面积=%f\n”,v);}#include<stdio。
h>#include〈stdlib.h〉#include〈conio.h〉main(){char ch;system(”cls”);printf(”|—-—---—-—----——-————--—-|\n”);printf("|请输入编号(0-7)|\n");printf(”|————————-——----———------|\n");printf(”|1—-创建通讯录|\n”);printf("| 2—-显示通讯录|\n”);printf("| 3——查询通讯录|\n");printf("|4——修改通讯录|\n");printf("|5——添加通讯录|\n");printf(”|6——删除通讯录|\n");printf(”|7——排序通讯录|\n");printf("| 0——退出|\n");printf("|—————-—--——————--———----|\n");printf("请输入选项\n”);ch=getch();putch(ch);}#include〈stdio。
大学计算机基础教程课后习题答案(大一)

计算机基础作业第一章计算机与信息社会习题 1一、思考题:1.计算机的发展经历了哪几个阶段?各阶段的主要特征是什么?答:计算机经历了电子管、晶体管、中小规模集成电路和大、超大规模集成电路等 4 个阶段。
电子管计算机的特征是:采用电子管作为计算机的逻辑元件,内存储器采用水银延迟线,外存储器采用磁鼓、纸带、卡片等,运算速度只有每秒几千次到几万次基本运算,内存容量只有几千个字节,使用二进制表示的机器语言或汇编语言编写程序。
晶体管计算机的特征是:用晶体管代替了电子管,大量采用磁芯作为内存储器,采用磁盘、磁带等作为外存储器。
采用了中小规模集成电路的计算机的特征是:用集成电路代替了分立元件。
集成电路是把多个电子元器件集中在几平方毫米的基片上形成的逻辑电路。
采用了大、超大规模集成电路的计算机的特征是:以大规模、超大规模集成电路来构成计算机的主要功能部件,主存储器采用集成度很高的半导体存储器,目前计算机的最高速度可以达到每秒几十万亿次浮点运算。
4.计算机主要用于哪些领域?答:计算机主要应用在科学和工程计算、信息和数据处理、过程控制、计算机辅助系统及人工智能等领域。
7.信息技术都包含那些?答:信息技术主要包括信息基础技术、信息系统技术、信息应用技术三个层次。
二、选择题1.最早的计算机是用来进行(A) 的。
A )科学计算B)系统仿真C)自动控制D) 信息处理2.构成第二代计算机的主要电子元件是(B)A )电子管B)晶体管C)中 .小规模集成电路D)超大规模集成电路3.以下哪个不是计算机的特点(D)A )计算机的运行速度快B)计算机的准确度高C)计算机的存储容量巨大D)计算机的体积很小4 办公自动化属于计算机哪项应用(A)A )数据处理B)科学计算C)辅助设计D)人工智能5.以下关于信息的特征不正确的是(B)A )共享性B)不可存储C)可处理性D) 可传递第二章 计算机基础知识 习题 2. 思考题:2.计算机硬件有哪五部分组成?答:计算机由运算器、控制器、存储器、输入装置和输出装置五大部件组成。
C语言程序设计教程 杨路明 课后习题答案 北京邮电大学出版社

scanf("%d,%d",&x,&y); while((i*y)<=x) { if(x==(i*y)) {temp1=1;break;} temp2=i; i++; } if(temp1) printf("%d / %d = %d",x,y,i); else printf("%d / %d---> shang=%d,yushu=%d",x,y,temp2,x-y*temp2); getch(); } 9、 #include<stdio.h> void main() { float x,y,m=0,n=0; scanf("%f,%f",&x,&y); n=(x-2)*(x-2); m=(y-2)*(y-2); if((m+n)<=1) printf("(%.3f,%.3f)In the yuan",x,y); else printf("(%.3f,%.3f)out of the yuan",x,y); getch(); } 10、 #include<stdio.h> void main() { int temp=0,month,year;
1、void main() { int n,value; int i,count=0; float average = 0; long int sum = 0; scanf("%d",&n); for(i = 0; i < n; i++) { scanf("%d",&value); if(value%2 == 0) { sum+=value; count++; } } average = sum / (float)count; printf("the average is %f\n",average); }
大学计算机基础教材习题参考答案(完整)

附录4大学计算机基础教材各章习题(单选、填空、判断题)参考答案习题 11.1单选题1 2345678910D B B A C D C C A B1.2填空题1.地址2.机器码,地址码,指令系统3.系统软件,应用软件4.ROM 5.000111106.计算机程序7.系统软件8.阶码,尾数9.63 10.01.3判断题1 2 3 4 5 6 7 8 9 10××√√√×√×√×习题22.1单选题1 2345678910A CBCD C B B A D11 121314151617181920B BCD A C C A B C2.2判断题1 2345678910√××√√√√√√×2.3填空题1.进程管理、存储管理、设备管理、作业管理、文件管理2.PrintScreen,Alt+ PrintScreen3.就绪4.应用进程5.文件正名,类型名,类型名6.3 7.计算机管理员(超级用户)8.复制9.32位高性能文件系统,FAT3210.+习题33.1单选题1 2345678910D C A D C C D B B C3.2判断题1 2345678910√×√×√√√√√×3.3填空题1.Ctrl+X,Ctrl+C,Ctrl+V 2.个体工作自动化、工作流程自动化、以知识管理为核心3.空格5/94.月/日/年5.幻灯片母版、标题母版、讲义母版和备注母版6.大纲、普通、浏览7.预览8.页面9.ESC 10.来自文件习题 44.1单选题1 2345678910A D CB B DC C C A4.2判断题1 2345678910×√×√√√√×√√习题55.1单选题1 2345678910C B A B B B CD B C5.2填空题1.文件系统2.需求分析、概念设计、实现设计、物理设计3.数据恢复4.关系5.集合,关系6.属性,记录7.删除,更新8.性别="男"OR性别="女" 9.删除成绩表中成绩为不及格的记录10.表、查询、窗体、报表、宏、模块以及数据访问页5.3判断题1 2345678910√√√×√√√√×√习题66.1单选题1 2345678910B D A B A D D B A D6.2判断题1 2345678910√√×××√√×√×6.3填空题1.集线器、交换机、路由器、网关2.RJ45,ST或SC3.LAN、MAN、WAN4.普通以太网、快速以太网、高速以太网5.1~126、128~191、192~2236.非对称数字用户线路7.PING,IPCONFIG8.100M、155M、200M9.文字链接、图片链接、图片热点链接、书签10.C2C、B2C、B2B、B2G习题77.1单选题1 2345678910C D C B C D C B C B7.2判断题1 2345678910√×√××√√×√×7.3填空题1.无损压缩、有损压缩、混合压缩2.基于时间、基于图标或流程线、基于卡片或页面、基于传统程序语言3.亮度、色调、饱和度4.88.25.bit6.51207.五大类,传输感觉媒体的中介媒体8.多媒体硬件系统、多媒体操作系统、多媒体处理系统工具和用户应用软件9.RM/RMVB、MOV、ASF、WMV10.WAV、MIDI、MP3、WMA、CD、RA、AU、MD和VOC等习题88.1单选题1 2345678910A B B C B D C A D A8.2判断题1 2345678910×√√××√×√×√8.3填空题1.安全立法、行政管理、技术措施2.专用密钥(对称式)和公开密钥(非对称式)3.著作权法4.计算机软件保护条例5.计算机程序代码,计算机功能6.破坏性、传染性、隐蔽性、可触发性、衍生性、不可预见性7.EXE 8.接收方能核实、发送方不能抵赖、接收方不能伪造9.真实性、保密性、完整性、可用性、不可抵赖性、可控性、可审性10.不被未授权访问、篡改,拒绝服务攻击。
大学计算机基础人民邮电出版社第二版

⼤学计算机基础⼈民邮电出版社第⼆版《⼤学计算机基础》(2014年7⽉版)参考答案习题11. 选择题(1)计算机的软件系统可分为 D 。
A)程序和数据 B)操作系统和语⾔处理系统C)程序、数据和⽂档 D)系统软件和应⽤软件(2) ⼀个完整的计算机系统包括 D 。
A)计算机及其外部设备 B)主机、键盘、显⽰器C)系统软件和应⽤软件 D)硬件系统和软件系统(3) 读写存储器的英⽂缩写是 A 。
A)RAM B)ROM C)EPROM D)EPRAM(4) 把汇编语⾔源程序翻译成⽬标程序的程序是 A 。
A)汇编程序 B)编辑程序C)解释程序 D)编译程序(5) 下列不能⽤作存储容量单位的是B。
A)Byte B)MIPS C)KB D)GB(6) ASCII码(含扩展)可以⽤⼀个字节表⽰,ASCII码值的个数为 B。
A)1024 B)256 C)128 D) 80(7) 计算机中所有信息的存储都采⽤A。
A)⼆进制 B)ASCII码 C)⼗进制 D)⼗六进制(8) 在计算机领域中,所说的“裸机”是指C。
A)单⽚机 B)单板机C)不安装任何软件的计算机 D)只安装操作系统的计算机(9) 微型计算机中的内存储器,通常采⽤ C 。
A)光存储器 B)磁表⾯存储器 C)半导体存储器 D)磁芯存储器(10) 显⽰器显⽰图像的清晰程度,主要取决于显⽰器的 D。
A)对⽐度 B)亮度 C)尺⼨ D)分辨率2. 填空题: 请将每⼀个空的正确答案写在横线上。
(1) 世界上第⼀台电⼦计算机完成于1946年。
(2) ⼈们⽤于记录事物情况的物理符号称为数据。
(3) 信息具有普遍存在性、信息的可处理性、可传递性和共享性及信息的传递必须依附于载体。
(4) 两位⼆进制可表⽰ 4 种状态。
(5) ⼀般来说,现代信息技术包含3个层次的内容:即信息基础技术、信息系统技术和信息应⽤技术。
(6)按照冯·诺依曼原理,计算机硬件系统由运算器、控制器、存储器、输⼊设备和输出设备五个基本部分组成。