【c++ 实现】程序小练习 (二)

要求1. 读取键盘输入, 直到遇到 @ 符号为止, 并回显输入(数字除外), 将大 写字符转换为小写, 将小写字符转换为大写

#include "iostream"
#include "cctype"  // 字符函数库: 使用 toupper()  tolower()

using namespace std;

int main(){
    char ch;
    char c;
    cout << "enter input character : \n";
    cin.get(ch);
    while (ch != '@'){
        if (ch >= 'a' && ch <= 'z'){
            c = ::toupper(ch);  // 将小写字母转换为大写字母
            cout << ch << "'s upper: " << c << endl;
        }
        else if (ch >='A' && ch <= 'Z'){
            c = ::tolower(ch);  // 将大写字母转换为小写字母
            cout << ch << "'s lower: " << c << endl;
        }else{
            cout << "enter character " << ch << " is not alphabetic ! \n";
        }
        cin.get(ch);
    }
    cout << "Bye!\n";
    return 0;
}

要求2. 编写一个菜单驱动程序的雏形, 程序显示一个提供4个选项的菜单, 每个选项用一个字母标记. 如果用户使用有效选项之外的字母进行响应, 程序将提示用户输入一个有效的字母, 直到用户这样做为止 然后, 该程序使用一条switch语句, 根据用户的选择执行一个简单操作

#include "iostream"
using namespace std;

int main(){
    char choice;
    bool validInput = false;
    cout << "Please enter one of the following choices: \n"
         << "c) carnivore  p) pianist \n"
         << "t) tree  g) game \n";

    while (!validInput){
        cin.get(choice);
        cin.ignore();  // 使用这个用于清理cin多余的换行符, 必须多输出 "Please enter c, p, t, or g: " 这个语句
//        cin >> choice;
        if (choice == 'c' || choice == 'p' || choice == 't' || choice == 'g') {
            validInput = true;
        }else{
            cout << "Please enter c, p, t, or g: " << endl;
        }
    }
    switch (choice) {
        case 'c': cout << "A maple is a carnivore"<< endl;
            break;
        case 'p': cout << "A maple is a pianist" << endl;
            break;
        case 't': cout << "A maple is a tree" << endl;
            break;
        case 'g': cout << "A maple is a game" << endl;
            break;
    }
    return 0;
}

要求3. 打开一个文本文件, 逐个字符地读取该文件, 直到到达文件结尾, 然后指出该文件包含哪些字符

#include "iostream"
#include "fstream"

using namespace std;

int main(){
    ifstream inFile;
    inFile.open("Test.txt");
    if(!inFile.is_open()){
        cout << "could not open the file \n";
        cout << "program terminating \n";
        return 1;
    }
    char ch;
    int count = 0;
    while (inFile.get(ch)){
        count++;
    }
    cout << "character sums : " << count << endl;
    return 0;
}

要求4. 使用递归方法, 接受一个整数参数, 并返回该参数的阶乘, 例如: 3! = 32! = 32*1 , 0! = 1程序使用循环让用户输入不同的值, 程序将报告这些值的阶乘

#include "iostream"

using namespace std;

long factorial(int n); // function prototype

int main(){
    while (true){
        int i;
        cout << "Enter n: \n";
        cin >> i;
        if (i<0){ // Enter a negative number to exit the loop
            cout << "Negative numbers have no factorial! \n";
            break;
        }else{
            cout << factorial(i) << endl;
        }
    }
    return 0;
}

long factorial(int n){
    long fact = 0;
    if (n == 0){
        return 1;
    }
    fact = n * factorial(n-1);
    if (n==1)
    {
        ::printf("%d! = %ld\n", 1, fact);
    }else{
        ::printf("%d! = %ld\n", n, fact);
    }
    return fact;
}

要求. 用户输入最多10个高尔夫成绩, 并将其存储在一个数组中. 程序允许用户提早结束输入(输入 负数 结束), 并在一行上显示所有成绩, 然后报告平均成绩.

#include "iostream"
const int SIZE = 10;

double scores(double *begin, double *end);

using namespace std;


int main(){
    double score[SIZE];
    int count = 0;
    int count_ = 0;
    for (int i=0; i<SIZE; i++){
        cout << "No." << i+1 << "score is : \n";
        cin >> score[i];
        if (score[i] < 0){
            count_ += 1;
            break;
        }else{
            count_ += 1;;
        }
        count = count_;  // count numbers
    }
    double total = 0;
    total = scores(score + 0, score + count);
    cout << "The sum of scores : " << total << endl;
    double avg = total / count;
    cout << "The avg of scores : " << avg << endl;
    cout << "The scores of all the persons are ";
    for (int i=0; i<count; i++){
        cout << score[i] << "\t";
    }
    return 0;
}

double scores(double *begin, double *end){
    double *temp;
    double total = 0;
    for( temp=begin; temp != end; temp++ ){
        total += *temp;
    }
    return total;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值