程式邏輯:程式逐個字符讀取輸入的文本,將其保存到唯一句子的動態陣列中。(程式的這部分作業正常)。然后它會顯示一個提示并等待輸入其中一個數字。之后,它應該顯示所有的句子。
出于某種原因,我的程式跳過了 scanf 指令,也跳過了列印句子的 for 回圈,但同時它正確地列印了一行帶有發送編號的行。為什么會這樣?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>
#define MEM_STEP 5 * sizeof(wchar_t)
struct Text {
struct Sentence **text;
int size;
int n;
};
struct Sentence {
wchar_t *str;
int size;
};
struct Sentence *read_sentence(){
int size = MEM_STEP;
wchar_t *buf = (wchar_t*)malloc(size * sizeof(wchar_t));
wchar_t temp = getwchar();
int n = 0;
do{
if (n >= size - 2 * sizeof(wchar_t)) {
buf = realloc(buf, (size MEM_STEP) * sizeof(wchar_t));
size = MEM_STEP;
}
buf[n] = temp;
temp = getwchar();
n ;
}while (temp != '.' && temp != '\n');
buf[n] = temp;
buf[n 1] = '\0';
if (buf[0] == '\n') {
buf ;
}
struct Sentence *sentence = malloc(sizeof(struct Sentence));
sentence->str = buf;
sentence->size = size;
return sentence;
}
int is_sent_unique(struct Sentence** txt, struct Sentence* sent, int n){
for(int i = 0; i < n; i ){
int k = 0;
for(int j = 0; j < wcslen(sent->str); j ){
if (towupper(txt[i]->str[j]) == towupper(sent->str[j]))
k ;
}
if(k == wcslen(sent->str) && k == wcslen(txt[i]->str))
return 0;
}
return 1;
}
struct Text read_text(){
int size = MEM_STEP;
struct Sentence **text = malloc(size * sizeof(struct Sentence*));
struct Sentence *temp;
int n = 0;
int nlcount = 0;
do{
temp = read_sentence();
if (n >= size - 2 * sizeof(struct String*)) {
text = realloc(text, (size MEM_STEP)* sizeof(struct Sentence *));
size = MEM_STEP;
}
if(temp->str[0] == '\n' && temp->str[1] == '\0'){
nlcount ;
}else{
while (temp->str[0] == '\t' || temp->str[0] == ' ' || temp->str[0] == '\n') {
temp->str ;
}
if (is_sent_unique(text, temp, n)){
text[n] = temp;
n ;
}
nlcount = 0;
}
}while (nlcount < 2);
struct Text txt;
txt.text = text;
txt.size = size;
txt.n = n;
return txt;
}
int main()
{
setlocale(LC_ALL, "");
int func_numb = 0;
puts("Enter text:");
struct Text main_text = read_text();
puts("Enter the number of function:\n1.\n2.\n3.\n4.");
scanf("%d", &func_numb);
switch (func_numb)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
printf("Incorrect data\n");
}
for (int i = 0; i < main_text.n; i ) {
wprintf(L"%s\n", main_text.text[i]->str);
}
printf("Number of sents %d", main_text.n);
for (int i = 0; i < main_text.size; i ) {
free(main_text.text[i]);
}
free(main_text.text);
return 0;
}
uj5u.com熱心網友回復:
從你的口音我知道你生活在一個“wchar”而不是“char”可能有用的國家(輕描淡寫。:-))
但不知何故,這是讀取 wchars 和 scanf 之間的互動,而不是。當我在頂部附近的“閱讀句子”中將 getwchar 更改為 getchar 時,它開始作業。
我嘗試在“scanf”所在的位置使用“getchar()”,它回傳“EOF”:read_sentence 中的 getwchar 關閉了使用“getchar”和“scanf”的選項。
我不熟悉 getwchar,所以我會說你必須研究如何從“wchar”行為切換回來,以便 getchar 和 scanf 再次作業。
這是顯示這一點的最小程式:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, "");
puts("Enter char:");
wchar_t ch1 = getwchar();
int ch2 = getchar ();
printf ("ch1 = %d, ch2=%d.\n", ch1, ch2);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379859.html
標籤:C
上一篇:數字字串的最后一個字符被洗掉,如何在C中解決這個問題?
下一篇:在C中讀取矩陣
