Arquivado
Este tópico foi arquivado e está fechado para novas respostas.
Pergunta: Operadores "and" (&&) e "or" (||)
Por
Yuri Fernandes da Silva, em C/C++
Recommended Posts
-
Conteúdo Similar
-
Por Sharank
Strcat Function In C++
I'm new to C and C++ programming, can anyone give me a hint on what I'm doing wrong here. I'm trying to write to concat function that takes to pointers to chars and concatenates the second to the first. The code does do that, but the problem is that it adds a bunch of junk at the end.
For instance, when passing the arguments - "green" and "blue", the output will be "greenblue" plus a bunch of random characters. I also wrote the strlen function that strcat uses, which I will provide below it for reference. I'm using the online compiler at InterviewBit The exact instructions and specification is this:
int main(int argc, char** argv)
{
const int MAX = 100;
char s1[MAX];
char s2[MAX];
cout << "Enter your first string up to 99 characters. ";
cin.getline(s1, sizeof(s1));
int size_s1 = strlen(s1);
cout << "Length of first string is " << size_s1 << "\n";
cout << "Enter your second string up to 99 characters. ";
cin.getline(s2, sizeof(s2));
int size_s2 = strlen(s2);
cout << "Length of second string is " << size_s2 << "\n";
cout << " Now the first string will be concatenated with the second
string ";
char* a = strcat(s1,s2);
for(int i = 0; i<MAX; i++)
cout <<a;
// system("pause");
return 0;
}
//strcat function to contatenate two strings
char* strcat(char *__s1, const char *__s2)
{
int indexOfs1 = strlen(__s1);
int s2L = strlen(__s2);
cout <<s2L << "\n";
int indexOfs2 = 0;
do{
__s1[indexOfs1] = __s2[indexOfs2];
indexOfs1++;
indexOfs2++;
}while(indexOfs2 < s2L);
return __s1;
}
//Returns length of char array
size_t strlen(const char *__s)
{
int count = 0;
int i;
for (i = 0; __s != '\0'; i++)
count++;
return (count) / sizeof(__s[0]);
}
-
Por roberson abalaid
#include <stdio.h>
#include <stdlib.h>
int arr[3][5];
int main(){
printf("Favor inserir os dados...\n");
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
scanf("%d", &arr[j]);
}
}
printf("os valores inseridos foram...\n");
for(int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
printf(" %d ", arr[j]);
}
printf("\n");
}
return 0;
}
-
Por Mateus GP
Boa noite.
Estou tendo problemas para compilar um código, não tenho ideia do que está acontecendo.
main.cpp
#include <iostream> #include "vector2.hpp" int main(int, char **) { Vector2f test; //test = (const float)0.0f; test.dot(test); return 0; } vector2.hpp
#ifndef vector2_header #define vector2_header #include <cmath> template <typename number_t> class Vector2; template <typename number_t> Vector2<number_t> operator+(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs); template <typename number_t> Vector2<number_t> operator+(const Vector2<number_t> &lhs, const number_t rhs); template <typename number_t> Vector2<number_t> operator+(const number_t lhs, const Vector2<number_t> &rhs); template <typename number_t> Vector2<number_t> operator-(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs); template <typename number_t> Vector2<number_t> operator-(const Vector2<number_t> &lhs, const number_t rhs); template <typename number_t> Vector2<number_t> operator-(const number_t lhs, const Vector2<number_t> &rhs); template <typename number_t> Vector2<number_t> operator*(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs); template <typename number_t> Vector2<number_t> operator*(const Vector2<number_t> &lhs, const number_t rhs); template <typename number_t> Vector2<number_t> operator*(const number_t lhs, const Vector2<number_t> &rhs); template <typename number_t> Vector2<number_t> operator/(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs); template <typename number_t> Vector2<number_t> operator/(const Vector2<number_t> &lhs, const number_t rhs); template <typename number_t> Vector2<number_t> operator/(const number_t lhs, const Vector2<number_t> &rhs); template <typename number_t> class Vector2 { public: number_t x; number_t y; Vector2() : x(0.), y(0.) {} Vector2(const number_t x_, const number_t y_) : x(x_), y(y_) {} Vector2(const Vector2 &v2d) : x(v2d.x), y(v2d.y) {} friend Vector2 operator+<number_t>(const Vector2 &lhs, const Vector2 &rhs); friend Vector2 operator+<number_t>(const Vector2 &lhs, const number_t rhs); friend Vector2 operator+<number_t>(const number_t lhs, const Vector2 &rhs); friend Vector2 operator-<number_t>(const Vector2 &lhs, const Vector2 &rhs); friend Vector2 operator-<number_t>(const Vector2 &lhs, const number_t rhs); friend Vector2 operator-<number_t>(const number_t lhs, const Vector2 &rhs); friend Vector2 operator*<number_t>(const Vector2 &lhs, const Vector2 &rhs); friend Vector2 operator*<number_t>(const Vector2 &lhs, const number_t rhs); friend Vector2 operator*<number_t>(const number_t lhs, const Vector2 &rhs); friend Vector2 operator/<number_t>(const Vector2 &lhs, const Vector2 &rhs); friend Vector2 operator/<number_t>(const Vector2 &lhs, const number_t rhs); friend Vector2 operator/<number_t>(const number_t lhs, const Vector2 &rhs); Vector2 &operator=(const Vector2 &rhs); Vector2 &operator=(const number_t &rhs); Vector2 &operator+=(const Vector2 &rhs); Vector2 &operator+=(const number_t rhs); Vector2 &operator-=(const Vector2 &rhs); Vector2 &operator-=(const number_t rhs); Vector2 &operator*=(const Vector2 &rhs); Vector2 &operator*=(const number_t rhs); Vector2 &operator/=(const Vector2 &rhs); Vector2 &operator/=(const number_t rhs); number_t lenghtSq() const; number_t lenght() const; number_t dot(const Vector2 &rhs) const; number_t cross(const Vector2 &rhs) const; Vector2 &rotate(const number_t rad); Vector2 rotated(const number_t rad) const; Vector2 &normalize(); Vector2 normalized() const; number_t distanceSq(const Vector2 &rhs) const; number_t distance(const Vector2 &rhs) const; number_t angle(const Vector2 &rhs) const; Vector2 absv() const; }; typedef Vector2<float> Vector2f; #endif // !vector2_header vector2.cpp
#include "vector2.hpp" template <typename number_t> Vector2<number_t> operator+(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs.x + rhs.x, lhs.y + rhs.y); } template <typename number_t> Vector2<number_t> operator+(const Vector2<number_t> &lhs, const number_t rhs) { return Vector2<number_t>(lhs.x + rhs, lhs.y + rhs); } template <typename number_t> Vector2<number_t> operator+(const number_t lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs + rhs.x, lhs + rhs.y); } template <typename number_t> Vector2<number_t> operator-(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs.x - rhs.x, lhs.y - rhs.y); } template <typename number_t> Vector2<number_t> operator-(const Vector2<number_t> &lhs, const number_t rhs) { return Vector2<number_t>(lhs.x - rhs, lhs.y - rhs); } template <typename number_t> Vector2<number_t> operator-(const number_t lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs - rhs.x, lhs - rhs.y); } template <typename number_t> Vector2<number_t> operator*(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs.x * rhs.x, lhs.y * rhs.y); } template <typename number_t> Vector2<number_t> operator*(const Vector2<number_t> &lhs, const number_t rhs) { return Vector2<number_t>(lhs.x * rhs, lhs.y * rhs); } template <typename number_t> Vector2<number_t> operator*(const number_t lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs * rhs.x, lhs * rhs.y); } template <typename number_t> Vector2<number_t> operator/(const Vector2<number_t> &lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs.x / rhs.x, lhs.y / rhs.y); } template <typename number_t> Vector2<number_t> operator/(const Vector2<number_t> &lhs, const number_t rhs) { return Vector2<number_t>(lhs.x / rhs, lhs.y / rhs); } template <typename number_t> Vector2<number_t> operator/(const number_t lhs, const Vector2<number_t> &rhs) { return Vector2<number_t>(lhs / rhs.x, lhs / rhs.y); } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator=(const Vector2<number_t> &rhs) { if(this != &rhs) { x = rhs.x; y = rhs.y; } return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator=(const number_t &rhs) { x = rhs; y = rhs; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator+=(const Vector2<number_t> &rhs) { x += rhs.x; y += rhs.y; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator+=(const number_t rhs) { x += rhs; y += rhs; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator-=(const Vector2<number_t> &rhs) { x -= rhs.x; y -= rhs.y; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator-=(const number_t rhs) { x -= rhs; y -= rhs; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator*=(const Vector2<number_t> &rhs) { x *= rhs.x; y *= rhs.y; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator*=(const number_t rhs) { x *= rhs; y *= rhs; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator/=(const Vector2<number_t> &rhs) { x /= rhs.x; y /= rhs.y; return *this; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::operator/=(const number_t rhs) { x /= rhs; y /= rhs; return *this; } template <typename number_t> number_t Vector2<number_t>::lenghtSq() const { return x * x + y * y; } template <typename number_t> number_t Vector2<number_t>::lenght() const { return sqrt(x * x + y * y); } template <typename number_t> number_t Vector2<number_t>::dot(const Vector2<number_t> &rhs) const { return x * rhs.x + y * rhs.y; } template <typename number_t> number_t Vector2<number_t>::cross(const Vector2<number_t> &rhs) const { return x * rhs.x - y * rhs.y; } template <typename number_t> Vector2<number_t> &Vector2<number_t>::rotate(const number_t rad) { const number_t tx(x), dc(cos(rad)), ds(sin(rad)); x = x * dc - y * ds; y = tx * ds + y * dc; return *this; } template <typename number_t> Vector2<number_t> Vector2<number_t>::rotated(const number_t rad) const { Vector2<number_t> nw(*this); return nw.rotate(rad); } template <typename number_t> Vector2<number_t> &Vector2<number_t>::normalize() { const number_t len(1. / lenght()); x *= len; y *= len; return *this; } template <typename number_t> Vector2<number_t> Vector2<number_t>::normalized() const { Vector2<number_t> nw(*this); return nw.normalize(); } template <typename number_t> number_t Vector2<number_t>::distanceSq(const Vector2<number_t> &rhs) const { const number_t dx(x - rhs.x); const number_t dy(y - rhs.y); return dx * dx + dy * dy; } template <typename number_t> number_t Vector2<number_t>::distance(const Vector2<number_t> &rhs) const { const number_t dx(x - rhs.x); const number_t dy(y - rhs.y); return sqrt(dx * dx + dy * dy); } template <typename number_t> number_t Vector2<number_t>::angle(const Vector2<number_t> &rhs) const { Vector2<number_t> nv(normalized()); number_t ang(nv.dot(rhs.normalized())); if (ang > 1.0) ang = 1.0; if (ang < -1.0) ang = -1.0; return -acos(ang); } template <typename number_t> Vector2<number_t> Vector2<number_t>::absv() const { return Vector2<number_t>(abs(x), abs(y)); } O problema é que aparentemente funciona se copiar o código de vector2.cpp para main.cpp.
Alguém tem alguma sugestão? Se houver outra fora o problema, também é bem-vinda.
Obs: já tentei compilar pelo terminal também.
CMakeLists.txt
-
Por flipmartinz13
Alguém pode me ajudar nessa questão de C++? não estou conseguindo construir o algorítmo corretamente.
5.92) Faça um algoritmo que leia a matrícula, nome, sexo e três notas dos alunos de uma escola e obtenha os seguintes resultados:
a) A matrícula da aluna que obteve a maior média.
b) A matrícula do aluno que obteve a menor média.
c) O percentual de mulheres na turma.
d) Quantos alunos foram aprovados, independente do sexo.
e) O percentual de alunas aprovadas.
Obs.: o flag é uma matrícula igual a 0 (zero).