Primer_Six

#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    char ch ;
    cin.get(ch);
    while (ch!='@')
    {
        if(!isdigit(ch))
        {
            if(islower(ch))
                ch=toupper(ch);
            else if(isupper(ch))
                ch=tolower(ch);
            cout << ch;
        }
        cin.get(ch);
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    const int Size=10;
    double arr[Size];
    float add=0,ave;
    int i=0,j,k=0;
    cout<<"请输入整数:"<<endl;
    while ((cin>>arr[i])&&i<10)
    {
        add+=arr[i];
        i++;
    }
    ave=add/i;
    for(j=0;j<=i;j++)
    {
        if(arr[j]>ave)
            k++;
    }
    cout<<"average="<<ave<<endl;
    cout<<"有"<<k<<"个数大于此数。"<<endl;
    return 0;
}

#include <iostream>

using namespace std;

int main()
{
    cout<<"Please enter one of the following choices: "<<endl;
    cout <<"c) carnivore            p) pianist"<<endl;
    cout <<"t) tree                 g) game"<<endl;
    char ch;
    cin >> ch;
    while (ch!='c'&&ch!='p'&&ch!='t'&&ch!='g')
    {
        cout <<"Please enter a c, p, t, or g: "<<endl;
        cin >>ch;
    }
    switch (ch)
    {
         case 'c' :  cout << "A cat is a carnivore.\n";
                              break;
         case 'p' :  cout << "Radu Lupu is a pianist.\n";
                              break;
         case 't' :  cout << "A maple is a tree.\n";
                              break;
         case 'g' :  cout << "Golf is a game.\n";
                              break;
         default  :  cout <<  "The program shouldn't get here!\n";
    }
    return 0;
}

#include <iostream>

using namespace std;
const double LEV_1=5000;
const double LEV_2=15000;
const double LEV_3=35000;
const float rate_1=0.10;
const float rate_2=0.15;
const float rate_3=0.20;
int main()
{
    double tax,income;
    cout << "Please enter annual your income in tvarp: ";
    cin >> income;
    if (income<=LEV_1)
        tax=0;
    else if(income<=LEV_2)
        tax=(income-LEV_1)*rate_1;
    else if(income<=LEV_3)
        tax=(LEV_2-LEV_1)*rate_1+(income-LEV_2)*rate_2;
    else
        tax=(LEV_2-LEV_1)*rate_1+(LEV_3-LEV_2)*rate_2+(income-LEV_3)*rate_3;
    cout << "You owe Neutronia " << tax << " tvarps in taxes.\n";
    return 0;
}

#include <iostream>
#include <string>
using namespace std;
struct team
{
    string name;
    double money;
};
int main()
{
    cout<<"Please enter the number of people: "<<endl;
    int num ;
    int i,j,k;
    cin >> num;
    cin.get();
    team *ps=new team[num];
    for(i=0;i<num;i++)
    {
        cout << "Please enter the name of the "<<i+1<<" person."<<endl;
        getline(cin,ps[i].name);
        cout << "Please enter the money of the "<<i+1<<" person."<<endl;
        cin >> ps[i].money;
        cin.get();
    }
    cout<< "Grand patrons:"<<endl;
    j=0;
    k=0;
    for(i=0;i<num;i++)
    {
        if(ps[i].money>10000)
        {
            cout <<"name: "<<ps[i].name<<"         "<<"money: "<<ps[i].money<<endl;
            j++;
        }
    }
    if(j==0)
        cout <<"none"<<endl;
    cout<<"patrons: "<<endl;
    for(i=0;i<num;i++)
    {
        if(ps[i].money<10000)
        {
            cout <<"name: "<<ps[i].name<<"         "<<"money: "<<ps[i].money<<endl;
            k++;
        }
    }
    if(k==0)
        cout<<"none"<<endl;
    return 0;
}

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

int main()
{
    string word;
    char ch;
    int vowel = 0;
    int consonant = 0;
    int other = 0;
    cout << "Enter words(q to quit):"<<endl;
    cin >>word;
    while (word!="q")
    {
        ch=tolower(word[0]);
        if(isalpha(ch))
        {
            if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
                vowel++;
            else
                consonant++;
        }
        else other++;
        cin >>word;
    }
    cout << vowel <<" words beginning with vowels\n";
    cout << consonant << " words beginning with consonants\n";
    cout << other << " others\n";
    return 0;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE=60;
int main()
{
    char filename[SIZE];
    char ch;
    ifstream inFile;
    cout << "Please enter the filename: ";
    cin.getline(filename,SIZE);
    inFile.open(filename);
    if(!inFile.is_open())
    {
        cout << "Could not open the file "<< filename<<endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }
    int count = 0;
    inFile >> ch;
    while (inFile.good())
    {
        count++;
        inFile>>ch;

    }
    cout << count << "characters in "<< filename<<endl;
    inFile.close();

    return 0;
}

1.读取文件时需先定义ifstream,并且读取文件需加后缀名。

2.

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
struct team
{
    string name;
    double money;
};
int main()
{
    char filename[SIZE];
    int num ;
    int i,count1,count2;
    ifstream inFile;
    cout<<"Please enter name of date file: "<<endl;
    cin.getline(filename,SIZE);
    inFile.open(filename);
    if(!inFile.is_open())
    {
        cout << "Could not open the file "<< filename<<endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }

    inFile >> num;
    inFile.get();
    team *ps=new team[num];
    for(i=0;i<num;i++)
    {

        getline(inFile,ps[i].name);
        inFile >> ps[i].money;
        inFile.get();
    }
    cout<< "Grand patrons:"<<endl;
    count1=0;
    count2=0;
    for(i=0;i<num;i++)
    {
        if(ps[i].money>10000)
        {
            cout <<"name: "<<ps[i].name<<"         "<<"money: "<<ps[i].money<<endl;
            count1++;
        }
    }
    if(count1==0)
        cout <<"none"<<endl;
    cout<<"patrons: "<<endl;
    for(i=0;i<num;i++)
    {
        if(ps[i].money<10000)
        {
            cout <<"name: "<<ps[i].name<<"         "<<"money: "<<ps[i].money<<endl;
            count2++;
        }
    }
    if(count2==0)
        cout<<"none"<<endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值