1.编写一个要求用户输入两个整数的程序,giant程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.
#include <iostream>
using namespace std;
int main()
{
int x,y;
cin>>x;
cin>>y;
int sum=0;
for(int i=x;x<=y;x++)
{
sum=sum+x;
}
cout<<sum<<endl;
return 0;
}
3.编写一个要求用户输入数字的程序,每次输入后,程序都将报告到目前为止,所有输入的累积和,当用户输入0时,程序结束。
#include <iostream>4.Daphne以10%的单利投资了100美元,每一年利润都是投资额的10%,即每年10美元。而Cleo以5%的复利投资了100美元,利息是当前存款(包括获得的利息)的5%,Cleo在第一年投资100美元的盈利是5%得到了105美元,下一年的盈利是105美元的5%,以此类推编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示两个人的投资价值。
int main()
{
using namespace std;
int sum=0;
int num;
cout<<"Enter the number ;enter 0to quit:\n ";
cin>>num;
while(num!=0)
{
sum +=num;
cout<<sum;
cin>>num;
}
cout<<endl;
cout<<sum<<endl;
return 0;
}
#include <iostream>
int main()
{
using namespace std;
double daphne=100;
double cleo=100;
int count=0;
while(cleo<=daphne)
{
count++;
daphne +=10;
cleo *=1.05;
}
cout<<count<<endl;
cout<<"Daphne: "<<daphne<<endl;
cout<<"Cleo: "<<cleo<<endl;
return 0;
}
5.假设要销《c++for fool》一书,请编写一个程序,输入全年中每个月的销售量,图书数量而不是销售额
,程序通过循环,使用初始化为月份字符串的char*数组,或string对象数组,逐月进行体术并肩输入的数据出、、UN除杂䘝int数组中,然后程序计算数组中各元素的总数,并报告这一年的销售情况。
#include <iostream>6.完成编程练习5,但这一次使用一个二维数组来存储输入---3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
int main()
{
using namespace std;
int sales[12];
string month[12]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "};
int sum=0;
for(int i=0;i<12;i++)
{
cout<<"Please enter the sales of"<<month[i]<<"is: ";
cin>>sales[i];
sum +=sales[i];
}
for(int i=0;i<+12;i++)
{
cout<<"The sales of "<<month[i]<<"is: "<<sales[i]<<endl;
}
cout<<"The sales of a year is "<<sum<<endl;
return 0;
}
#include <iostream>7.设计一个名为car的结构,用它存储下述有关汽车的信息: 生产商(存储在字符数组或string对象中的字符串)、 生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。 接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取 数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。
#include<string>
int main()
{
using namespace std;
int sales[3][12];
string month[12]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "};
int sum[3]={0,0,0};
for(int i=0;i<3;i++)
{
for(int j=0;j<12;j++)
{
cout<<"Please enter the sales of "<<month[j]<<" in "<<(i+1)<<" year is: ";
cin>>sales[i][j];
sum[i] +=sales[i][j];
}
}
for(int i=0;i<=2;i++)
{
cout<<"The sales of "<<(i+1)<<" year is: "<<sum[i]<<endl;
}
cout<<"The sales of three year is "<<(sum[0]+sum[1]+sum[2])<<endl;
return 0;
}
#include <iostream>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 toal of 7 words. 您应该在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试
#include<string>
struct car
{
char producer[20];//生产商
int pro_year;//生产年份
};
int main()
{
using namespace std;
cout<<"How many cars do you wish to catalog?";
int count;
(cin>>count).get();
car *ca=new car[count];
for(int i=1;i<=count;i++)
{
cout<<"car #"<<i<<":"<<endl;
cout<<"Please enter the make :";
cin.getline(ca[i].producer,20);
// cin.getline(ca[i].producer,20);
cout<<"Please enter the year made: ";
(cin>>ca[i].pro_year).get();
}
cout<<"Here is your collection:"<<endl;
for(int i=1;i<=count;i++)
{
cout<<ca[i].pro_year<<" "<<ca[i].producer<<endl;
}
delete []ca;
return 0;
}
#include <iostream>9.编写一个满足前一个练习中描述的程序,但是用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
#include<cstring>
#
int main()
{
using namespace std;
int count=0;
char word[20];
cout<<"Enter worsds(to stop,type the word done):";
cin>>word;
while(strcmp(word,"done"))
{
count++;
cin>>word;
}
cout<<"You entered a total of "<<count<<"words"<<endl;
return 0;
}
#include <iostream>
#include<string>
int main()
{
using namespace std;
int count=0;
string word;
string c = "done";
cout<<"Enter worsds(to stop,type the word done):";
cin>>word;
while(word!=c)
{
count++;
cin>>word;
}
cout<<"You entered a total of "<<count<<" words"<<endl;
return 0;
}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行,然后程序将显是相应的函数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推,每一行包含的字符数等于用户指定的函数,在星号不够的情况下,在星号前面加上句点。
Enter number of rows: 5
....*
...**
..***
.****
*****
#include <iostream>
#include<string>
int main()
{
using namespace std;
int count;
cout<<"Enter number of row: ";
cin>>count;
for(int i=1;i<=count;i++)
{
for(int j=1;j<= (count-i);j++)
{
cout<<" . ";
}
for(int j=1;j<=i;j++)
cout<<" * ";
cout<<endl;
}
return 0;
}