c++上机作业5

一、求和

#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 str[50];
	int i=0,n=0,word=0,other=0;
	cout<<"输入字符串:";
	gets(str);
	while(str[i]!='\0')
	{  
		if(str[i]>='0'&&str[i]<='9') n++;
		else if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z') word++;
		else other++;
		i++;		
	}
	cout<<"其中的数字个数是: "<<n;
	cout<<"其中的字母个数是: "<<word;
	cout<<"其他字符个数是: "<<other;
}


三、数组分离

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值