实验1:c++简单程序设计(1)

实验结论ios

P63   2-28c++

1)If..else.. 进行判断,break continue控制程序流程函数

#include<iostream>ui

using namespace std;spa

int main(){3d

        char choice;blog

        while(1){ci

        cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";it

        cin>>choice;io

        if(choice=='A'||choice=='a') cout<<"Dates have been added"<<endl;

        else if(choice=='D'||choice=='d') cout<<"Dates have been deleted"<<endl;

        else if(choice=='S'||choice=='s') cout<<"Dates have been sorted"<<endl;

        else if(choice=='Q'||choice=='q') break;

        else cout<<"no such choice"<<endl;

        }

        return 0;

}

 

2)switch循环语句

#include<iostream>

#include<cstdlib>

using namespace std;

int main(){

        char choice;

        while(1){

        cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";

        cin>>choice;

        switch(choice){

        case 'A':

                 cout<<"Dates have been added"<<endl;

                 break;

        case 'D':

                 cout<<"Dates have been deleted"<<endl;

                 break;

        case 'S':

                 cout<<"Dates have been sorted"<<endl;

                 break;

        case 'Q': exit(0);

    default: cout<<"no such choice"<<endl; break;

        }

        }

        return 0;

}

 

P63   2-291穷举法找质数

1)for循环

#include<iostream>

#include<cmath>

using namespace std;

int main(){

        int i,m,n,t=1;

        for(i=1;i<=100;i++)

        {

                 t=1;

                 if(i==1) continue;//将1单独分离,不知道是否有通法。

                 else if(i==2)

                 cout<<i<<" ";

                 else

                 {

                 m=sqrt(i);

                 for(n=2;n<=m;n++)

                 {

                         if(i%n==0) t=0;

                 }

                 if(t==1) cout<<i<<" ";

                 }

        }

        return 0;

}

 

2)do..while  循环

#include<iostream>

#include<cmath>

using namespace std;

int main(){

        int a=1,m,i;

        bool t; //尝试使用了布尔型,虽然不必。

        do{

                 i=2;

                 m=sqrt(a);

                 if(a==1) {

                         t=false;

                 }

                 else

                 {

                         do{

                                  if(a%i==0)

                                  {

                                          t=false;

                                          break;

                                  }

                                  i++;

                         }while(i<=m);

                         if(i>m) t=true;

                 }

                 if(t==true)

                         cout<<a<<" ";

                 a++;

        }while(a<=100);

        return 0;

}

 

3)while循环

#include<iostream>

#include<cmath>

using namespace std;

int main(){

        int i,a=1,m;

        while(a<=100)

        {

                 i=2;

                 if(a==1) {a++;continue;}

                 m=sqrt(a);

                 while(i<=m)

                 {

                         if(a%i==0) break;

                         i++;

                 }

                 if(i>m) cout<<a<<" ";

                 a++;

        }

        return 0;

}

 

P63  2-32

1)数字固定

#include<iostream>

using namespace std;

int main(){

        int i,n;

        n=40;

        cout<<"Please guess the number(1~100)"<<endl;

        while(1) //同窗介绍的屡次输入的方式,还有其余方法吗?

        {

        cin>>i;

        if(i>40&&i<=100) cout<<"bigger than the number"<<endl;

        else if(i<40&&i>=0) cout<<"lower than the number"<<endl;

        else if(i==n) break;

        else cout<<"out of range"<<endl;

        }

        cout<<"gradulation!you are right!"<<endl;

        return 0;

}

 

2)数字随机

#include<iostream>

#include<ctime>

using namespace std;

int main()

{

        int rnum,i;

        srand(time(NULL)); //尝试使用了随机种子。

        rnum=rand()%(100)+1;

        while(1)

        {

        cin>>i;

        if(i>rnum&&i<=100) cout<<"bigger than the number"<<endl;

        else if(i<rnum&&i>=0) cout<<"lower than the number"<<endl;

        else if(i==rnum) break;

        else cout<<"out of range"<<endl;

        }

        cout<<"gradulation!you are right!"<<endl;

        return 0;

}

 

组合数

P63   2-34

#include<iostream>

using namespace std;

int comp(int n,int k) //组合数函数

{

        if(k>n) return 0;

        else if(k==n||k==0) return 1;

        else return (comp(n-1,k-1)+comp(n-1,k));

}

int main(){

        int n,k,num;

        cin>>n>>k;

        num=comp(n,k);

        cout<<num<<endl;

        return 0;

}

 

实验总结与体会

1.常用for循环致使do..while..和while两种循环不是很熟练。

2srand的随机种子十分有趣,对抽取随机编号程序十分有效。

3对程序排版仍是不是很熟练,有的时候会显得很乱。

4c++的代码形式相对于c十分的简练有效,特别是函数的时候,能系统自主选择最合适的方案这个设定颇有趣

相关文章
相关标签/搜索