ورود

View Full Version : سوال: مرتب کردن حروف یا اعداد در c++



ali449
شنبه 15 آبان 1395, 10:20 صبح
برنامه ای که 4 کاراکتر بگیره و اونا رو مرتب کنه؟(بدون استفاده از حلقه و آرایه ها)

نمونه ورودی:
s
g
a
l
خروجی:a g ls
ورودی:
2 9 5 3
خروجی: 2 3 5 7

علی بهمنی جلالی
یک شنبه 16 آبان 1395, 14:14 عصر
سلام
این برنامه، ۴ نویسهٔ ASCII می‌گیرد و نویسه‌ها را از بزرگ به کوچک در خروجی چاپ می‌کند.


#include <iostream>
using namespace std;


int main()
{
char a;
char b;
char c;
char d;


bool aPrint = true;
bool bPrint = true;
bool cPrint = true;
bool dPrint = true;


cout << "Enter four characters or numbers ( 0-9 ): " << endl;
cout << "Character or number 1: ";
cin >> a;
cout << "Character or number 2: ";
cin >> b;
cout << "Character or number 3: ";
cin >> c;
cout << "Character or number 4: ";
cin >> d;


char maximumValue = a;


if ( b > maximumValue )
{
maximumValue = b;
}


if ( c > maximumValue )
{
maximumValue = c;
}


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
aPrint = false;
}


else if ( b == maximumValue )
{
bPrint = false;
}


else if ( c == maximumValue )
{
cPrint = false;
}


else if ( d == maximumValue )
{
dPrint = false;
}


if ( aPrint == false )
{
maximumValue = b;


if ( c > maximumValue )
{
maximumValue = c;
}


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( b == maximumValue )
{
bPrint = false;
}


else if ( c == maximumValue )
{
cPrint = false;
}


else if ( d == maximumValue )
{
dPrint = false;
}


if ( bPrint == false )
{
maximumValue = c;


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( c == maximumValue )
{
cout << d;
}


else
{
cout << c;
}
}


if ( cPrint == false )
{
maximumValue = b;


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( b == maximumValue )
{
cout << d;
}


else
{
cout << b;
}
}


if ( dPrint == false )
{
maximumValue = b;


if ( c > maximumValue )
{
maximumValue = c;
}


cout << maximumValue << " ";


if ( b == maximumValue )
{
cout << c;
}


else
{
cout << b;
}
}
}


else if ( bPrint == false )
{
maximumValue = a;


if ( c > maximumValue )
{
maximumValue = c;
}


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
aPrint = false;
}


else if ( c == maximumValue )
{
cPrint = false;
}


else if ( d == maximumValue )
{
dPrint = false;
}


if ( aPrint == false )
{
maximumValue = c;


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( c == maximumValue )
{
cout << d;
}


else
{
cout << c;
}
}


if ( cPrint == false )
{
maximumValue = a;


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
cout << d;
}


else
{
cout << a;
}
}


if ( dPrint == false )
{
maximumValue = a;


if ( c > maximumValue )
{
maximumValue = c;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
cout << c;
}


else
{
cout << a;
}
}
}


else if ( cPrint == false )
{
maximumValue = a;


if ( b > maximumValue )
{
maximumValue = b;
}


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
aPrint = false;
}


else if ( b == maximumValue )
{
bPrint = false;
}


else if ( d == maximumValue )
{
dPrint = false;
}


if ( aPrint == false )
{
maximumValue = b;


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( b == maximumValue )
{
cout << d;
}


else
{
cout << b;
}
}


if ( bPrint == false )
{
maximumValue = a;


if ( d > maximumValue )
{
maximumValue = d;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
cout << d;
}


else
{
cout << a;
}
}


if ( dPrint == false )
{
maximumValue = a;


if ( b > maximumValue )
{
b = maximumValue;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
cout << b;
}


else
{
cout << a;
}
}
}


else if ( dPrint == false )
{
maximumValue = a;


if ( b > maximumValue )
{
maximumValue = b;
}


if ( c > maximumValue )
{
maximumValue = c;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
aPrint = false;
}


else if ( b == maximumValue )
{
bPrint = false;
}


else if ( c == maximumValue )
{
cPrint = false;
}


if ( aPrint == false )
{
maximumValue = b;


if ( c > maximumValue )
{
maximumValue = c;
}


cout << maximumValue << " ";


if ( b == maximumValue )
{
cout << c;
}


else
{
cout << b;
}
}


if ( bPrint == false )
{
maximumValue = a;


if ( c > maximumValue )
{
maximumValue = c;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
cout << c;
}


else
{
cout << a;
}
}


if ( cPrint == false )
{
maximumValue = a;


if ( b > maximumValue )
{
maximumValue = b;
}


cout << maximumValue << " ";


if ( a == maximumValue )
{
cout << b;
}


else
{
cout << a;
}
}
}
}

علی بهمنی جلالی
یک شنبه 16 آبان 1395, 14:25 عصر
این یکی، ۴ نویسهٔ ASCII می‌گیرد و نویسه‌ها را از کوچک به بزرگ در خروجی چاپ می‌کند.

#include <iostream>
using namespace std;


int main()
{
char a;
char b;
char c;
char d;


bool aPrint = true;
bool bPrint = true;
bool cPrint = true;
bool dPrint = true;


cout << "Enter four characters or numbers ( 0-9 ): " << endl;
cout << "Character or number 1: ";
cin >> a;
cout << "Character or number 2: ";
cin >> b;
cout << "Character or number 3: ";
cin >> c;
cout << "Character or number 4: ";
cin >> d;


char minimumValue = a;


if ( b < minimumValue )
{
minimumValue = b;
}


if ( c < minimumValue )
{
minimumValue = c;
}


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
aPrint = false;
}


else if ( b == minimumValue )
{
bPrint = false;
}


else if ( c == minimumValue )
{
cPrint = false;
}


else if ( d == minimumValue )
{
dPrint = false;
}


if ( aPrint == false )
{
minimumValue = b;


if ( c < minimumValue )
{
minimumValue = c;
}


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( b == minimumValue )
{
bPrint = false;
}


else if ( c == minimumValue )
{
cPrint = false;
}


else if ( d == minimumValue )
{
dPrint = false;
}


if ( bPrint == false )
{
minimumValue = c;


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( c == minimumValue )
{
cout << d;
}


else
{
cout << c;
}
}


if ( cPrint == false )
{
minimumValue = b;


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( b == minimumValue )
{
cout << d;
}


else
{
cout << b;
}
}


if ( dPrint == false )
{
minimumValue = b;


if ( c < minimumValue )
{
minimumValue = c;
}


cout << minimumValue << " ";


if ( b == minimumValue )
{
cout << c;
}


else
{
cout << b;
}
}
}


else if ( bPrint == false )
{
minimumValue = a;


if ( c < minimumValue )
{
minimumValue = c;
}


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
aPrint = false;
}


else if ( c == minimumValue )
{
cPrint = false;
}


else if ( d == minimumValue )
{
dPrint = false;
}


if ( aPrint == false )
{
minimumValue = c;


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( c == minimumValue )
{
cout << d;
}


else
{
cout << c;
}
}


if ( cPrint == false )
{
minimumValue = a;


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
cout << d;
}


else
{
cout << a;
}
}


if ( dPrint == false )
{
minimumValue = a;


if ( c < minimumValue )
{
minimumValue = c;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
cout << c;
}


else
{
cout << a;
}
}
}


else if ( cPrint == false )
{
minimumValue = a;


if ( b < minimumValue )
{
minimumValue = b;
}


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
aPrint = false;
}


else if ( b == minimumValue )
{
bPrint = false;
}


else if ( d == minimumValue )
{
dPrint = false;
}


if ( aPrint == false )
{
minimumValue = b;


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( b == minimumValue )
{
cout << d;
}


else
{
cout << b;
}
}


if ( bPrint == false )
{
minimumValue = a;


if ( d < minimumValue )
{
minimumValue = d;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
cout << d;
}


else
{
cout << a;
}
}


if ( dPrint == false )
{
minimumValue = a;


if ( b < minimumValue )
{
b = minimumValue;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
cout << b;
}


else
{
cout << a;
}
}
}


else if ( dPrint == false )
{
minimumValue = a;


if ( b < minimumValue )
{
minimumValue = b;
}


if ( c < minimumValue )
{
minimumValue = c;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
aPrint = false;
}


else if ( b == minimumValue )
{
bPrint = false;
}


else if ( c == minimumValue )
{
cPrint = false;
}


if ( aPrint == false )
{
minimumValue = b;


if ( c < minimumValue )
{
minimumValue = c;
}


cout << minimumValue << " ";


if ( b == minimumValue )
{
cout << c;
}


else
{
cout << b;
}
}


if ( bPrint == false )
{
minimumValue = a;


if ( c < minimumValue )
{
minimumValue = c;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
cout << c;
}


else
{
cout << a;
}
}


if ( cPrint == false )
{
minimumValue = a;


if ( b < minimumValue )
{
minimumValue = b;
}


cout << minimumValue << " ";


if ( a == minimumValue )
{
cout << b;
}


else
{
cout << a;
}
}
}
}

Ananas
چهارشنبه 19 آبان 1395, 19:14 عصر
سلام.
سعی کنید با فرمول انجام بدید که تک تک نخاید بهش دستور بدید.

#include <iostream>
using namespace std;


int main()
{
#define COUNT 10
char abcd[COUNT];

cout << "Enter four characters or numbers ( 0-9 ): " << endl;
for (int i = 0; i < COUNT; i++)
{
cout << "Character or number " << (i + 1) << ": ";
cin >> abcd[i];
};

for (int i = 0; i < COUNT; i++)
{
for (int j = i + 1; j < COUNT; j++)
{
if (abcd[i] > abcd[j])
{
int x = abcd[j];
abcd[j] = abcd[i];
abcd[i] = x;
};
};
};

for (int i = 0; i < COUNT; i++)
{
cout << abcd[i] << " ";
};

getchar();
getchar();

};