编程练习2

1. 输入学生成绩,输出等级,规则如下:

90~100    A

80~89     B

70~79     C

60~69     D

不及格E

 

#include<stdio.h>
#include <iostream>
using namespace std;
 
int main()
{
    int score;
    cout<<"please input scores"<<endl;
    cin>>score;
    cout<<"the grade is: ";
    if((score>=90)&&(score<=100))
        cout<<"A"<<endl;
    else if((score>=80)&&(score<=89))
        cout<<"B"<<endl;
    else if((score>=70)&&(score<=79))
        cout<<"C"<<endl;
    else if((score>=60)&&(score<=69))
        cout<<"D"<<endl;
    else cout<<"failed"<<endl;
}

2.计算器程序,用户输入运算数和四则运算符,输出计算结果。

 

#include<stdio.h>
#include <iostream>
#include<math.h>
using namespace std;
 
int main()
{
    while (1)
    {
        int a,b,result;
        char s;
        cout<<"please input two operation numbers"<<endl;
        cin>>a>>b;
        cout<<"please input the operation you want to do"<<endl;
        cin>>s;
        switch (s)
        {
 
        case('+'):
        {
            result=a+b;
            cout<<a<<"+"<<b<<"="<<result<<endl;
            break;
        }
        case('-'):
        {
            result=a-b;
            cout<<a<<"-"<<b<<"="<<result<<endl;
            break;
        }
        case('*'):
        {
            result=a*b;
            cout<<a<<"*"<<b<<"="<<result<<endl;
            break;
        }
        case('/'):
        {
            result=a/b;
            cout<<a<<"/"<<b<<"="<<result<<endl;
            break;
        }
        }
    }
    return 0;
}
 


3.输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。

 

#include<stdio.h>
#include <iostream>
#include<string.h>
using namespace std;
 
int main()
{
    while (1)
    {
        int i;
        int a=0;
        int b=0;
        int c=0;
        int d=0;
        char str[100];
        cout<<"please input a string"<<endl;
        gets(str); // 这里不能用cin
        cout<<str<<endl;
        for(i=0; i<=strlen(str); i++)
        {
            if(((str[i]>='A')&&(str[i]<='Z'))||((str[i]>='a')&&(str[i]<='z')))
                a++;
            else if ((str[i]<='9')&&(str[i]>='0'))
                b++;
            else if(str[i]==' ')   //注意是“==”!!
                c++;
            else d++;
 
        }
        cout<<"the NO. of the letter is:"<<a<<endl;
        cout<<"the NO. of the number is:"<<b<<endl;
        cout<<"the NO. of the blank is:"<<c<<endl;
        cout<<"the NO. of other character is:"<<d-1<<endl; //程序会自动将后面的结束符'\n'算上
    }
    return 0;
}
 

注意:

(1)在默认情况下,运算符“>>”将跳过空白符,然后读入后面的与变量类型相对应的值。因此,给一组变量输入值时可用空白符(空格、回车或Tab键)将键入的数值间隔开。例如

    

int i;
    float x;
    cin>>i>>x;

在输入时只需输入下面形式即可:

    23  56.78

    23

    56.78

(2) 当输入字符串时,运算符“>>”的作用是跳过空白,读入后面的非空白字符,直到遇到另一个空白字符为止,并在串尾放一个字符‘\0’。

 

正因为有此特点,所以在使用cin输入一个中间有空格的字符串时,会出现问题:

例如:

  

 char* str;
   cin>>str; 

当键入的字符串为:

Object_Oriented Programming!

   结果是:

 str指向的字符串为:“Object_Oriented”

解决方法:

void getstring()
{  cout<<“请输入字符串:”<<endl;
   gets(str);
}


当键入的字符串为:

Object_Oriented Programming!

   结果是:

   str指向的字符串为:

   

“Object_Oriented Programming!”

注:也可以使用cin.getline(),但gets()更简单

 

4.编写一个程序。该程序读取输入直到遇到#字符,然后报告读取的空格数目、读取的换行符数目以及读取的所有其他字符数目。

#include<stdio.h>
#include <iostream>
#include<string.h>
using namespace std;
 
int main()
{
    while (1)
    {
 
        int blank=0;
        int turn=0;
        int other=0;
        char ch;
        cout<<"please input a string,end up with '#':"<<endl;
        while((ch=getchar())!='#')  //注意符号优先级
        {
            if(ch==' ') blank++;
            else if (ch=='\n') turn++;
            else other++;
        }
 
        cout<<"the NO. of the blank is:"<<blank<<endl;
        cout<<"the NO. of the  newline is:"<<turn<<endl;
        cout<<"the NO. of other character is:"<<other<<endl; 
    }
    return 0;
}
 


5.编写一个程序。该程序读取整数,直到输入0。输入终止后,程序应该报告输入的偶数

(不包括0)总个数,偶数的平均值,输入的奇数总个数以及奇数的平均值。

 

 

#include<stdio.h>
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
    int even=0;
    int odd=0;
    int e_sum=0;
    int o_sum=0;
    int a;
    cout<<"please input a int array,end up with 0:"<<endl;
    while(1)
    {
        cin>>a;
        //scanf("%d",&a);
        if (a== 0) break;
        if(a%2== 0)
        {
            even++;
            e_sum+=a;
        }
        else
        {
            odd++;
            o_sum+=a;
        }
 
    }
    cout<<"the NO. of the even NO. is:"<<even<<" and its average is :"<<e_sum/even<<endl;
    cout<<"the NO. of the odd NO. is:"<<odd<<" and its average is :"<<o_sum/odd<<endl;
    return 0;
}


6.输入年月日,计算该天是该年的第多少天?(请使用case)

#include<stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int Total = 0,Day = 0,Month = 0,Year = 0,Leap = 0;
    cout<<"Please input: Year Month Day"<<endl;
    scanf("%d %d %d",&Year,&Month,&Day);
    switch(Month)
    {
    case 1:
        Total=0;
        break;
    case 2:
        Total=31;
        break;
    case 3:
        Total=59; // 默认为平年算
        break;
    case 4:
        Total=90;
        break;
    case 5:
        Total=120;
        break;
    case 6:
        Total=151;
        break;
    case 7:
        Total=181;
        break;
    case 8:
        Total=212;
        break;
    case 9:
        Total=242;
        break;
    case 10:
        Total=273;
        break;
    case 11:
        Total=303;
        break;
    case 12:
        Total=334;
        break;
    default:
        printf("error\n");
        break;
    }
    Total=Total+Day;
    if( Month > 2 && ( (Year%4 == 0 && Year%100 != 0) || (Year%400) == 0 ) ) //如果是闰年的话,则天数加1
    {
        Leap=1;
    }
    Total=Total+Leap;
    printf("%d:%d:%d is %dth Day\n", Year, Month, Day, Total);
    return 0;
 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值