题: 编写一个C++程序,它要求用户输入一个以 long 为单位的距离, 然后将它转换为码(yard,一long 等于 220 码)。
解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>intmain(){usingnamespacestd;intdistance=0,yard;cout<<"Please input a distance numebr in the unit of Long: ";cin>>distance;yard=distance*220;cout<<"The distance tranform in yards is: "<<yard<<endl;return0;}
#include<iostream>usingnamespacestd;voidblind_mice(){cout<<"Three blind mice."<<endl;return;}voidhow_they_run(){cout<<"See how they run"<<endl;return;}intmain(){blind_mice();blind_mice();how_they_run();how_they_run();return0;}
#include<iostream>intmain(){usingnamespacestd;intyears,months;cout<<"Enter your age: ";cin>>years;months=years*12;cout<<years<<" years is "<<months<<" monthes."<<endl;return0;}
#include<iostream>doublecelsiu2fahrenit(doublecelsius){return1.8*celsius+32.0;}intmain(){usingnamespacestd;doublecelsius;cout<<"Please enter a celsius value: ";cin>>celsius;cout<<celsius<<" degrees Celsius is "<<celsiu2fahrenit(celsius)<<" degrees Fahrenheit."<<endl;return0;}
#include<iostream>doublelight_years2astromonical_unit(doublelight_years){returnlight_years*63240;}intmain(){usingnamespacestd;doublelight_years;cout<<"nter the number of light years: ";cin>>light_years;cout<<light_years<<" light years = "<<light_years2astromonical_unit(light_years)<<" astromonical units."<<endl;return0;}
#include<iostream>usingnamespacestd;voiddisplay_time(doublehours,doubleminutes){cout<<"Time: "<<hours<<":"<<minutes<<endl;return;}intmain(){doublehours,minutes;cout<<"Enter the number of hours: ";cin>>hours;cout<<"Enter the number of minutes: ";cin>>minutes;display_time(hours,minutes);return0;}
#include<iostream>constintFoot2inch=12;intmain(){usingnamespacestd;intinput_height=0;cout<<"Please input you height in inch: __\b\b";cin>>input_height;intheight_foot=input_height/Foot2inch;intheight_inch=input_height%Foot2inch;cout<<"Your height in inch is: "<<input_height<<"; transforming in foot and inch is: "<<height_foot<<" foot "<<height_inch<<" inch."<<endl;return0;}
#include<iostream>constintFoot2Inch=12;constdoubleInch2Meter=0.0254;constdoubleKg2Pound=2.2;doubleBMI(doubleweight,doubleheight){returnweight/(height*height);}intmain(){usingnamespacestd;doubleheight_foot=0;doubleheight_inch=0;doubleweight_pound=0;cout<<"Please enter your height in foot and Inch2Meter."<<endl;cout<<"Enter the foot of height: __\b\b";cin>>height_foot;cout<<"Enter the inch of height: __\b\b";cin>>height_inch;cout<<"Please enter your weight in pound: __\b\b";cin>>weight_pound;doubleheight_meter=(height_foot*Foot2Inch+height_inch)*Inch2Meter;doubleweight_kg=weight_pound/Kg2Pound;doublebmi=BMI(weight_kg,height_meter);cout<<"Your BMI is: "<<bmi<<endl;return0;}
Enter a latitude in degree, minutes, and seconds:
First, enter the degree: 37Next, enter the minutes of arc: 51Finally, enter the seconds of arc: 1937 degrees, 51 minutes, 19seconds= 37.8553 degrees.
#include<iostream>intmain(){usingnamespacestd;doubledegree,minutes,seconds;cout<<"Enter a latitude in degree, minutes and seconds."<<endl;cout<<"First, enter the degree: ";cin>>degree;cout<<"Next, enter the minutes of arc: ";cin>>minutes;cout<<"Finally, enter the seconds of arc: ";cin>>seconds;doubledegree2=degree+minutes/60+seconds/3600;cout<<degree<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds = "<<degree2<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;longtotal_seconds;cout<<"Enter the number of seconds: ";cin>>total_seconds;intdays=total_seconds/86400;inthours=(total_seconds%86400)/3600;intminutes=((total_seconds%86400)%3600)/60;intseconds=((total_seconds%86400)%3600)%60;cout<<total_seconds<<"seconds = "<<days<<" days, "<<hours<<" hours, "<<minutes<<" minutes, "<<seconds<<" seconds."<<endl;return0;}
Enter the world's population: 7850176700Enther the population of China: 1411780000The population of the China is 17.9841% of the world population.
解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>intmain(){usingnamespacestd;longlongpopulation_world,population_China;cout<<"Enter the world's population: ";cin>>population_world;cout<<"Enter the population of China: ";cin>>population_China;doublerate=double(population_China)/population_world;cout<<"The population of the China is "<<rate*100<<"% of the world population."<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;doublekilometer,oil_liter;cout<<"Enter the distance that you've dirver in kilometer: ";cin>>kilometer;cout<<"Enter the comsumption of oil: ";cin>>oil_liter;doublekilometer_per_liter=kilometer/oil_liter;cout<<"The average fuel comsumption is "<<100/kilometer_per_liter<<" L/100km"<<endl;}
#include<iostream>intmain(){usingnamespacestd;constdoubleKm2Mile=0.6214;constdoubleGallon2Litre=3.875;doublefuel_comsuption_en=0.0;cout<<"Enter the fuel comsuption in European standard: ";cin>>fuel_comsuption_en;doublefuel_comsuption_us=(100*Km2Mile)/(fuel_comsuption_en/Gallon2Litre);cout<<"The fuel comsuption in US standard is "<<fuel_comsuption_us<<" Miles/Gallon (mpg)."<<endl;return0;}
What is your first name? Betty Sue
Waht is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22Name: Yewe, Betty Sue
Grade: C
Age: 22
#include<iostream>intmain(){usingnamespacestd;charfirst_name[40];charlast_name[40];chargrade_letter;intage;cout<<"What is your first name: ";cin.getline(first_name,40);cout<<"What is your last name: ";cin.getline(last_name,40);cout<<"What letter grade do you deserve: ";cin>>grade_letter;cout<<"What is your age: ";cin>>age;cout<<"Name: "<<last_name<<", "<<first_name<<endl;cout<<"Grade: "<<char(grade_letter+1)<<"\n";cout<<"Age: "<<age<<endl;return0;}
#include<iostream>#include<string>intmain(){usingnamespacestd;stringname;stringdessert;cout<<"Enter your name: \n";getline(cin,name);cout<<"Enter your favorite dessert:\n";getline(cin,dessert);cout<<"I have delicious "<<dessert;cout<<" for you, "<<name<<".\n";return0;}
#include<iostream>#include<cstring>intmain(){usingnamespacestd;charfirst_name[20],last_name[20];charfinal_name[50];cout<<"Enter your first name: ";cin.getline(first_name,20);cout<<"Enther your last name: ";cin.getline(last_name,20);strcpy(final_name,last_name);strcat(final_name,", ");strcat(final_name,first_name);cout<<"Here's the information in a single string: "<<final_name<<endl;return0;}
#include<iostream>#include<string>intmain(){usingnamespacestd;stringfirst_name,last_name;stringfinal_name;cout<<"Enter your first name: ";getline(cin,first_name);cout<<"Enther your last name: ";getline(cin,last_name);final_name=last_name+", "+first_name;cout<<"Here's the information in a single string: "<<final_name<<endl;return0;}
#include<iostream>#include<string>structCandyBar{std::stringname;doubleweight;intcalories;};intmain(){usingnamespacestd;CandyBarsnack={"Mocha Munch",2.3,350};// 初始化结构体
cout<<"The name of the CandyBar: "<<snack.name<<"\n";cout<<"The weight of the candy: "<<snack.weight<<"\n";cout<<"The calories information: "<<snack.calories<<endl;return0;}
#include<iostream>#include<string>structCandyBar{std::stringname;doubleweight;intcalories;};intmain(){usingnamespacestd;CandyBarcandbar[3]={{"Mocha Munch",2.3,350},{"Big Rabbit",5,300},{"Joy Boy",4.1,430}};cout<<"The name of the CandyBar: "<<candbar[0].name<<"\n"<<"The weight of the candy: "<<candbar[0].weight<<"\n"<<"The calories information: "<<candbar[0].calories<<"\n\n";cout<<"The name of the CandyBar: "<<candbar[1].name<<"\n"<<"The weight of the candy: "<<candbar[1].weight<<"\n"<<"The calories information: "<<candbar[1].calories<<"\n\n";cout<<"The name of the CandyBar: "<<candbar[2].name<<"\n"<<"The weight of the candy: "<<candbar[2].weight<<"\n"<<"The calories information: "<<candbar[2].calories<<endl;return0;}
#include<iostream>#include<string>structPizza{std::stringcompany;doublediameter;doubleweight;};intmain(){usingnamespacestd;Pizzapizza;cout<<"Enter the pizza company: ";getline(cin,pizza.company);cout<<"Enter the diameter of pizza: ";cin>>pizza.diameter;cout<<"Enter the weight of pizza: ";cin>>pizza.weight;cout<<"\nHere is the pizza information: \n"<<"Company: "<<pizza.company<<"\n"<<"Diameter: "<<pizza.diameter<<"\n"<<"Weight: "<<pizza.weight<<endl;return0;}
#include<iostream>#include<string>structPizza{std::stringcompany;doublediameter;doubleweight;};intmain(){usingnamespacestd;Pizza*pizza=newPizza;cout<<"Enter the diameter of pizza: ";cin>>pizza->diameter;cout<<"Enter the weight of pizza: ";cin>>pizza->weight;// 注意上语句输入完 weight 后,回车键留在输入流中,以下的 getline
// 碰到输入流中的回车就以为结束了,所以如果没有这个 cin.get() 先读
// 取回车,那么用户永远获得 company 的值。
cin.get();cout<<"Enter the pizza company: ";getline(cin,pizza->company);cout<<"\nHere is the pizza information: \n"<<"Company: "<<pizza->company<<"\n"<<"Diameter: "<<pizza->diameter<<"\n"<<"Weight: "<<pizza->weight<<endl;deletepizza;return0;}
#include<iostream>#include<string>structCandyBar{std::stringname;doubleweight;intcalories;};intmain(){usingnamespacestd;CandyBar*p_candybar=newCandyBar[3]{{"Mocha Munch",2.3,350},{"Big Rabbit",5,300},{"Joy Boy",4.1,430}};// 输出第一个结构体元素,按照数组的方式输出
cout<<"The name of the CandyBar: "<<p_candybar[0].name<<"\n"<<"The weight of the candy: "<<p_candybar[0].weight<<"\n"<<"The calories information: "<<p_candybar[0].calories<<"\n\n";// 输出第二个结构体元素,可以按照指针的逻辑输出
cout<<"The name of the CandyBar: "<<(p_candybar+1)->name<<"\n"<<"The weight of the candy: "<<(p_candybar+1)->weight<<"\n"<<"The calories information: "<<(p_candybar+1)->calories<<"\n\n";// 输出第三个结构体元素,又是数据的方式
cout<<"The name of the CandyBar: "<<p_candybar[2].name<<"\n"<<"The weight of the candy: "<<p_candybar[2].weight<<"\n"<<"The calories information: "<<p_candybar[2].calories<<endl;delete[]p_candybar;return0;}
#include<iostream>#include<array>intmain(){usingnamespacestd;array<double,3>result;cout<<"Enter threed result of the 40 meters runing time: \n";cin>>result[0];cin>>result[1];cin>>result[2];doubleave_result=(result[0]+result[1]+result[2])/3;cout<<"The all three time results are: "<<result[0]<<", "<<result[1]<<", "<<result[2]<<endl;cout<<"The average result: "<<ave_result<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;intnumber1,number2;cout<<"Enter the first number: ";cin>>number1;cout<<"Enter the second number: ";cin>>number2;if(number1>number2){inttmp;tmp=number1;number1=number2;number2=tmp;}ints=0;for(intnum=number1;num<number2+1;++num){s+=num;}cout<<"Sum the number from "<<number1<<" to "<<number2<<", sum = "<<s<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;doubles=0;doublech;while(1){cout<<"Enter a number (int/double) (0 to exit): ";cin>>ch;if(ch==0){break;}s+=ch;cout<<"Until now, the sum of the number you inputed is: "<<s<<endl;}return0;}
#include<iostream>intmain(){usingnamespacestd;doubledaphne_account=100;doublecleo_account=100;intyear=0;while(cleo_account<=daphne_account){++year;daphne_account+=10;cleo_account+=cleo_account*0.05;}cout<<"After "<<year<<" Years. "<<"Cleo's account is "<<cleo_account<<" while more than the one of Daphne which is "<<daphne_account<<"."<<endl;return0;}
#include<iostream>#include<string>intmain(){usingnamespacestd;stringmonths[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};intsell[12];inttotal_sales=0;cout<<"Enter the sales of book <<C++ for Fools>> each month."<<endl;for(inti=0;i<12;++i){cout<<months[i]<<":";cin>>sell[i];total_sales+=sell[i];}cout<<"\nThe total sales is "<<total_sales<<endl;for(inti=0;i<12;++i){cout<<months[i]<<": "<<sell[i]<<endl;}return0;}
#include<iostream>#include<string>intmain(){usingnamespacestd;stringmonths[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};intsells[3][12];inttotal_sales[3]={0,0,0};for(inti=0;i<3;++i){cout<<"Enter "<<i+1<<" year(s) sales of book <<C++ for Fools>> each month."<<endl;for(intj=0;j<12;++j){cout<<months[j]<<": ";cin>>sells[i][j];total_sales[i]+=sells[i][j];}}for(inti=0;i<3;++i){cout<<i+1<<" year(s) total sales is "<<total_sales[i]<<endl;}cout<<"There years total sales is "<<total_sales[0]+total_sales[1]+total_sales[2]<<endl;return0;}
题: 设计一个名为 car 的结构体,用它存储下述有关汽车的信息:生产商(存储在字符数组或 string 对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的 car 结构体组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car #1:
Please enter the maker: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the maker: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
#include<iostream>#include<string>intmain(){usingnamespacestd;structCar{stringcompany;intyear;};intcar_num=0;cout<<"How many cars do you wish to catalog? ";cin>>car_num;cin.get()// 读取输入流末尾的回车
Car*cars=newCar[car_num];for(inti=0;i<car_num;++i){cout<<"Please enter the maker: ";cin>>(cars+i)->company;cout<<"Please enter the year made: ";cin>>(cars+i)->year;}cout<<"\nHere is your collection: \n";for(inti=0;i<car_num;++i){cout<<cars[i].year<<" "<<cars[i].company<<endl;}delete[]cars;return0;}
#include<iostream>#include<cstring>intmain(){usingnamespacestd;intword_count=0;charch[80];cout<<"Enter a word (type 'done' to stop the program.): \n";do{cin>>ch;if(strcmp(ch,"done")!=0){word_count++;}}while(strcmp(ch,"done")!=0);cout<<"\nYou entered a total of "<<word_count<<" words."<<endl;return0;}
#include<iostream>#include<string>intmain(){usingnamespacestd;intword_count=0;stringch;cout<<"Enter a word (type 'done' to stop the program.): \n";do{cin>>ch;if(ch!="done"){word_count++;}}while(ch!="done");cout<<"\nYou entered a total of "<<word_count<<" words."<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;intline_num=0;cout<<"Enter the number of rows: ";cin>>line_num;cout<<"Output:"<<endl;for(inti=line_num;i>0;--i){for(intj=i-1;j>0;--j){cout<<".";}for(intj=line_num-(i-1);j>0;--j){cout<<"*";}cout<<"\n";}return0;}
#include<iostream>#include<array>intmain(){usingnamespacestd;constunsignedintsize=10;array<double,size>donation;doublesum_value=0;unsignedintlarge_avg=0,n=0;cout<<"Enter 10 double value or type non-digital value to exit: ";while((n<size)&&(cin>>donation[n])){sum_value+=donation[n];++n;}doubleavg=sum_value/n;for(inti=0;i<n;i++){if(donation[i]>avg)++large_avg;}cout<<"The average value is: "<<avg<<", there are "<<large_avg<<" larger than average value."<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;cout<<"Please enter one of the following choice: \n";cout<<"c) carnivore\tp) pianist.\n"<<"t) tree\tg) game"<<endl;boolexit=false;charc;while(!exit&&(cin>>c)){switch(c){case'c':cout<<"Tiger is a carnivore."<<endl;exit=true;break;case'p':cout<<"Mozart is a great pianst."<<endl;exit=true;break;case't':cout<<"A maple is a tree."<<endl;exit=true;break;case'g':cout<<"Supper Mario is a great game."<<endl;exit=true;break;default:cout<<"Please enter c, p, t, or g: q"<<endl;break;}}return0;}
题: 加入 Benevolent Order of Programmer 后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
1
2
3
4
5
6
7
8
// Benevolent order of programmers strcture
structbop{charfullname[strsize];// real name
chartitle[strsize];// job title
charbopname[strsize];// secret BOP name
intpreference;// 0 = fullname, 1 = title, 2 = bopname
};
Benevolent order of Programmers report.
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
Enter your choices: a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
Wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice: q
Bye!
#include<iostream>intmain(){usingnamespacestd;constintstrsize=80;structBop{charfullname[strsize];// real name
chartitle[strsize];// job title
charbopname[strsize];// secret BOP name
intpreference;// 0 = fullname, 1 = title, 2 = bopname
};constintsize=5;constBopbops[size]={{"Wimp Macho","bbb","c",0},{"Raki Rhodes","2XXXX","3XXXXX",1},{"Celia Laiter","2AAAA","3AAAAA",2},{"Hoppy Hipman","2BBBB","3BBBBB",0},{"Pat Hand","4CCCC","3CCCCC",1}};cout<<"Benevolent order of Programmers report.\n";cout<<"a. display by name b. display by title\n"<<"c. display by bopname d. display by preference\n"<<"q. quit"<<endl;charch;while(cin>>ch){if(ch=='q'){break;}for(inti=0;i<size;++i){switch(ch){case'a':cout<<bops[i].fullname<<"\n";break;case'b':cout<<bops[i].title<<"\n";break;case'c':cout<<bops[i].bopname<<"\n";break;case'd':cout<<bops[i].preference<<"\n";break;default:break;}}cout<<"Next choice: ";}cout<<"** Done **"<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;constdoubletax_rate1=0.1;constdoubletax_rate2=0.15;constdoubletax_rate3=0.20;doubleincome=0.0,tax=0.0;cout<<"Please enter your income: ";while((cin>>income)&&(income>0)){if(income<=5000){tax=0.0;}elseif(income<=15000){tax=(income-5000)*tax_rate1;}elseif(income<=35000){tax=(15000-5000)*tax_rate1+(income-15000)*tax_rate2;}else{tax=(15000-5000)*tax_rate1+(35000-15000)*tax_rate2+(income-35000)*tax_rate3;}cout<<"Income = "<<income<<", tax = "<<tax<<endl;cout<<"Please enter your income again or enter a negative value to quit: ";}return0;}
#include<iostream>#include<string>intmain(){usingnamespacestd;constintGrand_Amount=10000;structPatron{stringname;doubleamount;};intcontribute_num=0;cout<<"Enter the number of contributor: ";cin>>contribute_num;cin.get();// 读取输入流中的回车符
Patron*p_contribution=newPatron[contribute_num];for(inti=0;i<contribute_num;++i){cout<<"Enter the name of "<<i+1<<" contributor: ";getline(cin,p_contribution[i].name);cout<<"Enter the amount of donation: ";cin>>p_contribution[i].amount;cin.get();// 读取输入流中的回车符
}unsignedintgrand_amount_n=0;cout<<"\nGrand patron: "<<endl;for(inti=0;i<contribute_num;++i){if(p_contribution[i].amount>Grand_Amount){cout<<"Contributor name: "<<p_contribution[i].name<<"\n"<<"Contributor amount: "<<p_contribution[i].amount<<endl;++grand_amount_n;}}if(grand_amount_n==0){cout<<"None"<<endl;}boolis_empty=true;cout<<"\nPatrons: "<<endl;for(inti=0;i<contribute_num;++i){cout<<"Contributor name: "<<p_contribution[i].name<<"\n"<<"Contributor amount: "<<p_contribution[i].amount<<endl;is_empty=false;}if(is_empty){cout<<"** None **"<<endl;}return0;}
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others
#include<iostream>#include<cctype>#include<string>intmain(){usingnamespacestd;unsignedintvowels=0;unsignedintconsonants=0;unsignedintother=0;stringinput;cout<<"Enter words (q to quit): "<<endl;while(cin>>input){if(input=="q")break;if(isalpha(input[0])){switch(toupper(input[0])){case'A':;case'E':;case'I':;case'O':;case'U':++vowels;break;default:++consonants;break;}}else{++other;}}cout<<vowels<<" words beginning with vowels.\n"<<consonants<<" words beginning with consonants.\n"<<other<<" words beginning with other letter."<<endl;return0;}
#include<iostream>#include<fstream>#include<string>intmain(){usingnamespacestd;stringfn;ifstreamin_file_handle;unsignedintn=0;charch;cout<<"Enter a file name: ";getline(cin,fn);in_file_handle.open(fn.c_str());while((ch=in_file_handle.get())!=EOF){++n;}in_file_handle.close();cout<<"There are "<<n<<" characters in "<<fn<<" file."<<endl;return0;}
#include<iostream>intmain(){usingnamespacestd;doublex=0,y=0;doubleh_avg=0;cout<<"Enter two numbers: ";cin>>x>>y;while(x!=0&&y!=0){h_avg=2*x*y/(x+y);cout<<"The harmonic mean of "<<x<<" and "<<y<<" is "<<h_avg<<endl;cout<<"Enter the next two numbers: ";cin>>x>>y;}return0;}
#include<iostream>intinput(doubledata[],intmax_num){inti=0;std::cout<<"Enter up o 10 golf score (-1 to quit): "<<std::endl;while(std::cin>>data[i]){if(data[i]==-1){--i;break;}++i;if(i==max_num)break;}return(i<max_num)?i+1:max_num;}doublecalculate_average(constdoubledata[],intn){doublesum=0;for(size_ti(0);i<n;++i){sum+=data[i];}returnsum/n;}voidoutput(constdoubledata[],intn){std::cout<<"The score are: "<<std::endl;for(size_ti(0);i<n;++i){std::cout<<data[i]<<" ";}std::cout<<std::endl;}intmain(){doubleglf_score[10];intn=input(glf_score,10);doubleavg_score=calculate_average(glf_score,n);output(glf_score,n);std::cout<<"The average is: "<<avg_score<<std::endl;return0;}
#include<iostream>longdoubleprobability(unsignednumbers,unsignedpicks){longdoubleresult=1.0;longdoublen;unsignedp;for(n=numbers,p=picks;p>0;n--,p--){result*=n/p;}returnresult;}intmain(){unsignedintfield1=47;unsignedintfield2=27;std::cout<<"You have no chance in "<<probability(field1,5)*probability(field2,1)<<" of winning.\n"<<std::endl;return0;}
#include<iostream>longfactorial(intn){if(n==0){return1;}returnn*factorial(n-1);}intmain(){usingnamespacestd;intn;cout<<"Enter an integer number: ";while(!(cin>>n)){cin.clear();while(cin.get()!='\n'){continue;}cout<<"Please enter an integer number: ";}if(n<0){cout<<"Negative number don't have factorial."<<endl;exit(1);}longf=factorial(n);cout<<"The factorial of "<<n<<" is "<<f<<endl;return0;}
#include<iostream>intFill_array(doubledata[],intmax_num){std::cout<<"Enter double numbers (non-digital to quit): "<<std::endl;inti=0;while((i<max_num)&&(std::cin>>data[i]))++i;// return the size of array
returni;}voidShow_array(constdoubledata[],intn){std::cout<<"The size of array is: "<<n<<" and the data is: ";for(size_ti(0);i<n;++i){std::cout<<data[i]<<" ";}std::cout<<"\n";}voidReverse_array(doubledata[],intn){for(size_ti(0);i<n/2;++i){doublet=data[i];data[i]=data[n-1-i];data[n-1-i]=t;}return;}intmain(){doubledata[10];intn=Fill_array(data,10);Show_array(data,n);Reverse_array(data,n);Show_array(data,n);return0;}
#include<iostream>#include<cstring>usingnamespacestd;structstringy{char*str;intct;};intmain(){stringybeany;chartesting[]="Reality isn't what it used to be.";set(beany,testing);show(beany);show(beany,2);testing[0]='D';testing[1]='u';show(testing);show(testing,3);show("Done!");return0;}
请提供其中描述的函数和原型,从而完成该程序。注意,应有两个 show() 函数,每个都使用默认参数。请尽可能使用 cosnt参数。
set() 使用 new 分配足够的空间来存储指定的字符串。这里使用的技术与设计和实现类时使用的相似。(可能还必须修改头文件的名
称,删除using编译指令,这取决于所用的编译器。)
Personone;// default constructor
Persontwo("Smythecraft");// use #2 with one default argument
Personthree("Dimwidy","Sam");// use #2 with no defaults.
one.show();cout<<endl;one.FormalShow();