ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ 程序设计 (西北工业大学)

2022-05-01 17:01:26  阅读:192  来源: 互联网

标签:p2 p3 p1 const Point ++ C++ 西北工业大学 程序设计


  NOJ练习一

  1、计算A+B

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a,b;
 7     cin >> a >> b;
 8     cout << a + b;
 9     
10     return 0;
11 }
View Code

  2、面积及体积的计算

 1 #include <iostream>
 2 using namespace std;
 3 
 4 const double PI = 3.1415926;
 5 int main()
 6 {
 7     double r, h;
 8     cin >> r >> h;
 9     printf("%.2f\n", 2*PI*r);
10     printf("%.2f\n", PI*r*r);
11     printf("%.2f\n", 4*PI*r*r);
12     printf("%.2f\n", 4.0/3*PI*r*r*r);
13     printf("%.2f\n", PI*r*r*h);
14     return 0;
15 }
View Code

  3、总成绩和平均成绩

#include <iostream>
using namespace std;

int main()
{
    double m,l,e;
    cin >> m >> l >> e;
    printf("%.6f\n", m+l+e);
    printf("%.6f\n", (m+l+e)/3);
    return 0;
}
View Code

  NOJ练习八

  1、三角形

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 
 5 class Point{
 6     double x;
 7     double y;
 8 public:
 9     Point(double x = 0.0, double y = 0.0){
10         this->x = x;
11         this->y = y;
12     }
13     double getX()const{
14         return x;
15     }
16     double getY()const{
17         return y;
18     }
19 };
20 
21 class Triangle{
22     Point p1,p2,p3;
23     double lengthOfTwoPoint(const Point& p1, const Point& p2){
24         return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX()) + 
25             (p1.getY()-p2.getY())*(p1.getY()-p2.getY()) );
26     }
27 public:
28     Triangle(const Point& p1, const Point& p2, const Point& p3){
29         if(isTriangle(p1,p2,p3))
30         {
31             this->p1 = p1;
32             this->p2 = p2;
33             this->p3 = p3;
34         }        
35     }
36     bool isTriangle(const Point& p1, const Point& p2, const Point& p3){
37         if( lengthOfTwoPoint(p1,p2)+lengthOfTwoPoint(p1,p3)>lengthOfTwoPoint(p2,p3)&&
38             lengthOfTwoPoint(p2,p1)+lengthOfTwoPoint(p2,p3)>lengthOfTwoPoint(p1,p3)&&
39             lengthOfTwoPoint(p3,p1)+lengthOfTwoPoint(p3,p2)>lengthOfTwoPoint(p1,p2)
40             ){
41             cout << "YES" << endl;    
42             return true;
43         }
44         cout << "NO" << endl;    
45         return false;
46     }
47 };
48 
49 int main()
50 {
51     double x1,y1,x2,y2,x3,y3;
52     cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
53     Point p1(x1,y1),p2(x2,y2),p3(x3,y3);
54     Triangle t(p1,p2,p3);    
55     return 0;
56 }
View Code

  5、时钟

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Clock{
 5     int hour;
 6     int minute;
 7     int second;
 8 public:
 9     Clock& operator++(){
10         if(++second >= 60){
11             second -= 60;
12             minute++;
13         }
14         if(minute >= 60){
15             minute -= 60;
16             hour++;
17         }
18         hour %= 24;
19         return *this;
20     }
21     Clock operator++(int){ //后置++
22         Clock c = *this;
23         if(++second >= 60){
24             second -= 60;
25             minute++;
26         }
27         if(minute >= 60){
28             minute -= 60;
29             hour++;
30         }
31         hour %= 24;
32         return c;
33     }
34         Clock& operator--(){
35         if(--second <0){
36             second += 60;
37             minute--;
38         }
39         if(minute < 0){
40             minute += 60;
41             hour--;
42         }
43         if(hour < 0)
44             hour += 24;
45         return *this;
46     }
47     Clock operator--(int){ //后置--
48         Clock c = *this;
49         if(--second <0){
50             second += 60;
51             minute--;
52         }
53         if(minute < 0){
54             minute += 60;
55             hour--;
56         }
57         if(hour < 0)
58             hour += 24;
59         return c;
60     }
61     void SetTime(int hour, int minute, int second){
62         this->hour = hour;
63         this->minute = minute;
64         this->second = second;
65     }
66     void ShowTime(){
67         printf("%02d:%02d:%02d\n",hour, minute, second);
68     }
69 };
70 
71 int main()
72 {
73     int n;
74     int h,m,s;
75     scanf("%d:%d:%d",&h,&m, &s);
76     
77     Clock t1,t2;
78     t1.SetTime(h,m,s);
79     
80     scanf("%d",&n);
81     switch(n){
82         case 1: t2=t1++; t1.ShowTime(); t2.ShowTime();break;
83         case 2: t2=t1--; t1.ShowTime(); t2.ShowTime();break;
84         case 3: t2=++t1; t1.ShowTime(); t2.ShowTime();break;
85         case 4: t2=--t1; t1.ShowTime(); t2.ShowTime();break;        
86     }    
87     return 0;
88 }
View Code

 

标签:p2,p3,p1,const,Point,++,C++,西北工业大学,程序设计
来源: https://www.cnblogs.com/GoldenEllipsis/p/16213265.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有