赞
踩
1.编写一个C++程序,用于显示你的姓名和地址。
#include<iostream> #include<string> using namespace std; int main() { string name,address; name = "Meng Dahai"; address = "Goushicun"; cout << name << endl; cout << address << endl; cin.get(); cin.get(); return 0; }
2.编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(1 long 等于220 码)。
#include<iostream> #include<string> using namespace std; int main() { long int n; cin >> n; n = n* 220; cout << n << endl; cin.get(); cin.get(); return 0; }
Three bline mice
Three bline mice
See how they run
See how they run
其中一个函数要调用两次,该函数成前两行;另一个函数也调用两次,该函数生成其余的输出。
#include<iostream> #include<string> using namespace std; void mice(){ cout << "Three blind mice" << endl; } void run(){ cout << "See how they run" << endl; } int main() { mice(); mice(); run(); run(); cin.get(); cin.get(); return 0; }
4.编写一个程序,由用户输入其年龄,然后显示该年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age: 29
#include<iostream> #include<string> using namespace std; int main() { long int age; cout << "Please enter your age." << endl; cin >> age; age *= 12; cout << age << endl; cin.get(); cin.get(); return 0; }
5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果。
Please enter a Celsius value:20
20 degree Celsius is 68 degrees Fahrenheit.
#include<iostream> #include<string> using namespace std; void Fah(double c) { double f = 1.8 * c + 32.0; printf("%.0f degrees Celsius is %.0f degrees Fahrenheit.",c,f); } int main() { double c; cout << "Please enter a Celsius value: "; cin >> c; Fah(c); cin.get(); cin.get(); return 0; }
6.编写一个程序,其中,main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果。
Enter the number of light years:4.2
4.2 light year = 265608 astronomical units
天文单位是从地球到太阳的平均距离,光年时光一年走的距离,除太阳外,最近的恒星大约离地球4.2光年。请使用double类型,转换公式为
1 光年 = 63240 天文单位
#include<iostream> #include<string> using namespace std; void a(double ly) { double au = ly * 63240; printf("%.1f light years = %.0f astronomical units.",ly,au); } int main() { double c; cout << "Enter the number of light years: "; cin >> c; a(c); cin.get(); cin.get(); return 0; }
Enter the of hours:9
Enter the number of minutes:28
Time:9:28
#include<iostream> #include<string> using namespace std; void time(int h,int m) { printf("Time: %d:%d",h,m); } int main() { int hour,minutes; cout << "Enter the number of hour: "; cin >> hour; cout << "Enter the number of minuites:"; cin >> minutes; time(hour,minutes); cin.get(); cin.get(); return 0; }
1.编写一个小程序,要求用户使用一个整数表示自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个符号常量const来表示转换因子。
#include<iostream> using namespace std; const int transfer = 12; int main() { int phigh; cout << "Please enter your height:___\b\b\b"; cin >> phigh; cout << "yingucnwei:" << phigh/transfer << " " << "yingchishi:" << phigh%transfer << endl; cin.get(); cin.get(); return 0; }
#include<iostream> #include<cmath> using namespace std; const double t1 = 0.0254,t2 = 2.2; int main() { double foot,inch,weight; cout << "Please enter your height,foot:___\b\b\b"; cin >> foot; cout << "inch:___\b\b\b"; cin >> inch; cout << "weight:___\b\b\b"; cin >> weight; foot = (foot * 12 + inch) * t1; weight = weight / 2.2; foot = pow(foot,2); weight = weight / foot; cout << "BMI:" << weight << endl; cin.get(); cin.get(); return 0; }
3.编写一个程序,要求用户以度、分、秒的方式输入一个维度,然后以单位显示该维度。1°等于60‘,1’等于60‘’,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。下面是该程序运行的输出。
Enter a latitude in degrees, minutes, and seconds:
First, enter the degree: 37
Next, enter the minutes of arc:51
Finally, enter the seconds of arc:19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees
#include<iostream> using namespace std; int main() { double degrees,minutes,seconds; cout << "Enter a latitude in degrees, minutes, and seconds" << endl; cout << "First, enter the degrees:___\b\b\b"; cin >> degrees; cout << "Next, enter the minutes of arc:___\b\b\b"; cin >> minutes; cout << "Finally,enter the seconds of arc:___\b\b\b"; cin >> seconds; double y; y = degrees + minutes / 60 + seconds / 3600; printf("%.0f degrees, %.0f minutes, %.0f seconds = %.4f degrees",degrees,minutes,seconds,y); cin.get(); cin.get(); return 0; }
Enter the number of seconds:31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds.
#include<iostream> using namespace std; int main() { long long _seconds; cout << "Enter the number of seconds:__________\b\b\b\b\b\b\b\b\b\b" << endl; cin >> _seconds; int days,hours,minutes,seconds; days = _seconds / 86400; hours = (_seconds % 86400) / 3600; minutes = (_seconds % 86400) % 3600 / 60; seconds = ((_seconds % 86400) % 3600) % 60; printf("%ld seconds = %d days, %d hours, %d minutes, %d seconds",_seconds,days,hours,minutes,seconds); cin.get(); cin.get(); return 0; }
5 编写一个程序,要求用户输入全球当前的人口和美国当前的人口。将这些信息存储在 long long 变量中,使程序输出显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
Enter the world’s population:6898758899
Enter population of US:310783781
The population of the US is 4.50492% of the world population
#include<iostream> using namespace std; int main() { double w,u; cout << "Enter the world's population:______________\b\b\b\b\b\b\b\b\b\b\b\b\b"; cin >> w; cout << "Enter the population of the US:_______________\b\b\b\b\b\b\b\b\b\b\b\b\b"; cin >> u; double rate; rate = (u / w) * 100; printf("The population of the US is %.5f%% of the world population.",rate); cin.get(); cin.get(); return 0; }
6 编写一个程序,要求用户输入驱车里程(单位是英里)和使用汽油量(单位为是加仑),然后指出汽车耗油量为 1 加仑的里程,即油耗。如果愿意,也可以要求用户以千米为单位输入距离,并以升为单位输入汽油量,然后输出欧洲风格的结果——即每 100km 的耗油量(升)。
#include<iostream> using namespace std; int main() { double l,oil; cout << "Please enter l :"; cin >> l; cout << "Please enter oil :"; cin >> oil; l = (oil / l) * 100; cout << l << endl; cin.get(); cin.get(); return 0; }
7.编写一个程序,要求用户按欧洲风格输入汽车的油耗(每 100 km 消耗的汽油量,单位是升),然后将其转换为,美国风格的耗油量——每加仑多少英里。注意。100 km=62.14 mile,1 美制加仑=3.785升。因此,19 mile/gas大约合 12.4 L/100 km, 27 mile/gas 大约合8.7 L/100km。
#include<iostream> using namespace std; int main() { //欧洲输入,美国风格 double l,oil; cout << "Please enter mile :"; cin >> l; cout << "Please enter oil :"; cin >> oil; l = l / 62.14 * 100; l = l / oil; cout << l << "mpg" << endl; cin.get(); cin.get(); return 0; }
1 编写一个C++程序,如下述输出示例所示的那样请求并显示信息。
What is your first name? Betty Sue
What is your last name? Yewe What letter grade do you deserve? B What is your age? 22
Name: Yewe, Betty Sue Grade:
Age: 22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。
#include<iostream> #include<algorithm> #include<vector> using namespace std; //预编译指令 int main() { char first_name[20], last_name[20]; char grade; int age; //定义程序中的变量 cout << "What is your first name? "; cin.getline(first_name,20); cout << "What is your last name? "; cin.getline(last_name,20); cout << "What letter grade do you deserve? "; cin >> grade; cout << "What is your age? "; cin >> age; cout << "Name " << last_name << " , " << first_name << endl; cout << "Grade: " << grade << endl; cout << "Age: " << age << endl; cin.get(); return 0; }
2.修改程序清单4.4,使用C++string类而不是char数组。
#include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; //预编译指令 int main() { string name; string dessert; cout << "Enter your name: " << endl; getline(cin, name); cout << "Enter your favorite dessert: " << endl; getline(cin, dessert); cout << "I have some delicious " << dessert; cout << "for you, " << name << endl; cin.get(); return 0; }
3,编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形:
Enter your first name: Flip Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip
#include<iostream> #include<algorithm> #include<vector> #include<cstring> //为使用字符处理函数 using namespace std; //预编译指令 const int size = 20; int main() { char first_name[size], last_name[size]; char full_name[size*2]; cout << "Enter your first name: "; cin.getline(first_name, 20); cout << "Enter your last name: "; cin.getline(last_name, 20); strcpy(full_name, last_name); strcat(full_name, ", "); strcat(full_name,first_name); cout << "Here's the information in a single string: "; cout << full_name << endl; cin.get(); return 0; }
4,编写一个程序,它要求用户首先输入其名,再输入其姓:然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:
Enter your first name: Flip Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip
#include<iostream> #include<string> #include<cstring> using namespace std; //使用string 重做问题三,string的优势在于可以在字符串的处理上就能欧直接使用运算符 const int SIZE = 20; int main() { string full_name; string first_name, last_name; cout << "Enter your first name: "; getline(cin, first_name); cout << "Enter your last name: "; getline(cin, last_name); full_name = first_name + "." + last_name; cout << "Here's the information in a single string: "; cout << full_name << endl; return 0; }
5.结构CandyBar包含3个成员。第一个成员存储了糖块的品牌:第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为“Mocha Munch”、 2.3和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。
#include<iostream> #include<string> #include<cstring> using namespace std; struct CandyBar { string brand; double weight; unsigned int calorie; }; int main() { CandyBar snack = { "Mocha Much",2.3,350 }; cout << snack.brand << endl; cout << snack.weight << endl; cout << snack.calorie << endl; return 0; }
6.结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。
#include<iostream> #include<string> #include<cstring> using namespace std; struct CandyBar { string brand; double weight; unsigned int calorie; }; int main() { CandyBar snack[3] = { {"Mocha Much",2.3,350},{"dagou shi",2.3,350},{"gou shi",2.3,350} }; for (int i = 0; i < 3; i++) { cout << snack[i].brand << endl; cout << snack[i].weight << endl; cout << snack[i].calorie << endl; } return 0; }
披萨饼公司的名称,可以有多个单词组成。و
披萨饼的直径。.
披萨饼的重量。
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout.
#include<iostream> #include<string> #include<cstring> using namespace std; struct Pizza { string company; double diameter; double weight; }; int main() { Pizza p {}; cout << "Please enter pizza company: "; cin >> p.company; cout << "Please enter pizza diameter: "; cin >> p.diameter; cout << "Please enter pizza weight: "; cin >> p.weight; cout << p.company << endl; cout << p.diameter << endl; cout << p.weight << endl; return 0; }
8.完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径。
#include<iostream> #include<string> #include<cstring> using namespace std; struct Pizza { string company; double diameter; double weight; }; int main() { Pizza* p = new Pizza; cout << "Please enter pizza diameter: "; cin >> p->diameter; cout << "Please enter pizza company: "; cin >> p->company; cout << "Please enter pizza weight: "; cin >> p->weight; cout << p->company << endl; cout << p->diameter << endl; cout << p->weight << endl; delete p; return 0; }
9,完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。
#include<iostream> #include<string> #include<cstring> using namespace std; struct CandyBar { string brand; double weight; unsigned int calorie; }; int main() { CandyBar* psnack = new CandyBar[3]; for (int i = 0; i < 3; i++) { psnack[i].brand = "Mocha Much"; psnack[i].calorie = 956; psnack[i].weight = 4.23; } for (int i = 0; i < 3; i++) { cout << psnack[i].brand << endl; cout << psnack[i].weight << endl; cout << psnack[i].calorie << endl; } delete [] psnack; return 0; }
10,编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。
#include<iostream> #include<string> #include<cstring> #include<array>//ǵͷļ using namespace std; int main() { array<double, 3> record_list; double average; for (int i = 0; i < 3; i++) { cout << "Please enter " << i << endl; cin >> record_list[i]; } for (int i = 0; i < 3; i++) { cout << record_list[i] << endl; } return 0; }
1.编写一个要求用户输入两个整数的程序。输出包括这两个数的两数之间的数值之和。
#include<iostream> using namespace std; typedef long long ll; ll sum = 0; int main() { int min, max; cout << "Please enter two figure:"; cin >> min >> max; for (int i = min;i <= max; i++) { sum += i; } cout << sum << endl; return 0; }
2.使用array对象(而不是数组)和long double,计算 100!的值。
#include <iostream> #include <array> using namespace std; typedef long double ld; const int Size = 101; int main() { array<ld,Size> arr; // 初始化0和1的阶乘 arr[0] = arr[1] = 1; for (int i = 2;i < Size; i++) { arr[i] = i * arr[i-1]; } cout << arr[100] << endl; return 0; }
3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。
#include <iostream> using namespace std; int main() { double temp, sum = 0; do{ cout << "Input a numeral to add:"; cin >> temp; sum += temp; }while (temp != 0); cout << sum << endl; return 0; }
4.Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:
利息=0.10x原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%,:
利息=0.05x当前存款
Cleo在第一年投资100美元的盈利是5%—得到了105美元。下一年的盈利是105美元的5%—即5.25美元,依此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过 Daphne的投资价值,并显示此时两个人的投资价值。
#include <iostream> using namespace std; int main() { double Db = 100, Cb = 100; int year = 0; while (Cb <= Db) { Cb = Cb * 1.05; Db += 10; year++; } cout << "year:" << year << endl; cout << "Daphne:" << Db << " " << "Cleo:" << Cb << endl; return 0; }
5 假设要销售C++ For Fools 一书。请编写一个程序,输入全年中每个月的销售量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char *数组(或者string对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。然后,计算数组元素的总和,并报告这一年的销售情况。
#include <iostream> #include<string> using namespace std; typedef unsigned int ui; const int Size = 12; const string month[Size] = {"January","February","March","April","May","June","July","August","September","October","November","December"}; ui sum = 0; int main() { int sale_account[12] = {}; for (int i = 0;i < Size; i++) { cout << "Please enter the " << month[i] <<"\' amount:" ; cin >> sale_account[i]; } cout << "Input DONE!" << endl; for (int i = 0;i < Size; i++) { cout << "The " << month[i] << " \' amount:" << sale_account[i] << endl; sum += sale_account[i]; } cout << "Total sale " << sum << " this year." << endl; return 0; }
6.重做第五题,这一次使用二维数组来存储三年中每个月的的销售量,程序将报告每年的销售量以及三年的总销售量。
#include <iostream> #include<string> using namespace std; typedef unsigned int ui; const int MONTH = 12; const int YEAR = 3; const string month[MONTH] = {"January","February","March","April","May","June","July","August","September","October","November","December"}; ui sum = 0, part_sum; int main() { int sale_account[YEAR][MONTH] = {}; for (int j = 0;j < YEAR; j++) { cout << "This is " << j + 1 << " year" << endl; for (int i = 0;i < MONTH; i++) { cout << "Please enter the " << month[i] <<"\' amount:" ; cin >> sale_account[j][i]; sum += sale_account[j][i]; } cout << "This is end " << j + 1 << " year" << endl; } cout << "Input DONE!" << endl; for (int j = 0;j < YEAR; j++) { cout << j + 1 << "year" << endl; part_sum = 0; for (int i = 0;i < MONTH; i++) { cout << "The " << month[i] << " \' amount:" << sale_account[j][i] << endl; part_sum += sale_account[j][i]; } cout << "Total sale " << part_sum << " in " << j + 1 << " year." << endl; } cout << "Total sale " << sum << " three year." << endl; return 0; }
7.设计一个名为car的结构,用它存储有关汽车的信息——生产商、生产年份。编写一个程序,询问用户有多少量汽车。随后,使用new来创建一个由相应的的car结构体组成的动态数组。接下来,提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串。最后,显示每个结构体的内容。改程序的运行情况如下。
How many cars do you wish to catalog? 2
Car #1:
Please enter the marker: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the marker: Kaiser
Please enter the year made: 1951
Here is you collection:
1952 Hudson Hornet
1951 Kaiser
#include <iostream> #include<string> using namespace std; typedef struct car { string manufacturer; int date; }Car; int main() { int car_number; cout << "How many cars do you wish to catalog? "; cin >> car_number; cin.get();// 如果没有这一句则程序运行出现问题 Car* pcar; pcar = new Car[car_number]; for(int i = 0;i < car_number; i++) { cout << "Car #" << i + 1 << endl; cout << "Please enter the marker: "; getline(cin,pcar[i].manufacturer); cout << "Please enter the year made: "; cin >> pcar[i].date; cin.get(); } cout << "Here is you collection:" << endl; for (int i = 0;i < car_number; i++) { cout << pcar[i].date << " " << pcar[i].manufacturer << endl; } return 0; }
8.编写一个程序,它使用了一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序输出用户输入了多少单词,(不包括 done 在内)。下面该程序的运行情况。
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
应在程序中包含头文件 cstring ,并使用函数 strcmp()来进行比较。
#include <iostream> #include<cstring> using namespace std; const int SIZE = 20; const char FINISHED[] = "done"; int main() { int counter = 0; char words[SIZE]; cout << "Enter words (to stop, type the word done): " << endl; while (strcmp(FINISHED, words) != 0) { counter ++; cin >> words; cin.get(); // 题目要求读取单词,因此使用cin,并使用get()删除空白字符 } cout << "You entered a total of " << counter-1 << " words." << endl; return 0; }
9.重做编程练习8,但使用string对象而不是字符数组。请在程序中包含头文件,并使用关系运算符来进行比较。
#include <iostream> #include <string> using namespace std; const char FINISHED[] = "done"; int main() { int counter = 0; string words; cout << "Enter words (to stop, type the word done): " << endl; while (words != FINISHED) { counter ++; cin >> words; cin.get(); // 题目要求读取单词,因此使用cin,并使用get()删除空白字符 } cout << "You entered a total of " << counter-1 << " words." << endl; return 0; }
10.编写一个使用嵌套循环地程序,要求用户输入一个值,指定要显示的行数。然后,程序将显示相应函数的星号,其中第1行包括一个型号,第2行包括两个星号,以此类推。每一行包含的字符数等于当前行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下。
#include <iostream> #include <string> using namespace std; int main() { int n; cout << "Enter the number of rows:"; scanf("%d", &n); for (int i = 0;i < n; i++) { for (int j = 0;j < n - i - 1; j++) { cout << "."; } for (int j = 0;j <= i; j++) { cout << "*"; } cout << endl; } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。