/*2014,01.10 Arby*/ /*编程练习6.11*/ /*输入字符串,并检测到@进行结束操作*/ #include <iostream> using namespace std; int main() { /*初始化*/ char key_input[50]; int i = 0; /*操作*/ cout << "please enter words: "<< endl; cin.getline(key_input, 50); int length = strlen(key_input); while(i < length && key_input[i] != '@') { cout << key_input[i]; i++; } cin.get(); cin.get(); return 0; }
2.
/*2014,01.10 Arby*/ /*编程练习6.11*/ /*读取数字的循环,遇到错误输入情况的解决之道*/ #include <iostream> using namespace std; int main() { /*初始化*/ const int double_size = 10; double donations[double_size]; int i = 0; double donations_sum = 0, donations_average = 0; int count = 0; /*操作*/ cout << "enter the " << i+1 << " number: "; while((i < double_size) && (cin >> donations[i])) { donations_sum = donations_sum + donations[i]; i++; cout << "enter the " << i+1 << "number: "; } while(!(cin >> donations[i])) /*删除错误输入*/ { cin.clear(); while(cin.get() != '\n') continue; break; } /*必须全部输入10个数时,有错误时应该进行修改才可以*/ /* 1. 重置cin以接受新的输入 2.删除错误输入 3.提示用户再输入 */ /* for(i = 0; i < double_size; i++) { cout << "enter the " << i+1 << " number: "; while(!(cin >> donations[i])) { cin.clear(); while(cin.get() != '\n') */ /*这里是作为错误提醒用的还是输入的是10个数字*/ /* continue; cout << "please enter a number: "; } } */ donations_average = donations_sum / i; while((--i) >= 0) { if(donations[i] > donations_average) count ++; } /*输出*/ cout << "the average is:" << donations_average << endl; cout <<"And the count which bigger the average numbers is: " << count << endl; cin.get(); cin.get(); return 0; }
3.
/*2014,01.10 Arby*/ /*编程练习6.11*/ /*switch 语句的使用 */ #include <iostream> using namespace std; int main() { /*初始化*/ char ch_enter; /*操作*/ cout << "please enter one of the following choices: " <<endl; cout << "c) carnivore p)pianist" <<endl; cout << "t) tree g)game" << endl; cin>>ch_enter; while(ch_enter != 'c' && ch_enter != 'p' && ch_enter != 't' && ch_enter != 'g') { cout << "please enter a c, p, t, or g:"; cin >> ch_enter; } switch(ch_enter) { 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; default: ; } cin.get(); cin.get(); return 0; }
4.
/*2014,01.10 Arby*/ /*编程练习6.11*/ /*switch 语句的使用, 结构的使用 */ #include <iostream> using namespace std; const int strsize = 20; struct bop { char fullname[strsize]; char title[strsize]; char bopname[strsize]; int preference; //0 = fullname, 1 = title, 2 = bopname }; void display_preference(bop mem, int pre); int main() { /*初始化*/ const int structsize = 3; bop member_infor[structsize] = { {"xiaohe", "hexiao", "hehe", 0}, {"xiaoke", "kexiao", "keke", 1}, {"xiaoyou", "youxiao", "youyou", 2} }; char ch; int i = 0; /*操作*/ cout << "Benevolent order of programmers report" << endl; cout << "a. display by name b.display by title" << endl; cout << "c. display by bopname d.display by preference" << endl; cout << "q.quit" <<endl; cout << "enter your choice:"; cin >> ch; while(ch != 'q') { switch(ch) { case 'a': for(i = 0; i < structsize; i++) { cout << member_infor[i].fullname << endl; } break; case 'b': for(i = 0; i < structsize; i++) { cout << member_infor[i].title << endl; } break; case 'c': for(i = 0; i < structsize; i++) { cout << member_infor[i].bopname << endl; } break; case 'd': for(i = 0; i < structsize; i++) { display_preference(member_infor[i], member_infor[i].preference); } break; default: ; } cout << "next choice: "; cin>>ch; } cout << "Bye!" << endl; cin.get(); cin.get(); return 0; } void display_preference(bop mem, int pre) { switch(pre) { case 0: cout << mem.fullname << endl; break; case 1: cout << mem.title << endl; break; case 2: cout << mem.bopname << endl; break; default: ; } }
6.
/*2014,01.10 Arby*/ /*编程练习6.11*/ /*结构,if else */ #include <iostream> #include <string> #include <iomanip> using namespace std; struct donate { string strname; double money; }; int main() { /*初始化*/ int number = 0; int i = 0; int count = 0; /*操作*/ cout << "please enter the number of the donators: "; cin >> number; cin.get(); //输入数字和字符串要注意的 donate *member = new donate[number]; for(i = 0; i < number; i++) { cout << "please enter the name:"; getline(cin, member[i].strname); cout << "please enter the money:"; cin >> member[i].money; cin.get(); if(member[i].money > 1000) count++; } if(count == 0) cout<< "none" << endl; else if(count > 0) { cout << "grand patrons is a important donator." << endl; for(i = 0; i< number; i++) if(member[i].money > 1000) cout << member[i].strname << " " << member[i].money << endl; } if(count == number) { cout << "none" <<endl; } else { cout << "patrons" << endl; for(i = 0; i< number; i++) if(member[i].money <= 1000) cout << member[i].strname << " " <<setw(6)<< member[i].money << endl; } cin.get(); cin.get(); return 0; }
7.
/*2014,01.10 Arby*/ /*编程练习6.11*/ /*结构,if else */ #include <iostream> #include <cstring> using namespace std; int main() { /*初始化*/ const int char_size = 20; char words[char_size]; int yuan_count = 0, fu_count = 0, other_count = 0; /*操作*/ cout << "enter words" << endl; cin>>words; while(strcmp(words ,"q")) { if(isalpha(words[0])) { if(words[0] == 'a' || words[0] == 'e' || words[0] == 'i' || words[0] == 'o' || words[0] == 'u') yuan_count++; else fu_count++; } else other_count++; cin>>words; } /*输出*/ cout << yuan_count << "words beginning with vowels" <<endl; cout << fu_count << "words beginning with consonants" << endl; cout << other_count << "others" <<endl; cin.get(); cin.get(); return 0; }
9.
/*2014,01.10 Arby*/ /*编程练习6.11*/ /*文件中信息读取,这里其实可以检测\n来进行检验的 */ #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; struct donate { string strname; double money; }; int main() { /*初始化*/ ifstream in_file; int number = 0; int i =0; int count = 0; /*操作*/ in_file.open("file.txt"); if(!in_file.is_open()) { cout << "could not open the file" << endl; cout << "promming terminating" << endl; exit(EXIT_FAILURE); } in_file >> number; in_file.get(); donate *member = new donate[number]; for(i = 0; i < number; i++) { getline(in_file, member[i].strname); in_file>>member[i].money; in_file.get(); if(member[i].money > 1000) count++; } /*输出*/ if(count == 0) cout<< "none" << endl; else if(count > 0) { cout << "grand patrons is a important donator." << endl; for(i = 0; i< number; i++) if(member[i].money > 1000) cout << member[i].strname << " " << member[i].money << endl; } if(count == number) { cout << "none" <<endl; } else { cout << "patrons" << endl; for(i = 0; i< number; i++) if(member[i].money <= 1000) cout << member[i].strname << " " << member[i].money << endl; } cin.get(); cin.get(); return 0; }