第六章 分支语句和逻辑操作符
第六章了,后面感觉开始变难,加油。
第一题:读取键盘输入转换大小写后回显(数字除外)直到输入”@“停止。使用cctype函数系列.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
char ch,temp;
cout<< "Enter a word (quit with '@'): ";
cin>>ch;
while(ch!='@')
{
if (!isalpha(ch))
{
cout << "Please enter a word: ";
}
else if (ch>='a'&&ch<='z')
{
temp=ch-32;
cout<<"Input is "<<ch<<" and "<<"Output is "<<temp<<endl;
}
else if (ch>='A'&&ch<='Z')
{
temp=ch+32;
cout<<"Input is "<<ch<<" and "<<"Output is "<<temp<<endl;
}
cin>>ch;
}
cout<<"Input is Over."<<endl;
system("pause");
return 0;
}
第二题:编写一个最多输入10个值的程序,输入值存入double 数组,遇到非数字输入则停止,并报告数组中的平均值及大于平均值的个数
#include<iostream>
using namespace std;
int main()
{
cout << "Enter donation value (quit with no number): ";
double *p = new double[10];
int i = 0,count=0;
double sum = 0, aveg = 0;
while (cin>>p[i])//当非数字时则读取失败,程序结束
{
sum += p[i];
i++;
aveg = sum / i;
if (i > 9)
break;
}
for (int j = 0; j < i; j++)
{
if (p[j] > aveg)
count++;
}
cout << "The average is " << aveg << endl;
cout << "Number greater than average is " << count << endl;
system("pause");
return 0;
}
第三题:按要求编写一个菜单,提供四个选项及对应输出,使用switch,若用户未输入预定选项,则提醒其更正。
#include<iostream>
void showmenu(void);
using namespace std;
int main()
{
cout << "Please enter one of the following choice: \n";
showmenu();
char choice;
while (cin >> choice)
{
switch (choice)
{
case 'c':
case 'C':cout << "What is carnivore?\n";
break;
case 'p':
case 'P':cout << "I am a pianist.\n";
break;
case 't':
case 'T': cout << "A maple is a tree.\n";
break;
case 'g':
case 'G': cout << "Let's play a game!\n";
break;
default:
cout << "Please enter a c,p,t,or g: ";
cin.get();
break;
}
}
system("pause");
return 0;
}
void showmenu()
{
cout << "c) carnivore p) pianist\n"
"t) tree g) game\n";
}
第四题:按要求编写结构体,并创建结构体数组,并根据用户输入显示数组中每个结构体内的特定信息
#include<iostream>
using namespace std;
void showmenu();
const int strsize = 20;
struct bop
{
char fullname[strsize]; //real name
char title[strsize]; //job title
char bopname[strsize]; //secret BOP name
int preference; //0 = fullname,1=title,2=bopname
};
int main()
{
cout << "Please enter one of the following choice: \n";
bop member[5];
member[0] = { "Wimp Macho", "Computer Scientist", "WM", 0 };
member[1] = { "Raki Rhodes", "Junior Progammer", "Jack", 1 };
member[2] = { "Celia Laiter", "Math Teacher", "MIPS", 2 };
member[3] = { "Hoppy Hipman", "Analyst Trainee", "Martin", 1};
member[4] = { "Pat Hand", "BUS Driver", "LOOPY", 2};
showmenu();
char choice;
cin >> choice;
while (choice!='q')
{
switch (choice)
{
case 'a': for (int i = 0; i < 5; i++)
cout << member[i].fullname << endl;
break;
case 'b': for (int i = 0; i < 5; i++)
cout << member[i].title << endl;
break;
case 'c':for (int i = 0; i < 5; i++)
cout << member[i].bopname << endl;
break;
case 'd':for(int i = 0; i < 5; i++)
{
switch (member[i].preference)
{
case 0: cout << member[i].fullname << endl;
break;
case 1: cout << member[i].title << endl;
break;
case 2: cout << member[i].bopname << endl;
break;
}
}
break;
default:cout << "Please enter right choice: ";
break;
}
cin >> choice;
}
cout << "Bye!\n";
cin.get();
return 0;
}
void showmenu()
{
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name b. display by title\n"
"c. display by bopname d. display by preference\n"
"q. quit\n";
}
第五题:按题目要求计算税收,输入负数或非负数结束
#include<iostream>
using namespace std;
int main()
{
cout << "Please enter your salary: ";
double salary;
while (cin >> salary&&salary>=0)
{
double tax = 0;
if (salary >= 35000)
tax = (salary - 35000)*0.2 + 20000 * 0.15 + 10000 * 0.1;
if (salary >= 15001 && salary <= 35000)
tax = (salary - 15000)*0.15+10000*0.1;
if (salary >= 5001 && salary <= 15000)
tax = (salary - 5000)*0.1;
cout << "Tax is : " << tax << " tvarps." << endl;
cout << "Please enter your salary: ";
continue;
}
cout << "Over.\n";
system("pause");
return 0;
}
第六题:使用动态分配的结构数组,从键盘输入,每个结构有两个成员,一个string,一个double,统计double大于10000的人,并标记为GP,其余标记为P,无则标记为none
碰到一点问题,,,
问题解决,,,开始一定要输入数字,,,
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int CSIZE = 20;
struct record{ string name; double donation; };
int main()
{
cout << "How many donors ? ";
int num_of_donors;
cin >> num_of_donors;
record *d = new record[num_of_donors];
record *gp = new record[num_of_donors];
record *p = new record[num_of_donors];
int i=0,j = 0, k = 0;
while (i < num_of_donors)
{
cin.get();
cout << "#"<<i+1<<endl;
cout << "The donor's name: ";
getline(cin,d[i].name);
cout << "The donation : ";
cin >> d[i].donation;
if (d[i].donation>10000)
{
gp[j].name =d[i].name;
gp[j].donation = d[i].donation;
j++;
}
else
{
p[k].name=d[i].name;
p[k].donation = d[i].donation;
k++;
}
i++;
}
cout << "Grand Patrons : " << endl;
if (j < 1)
cout << "none.\n";
else
{
for (int sq = 0; sq < j; sq++)
cout << "Name : " << gp[sq].name << " Donation : " << gp[sq].donation << endl;
}
cout << "Patrons : " << endl;
if (k < 1)
cout << "none.\n";
else
{
for (int sq = 0; sq < k; sq++)
cout << "Name : " << p[sq].name << " Donation : " << p[sq].donation << endl;
}
delete[]d;
delete[]gp;
delete[]p;
system("pause");
return 0;
}
第七题:统计输入的字符串中有多少个单词以元音开头,多少个以福音开头,输入字母q退出。
#include<iostream>
#include<string>
using namespace std;
const int Arsize = 20;
int main()
{
cout << "Enter words ( q to quit):\n";
char word[Arsize];
cin>>word;
int vowel = 0, consonant=0 , other = 0;
while (strcmp(word,"q"))
{
if (isalpha(word[0]))
{
switch (word[0])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':++vowel ;
break;
default:++consonant;
}
}
else
{
++other;
}
cin >> word;
}
cout << vowel << " words beginning with vowels\n";
cout << consonant << " words beginning with consonants\n";
cout << other << " others\n";
system("pause");
return 0;
}
第八题:大开一个文本文件,逐字符读到末尾,统计该文件一共有多少个字符。
测试用txt:
one two ree 4 ive 6 eve . nin
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
ifstream inFile;
int main()
{
inFile.open("sinput.txt");
char read;
int count = 0;
inFile >> read;
if(!inFile.is_open())
{
cout << "Open file failed!" << endl;
}
while (!inFile.eof())
{
count++;
inFile >> read;
}
cout << count<<endl;
system("pause");
return 0;
}
注意:测试时,文件存放位置为exe所在文件夹,并直接双击exe运行。
第九题:从指定文件夹读取内容,并按照题6的格式存储并统计。
测试用txt:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
ifstream inFile;
struct record
{
string name;
double donation;
};
int main()
{
cout << "How many donors? ";
int num_of_donors;
cin >> num_of_donors;
inFile.open("nine.txt");
if (!inFile.is_open())
{
cout << "Open file failed!" << endl;
}
record *p = new record[num_of_donors];
//cout << p->donation<<endl;
//inFile >> p->donation;;
int i = 0;
for (; i < num_of_donors;)
{
inFile >> (p + i)->donation;;
cout << (p + i)->donation << endl;
inFile.get();
getline(inFile,(p+i)->name);
if (!inFile.eof())
cout << (p + i)->name << endl;
else
(p + i)->name = "NONAME";
i++;
}
//#if DEBUG
cout << "GP: \n";
for (int j = 0; j < num_of_donors; j++)
{
if (p[j].donation>10000)
cout << p[j].name << " " << p[j].donation << endl;
}
cout << "P: \n";
for (int k = 0; k < num_of_donors; k++)
{
if (p[k].donation<=10000)
cout << p[k].name << " " << p[k].donation << endl;
}
//#endif
delete []p;
cout << endl;
system("pause");
return 0;
}
注意,依然是直接点击exe进行测试。
第六章结束,下一章,函数。加油学~