4点起步C++(第四集)

本集的主要内容,简单程序设计

1.输出 Good Moring 和Welcome to China!

#include <iostream>
using namespace std;
int main()
{
    cout<<"Good Moring!"<<endl;
    cout<<"Welcome to China!"<<endl;
    return 0;
}

运行
在这里插入图片描述


2.求出下列数据类型,表达式火变量在内存中占的字节的大小。
1). int , short, float, double, int*, double*, int**;
2). 2.5+8+‘A’, float(5+2.0);
3).int i[10]; float f [10]; char s [ ]=“str”;

#include <iostream>
using namespace std;
int main()
{
    cout<<"The size of int is:"<<sizeof(int)<<endl;
    cout<<"The size of short is:"<<sizeof(short)<<endl;
    cout<<"The size of float is:"<<sizeof(float)<<endl;
    cout<<"The size of int* is:"<<sizeof(int*)<<endl;
    cout<<"The size of double* is:"<<sizeof(double*)<<endl;
    cout<<"The size of int** is:"<<sizeof(int**)<<endl;
    cout<<"The size of 2.5+8+'A' is:"<<sizeof(2.5+8+'A')<<endl;
    cout<<"The size of float(5+2.0) is:"<<sizeof(float(5+2.0))<<endl;
    int i[10];
    cout<<"The size of i[10] is :"<<sizeof(i)<<endl;
    char s[]="Hello World";
    char *p = "Hello World";
    cout<<"The size of s is:"<<sizeof(s)<<endl;
    cout<<"The size of *p is:"<<sizeof(p)<<endl;
    return 0;
}

运行

在这里插入图片描述


3.简单计算器

#include <iostream>
using namespace std;
int main()
{
    double a,b,result;
    char c;
    int flag=0;
    cout<<"Input a number:";
    cin>>a;
    cout<<"choose calculate type:";
    cin>>c;
    cout<<"Input other number:";
    cin>>b;
    switch(c)
    {
        case'+':result = a+b;break;
        case'-':result = a-b;break;
        case'*':result = a*b;break;
        case'/':result = a/b;break;
        default : flag=1;
    }
    if(flag==1||((c=='/')&&(b==0)))
        cout<<"Iuput is wrong!";
    else
        cout<<a<<c<<b<<"=:"<<result<<endl;
    return 0;
}

4.求s(n)=1! - 2! + 3! - 4! + 5!.. n!的值

#include <iostream>
using namespace std;
int main()
{
    int fac = 1,sum=0;
    int i,k,n;
    cin>>n;
    for(i=1,k=1;i<=n;i++){
        fac=fac*i;
        sum+=k*fac;
        k=-1*k;
    }
    cout<<"sum="<<sum<<endl;
    return 0;
}

示例:输入数字3

在这里插入图片描述


5.一个10X10的方阵,每个格子里的数字的值是它所在的行值与列值相乘的结果,求出阴影部分的数值的乘积,非阴影部分的和。
在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   double result1=1,result2=0;
   for(int i=1;i<=10;i++){
    for(int j=1;j<=10;++j){
        if(((i>=1)&&(i<=4))&&((j>=1)&&(j<=7))
           ||((i>=7)&&(i<=9))&&((j>=7)&&(j<=9)))
           result1 = result1*i*j;
           else
           result2 = result2+i*j;
    }
   }
   cout<<"The product is :"<<result1<<endl;
   cout<<"The sum is:"<<result2<<endl;
    return 0;
}

运行

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值