Ich machte Binärrechner. Es funktioniert, aber nur, wenn die Zahlen nicht mit 0 beginnen. Wenn ja, zeigt das: Problem mit substr in C++
Wie sollte ich das Programm bitte reparieren? (HINEIN bedeutet Ergebnis) Teil des Codes mit substr:
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
ganze Programm: `
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
using namespace std;
int main()
{
cout << "Zadejte dve binarni cisla:" << endl;
string cislo1, cislo2;
cin >> cislo1 >> cislo2;
int c = 0;
for (c; c < cislo1.length(); c++)
{
if ((cislo1[c] == '0') || (cislo1[c] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
int d = 0;
for (d; d < cislo2.length(); d++)
{
if ((cislo2[d] == '0') || (cislo2[d] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
string stringS2 = "";
for(unsigned i = 0; i < cislo1.length(); i++)
{
stringS2 += cislo1[cislo1.length()-1-i];
}
cislo1 = stringS2;
string stringS3 = "";
for(unsigned i = 0; i < cislo2.length(); i++)
{
stringS3 += cislo2[cislo2.length()-1-i];
}
cislo2 = stringS3;
/*
cout << cislo1 << endl;
cout << cislo2 << endl;
cout << cislo1.length() << endl;
cout << cislo2.length() << endl;
*/
int p;
if (cislo1.length() > cislo2.length())
{
p = cislo1.length();
int f = cislo2.length();
for(f; f < cislo1.length(); f++)
{
cislo2[f] = '0';
}
}
else
{
p = cislo2.length();
int f = cislo1.length();
for(f; f < cislo2.length(); f++)
{
cislo1[f] = '0';
}
}
int e = 0;
//cout << p << endl;
string vysledek;
int t;
t = (p - 1);
cislo1[p];
cislo2[p];
for(e; e < p; e++)
{
/*nasledujici podminky jsou pro pripad, kdy cisla maji odlisny pocet znaku
if(cislo1[e] == NULL)
{
cislo1[e] == '0';
}
if(cislo2[e] == NULL)
{
cislo2[e] == '0';
}*/
if (cislo1[e] == '0' && cislo2[e] == '0')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1')|| ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
else if ((cislo1[e] == '1' && cislo2[e] == '0') || (cislo1[e] == '0' && cislo2[e] == '1'))
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '0';
}
else
{
vysledek[t] = '1';
}
// cout << vysledek[t] << endl;
t--;
}
else if (cislo1[e] == '1' && cislo2[e] == '1')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
}
cout << "Soucet: ";
int u;
u = (p - 1);
if(((cislo1[u] == '0' && cislo2[u] == '1')&& vysledek[0] == '0') || ((cislo1[u] == '1' && cislo2[u] == '0')&& vysledek[0] == '0') || (cislo1[u] == '1' && cislo2[u] == '1'))
cout << '1';
int o = 0;
int z;
int w = 0;
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
return 0;
}
`
Das richtige Werkzeug, um solche Probleme zu lösen, ist Ihr Debugger. Sie sollten Schritt für Schritt durch Ihren Code * gehen, bevor Sie auf Stack Overflow nachfragen. Für weitere Hilfe lesen Sie bitte [Wie kleine Programme zu debuggen (von Eric Lippert)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Zumindest sollten Sie Ihre Frage bearbeiten, um ein [minimales, vollständiges und verifizierbares] (http://stackoverflow.com/help/mcve) Beispiel einzufügen, das Ihr Problem zusammen mit den Beobachtungen, die Sie in der Debugger. –
Ist es wirklich einfacher, ein Bild vom Eingabeaufforderungsfenster zu machen, es zu imgur zu posten und hier zu verlinken, als wenn man den Text einfach kopieren und einfügen würde? –
@DanKorn Es funktioniert nicht ctrl + c – Tux