第五章 循环和关系表达式

来源:互联网 发布:淘宝排版教程 编辑:程序博客网 时间:2024/05/29 09:55

第五章 循环和关系表达式

第一题 输入两个整数并计算这两个整数中间所有数的总和。包括这个两个整数。

nclude<iostream>using namespace std;int main(){    cout << "Enter integer A: ";    int a;    cin >> a;    cout << "Enter integer B: ";    int b;    cin >> b;    int temp;    if (a>b)    {        temp = b;        b = a;        a = temp;    }    int sum=0;    for (int i = a; i <= b; i++)    {        sum += i;    }        cout <<"The sum value Between A and B is: "<<sum<<endl;        system("pause");        return 0;}

结果显示5.1

第二题 累加输入值并显示,直到输入0。

#include<iostream>using namespace std;int main(){    int a;    cout << "Enter a number: ";    cin >> a;    int sum = 0;    while (a!=0)    {        sum += a;        cout << "Now the result is : "<<sum<<endl;        cout << "Enter a new number: ";        cin >> a;    }    cout << "The final result is "<<sum<<". Program ends."<< endl;    system("pause");    return 0;}

结果显示5.2

第三题 两人透支,一个单利,一个复利,计算何时复利超过单利。

#include<iostream>using namespace std;int main(){    int Daphne = 100;    double Cleo = 100;    int n=1;    for (; Daphne>=Cleo; n++)    {        Daphne = Daphne + 10;        Cleo = Cleo*(1 + 0.05);    }    Daphne = Daphne + 10;    Cleo = Cleo*(1 + 0.05);        cout << "After " << n << " years,Daphne has ";    cout << Daphne << " , Cleo has " << Cleo << endl;    system("pause");    return 0;}

结果显示5.3

第四题 使用初始化的月份字符串数组作为提示,根据用户输入记录每月销售书本数量,计算一年的销售总数量并显示。

#include<iostream>#include<string>using namespace std;int main(){    string month[12] = { "January","Febuary" ,"March","Apirl","May","June","July","August","September","October","November","December"};    int saleNum[12] = {0};    int amount=0;    for (int n=0; n<=11; n++)    {        cout << month[n]<<" sales ___ "<<"books.\b\b\b\b\b\b\b\b\b\b";        cin >> saleNum[n];        amount += saleNum[n];    }    cout << "This year sales " <<amount<<" books. "<<endl;    system("pause");    return 0;}

结果显示5.4

第五题 完成上一题,使用二维数组存储三年的销售额,计算并显示每年的销售数及三年总销售数。

#include<iostream>#include<string>using namespace std;int main(){    string month[3][12] = { { "2015January", "Febuary", "March", "Apirl", "May", "June", "July", "August", "September", "October", "November", "December" },    { "2016January", "Febuary", "March", "Apirl", "May", "June", "July", "August", "September", "October", "November", "December" },     { "2017January", "Febuary", "March", "Apirl", "May", "June", "July", "August", "September", "October", "November", "December" } };    int saleNum[3][12] = { 0 };    int amount[3] = { 0 };    for (int n = 0; n <= 2; n++)    {        for (int m = 0; m <= 11; m++)        {cout << month[n][m] << " sales ___ " << "books.\b\b\b\b\b\b\b\b\b\b";        cin >> saleNum[n][m];        amount[n] += saleNum[n][m];        }    }    cout << "2015 year sales " << amount[0] << " books. " << endl;    cout << "2016 year sales " << amount[1] << " books. " << endl;    cout << "2017 year sales " << amount[2] << " books. " << endl;    cout << "3 year sales " << amount[0] + amount[1] + amount[2]<< " books. " << endl;    system("pause");    return 0;}

结果显示5.5-1
5.5-2

第六题 设计一个car结构,存储生产商,及年份,从键盘输入并存储,使用new来创建car结构动态数组,并显示所有结果。

#include<iostream>#include<string>using namespace std;struct car{ char name[30]; int time; };int main(){       cout << "How many cars do you wish to catalong ? ";    int carNum;    cin >> carNum;    cin.get();    car *p = new car[carNum];    for (int i = 0; i < carNum;i++)    {         cout << "Car  #" << i + 1<<":\n";        cout << "Please enter the make: ";        cin.get(p[i].name,29);        cout << "Please enter the year made: ";        cin>>p[i].time;         cin.get();    }    cout << "Here is your collection:\n ";    for (int i = 0; i < carNum; i++)    {        cout << p[i].time << "  " << p[i].name << endl;    }    system("pause");    return 0;}

结果显示5.6

第七题 设计一个使用char数组的循环结构读取单词,以done结束,统计总共输入了多少单词。

#include<iostream>#include<string>using namespace std;int main(void){    char temp[30] = {0};    cout << "Enter words(to stop,type the word done): \n";    cin >> temp;    int count = 0;    while (strcmp(temp, "done") != 0)    {        cin >> temp;        count++;    }    cout << "You enterde a total of "<<count<<" words." << endl;    system("pause");    return 0;}

结果显示5.7

第八题 设计一个使用string数组的循环结构读取单词,使用关系操作符以done结束,统计总共输入了多少单词。

#include<iostream>#include<string>using namespace std;int main(void){    string temp;    cout << "Enter words(to stop,type the word done): \n";    cin >> temp;    int count = 0;    while (temp!="done")    {        cin >> temp;        count++;    }    cout << "You enter a total of " << count << " words." << endl;    system("pause");    return 0;}

结果显示5.8

第九题 设编写循环程序,根据键盘输入确定输出多少行,按要求显示*。

#include<iostream>#include<string>using namespace std;int main(void){    int num;    cout << "Enter number of rows: ";    cin >> num;    int i;    for (i = 1; i <= num; i++)    {        for (int j = 1; j <= num - i; j++)        {            cout << ".";        }        for (int k = 1; k <= i; k++)        {            cout << "*";        }        cout <<endl;    };    system("pause");    return 0;}

结果显示5.9

第五章结束^.^.

0 0
原创粉丝点击