第五次C++作业

来源:互联网 发布:网狐6603数据库文档 编辑:程序博客网 时间:2024/06/17 03:16

项目一 求和

#include <iostream>  using namespace std;  int main()  {      int a[10],x=0,y=0,z=0;      cout<<"输入10个数:";      for(x=0;x<10;x++)      {          cin>>a[x];         if(a[x]>=0)              y+=a[x];          else              z+=a[x];      }      cout<<"正数之和为:"<<y;      cout<<"负数之和为:"<<z;    }  
项目二  字符串

#include<iostream>    #include<cstdio>    using namespace std;    int main()    {        char a[50];        int i=0,n=0,j=0,k=0;        cout<<"请输入字符串"<<endl;        gets(a);        while(a[i]!='\0')        {            if(a[i]>='0'&&a[i]<='9')                n++;            else if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))                j++;            else               k++;            i++;        }        cout<<"其中的数字个数是: "<<n<<endl;        cout<<"其中的字母个数是: "<<j<<endl;        cout<<"其中的其他字符个数是: "<<k<<endl;        return 0;    }

项目三  数组分离

#include<iostream>  using namespace std;  int main()  {      int a[10],b[10],c[10],num,x,y=0,z=0;      cout<<"请输入10个数放在数组a";        for(x=0;x<10;x++)      {         cout<<"第"<<x+1<<"个数为:";           cin>>num;       a[x]=num;          if(num%2==1)         {             b[y]=num;            y++;          }          else           {              c[z]=num;              z++;          }      }      cout<<"数组a是:";      for(x=0;x<10;x++)          cout<<a[x]<<", ";     cout<<"奇数数组b有";        for(x=0;x<y;x++)          cout<<b[x]<<", ";      cout<<endl;      cout<<"偶数数组c有";        for(x=0;x<z;x++)          cout<<c[x]<<", ";  }  #include<iostream>using namespace std;int main(){int a[10],b[10],c[10],num,x,y=0,z=0;cout<<"请输入10个数放在数组a";  for(x=0;x<10;x++){cout<<"第"<<x+1<<"个数为:"; cin>>num;a[x]=num;if(num%2==1){b[y]=num;y++;}else {c[z]=num;z++;}}cout<<"数组a是:";for(x=0;x<10;x++)cout<<a[x]<<", ";cout<<"奇数数组b有";  for(x=0;x<y;x++)cout<<b[x]<<", ";cout<<endl;cout<<"偶数数组c有";  for(x=0;x<z;x++)cout<<c[x]<<", ";}


0 0
原创粉丝点击