c++ 实验一
发布日期:2022-03-30 20:19:30 浏览次数:33 分类:博客文章

本文共 2808 字,大约阅读时间需要 9 分钟。

实验代码及运行结果

实验2-28

if else 语句

#include
using namespace std;int main(){ while(true) { char m; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<
>m; if(m=='A') { cout<<"data has been Added"<

switch语句

#include
using namespace std;int main(){ while(true) { char m; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<
>m; switch(m) { case 'A': cout<<"data has been Added"<

 

 

2-29

while语句

#include
#include
using namespace std;int main(){ int i=1,j,num=0; while(++i<=100) { j=2; while(j

do while

#include
#include
using namespace std;int main(){ int i=1,j,num=0; do { j=2; while(j

for 

#include
#include
using namespace std;int main(){ int i,j,num=0; for(i=2;i<=100;i++) { for(j=2;j

 

 

2-32

while

#include
using namespace std;int main(){ int i=80,j; cout<<"Please guess a numer from 1 to 100"<
>j; if(j>i) { cout<<"The number is smaller than your number"<

do while

#include
using namespace std;int main(){ int i=80,j; cout<<"Please guess a numer from 1 to 100"<
>j; if(j>i) { cout<<"The number is smaller than your number"<

 

2-34 多少种取法

#include
using namespace std;int jc(int);int main(){ int i, n,c; cin >> n; i = 3; c = jc(n) / (jc(i) * jc(n - i)); cout << c << endl; return 0;}int jc(int m){ int p=1,k; for (k=1; k<=m;k++) { p = p * k; } return p;}

 

输入共有5种球,从中取3个,得出共有10种可能。

穷举法求取法

#include
#include
using namespace std;void pd(int);int main(){ int a,b,c,count = 0; for (a = 1; a <=5;a++) { for (b = 1; b <= 5;b++) { for (c = 1; c <= 5;c++) { if(a!=b&&a!=c&&b!=c) { ++count; cout << count << ends; pd(a); pd(b); pd(c); cout << endl; } } } } return 0;}void pd(int x){ switch(x) { case 1: cout << setw(5)<<"Red" << ends; break; case 2: cout << setw(5)<<"Yellow" << ends; break; case 3: cout << setw(5) << "Blue" << ends; break; case 4: cout << setw(5) << "White" << ends; break; case 5: cout << setw(5) << "Black" << ends; break; }}

结果太长,截一小段

 

实验总结和体会

1、c和c++两者有相同也有不同,需多练习体会。

2、一些基础需要再看看再理解理解,过久没写不熟悉,导致写的很慢。

 

转载地址:https://www.cnblogs.com/aiwenzhuo/p/10547801.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:c++ 实验五 类的继承、派生和多态(2)
下一篇:c++实验二

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月15日 20时37分21秒