所以我是一個初學者,我一直在嘗試創建一個應用程式,用戶在其中輸入一個四位數字,然后列印該數字的數字,最后從這些數字中創建一個最大可能的數字。因此,如果您輸入 1234,可以說,應該列印出 4321。如果輸入 6271 應該列印 7621 等等。當它全部在 int main() 函式中時,我讓它很容易作業,但是對于我的一生,我不能讓它作為一個單獨的函式鏈作業,最后它只是列印出起始數字或每個巨大的數字時間。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int vratiBroj(int n) {
for (int i = 0; i < 3; i )
if (n < 1000 || n>9999) {
printf("Pogresan unos! Imate jos %d pokusaja.Probajte ponovo:\n", 3 - i);
scanf("%d", &n);
if (i == 2) {
printf("Nazalost ponestalo vam je pokusaja da unesete trazeni broj.");
}
}
return n;
}
void vratiCifre(int pocetniBroj, int * ch, int * cs, int *cd, int * cj) {
//int* ch, * cs, * cd, * cj;
ch = pocetniBroj / 1000;
cs = (pocetniBroj % 1000) / 100;
cd = ((pocetniBroj % 1000) % 100) / 10;
cj = ((pocetniBroj % 1000) % 100) % 10;
printf("%d, %d, %d, %d", ch, cs, cd, cj);
return ch, cs, cd, cj;
}
int formirajNajveci(int c1, int c2, int c3, int c4) {
int prvi=0,drugi=0,treci=0,cetvrti=0,min=0,max1=0, max2=0, min1=0, min2=0;
int najvecibroj=0;
if (c1 > c2) max1 = c1,min1=c2;
if (c2 > c1) max1 = c2, min1 = c1;
if (c3 > c4) max2 = c3, min2=c4;
if (c4 > c3)max2 = c4, min2=c3;
if (min2 > max1) {
int temp;
temp = min2;
min2 = max1;
max1 = temp;
}
if (min1 > max2) {
int temp;
temp = min1;
min1 = max2;
max2 = temp;
}
if (max1 > max2) prvi = max1,drugi=max2;
if (max2 > max1) prvi = max2, drugi = max1;
if (min1 > min2) treci = min1, cetvrti = min2;
if (min2 > min1) treci = min2, cetvrti = min1;
najvecibroj = prvi * 1000 drugi * 100 treci * 10 cetvrti;
return najvecibroj;
}
void domaci(int n) {
printf("\nNajveci mogu broj je: %d", n);
}
int main() {
int n;
int c1=0, c2=0, c3=0, c4=0;
printf("Unesite broj:\n");
scanf("%d", &n);
int a = vratiBroj(n);
printf("%d\n", a);
vratiCifre(a, c1, c2, c3, c4);
formirajNajveci(c1, c2, c3, c4);
domaci(a);
}
In int main is where my problem lies. The idea is that user enters a number n, which then goes through function vratiBroj which checks if it is indeed a four digit number. The number, if valid, is then supposed to go through the vratiCifre function, where its digits would be taken. The variable pocetniBroj (startingNumber) is supposed to be the number n. The digits are printed, and then I need to transfer the variables of four digits ch,cs,cd,cj into the formirajNajveci function, as c1,c2,c3,c4. Fianlly, after the formirajNajveci fonction forms the biggest number, that number is supposed to be sent into the domaci function and printed out.
Ive tried various stuff but I keep running in circles, tried using some pointer shenaningans, tried making new int variables for funciton results,
uj5u.com熱心網友回復:
您不是在閱讀數字,而是在閱讀字串。該scanf函式正在決議字串并將其轉換為整數,這只會使問題變得更加困難。將值讀取為字串,將其排序為字串,然后將其寫出。無需將其轉換為數字。例如:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) { return *(char *)b - *(char *)a; }
int
main(void)
{
char b[6];
if(
scanf("%5[0-9]", b) != 1
|| strlen(b) != 4
) {
fprintf(stderr, "Invalid input\n");
return 1;
}
qsort(b, 4, 1, cmp);
puts(b);
return 0;
}
請注意,上面的驗證輸入有點草率。類似的字串12345將被拒絕,但1234k會被接受。由于尚不完全清楚您要接受什么輸入或如何處理無效輸入,因此將該驗證留作練習。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455488.html
下一篇:多重二叉搜索樹
