[nefu]C++程序设计与分析实验 - 锐格
第二章 C++基础
5330
| #include<iostream> |
| #include<iomanip> |
| |
| using namespace std; |
| |
| int main() |
| { |
| cout<<123<<endl<<setw(6)<<456<<endl; |
| return 0; |
| } |
5331
| #include<iostream> |
| |
| using namespace std; |
| |
| const double PI =3.14; |
| |
| int main() |
| { |
| int a; |
| cin>>a; |
| float s = a*a*PI; |
| cout<<"s is "<<s<<endl; |
| return 0; |
| } |
5332
| #include<iostream> |
| |
| using namespace std; |
| |
| int main() |
| { |
| void *a = nullptr; |
| int b; |
| char c; |
| cin>>b>>c; |
| a = &b; |
| printf("%d\n", *(int*)(a)); |
| a = &c; |
| cout<<*(char*)a<<endl; |
| return 0; |
| } |
5333
| #include<iostream> |
| |
| using std::cout; |
| using std::cin; |
| |
| template<typename T> |
| T getNum(T& num) |
| { |
| return num*num*num; |
| } |
| |
| int main() |
| { |
| int i; |
| long j; |
| double k; |
| cin>>i>>j>>k; |
| |
| printf("%d*%d*%d= %d\n", i, i, i, getNum(i)); |
| printf("%ld*%ld*%ld= %ld\n", j, j ,j , getNum(j)); |
| printf("%.2lf*%.2lf*%.2lf= %.3lf\n", k, k, k, getNum(k)); |
| return 0; |
| } |
5334
| #include<iostream> |
| |
| using std::cout; |
| using std::cin; |
| using std::endl; |
| |
| int main() |
| { |
| int *p=new int(); |
| *p=10; |
| cout<<*p<<endl; |
| delete(p); |
| return 0; |
| } |
5335
| #include<iostream> |
| |
| using std::cout; |
| using std::cin; |
| using std::endl; |
| |
| void swap(int &a,int &b) |
| { |
| int tmp = a; |
| a = b; |
| b = tmp; |
| } |
| |
| int main() |
| { |
| int x,y; |
| cin>>x>>y; |
| swap(x, y); |
| printf("x=%d y=%d", x, y); |
| return 0; |
| } |
5336
| #include<iostream> |
| |
| using std::cout; |
| using std::cin; |
| using std::endl; |
| |
| template<int n> |
| struct Fib{ |
| static constexpr int value = Fib<n-1>::value + Fib<n-2>::value; |
| }; |
| |
| template <> struct Fib<1>{ |
| static constexpr int value = 1; |
| }; |
| |
| template <> struct Fib<0>{ |
| static constexpr int value = 0; |
| }; |
| |
| int main() |
| { |
| constexpr static int values[] = { |
| Fib<1>::value, Fib<2>::value, Fib<3>::value, Fib<4>::value, Fib<5>::value, |
| Fib<6>::value, Fib<7>::value, Fib<8>::value, Fib<9>::value, Fib<10>::value, |
| Fib<11>::value, Fib<12>::value, Fib<13>::value, Fib<14>::value, Fib<15>::value, |
| Fib<16>::value, Fib<17>::value, Fib<18>::value, Fib<19>::value, Fib<20>::value |
| }; |
| for(auto& i : values) |
| { |
| cout<<i<<endl; |
| } |
| return 0; |
| } |
5337
| #include<iostream> |
| #include<cmath> |
| |
| using std::cout; |
| using std::cin; |
| using std::endl;using std::sqrt; |
| |
| template<typename T> |
| T sroot(T& num){ |
| return sqrt(num); |
| } |
| |
| int main() |
| { |
| int i; long l; double b; |
| cin>>i>>l>>b; |
| cout<<"sqrt(i)= "<<sroot(i)<<" sqrt(l)= "<<sroot(l)<<" sqrt(d)= "<<sroot(b)<<endl; |
| return 0; |
| } |
5338
| #include<iostream> |
| #include<cmath> |
| |
| using std::cout; |
| using std::cin; |
| using std::endl;using std::sqrt; |
| |
| template<typename T> |
| void swap(T& p1,T& p2) |
| { |
| auto tmp = p1; |
| p1=p2; |
| p2=tmp; |
| } |
| |
| int main() |
| { |
| int a, b; |
| cin >> a >> b; |
| |
| if (a > b) { |
| swap(a, b); |
| } |
| |
| cout << "x= " << a << " " << "y= " << b; |
| |
| return 0; |
| } |
第3章 类和对象
5339
| #include <iostream> |
| |
| using std::cin; using std::cout; |
| |
| class Score final { |
| private: |
| int mid; |
| int fin; |
| |
| public: |
| Score()=default; |
| ~Score()=default; |
| |
| void set(int mid_p, int fin_p) { |
| mid = mid_p; |
| fin = fin_p; |
| } |
| |
| void get() { |
| std::cout << "mid is " << mid << " " << "fin is " << fin << std::endl; |
| |
| |
| printf("total is %d\n", (int)(mid * 0.3 + fin * 0.7)); |
| } |
| }; |
| |
| int main() { |
| Score s1; |
| s1.set(80, 88); |
| s1.get(); |
| |
| Score s2; |
| s1.set(90, 92); |
| s1.get(); |
| |
| return 0; |
| } |
5340
| #include <iostream> |
| |
| using std::cin; using std::cout; |
| |
| class Score final { |
| private: |
| int mid; |
| int fin; |
| |
| public: |
| Score()=default; |
| ~Score()=default; |
| |
| void set(int mid_p, int fin_p) { |
| mid = mid_p; |
| fin = fin_p; |
| } |
| |
| void get() { |
| std::cout << "mid is " << mid << " " << "fin is " << fin << std::endl; |
| |
| |
| printf("total is %d\n", (int)(mid * 0.3 + fin * 0.7)); |
| } |
| }; |
| |
| int main() { |
| int a1,a2,b1,b2; |
| cin>>a1>>b1>>a2>>b2; |
| Score s1; |
| s1.set(a1, b1); |
| s1.get(); |
| |
| Score s2; |
| s1.set(a2, b2); |
| s1.get(); |
| |
| return 0; |
| } |
本文作者:Do1phln
本文链接:https://www.cnblogs.com/cjjcn/p/16118575.html
版权声明:本作品采用CC BY 4.0许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步