编程题

7-3 How many days passed (

Develop a programming to detemine how many days passed in the year upto the user-entered date. (Include this day.)
输入格式:
Input Specification: Each input file contains one test case, which gives a date in yyyy-mm-dd.

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a;
  6. cin>>a;
  7. int b=a/10000;
  8. int temp;
  9. int c,d;
  10. int num=0;
  11. if((b%4==0&&b%100!=0)||(b%400==0))
  12. {
  13. temp=a-b*10000;
  14. c=temp/100;
  15. d=temp%100;
  16. if(c==1)
  17. num=d;
  18. if(c==2)
  19. num=31+d;
  20. if(c==3)
  21. num=31+29+d;
  22. if(c==4)
  23. num=31+29+31+d;
  24. if(c==5)
  25. num=31+29+31+30+d;
  26. if(c==6)
  27. num=31+29+31+30+31+d;
  28. if(c==7)
  29. num=31+29+31+30+31+30+d;
  30. if(c==8)
  31. num=31+29+31+30+31+30+31+d;
  32. if(c==9)
  33. num=31+29+31+30+31+30+31+31+d;
  34. if(c==10)
  35. num=31+29+31+30+31+30+31+31+30+d;
  36. if(c==11)
  37. num=31+29+31+30+31+30+31+31+30+31+d;
  38. if(c==12)
  39. num=31+29+31+30+31+30+31+31+30+31+30+d;
  40. }
  41. else if(b%4!=0)
  42. {
  43. temp=a-b*10000;
  44. c=temp/100;
  45. d=temp%100;
  46. if(c==1)
  47. num=d;
  48. if(c==2)
  49. num=31+d;
  50. if(c==3)
  51. num=31+28+d;
  52. if(c==4)
  53. num=31+28+31+d;
  54. if(c==5)
  55. num=31+28+31+30+d;
  56. if(c==6)
  57. num=31+28+31+30+31+d;
  58. if(c==7)
  59. num=31+28+31+30+31+30+d;
  60. if(c==8)
  61. num=31+28+31+30+31+30+31+d;
  62. if(c==9)
  63. num=31+28+31+30+31+30+31+31+d;
  64. if(c==10)
  65. num=31+28+31+30+31+30+31+31+30+d;
  66. if(c==11)
  67. num=31+28+31+30+31+30+31+31+30+31+d;
  68. if(c==12)
  69. num=31+28+31+30+31+30+31+31+30+31+30+d;
  70. }
  71. cout<<num;
  72. return 0;
  73. }

7-4 piecewise function

Develop a program to calculate a piecewise function.
image.png
Input Specification:
Input a decimal number (real number) for x

  1. #include<iostream>
  2. #include<string>
  3. #include <iomanip>
  4. using namespace std;
  5. int main()
  6. {
  7. float x,y;
  8. cin>>x;
  9. if(x<0)
  10. {
  11. y=5*x*x+7;
  12. }
  13. else if(x>=20)
  14. {
  15. y=25*x-9;
  16. }
  17. else if(x<20&&x>=0)
  18. {
  19. y=3*x*x+2*x-3;
  20. }
  21. cout<<"x="<<fixed<<setprecision(2)<<x<<",y="<<setprecision(2)<<y;
  22. }

7-2 number of “pass” (8 分)

Input a series of characters, count the number of “pass”.
Input:
A series of characters end with enter. (<80)
Output:
The number “pass”
Input sample:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str;
  7. int a=0;
  8. getline(cin,str);
  9. for(int i=0;i<=sizeof(str);i++)
  10. {
  11. if(str[i]=='p')
  12. {a++;}
  13. }
  14. cout<<a<<endl;
  15. }

7-1 sum and average (8 分)

Input 4 integers, print their sum and average.
Input Specification:
Input 4 integers in one line, separate each other by 1 space.
Output Specification:

  1. #include <stdio.h>
  2. int main ()
  3. {
  4. int a,b,c,d;
  5. int Sum;
  6. float Average;
  7. scanf("%d %d %d %d",&a,&b,&c,&d);
  8. Sum=a+b+c+d;
  9. Average=Sum/4.0;
  10. printf("Sum = %d; Average = %.2f",Sum,Average);
  11. return 0;
  12. }

7-1 求n个整数的最小值

  1. #include <iostream>
  2. using namespace std;
  3. int Min(int a[],int n){
  4. int min=a[0];
  5. for(int i=0;i<n;i++){
  6. if(min>a[i])
  7. min=a[i];
  8. }
  9. return min;
  10. }
  11. int main(){
  12. int n;
  13. cin >> n;
  14. int a[n];
  15. for(int i=0;i<=n;i++)
  16. cin >> a[i];
  17. cout << Min(a,n);
  18. return 0;
  19. }

7-12 number of a-e (10 分)

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str;
  7. getline(cin,str);
  8. int a_count=0,b_count=0,c_count=0,d_count=0,e_count=0;
  9. int str_len=str.length();
  10. char ch;
  11. for(int i=0;i<str_len;i++)
  12. {
  13. if(str[i]=='a')
  14. a_count++;
  15. else if(str[i]=='b')
  16. b_count++;
  17. else if(str[i]=='c')
  18. c_count++;
  19. else if(str[i]=='d')
  20. d_count++;
  21. else if(str[i]=='e')
  22. e_count++;
  23. }
  24. cout<<"a"<<"("<<a_count<<")"<<"b"<<"("<<b_count<<")"<<"c"<<"("<<c_count<<")"<<"d"<<"("<<d_count<<")"<<"e"<<"("<<e_count<<")"<<endl;
  25. }

7-2 number of “pass”

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. char s[80];
  5. cin.get(s,80);
  6. int pass=0;
  7. for(int i=0;i<80;i++){
  8. if(s[i]=='p'&&s[i+1]=='a'&&s[i+2]=='s'&&s[i+3]=='s')
  9. ++pass;
  10. }
  11. cout << pass;
  12. return 0;
  13. }

7-3 Count words

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int re_search(string st)//this is the first time you're flying by yourself
  5. {
  6. int i = 0, temp = 0, num = 0,len;
  7. for (i = 0; st[i] != '\0'; i++)
  8. {
  9. if ((st[i] != ' ')&&(temp == 0))
  10. {
  11. temp = 1;
  12. num++;
  13. }
  14. else if (st[i] ==' ')
  15. temp = 0;
  16. }
  17. return num;
  18. }
  19. int main()
  20. {
  21. string user_input;
  22. getline(cin,user_input);
  23. cout << re_search(user_input) << endl;
  24. }

7-4 Count the character numbers of each category

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. char s[80];
  5. cin.get(s,80);
  6. int letter=0,space=0,digit=0,other=0;
  7. for(int i=0;s[i]!='\0';i++){
  8. if((s[i]<='z'&&s[i]>='a')||(s[i]<='Z'&&s[i]>='A'))
  9. letter++;
  10. else if(s[i]==' '||s[i]=='\t')
  11. space++;
  12. else if(s[i]>='0'&&s[i]<='9')
  13. digit++;
  14. else
  15. other++;
  16. }
  17. cout<<"letter="<<letter<<",space="<<space<<",digit="<<digit<<",other="<<other;
  18. return 0;
  19. }

7-16 mm-dd-yyyy to yyyy-mm-dd

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(){
  5. string s;
  6. cin >> s;
  7. string yyyy = s.substr(6,4);
  8. string mm = s.substr(0,2);
  9. string dd = s.substr(3,2);
  10. string re = yyyy+"-"+mm+"-"+dd;
  11. cout << re;
  12. return 0;
  13. }

7-17 Converting Fahrenheit degree to Centigrade degree

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int c,f;
  5. cin >> f;
  6. c = (f-32)*5/9;
  7. cout << "the corresponding centigrade degree:c=" << c;
  8. return 0;
  9. }

7-18 Decimal Equivalent of a Binary Number

  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. #include <cstring>
  5. using namespace std;
  6. int main(){
  7. char str[10];
  8. cin >> str;
  9. char cha[10] = {0};
  10. strcpy(cha,str);
  11. string str2 = " * ";
  12. string str3 = " + ";
  13. string str1[10];
  14. for(int i=strlen(str)-1,j=0; i>=0,j<=strlen(str)-1; i--,j++){
  15. if(j != strlen(str)-1){
  16. str1[j] += cha[i];
  17. str1[j] += str2;
  18. str1[j] += std::to_string((int)pow(2,j));
  19. str1[j] += str3;
  20. }else{
  21. str1[j] += cha[i];
  22. str1[j] += str2;
  23. str1[j] += std::to_string((int)pow(2,j));
  24. }
  25. }
  26. cout << str << " = ";
  27. for(int i=0; i<=strlen(str); i++)
  28. cout << str1[i];
  29. return 0;
  30. }

7-19 Salary Calculator_1

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int h;
  5. cin>>h;
  6. if(h<=40)
  7. cout<<h*10;
  8. else
  9. cout<<400+(h-40)*15;
  10. return 0;
  11. }

7-20 Say Hello to Integers

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int A,B;
  5. cin>>A>>B;
  6. cout<<"Hello, "<<A<<" and "<<B<<"!";
  7. return 0;
  8. }

7-21 角度 -> 弧度*

  1. #include <iostream>
  2. #define _USE_MATH_DEFINES
  3. #include <math.h>
  4. using namespace std;
  5. int main(){
  6. double d1,d2,d3,s,res;
  7. cin >> d1 >> d2 >> d3;
  8. s = d1 * 3600 + d2 * 60 + d3;
  9. res = double(s) * M_PI / 180 / 3600;
  10. printf("%.10f\n",res);
  11. return 0;
  12. }

7-22 sum and average

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int A,B,C,D;
  5. cin >> A >> B >> C >> D;
  6. double Sum,Average;
  7. Sum = A+B+C+D;
  8. Average = Sum/4;
  9. printf("Sum = %.0f; Average = %.2f",Sum,Average);
  10. system("pause");
  11. return 0;
  12. }

7-23 ZY01-输出处理输出-理财计算

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. double ratio;
  5. cin >> ratio;
  6. if(0 <= ratio && ratio <= 10){
  7. double money = 10000*(1+0.01*ratio)*(1-0.01*ratio);
  8. printf("%.3f",money);
  9. }
  10. system("pause");
  11. return 0;
  12. }

7-24 Area of circle

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main(){
  5. float r,area;
  6. cin>>r;
  7. area = r*r*3.1415;
  8. cout<<"r = "<<setprecision(2)<<fixed
  9. <<r<<", area = "<<setprecision(2)<<fixed<<area;
  10. return 0;
  11. }

7-26 Output two integers

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int A,B;
  5. cin>>A>>B;
  6. cout<<"The numbers are "<<A<<" and "<<B;
  7. return 0;
  8. }

7-50 How many days passed

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a;
  6. cin>>a;
  7. int b=a/10000;
  8. int temp;
  9. int c,d;
  10. int num=0;
  11. if((b%4==0&&b%100!=0)||(b%400==0))
  12. {
  13. temp=a-b*10000;
  14. c=temp/100;
  15. d=temp%100;
  16. if(c==1)
  17. num=d;
  18. if(c==2)
  19. num=31+d;
  20. if(c==3)
  21. num=31+29+d;
  22. if(c==4)
  23. num=31+29+31+d;
  24. if(c==5)
  25. num=31+29+31+30+d;
  26. if(c==6)
  27. num=31+29+31+30+31+d;
  28. if(c==7)
  29. num=31+29+31+30+31+30+d;
  30. if(c==8)
  31. num=31+29+31+30+31+30+31+d;
  32. if(c==9)
  33. num=31+29+31+30+31+30+31+31+d;
  34. if(c==10)
  35. num=31+29+31+30+31+30+31+31+30+d;
  36. if(c==11)
  37. num=31+29+31+30+31+30+31+31+30+31+d;
  38. if(c==12)
  39. num=31+29+31+30+31+30+31+31+30+31+30+d;
  40. }
  41. else if(b%4!=0)
  42. {
  43. temp=a-b*10000;
  44. c=temp/100;
  45. d=temp%100;
  46. if(c==1)
  47. num=d;
  48. if(c==2)
  49. num=31+d;
  50. if(c==3)
  51. num=31+28+d;
  52. if(c==4)
  53. num=31+28+31+d;
  54. if(c==5)
  55. num=31+28+31+30+d;
  56. if(c==6)
  57. num=31+28+31+30+31+d;
  58. if(c==7)
  59. num=31+28+31+30+31+30+d;
  60. if(c==8)
  61. num=31+28+31+30+31+30+31+d;
  62. if(c==9)
  63. num=31+28+31+30+31+30+31+31+d;
  64. if(c==10)
  65. num=31+28+31+30+31+30+31+31+30+d;
  66. if(c==11)
  67. num=31+28+31+30+31+30+31+31+30+31+d;
  68. if(c==12)
  69. num=31+28+31+30+31+30+31+31+30+31+30+d;
  70. }
  71. cout<<num;
  72. return 0;
  73. }

7-53 piecewise function

  1. #include<iostream>
  2. #include<string>
  3. #include <iomanip>
  4. using namespace std;
  5. int main()
  6. {
  7. float x,y;
  8. cin>>x;
  9. if(x<0)
  10. {
  11. y=5*x*x+7;
  12. }
  13. else if(x>=20)
  14. {
  15. y=25*x-9;
  16. }
  17. else if(x<20&&x>=0)
  18. {
  19. y=3*x*x+2*x-3;
  20. }
  21. cout<<"x="<<fixed<<setprecision(2)<<x<<",y="<<setprecision(2)<<y;
  22. }

7-68 单继承中的构造函数与析构函数

  1. #include <iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. const double PI=3.14;
  5. //请编写你的代码
  6. class Dot{
  7. private:
  8. float x;
  9. float y;
  10. public:
  11. Dot(float a,float b):x(a),y(b){
  12. cout<<"Dot constructor called"<<endl;
  13. }
  14. ~Dot(){
  15. cout<<"Dot destructor called"<<endl;
  16. }
  17. };
  18. class Cir:public Dot{
  19. private:
  20. float r;
  21. public:
  22. Cir(float a,float b,float c):Dot(a,b),r(c){
  23. cout<<"Cir constructor called"<<endl;
  24. }
  25. ~Cir(){
  26. cout<<"Cir destructor called"<<endl;
  27. }
  28. double getArea(){
  29. return PI*r*r;
  30. }
  31. };
  32. int main(){
  33. float x,y,r;
  34. cin>>x>>y>>r;
  35. Cir c(x,y,r);
  36. cout<<fixed<<setprecision(2)<<c.getArea()<<endl;
  37. //system("pause");
  38. return 0;
  39. }

选择题

A}W2$)YZ5(X3M3YO5M{@4HS.png
S$R%$0M90NZ(ZHYCQTU5IAL.png
TMA2)}C9Z6F81[]OHK{D1RN.png
f3b0bd253e7aa1d3df5793723e1f02c.jpg6b305b6588985fd6b440080d057ef43.jpg
f788df291933fb0a2d3998e74bf8e6f.jpg
384d3de9c4c73e7716151b2bcec9048.jpg
e334c1e32c378a78b421334e2a4c98f.jpg

判断题

image.png

函数题

6-2 时钟模拟

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class MyTime{
  5. public:
  6. MyTime(int h=0, int m=0, int s=0):hour(h),minute(m),second(s){};
  7. MyTime operator++();
  8. friend istream & operator >>(istream&, MyTime&);
  9. friend ostream & operator <<(ostream&, MyTime&);
  10. private:
  11. int hour,minute,second;
  12. };
  13. MyTime MyTime::operator ++(){
  14. second++;
  15. if(second==60){
  16. minute++;
  17. second=0;
  18. if(minute==60){
  19. hour++;
  20. minute=0;
  21. }
  22. }
  23. hour %= 24;
  24. return *this;
  25. }
  26. istream &operator >> (istream &is, MyTime &t){
  27. is >> t.hour >> t.minute >> t.second;
  28. return is;
  29. }
  30. ostream &operator << (ostream &os, MyTime &t){
  31. os << t.hour << ":" << t.minute << ":" << t.second;
  32. return os;
  33. }

6-3 复数的加减运算(运算符重载

  1. class CComplex{
  2. public:
  3. CComplex(){
  4. real=0;
  5. imag=0;
  6. }
  7. CComplex(double r, double i){
  8. real=r;
  9. imag=i;
  10. }
  11. friend istream& operator << (istream&, CComplex&);
  12. friend ostream& operator >> (ostream&, CComplex&);
  13. CComplex operator+(CComplex c){
  14. return CComplex(real+c.real, imag+c.imag);
  15. }
  16. CComplex operator-(CComplex c){
  17. return CComplex(real-c.real, imag-c.imag);
  18. }
  19. double real, imag;
  20. };
  21. istream &operator >>(istream& is, CComplex& c){
  22. is >> c.real >> c.imag;
  23. return is;
  24. }
  25. ostream &operator <<(ostream& os, CComplex& c){
  26. os << c.real << showpos << c.imag << "i" << noshowpos << endl;
  27. }

6-6 使用类计算矩形的面积

  1. void Rectangle::setLength(int l)//设置矩形的长度
  2. {
  3. length=l;
  4. }
  5. void Rectangle::setWidth(int w) //设置矩形的宽度
  6. {
  7. width=w;
  8. }
  9. int Rectangle::getArea() //计算并返回矩形的面积
  10. {
  11. return width*length;
  12. }

6-7 Point类的声明和实现

  1. class Point
  2. {
  3. private:
  4. int x, y;
  5. public:
  6. void setX(double x1) { x = x1; }
  7. void setY(double y1) { y = y1; }
  8. double getX() { return x; }
  9. double getY() { return y; }
  10. };

6-8 圆类的定义

  1. class Circle{
  2. double r,area;
  3. double PI = 3.14159;
  4. public:
  5. void setR(double r){
  6. this->r = r;
  7. }
  8. double getR(){
  9. return r;
  10. }
  11. double getArea(){
  12. return area = r*r*PI;
  13. }
  14. };

6-9 面积计算器(函数重载)

  1. int area(int x,int y){
  2. return x*y;
  3. }
  4. int area(int x,int y,int z){
  5. return (x*y+x*z+z*y)*2;
  6. }

6-10 两个整数的交换

  1. void swap ( int &a, int &b ){
  2. int term;
  3. term = a;
  4. a = b;
  5. b = term;
  6. }

6-25 类的声明与成员函数的实现—Car类

  1. void Car::disp_welcomemsg()
  2. {
  3. cout<<"Welcome to the car world!"<<endl;
  4. }
  5. void Car::set_wheels(int wheels)
  6. {
  7. m_nWheels=wheels;
  8. }
  9. int Car::get_wheels()
  10. {
  11. return m_nWheels;
  12. }

6-69 狗的继承

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Animal{
  5. int age;
  6. public:
  7. Animal(){}
  8. Animal(int age){
  9. this->age = age;
  10. }
  11. int getAge(){
  12. return age;
  13. }
  14. };
  15. class Dog:public Animal{
  16. int age;
  17. string color;
  18. public:
  19. Dog(int age,string color):Animal(){
  20. this->age = age;
  21. this->color = color;
  22. }
  23. void showInfor(){
  24. cout<<"age:"<<age<<endl;
  25. cout<<"color:"<<color<<endl;
  26. }
  27. };

6-70 派生类的定义和使用

  1. class Animal{
  2. public:
  3. Animal(){}
  4. void speak(){
  5. cout<<"animal language!"<<endl;
  6. }
  7. };
  8. class Cat:public Animal{
  9. string m_strName;
  10. public:
  11. Cat(string name):Animal(){
  12. m_strName = name;
  13. }
  14. void print_name(){
  15. cout<<"cat name: "<<m_strName<<endl;
  16. }
  17. };

6-71 汽车类的继承

  1. Vehicle::Vehicle(int wheels,float weight){
  2. this->wheels = wheels;
  3. this->weight = weight;
  4. }
  5. int Vehicle::get_wheels(){
  6. return wheels;
  7. }
  8. float Vehicle::get_weight(){
  9. return weight;
  10. }
  11. float Vehicle::wheel_load(){}
  12. void Vehicle::show()
  13. {
  14. cout<<"Type:Vehicle"<<endl;
  15. cout<<"Wheel:"<<get_wheels()<<endl;
  16. cout<<"Weight:"<<get_weight()<<"kg"<<endl;
  17. }
  18. class Car:public Vehicle{
  19. private:
  20. int passenger_load;
  21. public:
  22. Car(int wheels,float weight,float passenger):Vehicle(wheels,weight)
  23. {
  24. passenger_load=passenger;
  25. }
  26. void show()
  27. {
  28. cout<<"Type:Car"<<endl;
  29. Vehicle::show();
  30. cout<<"Load:"<<passenger_load<<" persons"<<endl;
  31. }
  32. };

6-4 鸭子也是鸟

  1. class Bird{
  2. public:
  3. void speak(){
  4. cout << "Jiu-Jiu-Jiu" << endl;
  5. }
  6. };
  7. class Duck: public Bird{
  8. private:
  9. string name;
  10. public:
  11. Duck(string name){
  12. this->name = name;
  13. }
  14. void printName(){
  15. cout << "Dird Name:" << name << endl;
  16. }
  17. };

6-1 重载+-运算符

  1. A A::operator+(A&a)
  2. {
  3. return A(x+a.x,y+a.y);
  4. }
  5. A A::operator-(A&a)
  6. {
  7. return A(x-a.x,y-a.y);
  8. }

填空题

5.1#include using namespace std ; void swap_vals( float& val1, float& val2 ) ; main() {

  1. #include <iostream>
  2. using namespace std ;
  3. void swap_vals( float& val1, float& val2 ) ;
  4. main()
  5. {
  6. float num1, num2 ;
  7. cin>>
  8. num1>num2
  9. swap_vals
  10. float& val1,float& val2
  11. 第二个题
  12. #include <iostream>
  13. #include <iomanip>
  14. using namespace std ;
  15. class
  16. bank_account
  17. class
  18. public
  19. private
  20. account_number = acc_no;
  21. bank_account
  22. fixed

#include using namespace std; class Point{ double x,y;

  1. static int number;
  2. number++;
  3. number--;
  4. number
  5. int Point::staic int number=0;

5-2

已知平面上的一点由其横纵坐标来标识。本题要求按照已给代码和注释完成一个基本的“点”类的定义(坐标均取整型数值)。并通过主函数中的点类对象完成一些简单操作,分析程序运行结果,将答案写在对应的空格中。

  1. iostream
  2. private:
  3. public:
  4. Point
  5. x=a
  6. y=b
  7. Point&
  8. ~Point()
  9. "("<<x<<","<<y<<")"
  10. cin>>a>>b;
  11. return 0;
  12. (0,0)
  13. (10,10)
  14. Deconstructed Point(0,0)
  15. Deconstructed Point(10,10)