Code Corona

نسخه‌ی كامل: تبدیل سی پلاس پلاس به سی
شما هم اكنون متن قالب بندی نشده را می‌بینید.مشاهده‌ی نسخه‌ی اصلی
صفحات: 1 2
سلام خسته نباشید من یه سری برنامه ++C دارم که می خوام انارو به C تبدیل کنم اما ارور می ده کمک کنید.
تا فردا شب حتما می خوام

1.فاکتوریل
کد:
[align=left]#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
using namespace std;
const unsigned long long _DIV_NUM = 1000000000000000;
unsigned long long Arr[2000000];
int main()
{
ofstream cout ("BigFact.out");
int n ;
while(cin >> n) //while there's number 2 read :
{
clock_t startTime= clock(); //for calculating time
long long carry =0 , tmp=0;
unsigned int up =1;
cout << n << "!\n";

//------begin of calculations :
Arr[0]=1; //0! && 1! =1
for(int i=2; i<=n; i++) //calcuating n! :
{
for(unsigned int current=0; current<up; current++) //while we have digits in Arr :
{
tmp =(i* Arr[current]+carry ); //
Arr[current]= tmp % _DIV_NUM ; //numbers in Arr must be less than 1000 (only 3-digits in all the Arr cells)/
carry = tmp / _DIV_NUM ; //calcuating carry/
}
if(carry) //if we have carry then :
{
Arr[up++] = carry; //put carry into Arr and increase Arr size/
carry =0; //reset carry/
}
}
//-------end of calculations/
//-------begin of printing Factorial :
cout << Arr[up-1]; //print first digit[s]/
//calcuating the count of factorial digits :
int factLen=0;
for(;Arr[up-1]; factLen++ , Arr[up-1]/=10); //first digits count
factLen += (up-1) * 15 ; //digits count
//print the n! :
for(int i=up-2; i>=0; i--)
{
cout << setw(15) << setfill('0') << Arr[i];
Arr[i]=0; //reset Arr cells 4 next n!/
}

clock_t endTime= clock();
cout << "\nCaculating time : " << setprecision(3) << fixed << (double)(endTime - startTime) / CLOCKS_PER_SEC << endl ;
cout << "Digits count : " << factLen << endl << endl;
}
//-------end of printing Factorial/
return 0;[/align]}

2.مثلث
کد:
#include <iostream>
#include <iomanip>
using namespace std;
int fact(int num) {
//variable to hold the value of
int num_factorial = 1;
// while num is greater than one multiply it by the value that is referenced
// by num_factorial, then reassign it to num_factorial. finally decrement the
// value of num and start again
while(num > 1){
num_factorial *= num;
num--;
}//end of while
return num_factorial;
}// end of fact function
//comb finds the value of a combination of 2 numbers
int comb(int n , int k) {
int combination = (fact(n)/(fact(k) * fact((n-k))));
return combination;
}//end of comb function
//printTab prints out a specified number of rows determined by the max rows
//minus the number of the current row
void printTab(int n, int max){
//set the counter equal to max minus the current row
//then print out 4 spaces during ever iteration
for(int i =(max-n); i > 0; i--) {
cout << "";
}//end of for
}// end of printTab function
// main function
int main() {
//vars to hold the variables in each loop
int max,row,column;
//print a blank line on execution to prevent the output from being right up against the CLI id
cout << endl;
//notes where the beginning of execution is for the goto loop
// and prompts the user for input
beginning: cout << "Enter the maximum number of rows: ";
cin >> max;
cout << endl;
// if the maximum rows is above 14 go back so we prevent a buffer overflow of sourts
if (max > 13 || max < 1){
cout << "Number out of range...Try again!\n";
goto beginning;
}//end of goto if
//increment row as long as it is less than the max rows;
for(row = 0; row < max ; row++){
//printTab aides in making output look better by tabbing over a n times depending on current
//value of the row compared to the maximum number of rows
cout << setw(2);
printTab(row,(max - 1));
//increment column as long as column is less then row +1.
//this only works since the maximum values of numbers in a row is 1+the row number
//ie in row 6 there are seven values in Pascal’s triangle,.
for(column = 0; column < row + 1; column++){
// if the column number equals 0 output "1" since it would pass
// 0 and 0 to the combination function which would cause execution fail
// from a divide by zero exception
if(row == 0){
cout << setw(4);
cout << "1";
break;

} else {
//use the iomanip function setw to make the spacing between values to
//ensure proper output and finally output the value and the spacing
cout << setw(4);
cout << comb(row,column) << "";
}//end of row if else
}//end of column for
//output and end line to ensure the next row starts on a new line
cout << endl;
}//end of row for
return 0;
}
اعضای محترم این تالار کجایید
یکی بیاد جواب مارو بده تا فردا بیشتر وقت نیست!!!
عزیز من قرار نیست اینجا کسی تکلیفها و پروژه های مردم رو انجام بده.
شما سوال کن ، ما در خدمتیم
برای شروع کار :
به جای cout ها ار تابع printf و به جای cin ها از تابع scanf استفاده کن
همین کار کلی کد شما رو تغییر میده و شکل زبان C بهش میده.
من اینارو انجام دادم اما تو RUN بر نامه ارور میده و من با تابع ها چه کار کنم مثل تابع <iomanip>و این خط
کد:
(cout << setw(15) << setfill('0') << Arr[i];)lj,[
متوجه نمیشم
سهیل خان اگه میشه راهنمایی کن تا شنبه باید تحویل بدمشون(جو و ن دانشگاه آزادیم.... اگه پاس نشه... )
خوب اگه برنامه c رو توی کامپایلرهای ++C بنویسی با iomanip وامثال اون مشکلی نخواهی داشت . اگه داری توی یک کامپایلر c کد می نویسی خوب توابعی از iomanip رو که استفاده کردی خودت بنویس یا اینکه توابع معادل c رو براشون پیدا کن .
نوشتن این 2 تا برنامه از اول به مراتب آسونتر از تبدیلشون به C
شما یک توضیح بده که این 2 تا برنامت دقیقا چی ها رو محاسبه میکنن ، بعدش با هم می نویسیم.
مثلا برنامه ی بالایی به نظر میرسه فاکتوریل اعداد بزرگ باشه که اگه یک جستجویی تو همین forum بکنین به نتایج خوبی برسین چون قبلا دربارش بحث شده.
ولی اگه باز هم میخواین همینو به C تبدیل کنین اون خط :
کد:
cout << setw(15) << setfill('0') << Arr[i];
رو کلا حذفش کنین ، دقت اعشاری میخواین چیکار Victory
اولی فاکتریل اعداد بزرگه و دومی مثلث خیام هستش اینا رو با سی پلاس پلاس داشتم اما استاده میگه با سی باید باشه تا شنبه باید تحویل بدم اگه میشه برادری کنید من الان 2تا درس دیگه امتحان دارم مارو بی نصیب نذارید بچه ها.
شرمنده با تشکر....
اقایون من هی میام تو این تاپیک مببینم جوابمو!!Sad من ترم اولی هستم کمک فوری نیازمندم میدونم که خودخواه نست اما کمک کنید نزارید بیفتم.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int Arr[10000];

main()
{
int n ;
while(scanf("%d",&n))
{
int carry =0 , up=1 , tmp=0 , i=0 , current=0;
printf("n");
printf ("!\n");
Arr[0]=1;
for(i=2;i<=n;i++)
{
for(current=0; current<up; current++)
{
tmp =(i* Arr[current]+carry );
Arr[current]= tmp % 100 ;
carry = tmp / 100;
}

if(carry)
{
Arr[up++] = carry;
carry =0;
}
}
printf("Arr[up-1]");
}
getch();
return 0;

}
این هم اضافه شد. تو خروجی عدد رو چاپ نمیکنه اگه میشه هرچی زودتر جوابمو بگیرم. ببخشید آخر ترم شنبه باید تحویل بدمشون.
سلام
اینم یک Big factorial به زبان C :
کد:
// Multiplication in base 100

// Finding 1! to 5982! without causing C to choke and splutter and
// print out 1.xyzE+mn.  Each char's ascii code is the place value in
// the centimal (base 100) system. To increase the limit of 5982, increase
// s[n].
//
// Licensed under GNU GPL by Abhishek Dasgupta
// Date created: Tue May 23 2006 A.D.

#include <stdio.h>
#include <string.h>

#define SHFT 25
#define BASE 100

void multiply(char s[], long int m)
{

    int i;
    long int c = 0, v = 0;
    long int n = strlen(s);

    for (i = n - 1; i >= 0; i--) {
    v = c + m * (int) (s[i] - SHFT);
    s[i] = v % BASE + SHFT;
    c = v / BASE;
    }

    // Accomodate the carry-over.
    if (c > 0) {
    while (c) {
        for (i = n; i >= 1; i--)
        s[i] = s[i - 1];    // quite time consuming, this.
        s[0] = c % BASE + SHFT;
        c /= BASE;
        s[++n] = 0;
    }
    }

}

int main(int argc, char *argv[])
{

    char s[10000];
    s[0] = 1 + SHFT;
    s[1] = 0;

    int i;
    if (argc == 1) {
    printf
        ("Factorial of what? Give the number after '%s'\n",argv[0]);
    return 0;
    }
    int num = atoi(argv[1]);
    if (num > 5982) {
    printf
        ("factorial: Numbers larger than 5982 will give \nincorrect answers due to memory limit of array!\n");
    return;
    }

    for (i = 2; i <= num; i++)
    multiply(s, i);

    printf("%d", s[0] - SHFT);
    for (i = 1; s[i]; i++)
    printf("%02d", s[i] - SHFT);
    printf("\n");
}
صفحات: 1 2
آدرس اصلی