/*编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间
(包含这两个整数)所有整数的和。例如,如果用户输入的是2和9,则程序将指
出2~9之间所有整数的和为44.*/#include<iostream>usingnamespace std;intmain(){int A;int B;
cout <<"please enter two numbers:"<< endl;
cin >> A;
cin>>B;
cout <<"your entered number are: "<< A <<" and "<< B << endl;if(A == B){
cout <<" enter number is error"<< endl;return0;}if(A < B){int C =0;for(int i = A; A <= B ; A++){
C += A;}
cout << C << endl;}else{int C =0;for(int i = B; B <= A ; B++){
C += B ;}
cout << C << endl;}system("PAUSE");}
/*编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前位置,
所有输入的累计和。但用户输入0是,程序结束。*/#include<iostream>#include<array>usingnamespace std;intmain(){int a =1;int k =0;while(a !=0){
cout <<"please input a number:"<< endl;
cin >> a;
k += a;
cout <<"sum :"<< k << endl;}system("PAUSE");}
/*假设要销售《《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销量
(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char *数组
(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后,
程序计算数组中元素的总数,并报告这一年的销售情况。*/#include<iostream>#include<vector>usingnamespace std;intmain(){int sales[12];int sum =0;for(int i =0; i <12; i++){int k =0;
cout <<"please enter the sales of "<< i+1<<"th month"<< endl;
cin >> k;
sum += k;
sales[i]= k;}
cout <<"the all sales are "<< sum << endl;system("PAUSE");}
/*设计一个名为car的结构,用它存储下述有关汽车的信息:
生产商(存储在字符数组或string对象中的字符串)、
生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。
随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。
接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。
请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。
最后,程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
*/#include<iostream>#include<string>usingnamespace std;struct car {char name[30];int year;};intmain(){int catalogNumber;
cout <<"How many cars do you wish to catalog?"<< endl;(cin >> catalogNumber).get();
car* List =new car[catalogNumber];for(int i =0; i < catalogNumber;++i){
cout <<"Car #"<< i +1<< endl;
cout <<"Please enter the make:"<< endl;
cin.get(List[i].name,30);
cout <<"Please enter the year made:"<< endl;(cin >> List[i].year).get();}
cout <<"Hear is your collection:"<< endl;for(int i =0; i < catalogNumber; i++){
cout << List[i].year <<" "<< List[i].name << endl;}delete[] List;system("PAUSE");};
/*编写一个程序,它使用一个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,并使用函数石头人触摸屏()来进行比较测试。*/#include<iostream>#include<cstring>usingnamespace std;intmain(){char List1[5]{"done"};char List2[30]{"first"};int flag =0;
cout <<"Enter words (to stop, type the word done):"<< endl;while(strcmp(List2, List1)){(cin >> List2).get();
flag ++;}
cout <<"You entered a total of "<< flag -1<<" words."<< endl;system("PAUSE");}/*编写一个瞒住前一个练习中描述的程序,但使用string对象而不是字符数组。
请在程序中包含头文件string,并使用关系运算符来进行比较测试。*/#include<iostream>#include<string>usingnamespace std;intmain(){
string str1{"done"};
string str2;int flag =0;
cout <<"Enter words (to stop, type the word done):"<< endl;while(str2 != str1){(cin >> str2).get();
flag++;}
cout <<"You entered a total of "<< flag -1<<" words."<< endl;system("PAUSE");}
编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。
然后,程序将显示相应行数的星号,第二行包括两个星号,依次类推。
每一包含的字符数等于用户指定的行数,在星号不够的情况下,在星号
前面加上句点。改程序的运行情况如下:
Enter number of rows:5....*...**..***.*********#include<iostream>usingnamespace std;intmain(){int row;
cout <<"Please enter number of rows:"<< endl;
cin >> row;for(int i =0; i < row; i++){for(int j =0; j < i; j++){
cout <<".";}for(int j =0; j < row-i; j++){
cout <<"*";}
cout << endl;}system("PAUSE");}