الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[ تمت الإجابة ]سؤال : كيف يتم تنسيق الخرج في ++C

بدأه هيفـــاء في 28 أبريل 2013 · 3 رد · 765 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

 

اريد الاستفسار عن طريقة التنسيق ناتج ضرب عددين

كتبت برنامج بغة السي بلس بلس وكانت وظيفته يقوم بعرض ناتج ضرب عددين

 

وهادا هو الكود في بيئة .net

 


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

int main ()
{
    int a,bو,c;
    cout<<"Enter the First Number : ";
    cin>>a;
    cout<<"Enter the Second Number : ";
    cin>>b;

    c=a*b;

    cout<< "The Result is : "<<c;

}

 

سؤالي هو اريد تنسيق الناتج بحيث تظهر فراغات بين الرقم والاخر

مثال

إذا كانت قيمة

a = 75

وقيمة

b = 86

فإن القيمة التي تظهر في الشاشة هي

6540

 

اريد تعديل البرنامج بحيث يظهر الناتج على هذا الشكل

 

6   5   4   0

 

شكرا لحسن تعاونكم
   

تم تعديل هذه المشاركة بواسطة هيفـــاء في 28 أبريل 2013 في 00:42

#2

تفضلي :

#include<cmath>
#include<iostream>
using namespace std;

int main() {
    long result;
    int a, b, count = 0, itoa[64];
    cout << "Enter the First Number : ";
    cin >> a;
    cout << "Enter the Second Number : ";
    cin >> b;
    result = a*b;
    while (result > 9) {
        itoa[count] = result % 10;
        result = (int) result / 10;
        count++;
    }
    itoa[count] = result;
    cout << "The Result is : ";
    for (int i = count; i >= 0; i--)
        cout << itoa[count - i] << " ";
    return 0;
}
#3

in c lang like this

...const int size = 20;char buffer[size];int n = snprintf(buffer, size, "%d", c); // use the safe function: snprintf, do not use sprintfassert(n >= 0);   // format error is indicated by a negative valueassert(n < size); // n is the number of characters written, excluding the terminating null-characterfor(int i = 0; i < n; ++i) cout << buffer << ' ';

What it does:
1) write the c into a character array (buffer).
2) make sure you do not write past the buffer end (use snprintf[^])
3) make sure the conversion does not need more characters than provided by the buffer (take in count the return value and assert[^] that writing was not expecting a larger buffer).
4) finally write each character followed by a space

++c

like this

...
ostringstream oss;
oss << c;
assert(oss.good()); // no format error
// C++11 version
for(char ch: oss.str()) cout << ch << ' ';
// iterator based version
string s = oss.str();
for(string::iterator it = s.begin(); it != s.end(); ++it) cout << *it << ' ';

تم تعديل هذه المشاركة بواسطة ±MICRO في 28 أبريل 2013 في 14:48

1 −1
#4

شكرا كتير على جهودكم

اريد تبسيط الفكره اكثر لكي افهم الخوارزمية

الفكره الاساسية ان المستخدم يدخل قيمة متغير نوع int

ونفس القيمة اللي بيدخلها المستخدم تظهر ناتج بس بين الرقم والرقم مسافه

مثال

قيمة متغير تساوي 3868

الناتج الاخير 8 6 8 3

مواضيع مشابهة