C++学习-Day-12

    xiaoxiao2024-12-02  76

    C++学习-Day-12

    一、编程练习

    一、编程练习

    编写一个程序,读取键盘输入,直到遇到@为止,读取符号(数字除外),将大写转换成小写,小写转换成大写。 #include<iostream> #include<cctype> int main() { using namespace std; char ch; int line=0; while((ch=cin.get())!='@') { if(isupper(ch)) ch=tolower(ch); else if (islower(ch)) ch=toupper(ch); else while(isdigit(ch)) cin.ignore(1); cout<<ch; } cout<<"end input"; } 编写一个程序,最多输入10个值读入到double数组,当遇到非数字时结束输入并将这些数字的平均值以及数组大于平均值的个数显示出来。 #include<iostream> int main() { using namespace std; const int donation=10; double a[donation]; int nu=0; double total=0,ave; for(int i=0;i<donation;i++) { if(cin>>a[i]) { total+=a[i]; nu++; } else break; } ave=total/nu; int ov=0; for(int i=0;i<nu;i++) { if(a[i]>ave) ov++; } cout<<"The average value of the array is "<<ave <<" ,and "<<ov<<" digits are more than "<<ave; } 编写一个菜单驱动程序,有4个选项,每个选项用一个字母标记,如果用户使用了有效字符外的字母响应,程序提示用户使用有效字符直到用户使用了有效字符,程序使用一条switch语句。 #include<iostream> using namespace std; void showmenu(); int main() { char ch; showmenu(); cin>>ch; while(isalpha(ch)) { switch(ch) { case 'c': cout<<"A maple is a carnivore\n"; break; case 'p': cout<<"A maple is a pianist\n"; break; case 't': cout<<"A maple is a tree\n"; break; case 'g': cout<<"A maple is a game\n"; break; default: cout<<"Please enter a c,p,t or g:\n"; } showmenu(); cin>>ch; } } void showmenu() { cout<<"Please enter one of the following choices: \n"; cout<<"c) carnivore\t"<<"p) pianist\n" <<"t) tree\t\t"<<"g) game\n"; } 编写一个程序记录一个捐助基金的捐赠人信息,该程序要求用户输入捐赠者的姓名和金额,这些信息都被记录在一个结构体内。在读取所有者的信息后需要输出捐款金额大于10000的个人信息,并在显示列表写出标题“Grand Patrons”,其余的信息列表标题为“Patrons”,若某一类数目为零则显示“None”。 #include<iostream> using namespace std; struct donation { char name[20]; double money; }; int main() { int nu; cout<<"Please enter the number of donators: "; cin>>nu; donation *p=new donation[nu]; for(int i=0;i<nu;i++) { cout<<"Enter the name of the "<<i+1<<" donator: "; cin>>(p+i)->name; cout<<"Enter the quantity of donation: "; cin>>(p+i)->money; } int a=0,b=0; cout<<" Grand Patrons\n"; for(int i=0;i<nu;i++) { if((p+i)->money>10000) { cout<<(p+i)->name<<"\t\t"<<(p+i)->money<<endl; a++; } else b++; } if(a==0) cout<<"None\n"; cout<<" Patrons\n"; if(b==0) cout<<"None\n"; for(int i=0;i<nu;i++) { if((p+i)->money<=10000) { cout<<(p+i)->name<<"\t\t"<<(p+i)->money<<endl; } } }

    5.编写一个程序,每次读取一个单词,直到用户输入‘q’,该程序指出每个单词首字母是元音的有多少个,辅音的有多少个,其他的有多少个。

    #include<iostream> #include<cctype> #include<string> int main() { using namespace std; string ch; int vowel=0,con=0,others=0; cout<<"Enter words (q to quit):\n"; while(cin>>ch) { if(ch.size()==1&&ch[0]=='q') break; if(isalpha(ch[0])) { switch(ch[0]) { case 'a': case 'A': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u':vowel++; break; default:con++; } } else others++; } cout<<vowel<<" words beginning with vowels\n" <<con<<" words beginning with consonants\n" <<others<<" others"; } 编写一个程序,首先创建一个文本文件,并写入数据,接着再读取该文本文件的内容。 #include<iostream> #include<fstream> int main() { using namespace std; char name[20]; ofstream outfile; cout<<"Please input a filename: "; cin>>name; outfile.open(name); outfile<<"4\n" <<"Sam Stone\n" <<"2000\n" <<"Freida Flass\n" <<"100500\n" <<"Tammy Tubbs\n" <<"5000\n" <<"Rich Raptor\n" <<"55000"; outfile.close(); /********************************///create a new file and write some data into it ifstream infile; char ch; infile.open(name); while(infile.get(ch)) { cout<<ch; } infile.close(); }
    最新回复(0)