#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;
}