[C ++ Primer (5. Ausgabe) Übung] Übungsprogramm-Kapitel1 (Kapitel 1)

Das folgende Programm wurde von Teddy van Jerry (mir) geschrieben und ausgeführt, um die Richtigkeit zu gewährleisten. (Manchmal können einige Inhalte im Voraus für den Optimierungsprozess verwendet werden.)

Bevor wir die Codes durchkämmen

Kein C ++? Siehe meine Blog- Schritte zum Herunterladen von C ++ (Visual Studio 2019) ist .

Dieses Kapitel ist grundlegender und konzentriert sich auf die Entwicklung guter Gewohnheiten.
(Tipp: Bei Verwendung der englischen Version von VS werden die Quelldateien zu Quelldateien (nicht zu Ressourcendateien) hinzugefügt, und der Speicherort unterscheidet sich von der chinesischen Version.)


TIPP: markiert mit (C ++ / 11) Ist eine Frage, die unter dem C ++ / 11-Standard verwendet werden kann. Wenn es sich um eine alte Version handelt, sollte sie geändert werden.


Übung 1.3

#include <iostream>

int main() // If using void main(), don't write 'return 0' (Line 6).
{
    
    
	std::cout << "Hello World" << std::endl;
	return 0;
}

Übung 1.4

#include <iostream>
using namespace std; // namespace using declaration (now we can omit std::)

int main()
{
    
    
	int a = 0;
	int b = 0;
	cout << "Please enter two numbers." << endl;
	cin >> a >> b;
	cout << "The product of " << a << " and " << b << " is " << a * b << "." <<endl;
	return 0;
}

Übung 1.8

#include <iostream>
using namespace std;

int main()
{
    
    
	cout << "/*";
	cout << "*/";
	return 0;
}

Übung 1.9

#include <iostream>
using namespace std;

int main()
{
    
    
	int s = 0;
	int i = 50;
	while (i <= 100) {
    
    
		s += i;
		++i;
	}
	cout << "Sum of 50 to 100 inclusive is " << s << "." << endl;
	return 0;
}

Übung 1.10

#include <iostream>

int main()
{
    
    
	int s = 0;
	int i = 10;
	while (i >= 1) {
    
    
		s += i;
		--i;
	}
	std::cout << s << std::endl;
	return 0;
}

Übung 1.11

#include <iostream>
using namespace std;

int main()
{
    
    
	int a = 0; int b = 0; int i = 0; int t = 0;
	cout << "Please enter two numbers." << endl;
	cin >> a >> b;
	if ((a == b) | (a==b+1) | (a==b-1))
		cout << "Sorry, there's none." << endl;
	else {
    
    
		if(a>b)
		{
    
    
			t = a; a = b; b = t;
		}
		i = a+1;
		while (i < b - 1) {
    
    
            cout << i << " ";
			++i;
		}
		cout << b-1 << endl;
	}
	return 0;
}

Übung 1.12

#include <iostream>
using namespace std;

int main()
{
    
    
	int s = 0;
	for (int i = -100; i <= 100; i++)
		s += i;
	cout << s << endl;
	return 0;
}

Übung 1.13

#include <iostream>
using namespace std;

int main()
{
    
    
	int a = 0; int b = 0; int t = 0;
	cout << "Please enter two numbers." << endl;
	cin >> a >> b;
	if ((a == b) | (a == b + 1) | (a == b - 1))
		cout << "Sorry, there's none." << endl;
	else {
    
    
		if (a > b)
		{
    
    
			t = a; a = b; b = t;
		}
		for (int i = a + 1; i < b - 1; i++)
			cout << i << " ";
		cout << b - 1 << endl;
	     }
	return 0;
}

Übung 1.16

#include <iostream>
using namespace std;

int main()
{
    
    
	int i = 0;
	int s = 0;
	for (; cin >> i;)
//	while (cin >> i)
		s += i;
	cout << s << endl;
	return 0;
}

Übung 1.18

#include <iostream>
using namespace std;

int main()
{
    
    
	int a = 0;
	int b = 0;
	int s = 1;
	int t = 0;
	cout << "Make sure that the same numbers aren't seperated." << endl;
	cout << "When stopping inputting numbers, please enter Control-z before pressing the Enter button." << endl;
	if (cin >> b)
	{
    
    
		while (cin >> a) {
    
    
			if (a == b)
				s++;
			else 
			{
    
    
				if (s != 1)
					cout << b << " occurs " << s << " times" << endl;
                else cout << b << " occurs 1 time" << endl;
					b = a;
					s = 1;
					t++;
			}
		}
		if(s!=1)
			cout << b << " occurs " << s << " times" << endl;
		else cout << b << " occurs 1 time" << endl;
	}
	else cout << "no number" << endl;
	return 0;
}

Übung 1.20

Tipp: Laden Sie die Header-Datei unter der im Buch http://informit.com/title/0321714113 angegebenen URL herunter, geben Sie https://www.informit.com/store/c-plus-plus-primer-9780321714114 ein und achten Sie auf das Herunterziehen Um die Download-Optionen zu sehen.
Beispiel

#include <iostream>
#include "Sales_item.h"
using namespace std;

int main()
{
    
    
	Sales_item book;
	cin >> book;
	cout << book << endl;
	return 0;
}

Übung 1.21

#include <iostream>
#include "Sales_item.h"
using namespace std;

int main()
{
    
    
	Sales_item item1, item2;
	cin >> item1 >> item2;
	cout << item1 + item2 << endl;
	return 0;
}

Übung 1.22

#include <iostream>
#include "Sales_item.h"
using namespace std;

int main()
{
    
    
	Sales_item item;
	Sales_item sum;
	cout << "Please enter control-z when finishing inputting numbers." << endl;
	cin >> item;
	sum = item;
	while (cin >> item)
		sum += item;
	cout << sum << endl;
	return 0;
}

Übung 1.23

#include <iostream>
#include "Sales_item.h"
using namespace std;

int main()
{
    
    
	Sales_item item, init; //'init' refers to 'initial'
	int s = 1;
	int t = 0;
	int sum[1000] ;
	string name[1000];
	//This means there should be no more than 1000 ISBN numbers.
	for (int i = 0; i <= 999; i++)
		sum[i] = 0;
	cout << "Please enter control-z when finishing inputting information." << endl;
	cin >> init;
	while (cin >> item)
	{
    
    
		if (item.isbn() == init.isbn())
			s++;
		else
		{
    
    
			sum[t] = s; //store occurring times in sum[1000]
			name[t] = init.isbn(); //store ISBN in name[1000]
			s = 1;
			t++;
			init = item;
		}
	}
	for (int j = 0; j < t; j++)
	{
    
    
		if (sum[j] != 1)
			cout << name[j] << " occurs " << sum[j] << " times" << endl;
		else cout << name[j] << " occurs 1 time" << endl;
	}
	if (s != 1)
		cout << init.isbn() << " occurs " << s << " times" << endl;
	else cout << init.isbn() << " occurs 1 time" << endl;
	cout << "@Teddy van Jerry" << endl;
	return 0;
}

Kommentare finden Sie in meinem Blog [Thinking] , in dem auch andere Operationsmethoden (unter Verwendung von Vektor) vorgeschlagen werden.

Übung 1.25

#include <iostream>
#include "Sales_item.h"
using namespace std;

int main()
{
    
    
	Sales_item total, trans;
	if (cin >> total)
	{
    
    
		while(cin >> trans)
			if (total.isbn() == trans.isbn())
				total += trans;
			else
			{
    
    
				cout << total << endl;
				total = trans;
			}
	}
	else
	{
    
    
         cerr << "No data?!" << endl;
		 return -1;
	}
	return 0;
}

Nächstes Kapitel

[C ++ Primer (5. Ausgabe) Übung] Übungsprogramm-Kapitel 2 (Kapitel 2)

Ich denke du magst

Origin blog.csdn.net/weixin_50012998/article/details/108183173
Empfohlen
Rangfolge